|
| 1 | +// +build windows |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright The containerd Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package opts |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "path/filepath" |
| 24 | + "sort" |
| 25 | + |
| 26 | + "github.com/containerd/containerd/containers" |
| 27 | + "github.com/containerd/containerd/oci" |
| 28 | + runtimespec "github.com/opencontainers/runtime-spec/specs-go" |
| 29 | + "github.com/pkg/errors" |
| 30 | + runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" |
| 31 | + |
| 32 | + osinterface "github.com/containerd/cri/pkg/os" |
| 33 | +) |
| 34 | + |
| 35 | +// WithWindowsNetworkNamespace sets windows network namespace for container. |
| 36 | +// TODO(windows): Move this into container/containerd. |
| 37 | +func WithWindowsNetworkNamespace(path string) oci.SpecOpts { |
| 38 | + return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) error { |
| 39 | + if s.Windows == nil { |
| 40 | + s.Windows = &runtimespec.Windows{} |
| 41 | + } |
| 42 | + if s.Windows.Network == nil { |
| 43 | + s.Windows.Network = &runtimespec.WindowsNetwork{} |
| 44 | + } |
| 45 | + s.Windows.Network.NetworkNamespace = path |
| 46 | + return nil |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// WithWindowsMounts sorts and adds runtime and CRI mounts to the spec for |
| 51 | +// windows container. |
| 52 | +func WithWindowsMounts(osi osinterface.OS, config *runtime.ContainerConfig, extra []*runtime.Mount) oci.SpecOpts { |
| 53 | + return func(ctx context.Context, client oci.Client, _ *containers.Container, s *runtimespec.Spec) error { |
| 54 | + // mergeMounts merge CRI mounts with extra mounts. If a mount destination |
| 55 | + // is mounted by both a CRI mount and an extra mount, the CRI mount will |
| 56 | + // be kept. |
| 57 | + var ( |
| 58 | + criMounts = config.GetMounts() |
| 59 | + mounts = append([]*runtime.Mount{}, criMounts...) |
| 60 | + ) |
| 61 | + // Copy all mounts from extra mounts, except for mounts overriden by CRI. |
| 62 | + for _, e := range extra { |
| 63 | + found := false |
| 64 | + for _, c := range criMounts { |
| 65 | + if filepath.Clean(e.ContainerPath) == filepath.Clean(c.ContainerPath) { |
| 66 | + found = true |
| 67 | + break |
| 68 | + } |
| 69 | + } |
| 70 | + if !found { |
| 71 | + mounts = append(mounts, e) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Sort mounts in number of parts. This ensures that high level mounts don't |
| 76 | + // shadow other mounts. |
| 77 | + sort.Sort(orderedMounts(mounts)) |
| 78 | + |
| 79 | + // Copy all mounts from default mounts, except for |
| 80 | + // - mounts overriden by supplied mount; |
| 81 | + // - all mounts under /dev if a supplied /dev is present. |
| 82 | + mountSet := make(map[string]struct{}) |
| 83 | + for _, m := range mounts { |
| 84 | + mountSet[filepath.Clean(m.ContainerPath)] = struct{}{} |
| 85 | + } |
| 86 | + |
| 87 | + defaultMounts := s.Mounts |
| 88 | + s.Mounts = nil |
| 89 | + |
| 90 | + for _, m := range defaultMounts { |
| 91 | + dst := filepath.Clean(m.Destination) |
| 92 | + if _, ok := mountSet[dst]; ok { |
| 93 | + // filter out mount overridden by a supplied mount |
| 94 | + continue |
| 95 | + } |
| 96 | + s.Mounts = append(s.Mounts, m) |
| 97 | + } |
| 98 | + |
| 99 | + for _, mount := range mounts { |
| 100 | + var ( |
| 101 | + dst = mount.GetContainerPath() |
| 102 | + src = mount.GetHostPath() |
| 103 | + ) |
| 104 | + // TODO(windows): Support special mount sources, e.g. named pipe. |
| 105 | + // Create the host path if it doesn't exist. |
| 106 | + if _, err := osi.Stat(src); err != nil { |
| 107 | + // If the source doesn't exist, return an error instead |
| 108 | + // of creating the source. This aligns with Docker's |
| 109 | + // behavior on windows. |
| 110 | + return errors.Wrapf(err, "failed to stat %q", src) |
| 111 | + } |
| 112 | + src, err := osi.ResolveSymbolicLink(src) |
| 113 | + if err != nil { |
| 114 | + return errors.Wrapf(err, "failed to resolve symlink %q", src) |
| 115 | + } |
| 116 | + |
| 117 | + var options []string |
| 118 | + // NOTE(random-liu): we don't change all mounts to `ro` when root filesystem |
| 119 | + // is readonly. This is different from docker's behavior, but make more sense. |
| 120 | + if mount.GetReadonly() { |
| 121 | + options = append(options, "ro") |
| 122 | + } else { |
| 123 | + options = append(options, "rw") |
| 124 | + } |
| 125 | + s.Mounts = append(s.Mounts, runtimespec.Mount{ |
| 126 | + Source: src, |
| 127 | + Destination: dst, |
| 128 | + Options: options, |
| 129 | + }) |
| 130 | + } |
| 131 | + return nil |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// WithWindowsResources sets the provided resource restrictions for windows. |
| 136 | +func WithWindowsResources(resources *runtime.WindowsContainerResources) oci.SpecOpts { |
| 137 | + return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) error { |
| 138 | + if resources == nil { |
| 139 | + return nil |
| 140 | + } |
| 141 | + if s.Windows == nil { |
| 142 | + s.Windows = &runtimespec.Windows{} |
| 143 | + } |
| 144 | + if s.Windows.Resources == nil { |
| 145 | + s.Windows.Resources = &runtimespec.WindowsResources{} |
| 146 | + } |
| 147 | + if s.Windows.Resources.CPU == nil { |
| 148 | + s.Windows.Resources.CPU = &runtimespec.WindowsCPUResources{} |
| 149 | + } |
| 150 | + if s.Windows.Resources.Memory == nil { |
| 151 | + s.Windows.Resources.Memory = &runtimespec.WindowsMemoryResources{} |
| 152 | + } |
| 153 | + |
| 154 | + var ( |
| 155 | + count = uint64(resources.GetCpuCount()) |
| 156 | + shares = uint16(resources.GetCpuShares()) |
| 157 | + max = uint16(resources.GetCpuMaximum()) |
| 158 | + limit = uint64(resources.GetMemoryLimitInBytes()) |
| 159 | + ) |
| 160 | + if count != 0 { |
| 161 | + s.Windows.Resources.CPU.Count = &count |
| 162 | + } |
| 163 | + if shares != 0 { |
| 164 | + s.Windows.Resources.CPU.Shares = &shares |
| 165 | + } |
| 166 | + if max != 0 { |
| 167 | + s.Windows.Resources.CPU.Maximum = &max |
| 168 | + } |
| 169 | + if limit != 0 { |
| 170 | + s.Windows.Resources.Memory.Limit = &limit |
| 171 | + } |
| 172 | + return nil |
| 173 | + } |
| 174 | +} |
0 commit comments