github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_top.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  	"fmt"
    21  
    22  	"github.com/containerd/containerd"
    23  	"github.com/containerd/nerdctl/pkg/api/types"
    24  	"github.com/containerd/nerdctl/pkg/clientutil"
    25  	"github.com/containerd/nerdctl/pkg/cmd/compose"
    26  	"github.com/containerd/nerdctl/pkg/cmd/container"
    27  	"github.com/containerd/nerdctl/pkg/containerutil"
    28  	"github.com/containerd/nerdctl/pkg/labels"
    29  	"github.com/spf13/cobra"
    30  )
    31  
    32  func newComposeTopCommand() *cobra.Command {
    33  	var composeTopCommand = &cobra.Command{
    34  		Use:                   "top [SERVICE...]",
    35  		Short:                 "Display the running processes of service containers",
    36  		RunE:                  composeTopAction,
    37  		SilenceUsage:          true,
    38  		SilenceErrors:         true,
    39  		DisableFlagsInUseLine: true,
    40  	}
    41  	return composeTopCommand
    42  }
    43  
    44  func composeTopAction(cmd *cobra.Command, args []string) error {
    45  	globalOptions, err := processRootCmdFlags(cmd)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
    51  	if err != nil {
    52  		return err
    53  	}
    54  	defer cancel()
    55  	options, err := getComposeOptions(cmd, globalOptions.DebugFull, globalOptions.Experimental)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	c, err := compose.New(client, globalOptions, options, cmd.OutOrStdout(), cmd.ErrOrStderr())
    60  	if err != nil {
    61  		return err
    62  	}
    63  	serviceNames, err := c.ServiceNames(args...)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	containers, err := c.Containers(ctx, serviceNames...)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	stdout := cmd.OutOrStdout()
    72  	for _, c := range containers {
    73  		cStatus, err := containerutil.ContainerStatus(ctx, c)
    74  		if err != nil {
    75  			return err
    76  		}
    77  		if cStatus.Status != containerd.Running {
    78  			continue
    79  		}
    80  
    81  		info, err := c.Info(ctx, containerd.WithoutRefreshedMetadata)
    82  		if err != nil {
    83  			return err
    84  		}
    85  		fmt.Fprintln(stdout, info.Labels[labels.Name])
    86  		// `compose ps` uses empty ps args
    87  		err = container.Top(ctx, client, []string{c.ID()}, types.ContainerTopOptions{
    88  			Stdout:   cmd.OutOrStdout(),
    89  			GOptions: globalOptions,
    90  		})
    91  		if err != nil {
    92  			return err
    93  		}
    94  		fmt.Fprintln(stdout)
    95  	}
    96  
    97  	return nil
    98  }