-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.go
265 lines (220 loc) · 6.99 KB
/
matrix.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Copyright 2022 Arda Güçlü.
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 (
"context"
"errors"
"fmt"
"os"
"strconv"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth" // combined authprovider import
)
const (
example = `
# show [pod x node] matrix in terms of pod statuses in default namespace
kubectl pod-node-matrix
# show [pod x node] matrix in terms of pod statuses in given namespace
kubectl pod-node-matrix -n test-namespace
show [pod x node] matrix in terms of pod statuses in all namespaces
kubectl pod-node-matrix -A
# show pods statuses and node names in verbose format
# it is suggested only there are few nodes.
kubectl pod-node-matrix --verbose
`
longDesc = `
Shows [pod x node] matrix in terms of pod statuses
with suitable colors to mitigate troubleshooting effort.
When there is a problem at pods, detecting whether the problem
stems from nodes or deployments is arduous task.
pod-node-matrix shows pods and nodes in table view.
If one of columns is red, thereby it is reasonable to interfere
that node has a problem. If one of rows is red,
node might not be the cause instead it is better to check
pods, deployments, etc.
`
)
// PodNodeMatrixOptions provides information required
// to show the pod node matrix.
type PodNodeMatrixOptions struct {
configFlags *genericclioptions.ConfigFlags
genericclioptions.IOStreams
namespace string
allNamespaces bool
verbose bool
kubeClient kubernetes.Interface
}
// NewPodNodeMatrixOptions provides an instance of PodNodeMatrixOptions with default values.
func NewPodNodeMatrixOptions(streams genericclioptions.IOStreams) *PodNodeMatrixOptions {
return &PodNodeMatrixOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
}
// NewCmdPodNodeMatrix provides a cobra command wrapping PodNodeMatrixOptions
func NewCmdPodNodeMatrix(streams genericclioptions.IOStreams) *cobra.Command {
o := NewPodNodeMatrixOptions(streams)
cmd := &cobra.Command{
Use: "pod-node-matrix [flags]",
Short: "show [pod x node] matrix in terms of pod statuses",
Long: longDesc,
SilenceUsage: true,
Example: example,
RunE: func(c *cobra.Command, args []string) error {
if err := o.Complete(); err != nil {
return err
}
if err := o.Validate(args); err != nil {
return err
}
if err := o.Run(); err != nil {
return err
}
return nil
},
}
cmd.Flags().BoolVar(&o.verbose, "verbose", o.verbose, "If present, show pods statuses and node names in verbose format instead shortcuts. It is suggested only when you have few nodes.")
cmd.Flags().BoolVarP(&o.allNamespaces, "all-namespaces", "A", o.allNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
o.configFlags.AddFlags(cmd.Flags())
return cmd
}
// Complete sets all information required to show pod node matrix
func (o *PodNodeMatrixOptions) Complete() error {
config, err := o.configFlags.ToRESTConfig()
if err != nil {
return err
}
o.kubeClient, err = kubernetes.NewForConfig(config)
if err != nil {
return err
}
if o.allNamespaces {
o.namespace = ""
} else {
namespace, _, err := o.configFlags.ToRawKubeConfigLoader().Namespace()
if err != nil || len(namespace) == 0 {
namespace = "default"
}
o.namespace = namespace
}
return nil
}
// Validate ensures that all required arguments and flag values are provided
func (o *PodNodeMatrixOptions) Validate(args []string) error {
if len(args) > 0 {
return errors.New("unknown argument")
}
return nil
}
// nodeWrap is Node object with index value in addition to that.
type nodeWrap struct {
v1.Node
index int
}
// Run shows pod-node matrix according to the given parameters
func (o *PodNodeMatrixOptions) Run() error {
pods, err := o.kubeClient.CoreV1().Pods(o.namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
if len(pods.Items) == 0 {
return fmt.Errorf("no pod is found in namespace %s", o.namespace)
}
nodes, err := o.kubeClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
if len(nodes.Items) == 0 {
return errors.New("no node is found which is weird")
}
indx := 0
nodeMap := make(map[string]nodeWrap)
var nodeNames []string
for _, n := range nodes.Items {
nodeMap[n.Name] = nodeWrap{
Node: n,
index: indx,
}
nodeNames = append(nodeNames, n.Name)
indx += 1
}
err = o.Print(pods.Items, nodeMap, nodeNames)
if err != nil {
return err
}
return nil
}
// Print prints table view with colors to emphasize results
func (o *PodNodeMatrixOptions) Print(pods []v1.Pod, nm map[string]nodeWrap, nodeNames []string) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
for _, pod := range pods {
data := make([]string, len(nm)+1)
colors := make([]tablewriter.Colors, len(nm)+1)
data[0] = pod.Name
colors[0] = tablewriter.Colors{}
if val, ok := nm[pod.Spec.NodeName]; ok {
data[val.index+1] = string(pod.Status.Phase)
switch pod.Status.Phase {
case v1.PodPending:
if !o.verbose {
data[val.index+1] = "-"
}
colors[val.index+1] = tablewriter.Colors{tablewriter.FgYellowColor}
case v1.PodRunning:
fallthrough
case v1.PodSucceeded:
if !o.verbose {
data[val.index+1] = "✓"
}
colors[val.index+1] = tablewriter.Colors{tablewriter.FgGreenColor}
case v1.PodFailed:
if !o.verbose {
data[val.index+1] = "x"
}
colors[val.index+1] = tablewriter.Colors{tablewriter.FgRedColor}
}
} else {
colors[0] = tablewriter.Colors{tablewriter.BgRedColor}
}
table.Rich(data, colors)
}
nodeNames, footers := o.getHeadersAndFooters(nodeNames)
header := []string{"Pods"}
header = append(header, nodeNames...)
table.SetHeader(header)
table.Render()
if !o.verbose && footers != nil {
nodeMapping := tablewriter.NewWriter(os.Stdout)
nodeMapping.SetAlignment(tablewriter.ALIGN_LEFT)
nodeMapping.AppendBulk(footers)
nodeMapping.Render()
}
return nil
}
func (o *PodNodeMatrixOptions) getHeadersAndFooters(nodeNames []string) (headers []string, footers [][]string) {
if o.verbose {
return nodeNames, nil
}
for i, val := range nodeNames {
ind := strconv.Itoa(i)
headers = append(headers, ind)
footers = append(footers, []string{ind, val})
}
return
}