...

Source file src/k8s.io/kubectl/pkg/cmd/top/top.go

Documentation: k8s.io/kubectl/pkg/cmd/top

     1  /*
     2  Copyright 2016 The Kubernetes 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 top
    18  
    19  import (
    20  	"github.com/spf13/cobra"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/cli-runtime/pkg/genericiooptions"
    24  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    25  	"k8s.io/kubectl/pkg/util/i18n"
    26  	"k8s.io/kubectl/pkg/util/templates"
    27  	metricsapi "k8s.io/metrics/pkg/apis/metrics"
    28  )
    29  
    30  const (
    31  	sortByCPU    = "cpu"
    32  	sortByMemory = "memory"
    33  )
    34  
    35  var (
    36  	supportedMetricsAPIVersions = []string{
    37  		"v1beta1",
    38  	}
    39  	topLong = templates.LongDesc(i18n.T(`
    40  		Display resource (CPU/memory) usage.
    41  
    42  		The top command allows you to see the resource consumption for nodes or pods.
    43  
    44  		This command requires Metrics Server to be correctly configured and working on the server. `))
    45  )
    46  
    47  func NewCmdTop(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
    48  	cmd := &cobra.Command{
    49  		Use:   "top",
    50  		Short: i18n.T("Display resource (CPU/memory) usage"),
    51  		Long:  topLong,
    52  		Run:   cmdutil.DefaultSubCommandRun(streams.ErrOut),
    53  	}
    54  
    55  	// create subcommands
    56  	cmd.AddCommand(NewCmdTopNode(f, nil, streams))
    57  	cmd.AddCommand(NewCmdTopPod(f, nil, streams))
    58  
    59  	return cmd
    60  }
    61  
    62  func SupportedMetricsAPIVersionAvailable(discoveredAPIGroups *metav1.APIGroupList) bool {
    63  	for _, discoveredAPIGroup := range discoveredAPIGroups.Groups {
    64  		if discoveredAPIGroup.Name != metricsapi.GroupName {
    65  			continue
    66  		}
    67  		for _, version := range discoveredAPIGroup.Versions {
    68  			for _, supportedVersion := range supportedMetricsAPIVersions {
    69  				if version.Version == supportedVersion {
    70  					return true
    71  				}
    72  			}
    73  		}
    74  	}
    75  	return false
    76  }
    77  

View as plain text