This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp_opssight.go
341 lines (291 loc) · 11.5 KB
/
app_opssight.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package testutils
import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
"github.com/blackducksoftware/synopsysctl/pkg/opssight"
"github.com/blackducksoftware/synopsysctl/pkg/util"
)
// GetLatestOpsSightVersion ...
func GetLatestOpsSightVersion() string {
if TestConfig.OpsSight.Version != "" {
return TestConfig.OpsSight.Version
}
return "2.2.5"
}
// NewOpsSightTester ...
func NewOpsSightTester() *OpsSightTester {
t := OpsSightTester{
AppName: util.OpsSightName,
}
// Get default values for an OpsSight test
t.Name = GenName(t.AppName)
t.Namespace = CreateUniqueNamespace(t.AppName)
t.Version = GetLatestOpsSightVersion()
t.Labels = ""
// Config for OpsSight
t.FlagTree = opssight.FlagTree{}
t.FlagTree.EnableMetrics = "TRUE"
t.FlagTree.PerceiverEnablePodPerceiver = "TRUE"
t.FlagTree.PerceiverEnableImagePerceiver = "FALSE"
t.FlagTree.PerceiverEnableQuayPerceiver = "FALSE"
t.FlagTree.PerceiverEnableArtifactoryPerceiver = "FALSE"
return &t
}
// OpsSightTester ...
type OpsSightTester struct {
AppName string
Name string
Namespace string
Version string
Labels string
FlagTree opssight.FlagTree
}
// WaitUntilReady ...
func (t OpsSightTester) WaitUntilReady() {
err := util.WaitForPodsToAppear(KubeClient, t.Namespace, t.Labels)
if err != nil {
log.Fatalf("%+v", err)
}
util.WaitForPodsToBeRunningOrComplete(KubeClient, t.Namespace, t.Labels)
if err != nil {
log.Fatalf("%+v", err)
}
util.WaitForPodsToStopTerminating(KubeClient, t.Namespace)
if err != nil {
log.Fatalf("%+v", err)
}
}
// Verify ...
func (t OpsSightTester) Verify() error {
checks := []func() error{
t.checkPrometheusEnabled,
t.checkPodProcessorEnabled,
t.checkImageProcessorEnabled,
t.checkQuayProcessorEnabled,
t.checkArtifactoryProcessorEnabled,
}
var err error
for _, check := range checks {
err = check()
if err != nil {
return err
}
}
return nil
}
func (t OpsSightTester) getIDHelper(rtoName string) string {
// rtoName format: <name>-opssight-<resourcename>-12345678 || <name>-opssight-<resourcename>
// ID format: opssight-<resourcename>
rePattern := fmt.Sprintf("%s-(opssight-[a-z]+[-a-z]*[a-z])(-[0-9]+)*", t.Name)
r, _ := regexp.Compile(rePattern)
matches := r.FindStringSubmatch(rtoName)
if len(matches) >= 2 {
return matches[1]
}
return ""
}
func (t OpsSightTester) deploymentWithNameExists(deploymentName, namespace string) bool {
targetDeploymentID := deploymentName
found := false
deploymentList, err := util.ListDeployments(KubeClient, namespace, "")
if err != nil {
return false
}
for _, deployment := range deploymentList.Items {
deploymentID := t.getIDHelper(deployment.Name)
if deploymentID == targetDeploymentID {
found = true
break
}
}
return found
}
func (t OpsSightTester) podWithNameExists(podName, namespace string) bool {
targetPoID := podName
found := false
podList, err := util.ListPodsWithLabels(KubeClient, namespace, "")
if err != nil {
return false
}
for _, po := range podList.Items {
poID := t.getIDHelper(po.Name)
if poID == targetPoID {
found = true
break
}
}
return found
}
func (t OpsSightTester) serviceWithNameExists(serviceName, namespace string) bool {
targetServiceID := serviceName
found := false
serviceList, err := util.ListServices(KubeClient, namespace, "")
if err != nil {
return false
}
for _, svc := range serviceList.Items {
serviceID := t.getIDHelper(svc.Name)
if serviceID == targetServiceID {
found = true
break
}
}
return found
}
func (t OpsSightTester) configMapWithNameExists(configMapName, namespace string) bool {
targetConfigMapID := configMapName
found := false
configMapList, err := util.ListConfigMaps(KubeClient, namespace, "")
if err != nil {
return false
}
for _, cm := range configMapList.Items {
configMapID := t.getIDHelper(cm.Name)
if configMapID == targetConfigMapID {
found = true
break
}
}
return found
}
func (t OpsSightTester) serviceAccountWithNameExists(serviceAccountName, namespace string) bool {
targetServiceAccountID := serviceAccountName
found := false
serviceAccountList, err := util.ListServiceAccounts(KubeClient, namespace, "")
if err != nil {
return false
}
for _, sa := range serviceAccountList.Items {
serviceAccountID := t.getIDHelper(sa.Name)
if serviceAccountID == targetServiceAccountID {
found = true
break
}
}
return found
}
func (t OpsSightTester) clusterRoleBindingWithNameExists(clusterRoleBindingName string) bool {
targetClusterRoleBindingID := clusterRoleBindingName
found := false
clusterRoleBindingsList, err := util.ListClusterRoleBindings(KubeClient, "")
if err != nil {
return false
}
for _, crb := range clusterRoleBindingsList.Items {
clusterRoleBindingID := t.getIDHelper(crb.Name)
if clusterRoleBindingID == targetClusterRoleBindingID {
found = true
break
}
}
return found
}
func (t OpsSightTester) clusterRoleWithNameExists(clusterRoleName string) bool {
targetClusterRoleID := clusterRoleName
found := false
clusterRoleList, err := util.ListClusterRoles(KubeClient, "")
if err != nil {
return false
}
for _, cr := range clusterRoleList.Items {
clusterRoleID := t.getIDHelper(cr.Name)
if clusterRoleID == targetClusterRoleID {
found = true
break
}
}
return found
}
func (t OpsSightTester) checkPrometheusEnabled() error {
// Verify Deployment <name>-opssight-prometheus
foundDeployment := t.deploymentWithNameExists("opssight-prometheus", t.Namespace)
if strings.ToUpper(t.FlagTree.EnableMetrics) != strings.ToUpper(strconv.FormatBool(foundDeployment)) {
return fmt.Errorf("EnableMetrics is '%+v' but foundDeployment='%+v'", t.FlagTree.EnableMetrics, foundDeployment)
}
// Verify Service <name>-opssight-prometheus
foundMetricsService := t.serviceWithNameExists("opssight-prometheus", t.Namespace)
if strings.ToUpper(t.FlagTree.EnableMetrics) != strings.ToUpper(strconv.FormatBool(foundMetricsService)) {
return fmt.Errorf("EnableMetrics is '%+v' but foundMetricsService='%+v'", t.FlagTree.EnableMetrics, foundMetricsService)
}
// Verify ConfigMap <name>-opssight-prometheus
foundMetricsConfigMap := t.configMapWithNameExists("opssight-prometheus", t.Namespace)
if strings.ToUpper(t.FlagTree.EnableMetrics) != strings.ToUpper(strconv.FormatBool(foundMetricsConfigMap)) {
return fmt.Errorf("EnableMetrics is '%+v' but foundMetricsConfigMap='%+v'", t.FlagTree.EnableMetrics, foundMetricsConfigMap)
}
return nil
}
func (t OpsSightTester) checkPodProcessorEnabled() error {
// Verify Deployment <name>-opssight-pod-processor
foundDeployment := t.deploymentWithNameExists("opssight-pod-processor", t.Namespace)
if strings.ToUpper(t.FlagTree.PerceiverEnablePodPerceiver) != strings.ToUpper(strconv.FormatBool(foundDeployment)) {
return fmt.Errorf("PerceiverEnablePodPerceiver is '%+v' but foundDeployment='%+v'", t.FlagTree.PerceiverEnablePodPerceiver, foundDeployment)
}
// Verify Service <name>-opssight-pod-processor
foundService := t.serviceWithNameExists("opssight-pod-processor", t.Namespace)
if strings.ToUpper(t.FlagTree.PerceiverEnablePodPerceiver) != strings.ToUpper(strconv.FormatBool(foundService)) {
return fmt.Errorf("PerceiverEnablePodPerceiver is '%+v' but foundService='%+v'", t.FlagTree.PerceiverEnablePodPerceiver, foundService)
}
// Verify Service Account <name>-opssight-pod-processor
foundServiceAccount := t.serviceAccountWithNameExists("opssight-processor", t.Namespace)
if strings.ToUpper(t.FlagTree.PerceiverEnablePodPerceiver) != strings.ToUpper(strconv.FormatBool(foundServiceAccount)) {
return fmt.Errorf("PerceiverEnablePodPerceiver is '%+v' but foundServiceAccount='%+v'", t.FlagTree.PerceiverEnablePodPerceiver, foundServiceAccount)
}
// Verify Cluster Role <name>-opssight-pod-processor
foundClusterRole := t.clusterRoleWithNameExists("opssight-pod-processor")
if strings.ToUpper(t.FlagTree.PerceiverEnablePodPerceiver) != strings.ToUpper(strconv.FormatBool(foundClusterRole)) {
return fmt.Errorf("PerceiverEnablePodPerceiver is '%+v' but foundClusterRole='%+v'", t.FlagTree.PerceiverEnablePodPerceiver, foundClusterRole)
}
return nil
}
func (t OpsSightTester) checkImageProcessorEnabled() error {
// Verify Deployment <name>-opssight-image-processor
foundDeployment := t.deploymentWithNameExists("opssight-image-processor", t.Namespace)
if strings.ToUpper(t.FlagTree.PerceiverEnableImagePerceiver) != strings.ToUpper(strconv.FormatBool(foundDeployment)) {
return fmt.Errorf("PerceiverEnableImagePerceiver is '%+v' but foundDeployment='%+v'", t.FlagTree.PerceiverEnableImagePerceiver, foundDeployment)
}
// Verify Service <name>-opssight-image-processor
foundService := t.serviceWithNameExists("opssight-image-processor", t.Namespace)
if strings.ToUpper(t.FlagTree.PerceiverEnableImagePerceiver) != strings.ToUpper(strconv.FormatBool(foundService)) {
return fmt.Errorf("PerceiverEnableImagePerceiver is '%+v' but foundService='%+v'", t.FlagTree.PerceiverEnableImagePerceiver, foundService)
}
// Verify Cluster Role Binding <name>-opssight-image-processor
foundServiceAccount := t.clusterRoleBindingWithNameExists("opssight-image-processor")
if strings.ToUpper(t.FlagTree.PerceiverEnableImagePerceiver) != strings.ToUpper(strconv.FormatBool(foundServiceAccount)) {
return fmt.Errorf("PerceiverEnableImagePerceiver is '%+v' but foundServiceAccount='%+v'", t.FlagTree.PerceiverEnableImagePerceiver, foundServiceAccount)
}
// Verify Cluster Role <name>-opssight-image-processor
foundClusterRole := t.clusterRoleWithNameExists("opssight-image-processor")
if strings.ToUpper(t.FlagTree.PerceiverEnableImagePerceiver) != strings.ToUpper(strconv.FormatBool(foundClusterRole)) {
return fmt.Errorf("PerceiverEnableImagePerceiver is '%+v' but foundClusterRole='%+v'", t.FlagTree.PerceiverEnableImagePerceiver, foundClusterRole)
}
return nil
}
func (t OpsSightTester) checkQuayProcessorEnabled() error {
// Verify Deployment <name>-opssight-quay-processor
foundDeployment := t.deploymentWithNameExists("opssight-quay-processor", t.Namespace)
if strings.ToUpper(t.FlagTree.PerceiverEnableQuayPerceiver) != strings.ToUpper(strconv.FormatBool(foundDeployment)) {
return fmt.Errorf("PerceiverEnableQuayPerceiver is '%+v' but foundDeployment='%+v'", t.FlagTree.PerceiverEnableQuayPerceiver, foundDeployment)
}
// Verify Service <name>-opssight-quay-processor
foundService := t.serviceWithNameExists("opssight-quay-processor", t.Namespace)
if strings.ToUpper(t.FlagTree.PerceiverEnableQuayPerceiver) != strings.ToUpper(strconv.FormatBool(foundService)) {
return fmt.Errorf("PerceiverEnableQuayPerceiver is '%+v' but foundService='%+v'", t.FlagTree.PerceiverEnableQuayPerceiver, foundService)
}
return nil
}
func (t OpsSightTester) checkArtifactoryProcessorEnabled() error {
// Verify Deployment <name>-opssight-artifactory-processor
foundDeployment := t.deploymentWithNameExists("opssight-artifactory-processor", t.Namespace)
if strings.ToUpper(t.FlagTree.PerceiverEnableArtifactoryPerceiver) != strings.ToUpper(strconv.FormatBool(foundDeployment)) {
return fmt.Errorf("PerceiverEnableArtifactoryPerceiver is '%+v' but foundDeployment='%+v'", t.FlagTree.PerceiverEnableArtifactoryPerceiver, foundDeployment)
}
// Verify Service <name>-opssight-artifactory-processor
foundService := t.serviceWithNameExists("opssight-artifactory-processor", t.Namespace)
if strings.ToUpper(t.FlagTree.PerceiverEnableArtifactoryPerceiver) != strings.ToUpper(strconv.FormatBool(foundService)) {
return fmt.Errorf("PerceiverEnableArtifactoryPerceiver is '%+v' but foundService='%+v'", t.FlagTree.PerceiverEnableArtifactoryPerceiver, foundService)
}
return nil
}