-
Notifications
You must be signed in to change notification settings - Fork 67
/
utils.go
762 lines (697 loc) · 24.7 KB
/
utils.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
// Copyright (c) 2021 Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
package utils
import (
"context"
"encoding/json"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/ghodss/yaml"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
storagev1 "k8s.io/api/storage/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"
"k8s.io/klog"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func NewUnversionedRestClient(url, kubeconfig, ctx string) *rest.RESTClient {
klog.V(5).Infof("Create unversionedRestClient for url %s using kubeconfig path %s\n", url, kubeconfig)
config, err := LoadConfig(url, kubeconfig, ctx)
if err != nil {
panic(err)
}
oldNegotiatedSerializer := config.NegotiatedSerializer
config.NegotiatedSerializer = unstructuredscheme.NewUnstructuredNegotiatedSerializer()
kubeRESTClient, err := rest.UnversionedRESTClientFor(config)
// restore cfg before leaving
defer func(cfg *rest.Config) { cfg.NegotiatedSerializer = oldNegotiatedSerializer }(config)
if err != nil {
panic(err)
}
return kubeRESTClient
}
func NewKubeClient(url, kubeconfig, ctx string) kubernetes.Interface {
klog.V(5).Infof("Create kubeclient for url %s using kubeconfig path %s\n", url, kubeconfig)
config, err := LoadConfig(url, kubeconfig, ctx)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
return clientset
}
func NewKubeClientDynamic(url, kubeconfig, ctx string) dynamic.Interface {
klog.V(5).Infof("Create kubeclient dynamic for url %s using kubeconfig path %s\n", url, kubeconfig)
config, err := LoadConfig(url, kubeconfig, ctx)
if err != nil {
panic(err)
}
clientset, err := dynamic.NewForConfig(config)
if err != nil {
panic(err)
}
return clientset
}
func NewKubeClientAPIExtension(url, kubeconfig, ctx string) apiextensionsclientset.Interface {
klog.V(5).Infof("Create kubeclient apiextension for url %s using kubeconfig path %s\n", url, kubeconfig)
config, err := LoadConfig(url, kubeconfig, ctx)
if err != nil {
panic(err)
}
clientset, err := apiextensionsclientset.NewForConfig(config)
if err != nil {
panic(err)
}
return clientset
}
// func NewKubeClientDiscovery(url, kubeconfig, ctx string) *discovery.DiscoveryClient {
// klog.V(5).Infof("Create kubeclient discovery for url %s using kubeconfig path %s\n", url, kubeconfig)
// config, err := LoadConfig(url, kubeconfig, ctx)
// if err != nil {
// panic(err)
// }
// clientset, err := discovery.NewDiscoveryClientForConfig(config)
// if err != nil {
// panic(err)
// }
// return clientset
// }
func CreateMCOTestingRBAC(opt TestOptions) error {
// create new service account and new clusterrolebinding and bind the serviceaccount to cluster-admin clusterrole
// then the bearer token can be retrieved from the secret of created serviceaccount
mcoTestingCRBName := "mco-e2e-testing-crb"
mcoTestingSAName := "mco-e2e-testing-sa"
mcoTestingCRB := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: mcoTestingCRBName,
Labels: map[string]string{
"app": "mco-e2e-testing",
},
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: "cluster-admin",
APIGroup: "rbac.authorization.k8s.io",
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: mcoTestingSAName,
Namespace: MCO_NAMESPACE,
},
},
}
if err := CreateCRB(opt, true, mcoTestingCRB); err != nil {
return fmt.Errorf("failed to create clusterrolebing for %s: %v", mcoTestingCRB.GetName(), err)
}
mcoTestingSA := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: mcoTestingSAName,
Namespace: MCO_NAMESPACE,
},
}
if err := CreateSA(opt, true, MCO_NAMESPACE, mcoTestingSA); err != nil {
return fmt.Errorf("failed to create serviceaccount for %s: %v", mcoTestingSA.GetName(), err)
}
return nil
}
func DeleteMCOTestingRBAC(opt TestOptions) error {
// delete the created service account and clusterrolebinding
mcoTestingCRBName := "mco-e2e-testing-crb"
mcoTestingSAName := "mco-e2e-testing-sa"
if err := DeleteCRB(opt, true, mcoTestingCRBName); err != nil {
return err
}
if err := DeleteSA(opt, true, MCO_NAMESPACE, mcoTestingSAName); err != nil {
return err
}
return nil
}
func FetchBearerToken(opt TestOptions) (string, error) {
config, err := LoadConfig(
opt.HubCluster.ClusterServerURL,
opt.KubeConfig,
opt.HubCluster.KubeContext)
if err != nil {
return "", err
}
if config.BearerToken != "" {
return config.BearerToken, nil
}
clientKube := NewKubeClient(opt.HubCluster.ClusterServerURL, opt.KubeConfig, opt.HubCluster.KubeContext)
secretList, err := clientKube.CoreV1().
Secrets(MCO_NAMESPACE).
List(context.TODO(), metav1.ListOptions{FieldSelector: "type=kubernetes.io/service-account-token"})
if err != nil {
return "", err
}
for _, secret := range secretList.Items {
if secret.GetObjectMeta() != nil && len(secret.GetObjectMeta().GetAnnotations()) > 0 {
annos := secret.GetObjectMeta().GetAnnotations()
sa, saExists := annos["kubernetes.io/service-account.name"]
_, createByExists := annos["kubernetes.io/created-by"]
if saExists && !createByExists && sa == "mco-e2e-testing-sa" {
data := secret.Data
if token, ok := data["token"]; ok {
return string(token), nil
}
}
}
}
return "", fmt.Errorf("failed to get bearer token")
}
func LoadConfig(url, kubeconfig, ctx string) (*rest.Config, error) {
if kubeconfig == "" {
kubeconfig = os.Getenv("KUBECONFIG")
}
klog.V(5).Infof("Kubeconfig path %s\n", kubeconfig)
// If we have an explicit indication of where the kubernetes config lives, read that.
if kubeconfig != "" {
if ctx == "" {
// klog.V(5).Infof("clientcmd.BuildConfigFromFlags with %s and %s", url, kubeconfig)
return clientcmd.BuildConfigFromFlags(url, kubeconfig)
} else {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&clientcmd.ConfigOverrides{
CurrentContext: ctx,
}).ClientConfig()
}
}
// If not, try the in-cluster config.
if c, err := rest.InClusterConfig(); err == nil {
return c, nil
}
// If no in-cluster config, try the default location in the user's home directory.
if usr, err := user.Current(); err == nil {
klog.V(5).Infof("clientcmd.BuildConfigFromFlags for url %s using %s\n",
url,
filepath.Join(usr.HomeDir, ".kube", "config"))
if c, err := clientcmd.BuildConfigFromFlags(url, filepath.Join(usr.HomeDir, ".kube", "config")); err == nil {
return c, nil
}
}
return nil, fmt.Errorf("could not create a valid kubeconfig")
}
//Apply a multi resources file to the cluster described by the url, kubeconfig and ctx.
//url of the cluster
//kubeconfig which contains the ctx
//ctx, the ctx to use
//yamlB, a byte array containing the resources file
func Apply(url string, kubeconfig string, ctx string, yamlB []byte) error {
yamls := strings.Split(string(yamlB), "---")
// yamlFiles is an []string
for _, f := range yamls {
if len(strings.TrimSpace(f)) == 0 {
continue
}
obj := &unstructured.Unstructured{}
klog.V(5).Infof("obj:%v\n", obj.Object)
err := yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
var kind string
if v, ok := obj.Object["kind"]; !ok {
return fmt.Errorf("kind attribute not found in %s", f)
} else {
kind = v.(string)
}
klog.V(5).Infof("kind: %s\n", kind)
var apiVersion string
if v, ok := obj.Object["apiVersion"]; !ok {
return fmt.Errorf("apiVersion attribute not found in %s", f)
} else {
apiVersion = v.(string)
}
klog.V(5).Infof("apiVersion: %s\n", apiVersion)
clientKube := NewKubeClient(url, kubeconfig, ctx)
clientAPIExtension := NewKubeClientAPIExtension(url, kubeconfig, ctx)
// now use switch over the type of the object
// and match each type-case
switch kind {
case "CustomResourceDefinition":
klog.V(5).Infof("Install CRD: %s\n", f)
obj := &apiextensionsv1.CustomResourceDefinition{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientAPIExtension.ApiextensionsV1().
CustomResourceDefinitions().
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientAPIExtension.ApiextensionsV1().
CustomResourceDefinitions().
Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
existingObject.Spec = obj.Spec
klog.Warningf("CRD %s already exists, updating!", existingObject.Name)
_, err = clientAPIExtension.ApiextensionsV1().CustomResourceDefinitions().Update(context.TODO(), existingObject, metav1.UpdateOptions{})
}
case "Namespace":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &corev1.Namespace{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.CoreV1().
Namespaces().
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.CoreV1().Namespaces().Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s already exists, updating!", obj.Kind, obj.Name)
_, err = clientKube.CoreV1().Namespaces().Update(context.TODO(), existingObject, metav1.UpdateOptions{})
}
case "ServiceAccount":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &corev1.ServiceAccount{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.CoreV1().
ServiceAccounts(obj.Namespace).
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.CoreV1().
ServiceAccounts(obj.Namespace).
Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s/%s already exists, updating!", obj.Kind, obj.Namespace, obj.Name)
_, err = clientKube.CoreV1().ServiceAccounts(obj.Namespace).Update(context.TODO(), obj, metav1.UpdateOptions{})
}
case "ClusterRoleBinding":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &rbacv1.ClusterRoleBinding{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.RbacV1().
ClusterRoleBindings().
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.RbacV1().ClusterRoleBindings().Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s/%s already exists, updating!", obj.Kind, obj.Namespace, obj.Name)
_, err = clientKube.RbacV1().ClusterRoleBindings().Update(context.TODO(), obj, metav1.UpdateOptions{})
}
case "Secret":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &corev1.Secret{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.CoreV1().
Secrets(obj.Namespace).
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.CoreV1().Secrets(obj.Namespace).Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s/%s already exists, updating!", obj.Kind, obj.Namespace, obj.Name)
_, err = clientKube.CoreV1().Secrets(obj.Namespace).Update(context.TODO(), obj, metav1.UpdateOptions{})
}
case "ConfigMap":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &corev1.ConfigMap{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.CoreV1().
ConfigMaps(obj.Namespace).
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.CoreV1().
ConfigMaps(obj.Namespace).
Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s/%s already exists, updating!", obj.Kind, obj.Namespace, obj.Name)
_, err = clientKube.CoreV1().ConfigMaps(obj.Namespace).Update(context.TODO(), obj, metav1.UpdateOptions{})
}
case "Service":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &corev1.Service{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.CoreV1().
Services(obj.Namespace).
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.CoreV1().
Services(obj.Namespace).
Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s/%s already exists, updating!", obj.Kind, obj.Namespace, obj.Name)
_, err = clientKube.CoreV1().Services(obj.Namespace).Update(context.TODO(), obj, metav1.UpdateOptions{})
}
case "PersistentVolumeClaim":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &corev1.PersistentVolumeClaim{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.CoreV1().
PersistentVolumeClaims(obj.Namespace).
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.CoreV1().
PersistentVolumeClaims(obj.Namespace).
Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s/%s already exists, updating!", obj.Kind, obj.Namespace, obj.Name)
_, err = clientKube.CoreV1().PersistentVolumeClaims(obj.Namespace).Update(context.TODO(), obj, metav1.UpdateOptions{})
}
case "Deployment":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &appsv1.Deployment{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.AppsV1().
Deployments(obj.Namespace).
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.AppsV1().
Deployments(obj.Namespace).
Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s/%s already exists, updating!", obj.Kind, obj.Namespace, obj.Name)
_, err = clientKube.AppsV1().Deployments(obj.Namespace).Update(context.TODO(), obj, metav1.UpdateOptions{})
}
case "LimitRange":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &corev1.LimitRange{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.CoreV1().
LimitRanges(obj.Namespace).
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.CoreV1().
LimitRanges(obj.Namespace).
Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s/%s already exists, updating!", obj.Kind, obj.Namespace, obj.Name)
_, err = clientKube.CoreV1().LimitRanges(obj.Namespace).Update(context.TODO(), obj, metav1.UpdateOptions{})
}
case "ResourceQuota":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &corev1.ResourceQuota{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.CoreV1().
ResourceQuotas(obj.Namespace).
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.CoreV1().
ResourceQuotas(obj.Namespace).
Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s/%s already exists, updating!", obj.Kind, obj.Namespace, obj.Name)
_, err = clientKube.CoreV1().ResourceQuotas(obj.Namespace).Update(context.TODO(), obj, metav1.UpdateOptions{})
}
case "StorageClass":
klog.V(5).Infof("Install %s: %s\n", kind, f)
obj := &storagev1.StorageClass{}
err = yaml.Unmarshal([]byte(f), obj)
if err != nil {
return err
}
existingObject, errGet := clientKube.StorageV1().
StorageClasses().
Get(context.TODO(), obj.Name, metav1.GetOptions{})
if errGet != nil {
_, err = clientKube.StorageV1().StorageClasses().Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.ObjectMeta = existingObject.ObjectMeta
klog.Warningf("%s %s/%s already exists, updating!", obj.Kind, obj.Namespace, obj.Name)
_, err = clientKube.StorageV1().StorageClasses().Update(context.TODO(), obj, metav1.UpdateOptions{})
}
default:
switch kind {
case "MultiClusterObservability":
klog.V(5).Infof("Install MultiClusterObservability: %s\n", f)
default:
return fmt.Errorf("resource %s not supported", kind)
}
gvr := NewMCOGVRV1BETA2()
if apiVersion == "observability.open-cluster-management.io/v1beta1" {
gvr = NewMCOGVRV1BETA1()
}
// url string, kubeconfig string, ctx string
opt := TestOptions{
HubCluster: Cluster{
ClusterServerURL: url,
KubeContext: ctx,
},
KubeConfig: kubeconfig,
}
clientDynamic := NewKubeClientDynamic(url, kubeconfig, ctx)
if ns := obj.GetNamespace(); ns != "" {
existingObject, errGet := clientDynamic.Resource(gvr).
Namespace(ns).
Get(context.TODO(), obj.GetName(), metav1.GetOptions{})
if errGet != nil {
if ips, err := GetPullSecret(opt); err == nil {
obj.Object["spec"].(map[string]interface{})["imagePullSecret"] = ips
}
_, err = clientDynamic.Resource(gvr).
Namespace(ns).
Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.Object["metadata"] = existingObject.Object["metadata"]
klog.Warningf("%s %s/%s already exists, updating!", obj.GetKind(), obj.GetNamespace(), obj.GetName())
_, err = clientDynamic.Resource(gvr).Namespace(ns).Update(context.TODO(), obj, metav1.UpdateOptions{})
}
} else {
existingObject, errGet := clientDynamic.Resource(gvr).Get(context.TODO(), obj.GetName(), metav1.GetOptions{})
if errGet != nil {
if ips, err := GetPullSecret(opt); err == nil {
obj.Object["spec"].(map[string]interface{})["imagePullSecret"] = ips
}
_, err = clientDynamic.Resource(gvr).Create(context.TODO(), obj, metav1.CreateOptions{})
} else {
obj.Object["metadata"] = existingObject.Object["metadata"]
klog.Warningf("%s %s already exists, updating!", obj.GetKind(), obj.GetName())
_, err = clientDynamic.Resource(gvr).Update(context.TODO(), obj, metav1.UpdateOptions{})
}
}
}
if err != nil {
return err
}
}
return nil
}
//StatusContainsTypeEqualTo check if u contains a condition type with value typeString
func StatusContainsTypeEqualTo(u *unstructured.Unstructured, typeString string) bool {
if u != nil {
if v, ok := u.Object["status"]; ok {
status := v.(map[string]interface{})
if v, ok := status["conditions"]; ok {
conditions := v.([]interface{})
for _, v := range conditions {
condition := v.(map[string]interface{})
if v, ok := condition["type"]; ok {
if v.(string) == typeString {
return true
}
}
}
}
}
}
return false
}
//GetCluster returns the first cluster with a given tag
func GetCluster(tag string, clusters []Cluster) *Cluster {
for _, cluster := range clusters {
if tag, ok := cluster.Tags[tag]; ok {
if tag {
return &cluster
}
}
}
return nil
}
//GetClusters returns all clusters with a given tag
func GetClusters(tag string, clusters []Cluster) []*Cluster {
filteredClusters := make([]*Cluster, 0)
for i, cluster := range clusters {
if tag, ok := cluster.Tags[tag]; ok {
if tag {
filteredClusters = append(filteredClusters, &clusters[i])
}
}
}
return filteredClusters
}
func HaveServerResources(c Cluster, kubeconfig string, expectedAPIGroups []string) error {
clientAPIExtension := NewKubeClientAPIExtension(c.ClusterServerURL, kubeconfig, c.KubeContext)
clientDiscovery := clientAPIExtension.Discovery()
for _, apiGroup := range expectedAPIGroups {
klog.V(1).Infof("Check if %s exists", apiGroup)
_, err := clientDiscovery.ServerResourcesForGroupVersion(apiGroup)
if err != nil {
klog.V(1).Infof("Error while retrieving server resource %s: %s", apiGroup, err.Error())
return err
}
}
return nil
}
func HaveCRDs(c Cluster, kubeconfig string, expectedCRDs []string) error {
clientAPIExtension := NewKubeClientAPIExtension(c.ClusterServerURL, kubeconfig, c.KubeContext)
clientAPIExtensionV1 := clientAPIExtension.ApiextensionsV1()
for _, crd := range expectedCRDs {
klog.V(1).Infof("Check if %s exists", crd)
_, err := clientAPIExtensionV1.CustomResourceDefinitions().Get(context.TODO(), crd, metav1.GetOptions{})
if err != nil {
klog.V(1).Infof("Error while retrieving crd %s: %s", crd, err.Error())
return err
}
}
return nil
}
func HaveDeploymentsInNamespace(
c Cluster,
kubeconfig string,
namespace string,
expectedDeploymentNames []string,
) error {
client := NewKubeClient(c.ClusterServerURL, kubeconfig, c.KubeContext)
versionInfo, err := client.Discovery().ServerVersion()
if err != nil {
return err
}
klog.V(1).Infof("Server version info: %v", versionInfo)
deployments := client.AppsV1().Deployments(namespace)
for _, deploymentName := range expectedDeploymentNames {
klog.V(1).Infof("Check if deployment %s exists", deploymentName)
deployment, err := deployments.Get(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
klog.V(1).Infof("Error while retrieving deployment %s: %s", deploymentName, err.Error())
return err
}
if deployment.Status.Replicas != deployment.Status.ReadyReplicas {
err = fmt.Errorf("%s: Expect %d but got %d Ready replicas",
deploymentName,
deployment.Status.Replicas,
deployment.Status.ReadyReplicas)
klog.Errorln(err)
return err
}
for _, condition := range deployment.Status.Conditions {
if condition.Reason == "MinimumReplicasAvailable" {
if condition.Status != corev1.ConditionTrue {
err = fmt.Errorf("%s: Expect %s but got %s",
deploymentName,
condition.Status,
corev1.ConditionTrue)
klog.Errorln(err)
return err
}
}
}
}
return nil
}
func GetKubeVersion(client *rest.RESTClient) version.Info {
kubeVersion := version.Info{}
versionBody, err := client.Get().AbsPath("/version").Do(context.TODO()).Raw()
if err != nil {
klog.Errorf("fail to GET /version with %v", err)
return version.Info{}
}
err = json.Unmarshal(versionBody, &kubeVersion)
if err != nil {
klog.Errorf("fail to Unmarshal, got '%s': %v", string(versionBody), err)
return version.Info{}
}
return kubeVersion
}
func IsOpenshift(client *rest.RESTClient) bool {
//check whether the cluster is openshift or not for openshift version 3.11 and before
_, err := client.Get().AbsPath("/version/openshift").Do(context.TODO()).Raw()
if err == nil {
klog.V(5).Info("Found openshift version from /version/openshift")
return true
}
//check whether the cluster is openshift or not for openshift version 4.1
_, err = client.Get().AbsPath("/apis/config.openshift.io/v1/clusterversions").Do(context.TODO()).Raw()
if err == nil {
klog.V(5).Info("Found openshift version from /apis/config.openshift.io/v1/clusterversions")
return true
}
klog.V(5).Infof("fail to GET openshift version, assuming not OpenShift: %s", err.Error())
return false
}
// IntegrityChecking checks to ensure all required conditions are met when completing the specs
func IntegrityChecking(opt TestOptions) error {
return CheckMCOComponents(opt)
}
// GetPullSecret checks the secret from MCH CR and return the secret name
func GetPullSecret(opt TestOptions) (string, error) {
clientDynamic := NewKubeClientDynamic(
opt.HubCluster.ClusterServerURL,
opt.KubeConfig,
opt.HubCluster.KubeContext)
mchList, err := clientDynamic.Resource(NewOCMMultiClusterHubGVR()).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return "", err
}
if len(mchList.Items) == 0 {
return "", fmt.Errorf("can not find the MCH operator CR in the cluster")
}
mchName := mchList.Items[0].GetName()
mchNs := mchList.Items[0].GetNamespace()
getMCH, err := clientDynamic.Resource(NewOCMMultiClusterHubGVR()).
Namespace(mchNs).
Get(context.TODO(), mchName, metav1.GetOptions{})
if err != nil {
return "", err
}
spec := getMCH.Object["spec"].(map[string]interface{})
if _, ok := spec["imagePullSecret"]; !ok {
return "", fmt.Errorf("can not find imagePullSecret in MCH CR")
}
ips := spec["imagePullSecret"].(string)
return ips, nil
}