-
Notifications
You must be signed in to change notification settings - Fork 11
/
run.go
535 lines (470 loc) · 17.1 KB
/
run.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package run
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
coclient "github.com/openshift/client-go/config/clientset/versioned"
irclient "github.com/openshift/client-go/imageregistry/clientset/versioned"
"github.com/pkg/errors"
"github.com/redhat-openshift-ecosystem/provider-certification-tool/pkg/version"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/sonobuoy/pkg/buildinfo"
sonobuoyclient "github.com/vmware-tanzu/sonobuoy/pkg/client"
"github.com/vmware-tanzu/sonobuoy/pkg/config"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin/loader"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin/manifest"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"github.com/redhat-openshift-ecosystem/provider-certification-tool/pkg"
"github.com/redhat-openshift-ecosystem/provider-certification-tool/pkg/client"
"github.com/redhat-openshift-ecosystem/provider-certification-tool/pkg/status"
"github.com/redhat-openshift-ecosystem/provider-certification-tool/pkg/wait"
)
type RunOptions struct {
plugins *[]string
dedicated bool
sonobuoyImage string
imageRepository string
// PluginsImage
// defines the image containing plugins associated with the provider-certification-tool.
// this variable is referenced by plugin manifest templates to dynamically reference the plugins image.
PluginsImage string
timeout int
watch bool
watchInterval int
devCount string
mode string
upgradeImage string
}
const (
defaultRunTimeoutSeconds = 21600
defaultRunMode = "regular"
defaultUpgradeImage = ""
defaultDedicatedFlag = true
defaultRunWatchFlag = false
)
func newRunOptions() *RunOptions {
return &RunOptions{
plugins: &[]string{},
}
}
func hideOptionalFlags(cmd *cobra.Command, flag string) {
err := cmd.Flags().MarkHidden(flag)
if err != nil {
log.Debugf("Unable to hide flag %s: %v", flag, err)
}
}
func NewCmdRun() *cobra.Command {
var err error
var kclient kubernetes.Interface
var sclient sonobuoyclient.Interface
o := newRunOptions()
cmd := &cobra.Command{
Use: "run",
Short: "Run the suite of tests for provider validation",
Long: `Launches the provider validation environment inside of an already running OpenShift cluster`,
PreRun: func(cmd *cobra.Command, args []string) {
// Client setup
kclient, sclient, err = client.CreateClients()
if err != nil {
log.Fatal(err)
}
// Pre-checks and setup
err = o.PreRunCheck(kclient)
if err != nil {
log.Fatal(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
log.Info("Running OPCT...")
// Fire off sonobuoy
err := o.Run(kclient, sclient)
if err != nil {
log.WithError(err).Fatal("Error running the tool. Please check the errors and try again.")
}
log.Info("Jobs scheduled! Waiting for resources be created...")
// Wait for Sonobuoy to create
err = wait.WaitForRequiredResources(kclient)
if err != nil {
log.WithError(err).Fatal("error waiting for sonobuoy pods to become ready")
}
// Sleep to give status time to appear
s := status.NewStatusOptions(&status.StatusInput{Watch: o.watch, IntervalSeconds: o.watchInterval})
time.Sleep(s.GetIntervalSeconds())
err = s.WaitForStatusReport(cmd.Context(), sclient)
if err != nil {
log.WithError(err).Fatal("error retrieving aggregator status")
}
err = s.Update(sclient)
if err != nil {
log.Fatal(err)
}
err = s.Print(cmd, sclient)
if err != nil {
log.Fatal(err)
}
if !o.watch {
log.Info("Sonobuoy pods are ready!")
}
},
}
cmd.Flags().BoolVar(&o.dedicated, "dedicated", defaultDedicatedFlag, "Setup plugins to run in dedicated test environment.")
cmd.Flags().StringVar(&o.devCount, "dev-count", "0", "Developer Mode only: run small random set of tests. Default: 0 (disabled)")
cmd.Flags().StringVar(&o.mode, "mode", defaultRunMode, "Run mode: Availble: regular, upgrade")
cmd.Flags().StringVar(&o.upgradeImage, "upgrade-to-image", defaultUpgradeImage, "Target OpenShift Release Image. Example: oc adm release info 4.11.18 -o jsonpath={.image}")
cmd.Flags().StringArrayVar(o.plugins, "plugin", nil, "Override default conformance plugins to use. Can be used multiple times. (default plugins can be reviewed with assets subcommand)")
cmd.Flags().StringVar(&o.sonobuoyImage, "sonobuoy-image", fmt.Sprintf("%s/sonobuoy:%s", pkg.DefaultToolsRepository, buildinfo.Version), "Image override for the Sonobuoy worker and aggregator")
cmd.Flags().StringVar(&o.PluginsImage, "plugins-image", pkg.PluginsImage, "Image containing plugins to be executed.")
cmd.Flags().StringVar(&o.imageRepository, "image-repository", "", "Image repository containing required images test environment. Example: openshift-provider-cert-tool --mirror-repository mirror.repository.net/ocp-cert")
cmd.Flags().IntVar(&o.timeout, "timeout", defaultRunTimeoutSeconds, "Execution timeout in seconds")
cmd.Flags().BoolVarP(&o.watch, "watch", "w", defaultRunWatchFlag, "Keep watch status after running")
cmd.Flags().IntVarP(&o.watchInterval, "watch-interval", "", status.DefaultStatusIntervalSeconds, "Interval to watch the status and print in the stdout")
// Hide optional flags
hideOptionalFlags(cmd, "dedicated")
hideOptionalFlags(cmd, "dev-count")
hideOptionalFlags(cmd, "plugins-image")
return cmd
}
// PreRunCheck performs some checks before kicking off Sonobuoy
func (r *RunOptions) PreRunCheck(kclient kubernetes.Interface) error {
coreClient := kclient.CoreV1()
rbacClient := kclient.RbacV1()
// Get ConfigV1 client for Cluster Operators
restConfig, err := client.CreateRestConfig()
if err != nil {
return err
}
configClient, err := coclient.NewForConfig(restConfig)
if err != nil {
return err
}
// Check if Cluster Operators are stable
errs := checkClusterOperators(configClient)
if errs != nil {
for _, err := range errs {
log.Warn(err)
}
return errors.New("All Cluster Operators must be available, not progressing, and not degraded before validation can run")
}
// Get ConfigV1 client for Cluster Operators
irClient, err := irclient.NewForConfig(restConfig)
if err != nil {
return err
}
// Check if Registry is in managed state or exit
managed, err := checkRegistry(irClient)
if err != nil {
return err
}
if !managed {
return errors.New("OpenShift Image Registry must deployed before validation can run")
}
// TODO: checkOrCreate MachineConfigPool with:
// - node selectors: node-role.kubernetes.io/tests=''
// - paused: true
// https://issues.redhat.com/browse/OPCT-35
// Check if sonobuoy namespace already exists
p, err := coreClient.Namespaces().Get(context.TODO(), pkg.CertificationNamespace, metav1.GetOptions{})
if err != nil {
// If error is due to namespace not being found, we continue.
if !kerrors.IsNotFound(err) {
return err
}
}
// sonobuoy namespace exists so return error
if p.Name != "" {
return errors.New(fmt.Sprintf("%s namespace already exists", pkg.CertificationNamespace))
}
namespace := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: pkg.CertificationNamespace,
Labels: pkg.SonobuoyDefaultLabels,
},
}
if r.dedicated {
log.Info("Ensuring proper node label for dedicated mode exists")
nodes, err := coreClient.Nodes().List(context.TODO(), metav1.ListOptions{
LabelSelector: pkg.DedicatedNodeRoleLabelSelector,
})
if err != nil {
return errors.Wrap(err, "error getting the Node list")
}
if nodes.Items != nil && len(nodes.Items) == 0 {
return errors.New("No nodes with role required for dedicated mode (node-role.kubernetes.io/tests)")
}
tolerations, err := json.Marshal([]v1.Toleration{{
Key: pkg.DedicatedNodeRoleLabel,
Operator: v1.TolerationOpExists,
Value: "",
Effect: v1.TaintEffectNoSchedule,
}})
if err != nil {
return errors.Wrap(err, "error creating namespace Tolerations")
}
namespace.Annotations = map[string]string{
"scheduler.alpha.kubernetes.io/defaultTolerations": string(tolerations),
"openshift.io/node-selector": pkg.DedicatedNodeRoleLabelSelector,
}
}
_, err = kclient.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "error creating Namespace")
}
// Create Sonobuoy ServiceAccount
// https://github.com/vmware-tanzu/sonobuoy/blob/main/pkg/client/gen.go#L611-L616
sa := &v1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: pkg.SonobuoyServiceAccountName,
Namespace: pkg.CertificationNamespace,
Labels: pkg.SonobuoyDefaultLabels,
},
}
sa.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "ServiceAccount",
})
_, err = kclient.CoreV1().ServiceAccounts(pkg.CertificationNamespace).Create(context.TODO(), sa, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "error creating ServiceAccount")
}
log.Info("Ensuring the tool will run in the privileged environment...")
// Configure custom RBAC
// Replacing Sonobuoy's default Admin RBAC not working correctly on upgrades.
// https://github.com/vmware-tanzu/sonobuoy/blob/5b97033257d0276c7b0d1b20412667a69d79261e/pkg/client/gen.go#L445-L481
cr := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: pkg.PrivilegedClusterRole,
Namespace: pkg.CertificationNamespace,
Labels: pkg.SonobuoyDefaultLabels,
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{"*"},
Resources: []string{"*"},
Verbs: []string{"*"},
},
{
NonResourceURLs: []string{"/metrics", "/logs", "/logs/*"},
Verbs: []string{"get"},
},
},
}
cr.SetGroupVersionKind(schema.GroupVersionKind{
Group: rbacv1.GroupName,
Version: "v1",
Kind: "ClusterRole",
})
_, err = rbacClient.ClusterRoles().Update(context.TODO(), cr, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "error creating privileged ClusterRole")
}
log.Infof("Created %s ClusterRole", pkg.PrivilegedClusterRole)
crb := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: pkg.PrivilegedClusterRoleBinding,
Namespace: pkg.CertificationNamespace,
Labels: pkg.SonobuoyDefaultLabels,
},
Subjects: []rbacv1.Subject{
{
Kind: rbacv1.ServiceAccountKind,
Name: pkg.SonobuoyServiceAccountName,
Namespace: pkg.CertificationNamespace,
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: "ClusterRole",
Name: pkg.PrivilegedClusterRole,
},
}
crb.SetGroupVersionKind(schema.GroupVersionKind{
Group: rbacv1.GroupName,
Version: "v1",
Kind: "ClusterRoleBinding",
})
_, err = rbacClient.ClusterRoleBindings().Update(context.TODO(), crb, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "error creating privileged ClusterRoleBinding")
}
log.Infof("Created %s ClusterRoleBinding", pkg.PrivilegedClusterRoleBinding)
// All good
return nil
}
// createConfigMap generic way to create the configMap on the certification namespace.
func (r *RunOptions) createConfigMap(kclient kubernetes.Interface, sclient sonobuoyclient.Interface, cm *v1.ConfigMap) error {
_, err := kclient.CoreV1().ConfigMaps(pkg.CertificationNamespace).Create(context.TODO(), cm, metav1.CreateOptions{})
if err != nil {
return err
}
return nil
}
// Run setup and provision the certification environment.
func (r *RunOptions) Run(kclient kubernetes.Interface, sclient sonobuoyclient.Interface) error {
var manifests []*manifest.Manifest
imageRepository := pkg.DefaultToolsRepository
defaultSonobuoyImage := fmt.Sprintf("%s/sonobuoy:%s", pkg.DefaultToolsRepository, buildinfo.Version)
overrideSonobuoyImageSet := r.sonobuoyImage != defaultSonobuoyImage
if r.imageRepository != "" {
// sonobuoy-image override is used in dev environment to
// test custom aggregator/worker image. Not allowed to be used in
// production environment validated by OPCT, for that reason the instruction is to
// mirror the sonobuoy image to /sonobuoy:version when deploying in
// disconnected environment.
if overrideSonobuoyImageSet {
log.Errorf("The image override --sonobuoy-image cannot be used with --image-repository")
os.Exit(1)
}
imageRepository = r.imageRepository
log.Infof("Mirror registry is configured %s ", r.imageRepository)
}
// the flag --sonobuoy-image should not be used in default validation.
if overrideSonobuoyImageSet {
log.Warn("Flag --sonobuoy-image is not supported in official validation process, unset it if you are submitting the results to Red Hat.")
} else {
r.sonobuoyImage = fmt.Sprintf("%s/sonobuoy:%s", imageRepository, buildinfo.Version)
}
r.PluginsImage = fmt.Sprintf("%s/%s", imageRepository, r.PluginsImage)
// Let Sonobuoy do some preflight checks before we run
errs := sclient.PreflightChecks(&sonobuoyclient.PreflightConfig{
Namespace: pkg.CertificationNamespace,
DNSNamespace: "openshift-dns",
DNSPodLabels: []string{"dns.operator.openshift.io/daemonset-dns=default"},
PreflightChecksSkip: []string{"existingnamespace"}, // Skip namespace check since we create it manually
})
if len(errs) > 0 {
for _, err := range errs {
log.Error(err)
}
return errors.New("preflight checks failed")
}
// Create version information ConfigMap
if err := r.createConfigMap(kclient, sclient, &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: pkg.VersionInfoConfigMapName,
Namespace: pkg.CertificationNamespace,
},
Data: map[string]string{
"cli-version": version.Version.Version,
"cli-commit": version.Version.Commit,
"sonobuoy-version": buildinfo.Version,
"sonobuoy-image": r.sonobuoyImage,
},
}); err != nil {
return err
}
configMapData := map[string]string{
"dev-count": r.devCount,
"run-mode": r.mode,
"upgrade-target-images": r.upgradeImage,
}
if len(r.imageRepository) > 0 {
configMapData["mirror-registry"] = r.imageRepository
}
if err := r.createConfigMap(kclient, sclient, &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: pkg.PluginsVarsConfigMapName,
Namespace: pkg.CertificationNamespace,
},
Data: configMapData,
}); err != nil {
return err
}
if r.plugins == nil || len(*r.plugins) == 0 {
// Use default built-in plugins
log.Debugf("Loading default plugins")
var err error
manifests, err = loadPluginManifests(r)
if err != nil {
return nil
}
} else {
// User provided their own plugins at command line
log.Debugf("Loading plugins specific at command line")
for _, p := range *r.plugins {
asset, err := loader.LoadDefinitionFromFile(p)
if err != nil {
return err
}
manifests = append(manifests, asset)
}
}
if len(manifests) == 0 {
return errors.New("No validation plugins to run")
}
// Fill out the aggregator and worker configs
aggConfig := config.New()
if r.timeout > 0 {
aggConfig.Aggregation.TimeoutSeconds = r.timeout
}
if r.sonobuoyImage != "" {
aggConfig.WorkerImage = r.sonobuoyImage
}
// Set aggregator deployment namespace
aggConfig.Namespace = pkg.CertificationNamespace
// Ignore Existing SA created on preflight
aggConfig.ExistingServiceAccount = true
aggConfig.ServiceAccountName = pkg.SonobuoyServiceAccountName
aggConfig.SecurityContextMode = "none"
// Fill out the Run configuration
runConfig := &sonobuoyclient.RunConfig{
GenConfig: sonobuoyclient.GenConfig{
Config: aggConfig,
EnableRBAC: false, // RBAC is created in preflight
ImagePullPolicy: config.DefaultSonobuoyPullPolicy,
StaticPlugins: manifests,
PluginEnvOverrides: nil, // TODO We'll use this later
},
}
err := sclient.Run(runConfig)
return err
}
func checkClusterOperators(configClient coclient.Interface) []error {
var result []error
// List all Cluster Operators
coList, err := configClient.ConfigV1().ClusterOperators().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return []error{err}
}
// Each Cluster Operator should be available, not progressing, and not degraded
for _, co := range coList.Items {
for _, cond := range co.Status.Conditions {
switch cond.Type {
case configv1.OperatorAvailable:
if cond.Status == configv1.ConditionFalse {
result = append(result, errors.Errorf("%s is unavailable", co.Name))
}
case configv1.OperatorProgressing:
if cond.Status == configv1.ConditionTrue {
result = append(result, errors.Errorf("%s is still progressing", co.Name))
}
case configv1.OperatorDegraded:
if cond.Status == configv1.ConditionTrue {
result = append(result, errors.Errorf("%s is in degraded state", co.Name))
}
}
}
}
return result
}
// Check registry is in managed state. We assume Cluster Operator is stable.
func checkRegistry(irClient irclient.Interface) (bool, error) {
irConfig, err := irClient.ImageregistryV1().Configs().Get(context.TODO(), "cluster", metav1.GetOptions{})
if err != nil {
return false, err
}
if irConfig.Spec.ManagementState != operatorv1.Managed {
return false, nil
}
return true, nil
}