/
popeye.ts
219 lines (200 loc) · 6.62 KB
/
popeye.ts
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
/*
* Copyright 2020 IBM Corporation
*
* 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.
*/
import { Arguments, Row } from '@kui-shell/core'
import { doHelp, isUsage, getNamespace, getKind, doExecWithStdout } from '@kui-shell/plugin-kubectl'
interface PopeyeReport {
score: number
grade: string
sanitizers: Sanitizer[]
}
interface Sanitizer {
sanitizer: string
tally: {
ok: number
info: number
warning: number
error: number
score: number
}
issues: Record<string, Issue[]>
}
interface Issue {
group: string
level: number
message: string
}
function bodyToHeader(body: Row[]) {
if (body.length > 0) {
return {
key: body[0].key,
name: body[0].key,
attributes: body[0].attributes.map(({ key }) => ({ key, value: key }))
}
}
}
function formatIssueRow(group: string, message: string, level: number) {
return {
key: 'Group',
name: group,
attributes: [
{
key: 'STATUS',
value: level === 3 ? 'Error' : level === 2 ? 'Warning' : 'OK',
tag: 'badge',
css: level === 3 ? 'red-background' : level === 2 ? 'yellow-background' : 'green-background'
},
{
key: 'Message',
value: message
}
]
}
}
export default (command: string) => async (args: Arguments) => {
if (isUsage(args)) {
return doHelp(command, args)
} else {
const userAskForSection = (args.parsedOptions['s'] || args.parsedOptions['sections']) as string
let userAskResourceName
/**
* override the popeye command to force json output
* , and add support for fetching report by resource name
*
*/
const prepareArgsForPopeye = (args: Arguments) => {
userAskResourceName = userAskForSection && args.argvNoOptions[args.argvNoOptions.indexOf('popeye') + 1]
/** popeye doesn't support showing report for a single resource name
* e.g. popeye -s pod nginx
* so we fetch the report of pod and filter the result
*
*/
return userAskResourceName
? `${args.command.replace(userAskResourceName, '')} -o json`
: `${args.command} -o json`
}
const stdout = await doExecWithStdout(args, prepareArgsForPopeye, command)
const fullReport = JSON.parse(stdout).popeye as PopeyeReport
// console.error('fullReport', fullReport)
const ns = await getNamespace(args)
const formatTable = (body: Row[], title: string, footer?: string[]) => {
return {
header: bodyToHeader(body),
body,
statusColumnIdx: 0,
breadcrumbs: [{ label: 'Popeye' }, { label: ns }],
title,
footer
}
}
/**
* full report issued by command kubectl popeye
*
*/
const fullReportTable = () => {
const body = fullReport.sanitizers.map(({ sanitizer, tally }) => {
return {
key: 'Name',
name: sanitizer,
onclick: `${command} popeye -s ${sanitizer} -n ${ns}`,
attributes: [
{
key: 'Score',
value: tally.score.toString(),
tag: 'badge',
css: tally.score === 100 ? 'green-background' : 'red-background'
},
{ key: 'Error', value: tally.error.toString() },
{ key: 'Warning', value: tally.warning.toString() },
{ key: 'Info', value: tally.info.toString() },
{ key: 'Ok', value: tally.ok.toString() }
]
}
})
return formatTable(body, 'All resources', [`Overall score: ${fullReport.score} ${fullReport.grade}`])
}
/**
* section report issued by command e.g. kubectl popeye -s pod
*
*/
const sectionTable = async () => {
const body = []
fullReport.sanitizers.forEach(({ issues, sanitizer, tally }) => {
if (issues) {
Object.entries(issues).forEach(([resourceName, issueList]) => {
if (resourceName) {
let status = 0
issueList.forEach(({ level }) => (status = level > status ? level : status))
const shortNames = resourceName.split(/\//)
body.push({
key: 'Name',
name: shortNames.length > 1 ? shortNames[1] : shortNames[0], // e.g. split out the namespace in resourceName
onclick: `${command} popeye -s ${sanitizer} ${resourceName} -n ${ns}`,
attributes: [
{
key: 'Status',
value: status === 3 ? 'Error' : status === 2 ? 'Warning' : 'OK',
tag: 'badge',
css: status === 3 ? 'red-background' : status === 2 ? 'yellow-background' : 'green-background'
}
]
})
} else {
issueList.forEach(_ => body.push(formatIssueRow(_.group, _.message, _.level)))
}
})
} else {
// no issues, return the tally of this sanitizer
body.push({
key: 'Name',
name: sanitizer,
attributes: [
{
key: 'Score',
value: tally.score.toString(),
tag: 'badge',
css: tally.score === 100 ? 'green-background' : 'red-background'
},
{ key: 'Error', value: tally.error.toString() },
{ key: 'Warning', value: tally.warning.toString() },
{ key: 'Info', value: tally.info.toString() },
{ key: 'Ok', value: tally.ok.toString() }
]
})
}
})
return formatTable(body, await getKind(command, args, userAskForSection), [
`Overall score: ${fullReport.score} ${fullReport.grade}`
])
}
/**
* all issues in e.g. pod nginx
*
*/
const resourceNameTable = () => {
const body = fullReport.sanitizers[0].issues[userAskResourceName].map(({ group, level, message }) =>
formatIssueRow(group, message, level)
)
return formatTable(body, userAskResourceName.split(/\//)[1])
}
if (userAskResourceName) {
return resourceNameTable()
} else if (userAskForSection) {
return sectionTable()
} else {
return fullReportTable()
}
}
}