github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_stats.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 newStatsCommand() *cobra.Command {
    28  	var statsCommand = &cobra.Command{
    29  		Use:               "stats",
    30  		Short:             "Display a live stream of container(s) resource usage statistics.",
    31  		RunE:              statsAction,
    32  		ValidArgsFunction: statsShellComplete,
    33  		SilenceUsage:      true,
    34  		SilenceErrors:     true,
    35  	}
    36  
    37  	addStatsFlags(statsCommand)
    38  
    39  	return statsCommand
    40  }
    41  
    42  func addStatsFlags(cmd *cobra.Command) {
    43  	cmd.Flags().BoolP("all", "a", false, "Show all containers (default shows just running)")
    44  	cmd.Flags().String("format", "", "Pretty-print images using a Go template, e.g, '{{json .}}'")
    45  	cmd.Flags().Bool("no-stream", false, "Disable streaming stats and only pull the first result")
    46  	cmd.Flags().Bool("no-trunc", false, "Do not truncate output")
    47  }
    48  
    49  func processStatsCommandFlags(cmd *cobra.Command) (types.ContainerStatsOptions, error) {
    50  	globalOptions, err := processRootCmdFlags(cmd)
    51  	if err != nil {
    52  		return types.ContainerStatsOptions{}, err
    53  	}
    54  
    55  	all, err := cmd.Flags().GetBool("all")
    56  	if err != nil {
    57  		return types.ContainerStatsOptions{}, err
    58  	}
    59  
    60  	noStream, err := cmd.Flags().GetBool("no-stream")
    61  	if err != nil {
    62  		return types.ContainerStatsOptions{}, err
    63  	}
    64  
    65  	format, err := cmd.Flags().GetString("format")
    66  	if err != nil {
    67  		return types.ContainerStatsOptions{}, err
    68  	}
    69  
    70  	noTrunc, err := cmd.Flags().GetBool("no-trunc")
    71  	if err != nil {
    72  		return types.ContainerStatsOptions{}, err
    73  	}
    74  
    75  	return types.ContainerStatsOptions{
    76  		Stdout:   cmd.OutOrStdout(),
    77  		Stderr:   cmd.ErrOrStderr(),
    78  		GOptions: globalOptions,
    79  		All:      all,
    80  		Format:   format,
    81  		NoStream: noStream,
    82  		NoTrunc:  noTrunc,
    83  	}, nil
    84  }
    85  
    86  func statsAction(cmd *cobra.Command, args []string) error {
    87  	options, err := processStatsCommandFlags(cmd)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	defer cancel()
    97  
    98  	return container.Stats(ctx, client, args, options)
    99  }
   100  
   101  func statsShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
   102  	// show running container names
   103  	statusFilterFn := func(st containerd.ProcessStatus) bool {
   104  		return st == containerd.Running
   105  	}
   106  	return shellCompleteContainerNames(cmd, statusFilterFn)
   107  }