github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_kill.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "github.com/containerd/containerd" 21 "github.com/containerd/nerdctl/pkg/api/types" 22 "github.com/containerd/nerdctl/pkg/clientutil" 23 "github.com/containerd/nerdctl/pkg/cmd/container" 24 "github.com/spf13/cobra" 25 ) 26 27 func newKillCommand() *cobra.Command { 28 var killCommand = &cobra.Command{ 29 Use: "kill [flags] CONTAINER [CONTAINER, ...]", 30 Short: "Kill one or more running containers", 31 Args: cobra.MinimumNArgs(1), 32 RunE: killAction, 33 ValidArgsFunction: killShellComplete, 34 SilenceUsage: true, 35 SilenceErrors: true, 36 } 37 killCommand.Flags().StringP("signal", "s", "KILL", "Signal to send to the container") 38 return killCommand 39 } 40 41 func killAction(cmd *cobra.Command, args []string) error { 42 globalOptions, err := processRootCmdFlags(cmd) 43 if err != nil { 44 return err 45 } 46 killSignal, err := cmd.Flags().GetString("signal") 47 if err != nil { 48 return err 49 } 50 options := types.ContainerKillOptions{ 51 GOptions: globalOptions, 52 KillSignal: killSignal, 53 Stdout: cmd.OutOrStdout(), 54 Stderr: cmd.ErrOrStderr(), 55 } 56 57 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address) 58 if err != nil { 59 return err 60 } 61 defer cancel() 62 63 return container.Kill(ctx, client, args, options) 64 } 65 66 func killShellComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { 67 // show non-stopped container names 68 statusFilterFn := func(st containerd.ProcessStatus) bool { 69 return st != containerd.Stopped && st != containerd.Created && st != containerd.Unknown 70 } 71 return shellCompleteContainerNames(cmd, statusFilterFn) 72 }