From 305210d693bdfe4972120d8afc283311bf509f41 Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Tue, 2 Jul 2024 12:37:40 +0200 Subject: [PATCH] Add crictl `update-runtime-config` command The command can be used to update the runtime config, which only supports the pod CIDR for now. Signed-off-by: Sascha Grunert --- cmd/crictl/main.go | 1 + cmd/crictl/update_runtime_config.go | 67 +++++++++++++++++++++++++++++ docs/crictl.md | 63 ++++++++++++++------------- 3 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 cmd/crictl/update_runtime_config.go diff --git a/cmd/crictl/main.go b/cmd/crictl/main.go index fce4dee4f8..c06fc4ff4d 100644 --- a/cmd/crictl/main.go +++ b/cmd/crictl/main.go @@ -208,6 +208,7 @@ func main() { checkpointContainerCommand, runtimeConfigCommand, eventsCommand, + updateRuntimeConfigCommand, } runtimeEndpointUsage := fmt.Sprintf("Endpoint of CRI container runtime "+ diff --git a/cmd/crictl/update_runtime_config.go b/cmd/crictl/update_runtime_config.go new file mode 100644 index 0000000000..f0e9f07288 --- /dev/null +++ b/cmd/crictl/update_runtime_config.go @@ -0,0 +1,67 @@ +/* +Copyright 2024 The Kubernetes 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 main + +import ( + "context" + "fmt" + + "github.com/sirupsen/logrus" + "github.com/urfave/cli/v2" + pb "k8s.io/cri-api/pkg/apis/runtime/v1" +) + +const ( + podCIDRFlag = "pod-cidr" +) + +var updateRuntimeConfigCommand = &cli.Command{ + Name: "update-runtime-config", + Usage: "Update the runtime configuration", + UseShortOptionHandling: true, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: podCIDRFlag, + Aliases: []string{"p"}, + Usage: "The new Classless Inter-Domain Routing (CIDR) value to be used for pod IP addresses. If the CIDR is empty, runtimes should omit it.", + }, + }, + Action: func(c *cli.Context) error { + if c.NArg() != 0 || c.NumFlags() == 0 { + return cli.ShowSubcommandHelp(c) + } + + runtimeConfig := &pb.RuntimeConfig{} + if c.IsSet(podCIDRFlag) { + runtimeConfig.NetworkConfig = &pb.NetworkConfig{PodCidr: c.String(podCIDRFlag)} + } + + runtimeClient, err := getRuntimeService(c, 0) + if err != nil { + return err + } + + if _, err := InterruptableRPC(nil, func(ctx context.Context) (any, error) { + return nil, runtimeClient.UpdateRuntimeConfig(ctx, runtimeConfig) + }); err != nil { + return fmt.Errorf("update runtime config: %w", err) + } + + logrus.Info("Runtime config successfully updated") + return nil + }, +} diff --git a/docs/crictl.md b/docs/crictl.md index fd8315b9b0..bfaf64ca07 100644 --- a/docs/crictl.md +++ b/docs/crictl.md @@ -39,37 +39,38 @@ crictl [global options] command [command options] [arguments...] COMMANDS: -- `attach`: Attach to a running container -- `create`: Create a new container -- `exec`: Run a command in a running container -- `version`: Display runtime version information -- `images, image, img`: List images -- `inspect`: Display the status of one or more containers -- `inspecti`: Return the status of one or more images -- `imagefsinfo`: Return image filesystem info -- `inspectp`: Display the status of one or more pods -- `logs`: Fetch the logs of a container -- `port-forward`: Forward local port to a pod -- `ps`: List containers -- `pull`: Pull an image from a registry -- `run`: Run a new container inside a sandbox -- `runp`: Run a new pod -- `rm`: Remove one or more containers -- `rmi`: Remove one or more images -- `rmp`: Remove one or more pods -- `pods`: List pods -- `start`: Start one or more created containers -- `info`: Display information of the container runtime -- `stop`: Stop one or more running containers -- `stopp`: Stop one or more running pods -- `update`: Update one or more running containers -- `config`: Get and set `crictl` client configuration options -- `stats`: List container(s) resource usage statistics -- `statsp`: List pod(s) resource usage statistics -- `completion`: Output bash shell completion code -- `checkpoint`: Checkpoint one or more running containers -- `events, event`: Stream the events of containers -- `help, h`: Shows a list of commands or help for one command +- `attach`: Attach to a running container +- `create`: Create a new container +- `exec`: Run a command in a running container +- `version`: Display runtime version information +- `images, image, img`: List images +- `inspect`: Display the status of one or more containers +- `inspecti`: Return the status of one or more images +- `imagefsinfo`: Return image filesystem info +- `inspectp`: Display the status of one or more pods +- `logs`: Fetch the logs of a container +- `port-forward`: Forward local port to a pod +- `ps`: List containers +- `pull`: Pull an image from a registry +- `run`: Run a new container inside a sandbox +- `runp`: Run a new pod +- `rm`: Remove one or more containers +- `rmi`: Remove one or more images +- `rmp`: Remove one or more pods +- `pods`: List pods +- `start`: Start one or more created containers +- `info`: Display information of the container runtime +- `stop`: Stop one or more running containers +- `stopp`: Stop one or more running pods +- `update`: Update one or more running containers +- `config`: Get and set `crictl` client configuration options +- `stats`: List container(s) resource usage statistics +- `statsp`: List pod(s) resource usage statistics +- `completion`: Output bash shell completion code +- `checkpoint`: Checkpoint one or more running containers +- `events, event`: Stream the events of containers +- `update-runtime-config` Update the runtime configuration +- `help, h`: Shows a list of commands or help for one command `crictl` by default connects on Unix to: