cmd: don't alias context package, and use cliContext for cli.Context
Unfortunately, this is a rather large diff, but perhaps worth a one-time "rip off the bandaid" for v2. This patch removes the use of "gocontext" as alias for stdLib's "context", and uses "cliContext" for uses of cli.context. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
package run
|
||||
|
||||
import (
|
||||
gocontext "context"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -82,15 +82,15 @@ var platformRunFlags = []cli.Flag{
|
||||
}
|
||||
|
||||
// NewContainer creates a new container
|
||||
func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli.Context) (containerd.Container, error) {
|
||||
func NewContainer(ctx context.Context, client *containerd.Client, cliContext *cli.Context) (containerd.Container, error) {
|
||||
var (
|
||||
id string
|
||||
config = context.IsSet("config")
|
||||
config = cliContext.IsSet("config")
|
||||
)
|
||||
if config {
|
||||
id = context.Args().First()
|
||||
id = cliContext.Args().First()
|
||||
} else {
|
||||
id = context.Args().Get(1)
|
||||
id = cliContext.Args().Get(1)
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -99,41 +99,41 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
spec containerd.NewContainerOpts
|
||||
)
|
||||
|
||||
if sandbox := context.String("sandbox"); sandbox != "" {
|
||||
if sandbox := cliContext.String("sandbox"); sandbox != "" {
|
||||
cOpts = append(cOpts, containerd.WithSandbox(sandbox))
|
||||
}
|
||||
|
||||
if config {
|
||||
cOpts = append(cOpts, containerd.WithContainerLabels(commands.LabelArgs(context.StringSlice("label"))))
|
||||
opts = append(opts, oci.WithSpecFromFile(context.String("config")))
|
||||
cOpts = append(cOpts, containerd.WithContainerLabels(commands.LabelArgs(cliContext.StringSlice("label"))))
|
||||
opts = append(opts, oci.WithSpecFromFile(cliContext.String("config")))
|
||||
} else {
|
||||
var (
|
||||
ref = context.Args().First()
|
||||
ref = cliContext.Args().First()
|
||||
// for container's id is Args[1]
|
||||
args = context.Args().Slice()[2:]
|
||||
args = cliContext.Args().Slice()[2:]
|
||||
)
|
||||
opts = append(opts, oci.WithDefaultSpec(), oci.WithDefaultUnixDevices)
|
||||
if ef := context.String("env-file"); ef != "" {
|
||||
if ef := cliContext.String("env-file"); ef != "" {
|
||||
opts = append(opts, oci.WithEnvFile(ef))
|
||||
}
|
||||
opts = append(opts, oci.WithEnv(context.StringSlice("env")))
|
||||
opts = append(opts, withMounts(context))
|
||||
opts = append(opts, oci.WithEnv(cliContext.StringSlice("env")))
|
||||
opts = append(opts, withMounts(cliContext))
|
||||
|
||||
if context.Bool("rootfs") {
|
||||
if cliContext.Bool("rootfs") {
|
||||
rootfs, err := filepath.Abs(ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opts = append(opts, oci.WithRootFSPath(rootfs))
|
||||
cOpts = append(cOpts, containerd.WithContainerLabels(commands.LabelArgs(context.StringSlice("label"))))
|
||||
cOpts = append(cOpts, containerd.WithContainerLabels(commands.LabelArgs(cliContext.StringSlice("label"))))
|
||||
} else {
|
||||
snapshotter := context.String("snapshotter")
|
||||
snapshotter := cliContext.String("snapshotter")
|
||||
var image containerd.Image
|
||||
i, err := client.ImageService().Get(ctx, ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ps := context.String("platform"); ps != "" {
|
||||
if ps := cliContext.String("platform"); ps != "" {
|
||||
platform, err := platforms.Parse(ps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -152,14 +152,14 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
labels := buildLabels(commands.LabelArgs(context.StringSlice("label")), image.Labels())
|
||||
labels := buildLabels(commands.LabelArgs(cliContext.StringSlice("label")), image.Labels())
|
||||
opts = append(opts, oci.WithImageConfig(image))
|
||||
cOpts = append(cOpts,
|
||||
containerd.WithImage(image),
|
||||
containerd.WithImageConfigLabels(image),
|
||||
containerd.WithAdditionalContainerLabels(labels),
|
||||
containerd.WithSnapshotter(snapshotter))
|
||||
if uidmap, gidmap := context.String("uidmap"), context.String("gidmap"); uidmap != "" && gidmap != "" {
|
||||
if uidmap, gidmap := cliContext.String("uidmap"), cliContext.String("gidmap"); uidmap != "" && gidmap != "" {
|
||||
uidMap, err := parseIDMapping(uidmap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -174,7 +174,7 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
// currently the snapshotters known to support the labels are:
|
||||
// fuse-overlayfs - https://github.com/containerd/fuse-overlayfs-snapshotter
|
||||
// overlay - in case of idmapped mount points are supported by host kernel (Linux kernel 5.19)
|
||||
if context.Bool("remap-labels") {
|
||||
if cliContext.Bool("remap-labels") {
|
||||
cOpts = append(cOpts, containerd.WithNewSnapshot(id, image,
|
||||
containerd.WithRemapperLabels(0, uidMap.HostID, 0, gidMap.HostID, uidMap.Size)))
|
||||
} else {
|
||||
@@ -187,28 +187,28 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
// For some snapshotter, such as overlaybd, it can provide 2 kind of writable snapshot(overlayfs dir or block-device)
|
||||
// by command label values.
|
||||
cOpts = append(cOpts, containerd.WithNewSnapshot(id, image,
|
||||
snapshots.WithLabels(commands.LabelArgs(context.StringSlice("snapshotter-label")))))
|
||||
snapshots.WithLabels(commands.LabelArgs(cliContext.StringSlice("snapshotter-label")))))
|
||||
}
|
||||
cOpts = append(cOpts, containerd.WithImageStopSignal(image, "SIGTERM"))
|
||||
}
|
||||
if context.Bool("read-only") {
|
||||
if cliContext.Bool("read-only") {
|
||||
opts = append(opts, oci.WithRootFSReadonly())
|
||||
}
|
||||
if len(args) > 0 {
|
||||
opts = append(opts, oci.WithProcessArgs(args...))
|
||||
}
|
||||
if cwd := context.String("cwd"); cwd != "" {
|
||||
if cwd := cliContext.String("cwd"); cwd != "" {
|
||||
opts = append(opts, oci.WithProcessCwd(cwd))
|
||||
}
|
||||
if user := context.String("user"); user != "" {
|
||||
if user := cliContext.String("user"); user != "" {
|
||||
opts = append(opts, oci.WithUser(user), oci.WithAdditionalGIDs(user))
|
||||
}
|
||||
if context.Bool("tty") {
|
||||
if cliContext.Bool("tty") {
|
||||
opts = append(opts, oci.WithTTY)
|
||||
}
|
||||
|
||||
privileged := context.Bool("privileged")
|
||||
privilegedWithoutHostDevices := context.Bool("privileged-without-host-devices")
|
||||
privileged := cliContext.Bool("privileged")
|
||||
privilegedWithoutHostDevices := cliContext.Bool("privileged-without-host-devices")
|
||||
if privilegedWithoutHostDevices && !privileged {
|
||||
return nil, errors.New("can't use 'privileged-without-host-devices' without 'privileged' specified")
|
||||
}
|
||||
@@ -220,7 +220,7 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
}
|
||||
}
|
||||
|
||||
if context.Bool("net-host") {
|
||||
if cliContext.Bool("net-host") {
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get hostname: %w", err)
|
||||
@@ -232,7 +232,7 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
oci.WithEnv([]string{fmt.Sprintf("HOSTNAME=%s", hostname)}),
|
||||
)
|
||||
}
|
||||
if annoStrings := context.StringSlice("annotation"); len(annoStrings) > 0 {
|
||||
if annoStrings := cliContext.StringSlice("annotation"); len(annoStrings) > 0 {
|
||||
annos, err := commands.AnnotationArgs(annoStrings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -240,7 +240,7 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
opts = append(opts, oci.WithAnnotations(annos))
|
||||
}
|
||||
|
||||
if caps := context.StringSlice("cap-add"); len(caps) > 0 {
|
||||
if caps := cliContext.StringSlice("cap-add"); len(caps) > 0 {
|
||||
for _, cap := range caps {
|
||||
if !strings.HasPrefix(cap, "CAP_") {
|
||||
return nil, errors.New("capabilities must be specified with 'CAP_' prefix")
|
||||
@@ -249,7 +249,7 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
opts = append(opts, oci.WithAddedCapabilities(caps))
|
||||
}
|
||||
|
||||
if caps := context.StringSlice("cap-drop"); len(caps) > 0 {
|
||||
if caps := cliContext.StringSlice("cap-drop"); len(caps) > 0 {
|
||||
for _, cap := range caps {
|
||||
if !strings.HasPrefix(cap, "CAP_") {
|
||||
return nil, errors.New("capabilities must be specified with 'CAP_' prefix")
|
||||
@@ -258,13 +258,13 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
opts = append(opts, oci.WithDroppedCapabilities(caps))
|
||||
}
|
||||
|
||||
seccompProfile := context.String("seccomp-profile")
|
||||
seccompProfile := cliContext.String("seccomp-profile")
|
||||
|
||||
if !context.Bool("seccomp") && seccompProfile != "" {
|
||||
if !cliContext.Bool("seccomp") && seccompProfile != "" {
|
||||
return nil, errors.New("seccomp must be set to true, if using a custom seccomp-profile")
|
||||
}
|
||||
|
||||
if context.Bool("seccomp") {
|
||||
if cliContext.Bool("seccomp") {
|
||||
if seccompProfile != "" {
|
||||
opts = append(opts, seccomp.WithProfile(seccompProfile))
|
||||
} else {
|
||||
@@ -272,18 +272,18 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
}
|
||||
}
|
||||
|
||||
if s := context.String("apparmor-default-profile"); len(s) > 0 {
|
||||
if s := cliContext.String("apparmor-default-profile"); len(s) > 0 {
|
||||
opts = append(opts, apparmor.WithDefaultProfile(s))
|
||||
}
|
||||
|
||||
if s := context.String("apparmor-profile"); len(s) > 0 {
|
||||
if len(context.String("apparmor-default-profile")) > 0 {
|
||||
if s := cliContext.String("apparmor-profile"); len(s) > 0 {
|
||||
if len(cliContext.String("apparmor-default-profile")) > 0 {
|
||||
return nil, errors.New("apparmor-profile conflicts with apparmor-default-profile")
|
||||
}
|
||||
opts = append(opts, apparmor.WithProfile(s))
|
||||
}
|
||||
|
||||
if cpus := context.Float64("cpus"); cpus > 0.0 {
|
||||
if cpus := cliContext.Float64("cpus"); cpus > 0.0 {
|
||||
var (
|
||||
period = uint64(100000)
|
||||
quota = int64(cpus * 100000.0)
|
||||
@@ -291,32 +291,32 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
opts = append(opts, oci.WithCPUCFS(quota, period))
|
||||
}
|
||||
|
||||
if cpusetCpus := context.String("cpuset-cpus"); len(cpusetCpus) > 0 {
|
||||
if cpusetCpus := cliContext.String("cpuset-cpus"); len(cpusetCpus) > 0 {
|
||||
opts = append(opts, oci.WithCPUs(cpusetCpus))
|
||||
}
|
||||
|
||||
if cpusetMems := context.String("cpuset-mems"); len(cpusetMems) > 0 {
|
||||
if cpusetMems := cliContext.String("cpuset-mems"); len(cpusetMems) > 0 {
|
||||
opts = append(opts, oci.WithCPUsMems(cpusetMems))
|
||||
}
|
||||
|
||||
if shares := context.Int("cpu-shares"); shares > 0 {
|
||||
if shares := cliContext.Int("cpu-shares"); shares > 0 {
|
||||
opts = append(opts, oci.WithCPUShares(uint64(shares)))
|
||||
}
|
||||
|
||||
quota := context.Int64("cpu-quota")
|
||||
period := context.Uint64("cpu-period")
|
||||
quota := cliContext.Int64("cpu-quota")
|
||||
period := cliContext.Uint64("cpu-period")
|
||||
if quota != -1 || period != 0 {
|
||||
if cpus := context.Float64("cpus"); cpus > 0.0 {
|
||||
if cpus := cliContext.Float64("cpus"); cpus > 0.0 {
|
||||
return nil, errors.New("cpus and quota/period should be used separately")
|
||||
}
|
||||
opts = append(opts, oci.WithCPUCFS(quota, period))
|
||||
}
|
||||
|
||||
if burst := context.Uint64("cpu-burst"); burst != 0 {
|
||||
if burst := cliContext.Uint64("cpu-burst"); burst != 0 {
|
||||
opts = append(opts, oci.WithCPUBurst(burst))
|
||||
}
|
||||
|
||||
joinNs := context.StringSlice("with-ns")
|
||||
joinNs := cliContext.StringSlice("with-ns")
|
||||
for _, ns := range joinNs {
|
||||
nsType, nsPath, ok := strings.Cut(ns, ":")
|
||||
if !ok {
|
||||
@@ -330,22 +330,22 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
Path: nsPath,
|
||||
}))
|
||||
}
|
||||
if context.IsSet("gpus") {
|
||||
opts = append(opts, nvidia.WithGPUs(nvidia.WithDevices(context.IntSlice("gpus")...), nvidia.WithAllCapabilities))
|
||||
if cliContext.IsSet("gpus") {
|
||||
opts = append(opts, nvidia.WithGPUs(nvidia.WithDevices(cliContext.IntSlice("gpus")...), nvidia.WithAllCapabilities))
|
||||
}
|
||||
if context.IsSet("allow-new-privs") {
|
||||
if cliContext.IsSet("allow-new-privs") {
|
||||
opts = append(opts, oci.WithNewPrivileges)
|
||||
}
|
||||
if context.IsSet("cgroup") {
|
||||
if cliContext.IsSet("cgroup") {
|
||||
// NOTE: can be set to "" explicitly for disabling cgroup.
|
||||
opts = append(opts, oci.WithCgroup(context.String("cgroup")))
|
||||
opts = append(opts, oci.WithCgroup(cliContext.String("cgroup")))
|
||||
}
|
||||
limit := context.Uint64("memory-limit")
|
||||
limit := cliContext.Uint64("memory-limit")
|
||||
if limit != 0 {
|
||||
opts = append(opts, oci.WithMemoryLimit(limit))
|
||||
}
|
||||
var cdiDeviceIDs []string
|
||||
for _, dev := range context.StringSlice("device") {
|
||||
for _, dev := range cliContext.StringSlice("device") {
|
||||
if parser.IsQualifiedName(dev) {
|
||||
cdiDeviceIDs = append(cdiDeviceIDs, dev)
|
||||
continue
|
||||
@@ -357,9 +357,9 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
}
|
||||
opts = append(opts, oci.WithCDIDevices(cdiDeviceIDs...))
|
||||
|
||||
rootfsPropagation := context.String("rootfs-propagation")
|
||||
rootfsPropagation := cliContext.String("rootfs-propagation")
|
||||
if rootfsPropagation != "" {
|
||||
opts = append(opts, func(_ gocontext.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
|
||||
opts = append(opts, func(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
|
||||
if s.Linux != nil {
|
||||
s.Linux.RootfsPropagation = rootfsPropagation
|
||||
} else {
|
||||
@@ -372,39 +372,39 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
||||
})
|
||||
}
|
||||
|
||||
if c := context.String("blockio-config-file"); c != "" {
|
||||
if c := cliContext.String("blockio-config-file"); c != "" {
|
||||
if err := blockio.SetConfigFromFile(c, false); err != nil {
|
||||
return nil, fmt.Errorf("blockio-config-file error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if c := context.String("blockio-class"); c != "" {
|
||||
if c := cliContext.String("blockio-class"); c != "" {
|
||||
if linuxBlockIO, err := blockio.OciLinuxBlockIO(c); err == nil {
|
||||
opts = append(opts, oci.WithBlockIO(linuxBlockIO))
|
||||
} else {
|
||||
return nil, fmt.Errorf("blockio-class error: %w", err)
|
||||
}
|
||||
}
|
||||
if c := context.String("rdt-class"); c != "" {
|
||||
if c := cliContext.String("rdt-class"); c != "" {
|
||||
opts = append(opts, oci.WithRdt(c, "", ""))
|
||||
}
|
||||
if hostname := context.String("hostname"); hostname != "" {
|
||||
if hostname := cliContext.String("hostname"); hostname != "" {
|
||||
opts = append(opts, oci.WithHostname(hostname))
|
||||
}
|
||||
}
|
||||
|
||||
if context.Bool("cni") {
|
||||
if cliContext.Bool("cni") {
|
||||
cniMeta := &commands.NetworkMetaData{EnableCni: true}
|
||||
cOpts = append(cOpts, containerd.WithContainerExtension(commands.CtrCniMetadataExtension, cniMeta))
|
||||
}
|
||||
|
||||
runtimeOpts, err := commands.RuntimeOptions(context)
|
||||
runtimeOpts, err := commands.RuntimeOptions(cliContext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cOpts = append(cOpts, containerd.WithRuntime(context.String("runtime"), runtimeOpts))
|
||||
cOpts = append(cOpts, containerd.WithRuntime(cliContext.String("runtime"), runtimeOpts))
|
||||
|
||||
opts = append(opts, oci.WithAnnotations(commands.LabelArgs(context.StringSlice("label"))))
|
||||
opts = append(opts, oci.WithAnnotations(commands.LabelArgs(cliContext.StringSlice("label"))))
|
||||
var s specs.Spec
|
||||
spec = containerd.WithSpec(&s, opts...)
|
||||
|
||||
@@ -456,7 +456,7 @@ func validNamespace(ns string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func getNetNSPath(_ gocontext.Context, task containerd.Task) (string, error) {
|
||||
func getNetNSPath(_ context.Context, task containerd.Task) (string, error) {
|
||||
return fmt.Sprintf("/proc/%d/ns/net", task.Pid()), nil
|
||||
}
|
||||
|
||||
@@ -464,7 +464,7 @@ func getNetNSPath(_ gocontext.Context, task containerd.Task) (string, error) {
|
||||
// This is used from the `run` command to avoid creating a registry with auto-refresh enabled.
|
||||
// It also provides a way to override the CDI spec file paths if required.
|
||||
func withStaticCDIRegistry() oci.SpecOpts {
|
||||
return func(ctx gocontext.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
|
||||
return func(ctx context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
|
||||
_ = cdi.Configure(cdi.WithAutoRefresh(false))
|
||||
if err := cdi.Refresh(); err != nil {
|
||||
// We don't consider registry refresh failure a fatal error.
|
||||
|
||||
Reference in New Issue
Block a user