1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package run
import (
gocontext "context"
"encoding/csv"
"errors"
"fmt"
"strings"
"github.com/containerd/console"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/cmd/ctr/commands/tasks"
"github.com/containerd/containerd/containers"
clabels "github.com/containerd/containerd/labels"
"github.com/containerd/containerd/oci"
gocni "github.com/containerd/go-cni"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func withMounts(context *cli.Context) oci.SpecOpts {
return func(ctx gocontext.Context, client oci.Client, container *containers.Container, s *specs.Spec) error {
mounts := make([]specs.Mount, 0)
for _, mount := range context.StringSlice("mount") {
m, err := parseMountFlag(mount)
if err != nil {
return err
}
mounts = append(mounts, m)
}
return oci.WithMounts(mounts)(ctx, client, container, s)
}
}
// parseMountFlag parses a mount string in the form "type=foo,source=/path,destination=/target,options=rbind:rw"
func parseMountFlag(m string) (specs.Mount, error) {
mount := specs.Mount{}
r := csv.NewReader(strings.NewReader(m))
fields, err := r.Read()
if err != nil {
return mount, err
}
for _, field := range fields {
key, val, ok := strings.Cut(field, "=")
if !ok {
return mount, fmt.Errorf("invalid mount specification: expected key=val")
}
switch key {
case "type":
mount.Type = val
case "source", "src":
mount.Source = val
case "destination", "dst":
mount.Destination = val
case "options":
mount.Options = strings.Split(val, ":")
default:
return mount, fmt.Errorf("mount option %q not supported", key)
}
}
return mount, nil
}
// Command runs a container
var Command = cli.Command{
Name: "run",
Usage: "Run a container",
ArgsUsage: "[flags] Image|RootFS ID [COMMAND] [ARG...]",
SkipArgReorder: true,
Flags: append([]cli.Flag{
cli.BoolFlag{
Name: "rm",
Usage: "Remove the container after running, cannot be used with --detach",
},
cli.BoolFlag{
Name: "null-io",
Usage: "Send all IO to /dev/null",
},
cli.StringFlag{
Name: "log-uri",
Usage: "Log uri",
},
cli.BoolFlag{
Name: "detach,d",
Usage: "Detach from the task after it has started execution, cannot be used with --rm",
},
cli.StringFlag{
Name: "fifo-dir",
Usage: "Directory used for storing IO FIFOs",
},
cli.StringFlag{
Name: "cgroup",
Usage: "Cgroup path (To disable use of cgroup, set to \"\" explicitly)",
},
cli.StringFlag{
Name: "platform",
Usage: "Run image for specific platform",
},
cli.BoolFlag{
Name: "cni",
Usage: "Enable cni networking for the container",
},
}, append(platformRunFlags,
append(append(commands.SnapshotterFlags, []cli.Flag{commands.SnapshotterLabels}...),
commands.ContainerFlags...)...)...),
Action: func(context *cli.Context) error {
var (
err error
id string
ref string
rm = context.Bool("rm")
tty = context.Bool("tty")
detach = context.Bool("detach")
config = context.IsSet("config")
enableCNI = context.Bool("cni")
)
if config {
id = context.Args().First()
if context.NArg() > 1 {
return errors.New("with spec config file, only container id should be provided")
}
} else {
id = context.Args().Get(1)
ref = context.Args().First()
if ref == "" {
return errors.New("image ref must be provided")
}
}
if id == "" {
return errors.New("container id must be provided")
}
if rm && detach {
return errors.New("flags --detach and --rm cannot be specified together")
}
client, ctx, cancel, err := commands.NewClient(context)
if err != nil {
return err
}
defer cancel()
container, err := NewContainer(ctx, client, context)
if err != nil {
return err
}
if rm && !detach {
defer container.Delete(ctx, containerd.WithSnapshotCleanup)
}
var con console.Console
if tty {
con = console.Current()
defer con.Reset()
if err := con.SetRaw(); err != nil {
return err
}
}
var network gocni.CNI
if enableCNI {
if network, err = gocni.New(gocni.WithPluginDir([]string{gocni.DefaultCNIDir, "/usr/lib/cni"}), gocni.WithDefaultConf); err != nil {
return err
}
}
opts := tasks.GetNewTaskOpts(context)
ioOpts := []cio.Opt{cio.WithFIFODir(context.String("fifo-dir"))}
task, err := tasks.NewTask(ctx, client, container, context.String("checkpoint"), con, context.Bool("null-io"), context.String("log-uri"), ioOpts, opts...)
if err != nil {
return err
}
var statusC <-chan containerd.ExitStatus
if !detach {
defer func() {
if enableCNI {
if err := network.Remove(ctx, commands.FullID(ctx, container), ""); err != nil {
logrus.WithError(err).Error("network review")
}
}
task.Delete(ctx)
}()
if statusC, err = task.Wait(ctx); err != nil {
return err
}
}
if context.IsSet("pid-file") {
if err := commands.WritePidFile(context.String("pid-file"), int(task.Pid())); err != nil {
return err
}
}
if enableCNI {
netNsPath, err := getNetNSPath(ctx, task)
if err != nil {
return err
}
if _, err := network.Setup(ctx, commands.FullID(ctx, container), netNsPath); err != nil {
return err
}
}
if err := task.Start(ctx); err != nil {
return err
}
if detach {
return nil
}
if tty {
if err := tasks.HandleConsoleResize(ctx, task, con); err != nil {
logrus.WithError(err).Error("console resize")
}
} else {
sigc := commands.ForwardAllSignals(ctx, task)
defer commands.StopCatch(sigc)
}
status := <-statusC
code, _, err := status.Result()
if err != nil {
return err
}
if _, err := task.Delete(ctx); err != nil {
return err
}
if code != 0 {
return cli.NewExitError("", int(code))
}
return nil
},
}
// buildLabel builds the labels from command line labels and the image labels
func buildLabels(cmdLabels, imageLabels map[string]string) map[string]string {
labels := make(map[string]string)
for k, v := range imageLabels {
if err := clabels.Validate(k, v); err == nil {
labels[k] = v
} else {
// In case the image label is invalid, we output a warning and skip adding it to the
// container.
logrus.WithError(err).Warnf("unable to add image label with key %s to the container", k)
}
}
// labels from the command line will override image and the initial image config labels
for k, v := range cmdLabels {
labels[k] = v
}
return labels
}
|