|
| 1 | +//go: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 | + "errors" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/containerd/containerd/containers" |
| 27 | + "github.com/containerd/containerd/oci" |
| 28 | + imagespec "github.com/opencontainers/image-spec/specs-go/v1" |
| 29 | + runtimespec "github.com/opencontainers/runtime-spec/specs-go" |
| 30 | + "golang.org/x/sys/windows" |
| 31 | + runtime "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 32 | +) |
| 33 | + |
| 34 | +func escapeAndCombineArgsWindows(args []string) string { |
| 35 | + escaped := make([]string, len(args)) |
| 36 | + for i, a := range args { |
| 37 | + escaped[i] = windows.EscapeArg(a) |
| 38 | + } |
| 39 | + return strings.Join(escaped, " ") |
| 40 | +} |
| 41 | + |
| 42 | +// WithProcessCommandLineOrArgsForWindows sets the process command line or process args on the spec based on the image |
| 43 | +// and runtime config |
| 44 | +// If image.ArgsEscaped field is set, this function sets the process command line and if not, it sets the |
| 45 | +// process args field |
| 46 | +func WithProcessCommandLineOrArgsForWindows(config *runtime.ContainerConfig, image *imagespec.ImageConfig) oci.SpecOpts { |
| 47 | + if image.ArgsEscaped { |
| 48 | + return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) (err error) { |
| 49 | + // firstArgFromImg is a flag that is returned to indicate that the first arg in the slice comes from either the |
| 50 | + // image Entrypoint or Cmd. If the first arg instead comes from the container config (e.g. overriding the image values), |
| 51 | + // it should be false. This is done to support the non-OCI ArgsEscaped field that Docker used to determine how the image |
| 52 | + // entrypoint and cmd should be interpreted. |
| 53 | + // |
| 54 | + args, firstArgFromImg, err := getArgs(image.Entrypoint, image.Cmd, config.GetCommand(), config.GetArgs()) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + var cmdLine string |
| 60 | + if image.ArgsEscaped && firstArgFromImg { |
| 61 | + cmdLine = args[0] |
| 62 | + if len(args) > 1 { |
| 63 | + cmdLine += " " + escapeAndCombineArgsWindows(args[1:]) |
| 64 | + } |
| 65 | + } else { |
| 66 | + cmdLine = escapeAndCombineArgsWindows(args) |
| 67 | + } |
| 68 | + |
| 69 | + return oci.WithProcessCommandLine(cmdLine)(ctx, client, c, s) |
| 70 | + } |
| 71 | + } |
| 72 | + // if ArgsEscaped is not set |
| 73 | + return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) (err error) { |
| 74 | + args, _, err := getArgs(image.Entrypoint, image.Cmd, config.GetCommand(), config.GetArgs()) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + return oci.WithProcessArgs(args...)(ctx, client, c, s) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// getArgs is used to evaluate the overall args for the container by taking into account the image command and entrypoints |
| 83 | +// along with the container command and entrypoints specified through the podspec if any |
| 84 | +func getArgs(imgEntrypoint []string, imgCmd []string, ctrEntrypoint []string, ctrCmd []string) ([]string, bool, error) { |
| 85 | + //nolint:dupword |
| 86 | + // firstArgFromImg is a flag that is returned to indicate that the first arg in the slice comes from either the image |
| 87 | + // Entrypoint or Cmd. If the first arg instead comes from the container config (e.g. overriding the image values), |
| 88 | + // it should be false. |
| 89 | + // Essentially this means firstArgFromImg should be true iff: |
| 90 | + // Ctr entrypoint ctr cmd image entrypoint image cmd firstArgFromImg |
| 91 | + // -------------------------------------------------------------------------------- |
| 92 | + // nil nil exists nil true |
| 93 | + // nil nil nil exists true |
| 94 | + |
| 95 | + // This is needed to support the non-OCI ArgsEscaped field used by Docker. ArgsEscaped is used for |
| 96 | + // Windows images to indicate that the command has already been escaped and should be |
| 97 | + // used directly as the command line. |
| 98 | + var firstArgFromImg bool |
| 99 | + entrypoint, cmd := ctrEntrypoint, ctrCmd |
| 100 | + // The following logic is migrated from https://github.com/moby/moby/blob/master/daemon/commit.go |
| 101 | + // TODO(random-liu): Clearly define the commands overwrite behavior. |
| 102 | + if len(entrypoint) == 0 { |
| 103 | + // Copy array to avoid data race. |
| 104 | + if len(cmd) == 0 { |
| 105 | + cmd = append([]string{}, imgCmd...) |
| 106 | + if len(imgCmd) > 0 { |
| 107 | + firstArgFromImg = true |
| 108 | + } |
| 109 | + } |
| 110 | + if entrypoint == nil { |
| 111 | + entrypoint = append([]string{}, imgEntrypoint...) |
| 112 | + if len(imgEntrypoint) > 0 || len(ctrCmd) == 0 { |
| 113 | + firstArgFromImg = true |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + if len(entrypoint) == 0 && len(cmd) == 0 { |
| 118 | + return nil, false, errors.New("no command specified") |
| 119 | + } |
| 120 | + return append(entrypoint, cmd...), firstArgFromImg, nil |
| 121 | +} |
0 commit comments