...
1
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
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