ReconfigNetContainer reconfigures the container's network namespace path.
(ctx context.Context, c containerd.Container, client *containerd.Client, lab map[string]string)
| 36 | |
| 37 | // ReconfigNetContainer reconfigures the container's network namespace path. |
| 38 | func ReconfigNetContainer(ctx context.Context, c containerd.Container, client *containerd.Client, lab map[string]string) error { |
| 39 | networksJSON, ok := lab[labels.Networks] |
| 40 | if !ok { |
| 41 | return nil |
| 42 | } |
| 43 | var networks []string |
| 44 | if err := json.Unmarshal([]byte(networksJSON), &networks); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | netType, err := nettype.Detect(networks) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | if netType == nettype.Container { |
| 52 | network := strings.Split(networks[0], ":") |
| 53 | if len(network) != 2 { |
| 54 | return fmt.Errorf("invalid network: %s, should be \"container:<id|name>\"", networks[0]) |
| 55 | } |
| 56 | targetCon, err := client.LoadContainer(ctx, network[1]) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | netNSPath, err := ContainerNetNSPath(ctx, targetCon) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | spec, err := c.Spec(ctx) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | err = c.Update(ctx, containerd.UpdateContainerOpts( |
| 69 | containerd.WithSpec(spec, oci.WithLinuxNamespace( |
| 70 | specs.LinuxNamespace{ |
| 71 | Type: specs.NetworkNamespace, |
| 72 | Path: netNSPath, |
| 73 | })))) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | } |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | // ReconfigPIDContainer reconfigures the container's spec options for sharing PID namespace. |
| 82 | func ReconfigPIDContainer(ctx context.Context, c containerd.Container, client *containerd.Client, lab map[string]string) error { |
no test coverage detected
searching dependent graphs…