(ctx context.Context, containers []containerd.Container)
| 152 | } |
| 153 | |
| 154 | func namespaceUsedNetworks(ctx context.Context, containers []containerd.Container) (map[string][]string, error) { |
| 155 | used := make(map[string][]string) |
| 156 | for _, c := range containers { |
| 157 | // Only tasks under the ctx namespace can be obtained here |
| 158 | task, err := c.Task(ctx, nil) |
| 159 | if err != nil { |
| 160 | if errdefs.IsNotFound(err) { |
| 161 | log.G(ctx).Debugf("task not found - likely container %q was removed", c.ID()) |
| 162 | continue |
| 163 | } |
| 164 | return nil, err |
| 165 | } |
| 166 | status, err := task.Status(ctx) |
| 167 | if err != nil { |
| 168 | if errdefs.IsNotFound(err) { |
| 169 | log.G(ctx).Debugf("task not found - likely container %q was removed", c.ID()) |
| 170 | continue |
| 171 | } |
| 172 | return nil, err |
| 173 | } |
| 174 | switch status.Status { |
| 175 | case containerd.Paused, containerd.Running: |
| 176 | default: |
| 177 | continue |
| 178 | } |
| 179 | l, err := c.Labels(ctx) |
| 180 | if err != nil { |
| 181 | if errdefs.IsNotFound(err) { |
| 182 | log.G(ctx).Debugf("container %q is gone", c.ID()) |
| 183 | continue |
| 184 | } |
| 185 | return nil, err |
| 186 | } |
| 187 | networkJSON, ok := l[labels.Networks] |
| 188 | if !ok { |
| 189 | continue |
| 190 | } |
| 191 | var networks []string |
| 192 | if err := json.Unmarshal([]byte(networkJSON), &networks); err != nil { |
| 193 | return nil, err |
| 194 | } |
| 195 | netType, err := nettype.Detect(networks) |
| 196 | if err != nil { |
| 197 | return nil, err |
| 198 | } |
| 199 | if netType != nettype.CNI { |
| 200 | continue |
| 201 | } |
| 202 | for _, n := range networks { |
| 203 | used[n] = append(used[n], c.ID()) |
| 204 | } |
| 205 | } |
| 206 | return used, nil |
| 207 | } |
| 208 | |
| 209 | func WithDefaultNetwork(bridgeIP string) CNIEnvOpt { |
| 210 | return func(e *CNIEnv) error { |
no test coverage detected
searching dependent graphs…