-
Notifications
You must be signed in to change notification settings - Fork 39.2k
/
top_node.go
176 lines (152 loc) · 5.05 KB
/
top_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"errors"
"io"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/metricsutil"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
)
// TopNodeOptions contains all the options for running the top-node cli command.
type TopNodeOptions struct {
ResourceName string
Selector string
NodeClient corev1.CoreV1Interface
HeapsterOptions HeapsterTopOptions
Client *metricsutil.HeapsterMetricsClient
Printer *metricsutil.TopCmdPrinter
}
type HeapsterTopOptions struct {
Namespace string
Service string
Scheme string
Port string
}
func (o *HeapsterTopOptions) Bind(flags *pflag.FlagSet) {
if len(o.Namespace) == 0 {
o.Namespace = metricsutil.DefaultHeapsterNamespace
}
if len(o.Service) == 0 {
o.Service = metricsutil.DefaultHeapsterService
}
if len(o.Scheme) == 0 {
o.Scheme = metricsutil.DefaultHeapsterScheme
}
if len(o.Port) == 0 {
o.Port = metricsutil.DefaultHeapsterPort
}
flags.StringVar(&o.Namespace, "heapster-namespace", o.Namespace, "Namespace Heapster service is located in")
flags.StringVar(&o.Service, "heapster-service", o.Service, "Name of Heapster service")
flags.StringVar(&o.Scheme, "heapster-scheme", o.Scheme, "Scheme (http or https) to connect to Heapster as")
flags.StringVar(&o.Port, "heapster-port", o.Port, "Port name in service to use")
}
var (
topNodeLong = templates.LongDesc(i18n.T(`
Display Resource (CPU/Memory/Storage) usage of nodes.
The top-node command allows you to see the resource consumption of nodes.`))
topNodeExample = templates.Examples(i18n.T(`
# Show metrics for all nodes
kubectl top node
# Show metrics for a given node
kubectl top node NODE_NAME`))
)
func NewCmdTopNode(f cmdutil.Factory, options *TopNodeOptions, out io.Writer) *cobra.Command {
if options == nil {
options = &TopNodeOptions{}
}
cmd := &cobra.Command{
Use: "node [NAME | -l label]",
Short: i18n.T("Display Resource (CPU/Memory/Storage) usage of nodes"),
Long: topNodeLong,
Example: topNodeExample,
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, cmd, args, out); err != nil {
cmdutil.CheckErr(err)
}
if err := options.Validate(); err != nil {
cmdutil.CheckErr(cmdutil.UsageErrorf(cmd, "%v", err))
}
if err := options.RunTopNode(); err != nil {
cmdutil.CheckErr(err)
}
},
Aliases: []string{"nodes", "no"},
}
cmd.Flags().StringVarP(&options.Selector, "selector", "l", "", "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
options.HeapsterOptions.Bind(cmd.Flags())
return cmd
}
func (o *TopNodeOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error {
if len(args) == 1 {
o.ResourceName = args[0]
} else if len(args) > 1 {
return cmdutil.UsageErrorf(cmd, "%s", cmd.Use)
}
clientset, err := f.KubernetesClientSet()
if err != nil {
return err
}
o.NodeClient = clientset.Core()
o.Client = metricsutil.NewHeapsterMetricsClient(clientset.Core(), o.HeapsterOptions.Namespace, o.HeapsterOptions.Scheme, o.HeapsterOptions.Service, o.HeapsterOptions.Port)
o.Printer = metricsutil.NewTopCmdPrinter(out)
return nil
}
func (o *TopNodeOptions) Validate() error {
if len(o.ResourceName) > 0 && len(o.Selector) > 0 {
return errors.New("only one of NAME or --selector can be provided")
}
return nil
}
func (o TopNodeOptions) RunTopNode() error {
var err error
selector := labels.Everything().String()
if len(o.Selector) > 0 {
selector = o.Selector
}
metrics, err := o.Client.GetNodeMetrics(o.ResourceName, selector)
if err != nil {
return err
}
if len(metrics) == 0 {
return errors.New("metrics not available yet")
}
var nodes []v1.Node
if len(o.ResourceName) > 0 {
node, err := o.NodeClient.Nodes().Get(o.ResourceName, metav1.GetOptions{})
if err != nil {
return err
}
nodes = append(nodes, *node)
} else {
nodeList, err := o.NodeClient.Nodes().List(metav1.ListOptions{
LabelSelector: selector,
})
if err != nil {
return err
}
nodes = append(nodes, nodeList.Items...)
}
allocatable := make(map[string]v1.ResourceList)
for _, n := range nodes {
allocatable[n.Name] = n.Status.Allocatable
}
return o.Printer.PrintNodeMetrics(metrics, allocatable)
}