|
| 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 container |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + |
| 25 | + containerd "github.com/containerd/containerd/v2/client" |
| 26 | + |
| 27 | + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" |
| 28 | + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" |
| 29 | + "github.com/containerd/nerdctl/v2/pkg/clientutil" |
| 30 | + "github.com/containerd/nerdctl/v2/pkg/cmd/container" |
| 31 | + "github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker" |
| 32 | +) |
| 33 | + |
| 34 | +// HealthCheckCommand returns a cobra command for `nerdctl container healthcheck` |
| 35 | +func HealthCheckCommand() *cobra.Command { |
| 36 | + var healthCheckCommand = &cobra.Command{ |
| 37 | + Use: "healthcheck [flags] CONTAINER", |
| 38 | + Short: "Execute the health check command in a container", |
| 39 | + Args: cobra.ExactArgs(1), |
| 40 | + RunE: healthCheckAction, |
| 41 | + ValidArgsFunction: healthCheckShellComplete, |
| 42 | + SilenceUsage: true, |
| 43 | + SilenceErrors: true, |
| 44 | + } |
| 45 | + |
| 46 | + return healthCheckCommand |
| 47 | +} |
| 48 | + |
| 49 | +func healthCheckAction(cmd *cobra.Command, args []string) error { |
| 50 | + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + defer cancel() |
| 60 | + |
| 61 | + containerID := args[0] |
| 62 | + walker := &containerwalker.ContainerWalker{ |
| 63 | + Client: client, |
| 64 | + OnFound: func(ctx context.Context, found containerwalker.Found) error { |
| 65 | + if found.MatchCount > 1 { |
| 66 | + return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req) |
| 67 | + } |
| 68 | + return container.HealthCheck(ctx, client, found.Container) |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + n, err := walker.Walk(ctx, containerID) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } else if n == 0 { |
| 76 | + return fmt.Errorf("no such container %s", containerID) |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func healthCheckShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 82 | + return completion.ContainerNames(cmd, func(status containerd.ProcessStatus) bool { |
| 83 | + return status == containerd.Running |
| 84 | + }) |
| 85 | +} |
0 commit comments