github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/container/update.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/docker/cli/cli"
     9  	"github.com/docker/cli/cli/command"
    10  	"github.com/docker/cli/cli/command/completion"
    11  	"github.com/docker/cli/opts"
    12  	containertypes "github.com/docker/docker/api/types/container"
    13  	"github.com/pkg/errors"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  type updateOptions struct {
    18  	blkioWeight        uint16
    19  	cpuPeriod          int64
    20  	cpuQuota           int64
    21  	cpuRealtimePeriod  int64
    22  	cpuRealtimeRuntime int64
    23  	cpusetCpus         string
    24  	cpusetMems         string
    25  	cpuShares          int64
    26  	memory             opts.MemBytes
    27  	memoryReservation  opts.MemBytes
    28  	memorySwap         opts.MemSwapBytes
    29  	kernelMemory       opts.MemBytes
    30  	restartPolicy      string
    31  	pidsLimit          int64
    32  	cpus               opts.NanoCPUs
    33  
    34  	nFlag int
    35  
    36  	containers []string
    37  }
    38  
    39  // NewUpdateCommand creates a new cobra.Command for `docker update`
    40  func NewUpdateCommand(dockerCli command.Cli) *cobra.Command {
    41  	var options updateOptions
    42  
    43  	cmd := &cobra.Command{
    44  		Use:   "update [OPTIONS] CONTAINER [CONTAINER...]",
    45  		Short: "Update configuration of one or more containers",
    46  		Args:  cli.RequiresMinArgs(1),
    47  		RunE: func(cmd *cobra.Command, args []string) error {
    48  			options.containers = args
    49  			options.nFlag = cmd.Flags().NFlag()
    50  			return runUpdate(cmd.Context(), dockerCli, &options)
    51  		},
    52  		Annotations: map[string]string{
    53  			"aliases": "docker container update, docker update",
    54  		},
    55  		ValidArgsFunction: completion.ContainerNames(dockerCli, true),
    56  	}
    57  
    58  	flags := cmd.Flags()
    59  	flags.Uint16Var(&options.blkioWeight, "blkio-weight", 0, `Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)`)
    60  	flags.Int64Var(&options.cpuPeriod, "cpu-period", 0, "Limit CPU CFS (Completely Fair Scheduler) period")
    61  	flags.Int64Var(&options.cpuQuota, "cpu-quota", 0, "Limit CPU CFS (Completely Fair Scheduler) quota")
    62  	flags.Int64Var(&options.cpuRealtimePeriod, "cpu-rt-period", 0, "Limit the CPU real-time period in microseconds")
    63  	flags.SetAnnotation("cpu-rt-period", "version", []string{"1.25"})
    64  	flags.Int64Var(&options.cpuRealtimeRuntime, "cpu-rt-runtime", 0, "Limit the CPU real-time runtime in microseconds")
    65  	flags.SetAnnotation("cpu-rt-runtime", "version", []string{"1.25"})
    66  	flags.StringVar(&options.cpusetCpus, "cpuset-cpus", "", "CPUs in which to allow execution (0-3, 0,1)")
    67  	flags.StringVar(&options.cpusetMems, "cpuset-mems", "", "MEMs in which to allow execution (0-3, 0,1)")
    68  	flags.Int64VarP(&options.cpuShares, "cpu-shares", "c", 0, "CPU shares (relative weight)")
    69  	flags.VarP(&options.memory, "memory", "m", "Memory limit")
    70  	flags.Var(&options.memoryReservation, "memory-reservation", "Memory soft limit")
    71  	flags.Var(&options.memorySwap, "memory-swap", `Swap limit equal to memory plus swap: -1 to enable unlimited swap`)
    72  	flags.Var(&options.kernelMemory, "kernel-memory", "Kernel memory limit (deprecated)")
    73  	// --kernel-memory is deprecated on API v1.42 and up, but our current annotations
    74  	// do not support only showing on < API-version. This option is no longer supported
    75  	// by runc, so hiding it unconditionally.
    76  	flags.SetAnnotation("kernel-memory", "deprecated", nil)
    77  	flags.MarkHidden("kernel-memory")
    78  
    79  	flags.StringVar(&options.restartPolicy, "restart", "", "Restart policy to apply when a container exits")
    80  	flags.Int64Var(&options.pidsLimit, "pids-limit", 0, `Tune container pids limit (set -1 for unlimited)`)
    81  	flags.SetAnnotation("pids-limit", "version", []string{"1.40"})
    82  
    83  	flags.Var(&options.cpus, "cpus", "Number of CPUs")
    84  	flags.SetAnnotation("cpus", "version", []string{"1.29"})
    85  
    86  	return cmd
    87  }
    88  
    89  func runUpdate(ctx context.Context, dockerCli command.Cli, options *updateOptions) error {
    90  	var err error
    91  
    92  	if options.nFlag == 0 {
    93  		return errors.New("you must provide one or more flags when using this command")
    94  	}
    95  
    96  	var restartPolicy containertypes.RestartPolicy
    97  	if options.restartPolicy != "" {
    98  		restartPolicy, err = opts.ParseRestartPolicy(options.restartPolicy)
    99  		if err != nil {
   100  			return err
   101  		}
   102  	}
   103  
   104  	resources := containertypes.Resources{
   105  		BlkioWeight:        options.blkioWeight,
   106  		CpusetCpus:         options.cpusetCpus,
   107  		CpusetMems:         options.cpusetMems,
   108  		CPUShares:          options.cpuShares,
   109  		Memory:             options.memory.Value(),
   110  		MemoryReservation:  options.memoryReservation.Value(),
   111  		MemorySwap:         options.memorySwap.Value(),
   112  		KernelMemory:       options.kernelMemory.Value(),
   113  		CPUPeriod:          options.cpuPeriod,
   114  		CPUQuota:           options.cpuQuota,
   115  		CPURealtimePeriod:  options.cpuRealtimePeriod,
   116  		CPURealtimeRuntime: options.cpuRealtimeRuntime,
   117  		NanoCPUs:           options.cpus.Value(),
   118  	}
   119  
   120  	if options.pidsLimit != 0 {
   121  		resources.PidsLimit = &options.pidsLimit
   122  	}
   123  
   124  	updateConfig := containertypes.UpdateConfig{
   125  		Resources:     resources,
   126  		RestartPolicy: restartPolicy,
   127  	}
   128  
   129  	var (
   130  		warns []string
   131  		errs  []string
   132  	)
   133  	for _, container := range options.containers {
   134  		r, err := dockerCli.Client().ContainerUpdate(ctx, container, updateConfig)
   135  		if err != nil {
   136  			errs = append(errs, err.Error())
   137  		} else {
   138  			fmt.Fprintln(dockerCli.Out(), container)
   139  		}
   140  		warns = append(warns, r.Warnings...)
   141  	}
   142  	if len(warns) > 0 {
   143  		fmt.Fprintln(dockerCli.Out(), strings.Join(warns, "\n"))
   144  	}
   145  	if len(errs) > 0 {
   146  		return errors.New(strings.Join(errs, "\n"))
   147  	}
   148  	return nil
   149  }