cleanupNetwork removes cni network setup, specifically the forwards
(ctx context.Context, container containerd.Container, globalOpts types.GlobalCommandOptions)
| 128 | |
| 129 | // cleanupNetwork removes cni network setup, specifically the forwards |
| 130 | func cleanupNetwork(ctx context.Context, container containerd.Container, globalOpts types.GlobalCommandOptions) error { |
| 131 | return rootlessutil.WithDetachedNetNSIfAny(func() error { |
| 132 | // retrieve current active port mappings |
| 133 | dataStore, err := clientutil.DataStore(globalOpts.DataRoot, globalOpts.Address) |
| 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | containerLabels, err := container.Labels(ctx) |
| 138 | if err != nil { |
| 139 | return err |
| 140 | } |
| 141 | ports, err := portutil.LoadPortMappings(dataStore, globalOpts.Namespace, container.ID(), containerLabels) |
| 142 | if err != nil { |
| 143 | return fmt.Errorf("no oci spec: %q", err) |
| 144 | } |
| 145 | portMappings := []cni.NamespaceOpts{ |
| 146 | cni.WithCapabilityPortMap(ports), |
| 147 | } |
| 148 | |
| 149 | // retrieve info to get cni instance |
| 150 | spec, err := container.Spec(ctx) |
| 151 | if err != nil { |
| 152 | return err |
| 153 | } |
| 154 | networksJSON := spec.Annotations[labels.Networks] |
| 155 | var networks []string |
| 156 | if err := json.Unmarshal([]byte(networksJSON), &networks); err != nil { |
| 157 | log.G(ctx).WithError(err).WithField("container", container.ID()).Infof("unable to retrieve networking information for that container") |
| 158 | return nil |
| 159 | } |
| 160 | netType, err := nettype.Detect(networks) |
| 161 | if err != nil { |
| 162 | return err |
| 163 | } |
| 164 | |
| 165 | switch netType { |
| 166 | case nettype.Host, nettype.None, nettype.Container, nettype.Namespace: |
| 167 | // NOP |
| 168 | case nettype.CNI: |
| 169 | e, err := netutil.NewCNIEnv(globalOpts.CNIPath, globalOpts.CNINetConfPath, netutil.WithNamespace(globalOpts.Namespace), netutil.WithDefaultNetwork(globalOpts.BridgeIP)) |
| 170 | if err != nil { |
| 171 | return err |
| 172 | } |
| 173 | cniOpts := []cni.Opt{ |
| 174 | cni.WithPluginDir([]string{globalOpts.CNIPath}), |
| 175 | } |
| 176 | var netw *netutil.NetworkConfig |
| 177 | for _, netstr := range networks { |
| 178 | if netw, err = e.NetworkByNameOrID(netstr); err != nil { |
| 179 | return err |
| 180 | } |
| 181 | cniOpts = append(cniOpts, cni.WithConfListBytes(netw.Bytes)) |
| 182 | } |
| 183 | cniObj, err := cni.New(cniOpts...) |
| 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 |
no test coverage detected
searching dependent graphs…