/
apptest.go
825 lines (684 loc) · 22.6 KB
/
apptest.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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
package apptest
import (
"context"
"fmt"
"strings"
"time"
"github.com/giantswarm/apiextensions-application/api/v1alpha1"
"github.com/giantswarm/appcatalog"
"github.com/giantswarm/backoff"
"github.com/giantswarm/k8smetadata/pkg/label"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
const (
ControlPlaneCatalog = "control-plane-catalog"
ControlPlaneTestCatalog = "control-plane-test-catalog"
DefaultCatalog = "default"
DefaultTestCatalog = "default-test"
GiantSwarmCatalog = "giantswarm"
GiantSwarmTestCatalog = "giantswarm-test"
GiantSwarmOperationsPlatformCatalog = "giantswarm-operations-platform"
GiantSwarmOperationsPlatformTestCatalog = "giantswarm-operations-platform-test"
GiantSwarmPlaygroundCatalog = "giantswarm-playground"
GiantSwarmPlaygroundTestCatalog = "giantswarm-playground-test"
HelmStableCatalog = "helm-stable"
ReleasesCatalog = "releases"
ReleasesTestCatalog = "releases-test"
deployedStatus = "deployed"
failedStatus = "failed"
notInstalledStatus = "not-installed"
defaultNamespace = "giantswarm"
uniqueAppCRVersion = "0.0.0"
)
var (
giantSwarmCatalogs = map[string]string{
ControlPlaneCatalog: "https://giantswarm.github.io/control-plane-catalog/",
ControlPlaneTestCatalog: "https://giantswarm.github.io/control-plane-test-catalog/",
DefaultCatalog: "https://giantswarm.github.io/default-catalog/",
DefaultTestCatalog: "https://giantswarm.github.io/default-test-catalog/",
GiantSwarmCatalog: "https://giantswarm.github.io/giantswarm-catalog/",
GiantSwarmTestCatalog: "https://giantswarm.github.io/giantswarm-test-catalog/",
GiantSwarmOperationsPlatformCatalog: "https://giantswarm.github.io/giantswarm-operations-platform-catalog/",
GiantSwarmOperationsPlatformTestCatalog: "https://giantswarm.github.io/giantswarm-operations-platform-test-catalog/",
GiantSwarmPlaygroundCatalog: "https://giantswarm.github.io/giantswarm-playground-catalog/",
GiantSwarmPlaygroundTestCatalog: "https://giantswarm.github.io/giantswarm-playground-test-catalog/",
HelmStableCatalog: "https://charts.helm.sh/stable/packages/",
ReleasesCatalog: "https://giantswarm.github.io/releases-catalog/",
ReleasesTestCatalog: "https://giantswarm.github.io/releases-test-catalog/",
}
)
// Config represents the configuration used to setup the apps.
type Config struct {
KubeConfig string
KubeConfigPath string
Logger micrologger.Logger
Scheme *runtime.Scheme
}
// AppSetup implements the logic for managing the app setup.
type AppSetup struct {
ctrlClient client.Client
k8sClient kubernetes.Interface
logger micrologger.Logger
restConfig *rest.Config
}
// New creates a new configured app setup library.
func New(config Config) (*AppSetup, error) {
var err error
if config.KubeConfig == "" && config.KubeConfigPath == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.KubeConfig and %T.KubeConfigPath must not be empty at the same time", config, config)
}
if config.KubeConfig != "" && config.KubeConfigPath != "" {
return nil, microerror.Maskf(invalidConfigError, "%T.KubeConfig and %T.KubeConfigPath must not be set at the same time", config, config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
var restConfig *rest.Config
{
if config.KubeConfig != "" {
bytes := []byte(config.KubeConfig)
restConfig, err = clientcmd.RESTConfigFromKubeConfig(bytes)
if err != nil {
return nil, microerror.Mask(err)
}
} else if config.KubeConfigPath != "" {
restConfig, err = clientcmd.BuildConfigFromFlags("", config.KubeConfigPath)
if err != nil {
return nil, microerror.Mask(err)
}
} else {
// Shouldn't happen but returning error just in case.
return nil, microerror.Maskf(invalidConfigError, "%T.KubeConfig and %T.KubeConfigPath must not be empty at the same time", config, config)
}
}
var ctrlClient client.Client
{
if config.Scheme == nil {
config.Scheme = scheme.Scheme
}
// Extend the global client-go scheme which is used by all the tools under
// the hood. The scheme is required for the controller-runtime controller to
// be able to watch for runtime objects of a certain type.
appSchemeBuilder := runtime.SchemeBuilder(schemeBuilder{
v1alpha1.AddToScheme,
apiextensionsv1.AddToScheme,
})
err = appSchemeBuilder.AddToScheme(config.Scheme)
if err != nil {
return nil, microerror.Mask(err)
}
// Configure a dynamic rest mapper to the controller client so it can work
// with runtime objects of arbitrary types. Note that this is the default
// for controller clients created by controller-runtime managers.
// Anticipating a rather uncertain future and more breaking changes to come
// we want to separate client and manager. Thus we configure the client here
// properly on our own instead of relying on the manager to provide a
// client, which might change in the future.
mapper, err := apiutil.NewDynamicRESTMapper(rest.CopyConfig(restConfig))
if err != nil {
return nil, microerror.Mask(err)
}
ctrlClient, err = client.New(rest.CopyConfig(restConfig), client.Options{Scheme: config.Scheme, Mapper: mapper})
if err != nil {
return nil, microerror.Mask(err)
}
}
var k8sClient kubernetes.Interface
{
c := rest.CopyConfig(restConfig)
k8sClient, err = kubernetes.NewForConfig(c)
if err != nil {
return nil, microerror.Mask(err)
}
}
a := &AppSetup{
ctrlClient: ctrlClient,
k8sClient: k8sClient,
logger: config.Logger,
restConfig: restConfig,
}
return a, nil
}
// InstallApps creates catalog and app CRs for use in automated tests
// and ensures they are installed by our app platform.
func (a *AppSetup) InstallApps(ctx context.Context, apps []App) error {
var err error
err = a.createCatalogs(ctx, apps)
if err != nil {
return microerror.Mask(err)
}
err = a.createApps(ctx, apps)
if err != nil {
return microerror.Mask(err)
}
err = a.waitForDeployedApps(ctx, apps)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (a *AppSetup) UpgradeApp(ctx context.Context, current, desired App) error {
var err error
err = a.createCatalogs(ctx, []App{current, desired})
if err != nil {
return microerror.Mask(err)
}
// if current version have no specific version, use the latest instead.
if current.Version == "" && current.SHA == "" {
catalogURL, err := getCatalogURL(current)
if err != nil {
return microerror.Mask(err)
}
version, err := appcatalog.GetLatestVersion(ctx, catalogURL, current.Name, "")
if err != nil {
return microerror.Mask(err)
}
current.Version = version
}
err = a.createApps(ctx, []App{current})
if err != nil {
return microerror.Mask(err)
}
err = a.waitForDeployedApp(ctx, current)
if err != nil {
return microerror.Mask(err)
}
err = a.updateApp(ctx, desired)
if err != nil {
return microerror.Mask(err)
}
err = a.waitForDeployedApp(ctx, desired)
if err != nil {
return microerror.Mask(err)
}
return nil
}
// EnsureCRDs will register the passed CRDs in the k8s API used by the client.
func (a *AppSetup) EnsureCRDs(ctx context.Context, crds []*apiextensionsv1.CustomResourceDefinition) error {
var err error
for _, crd := range crds {
err = a.ensureCRD(ctx, crd)
if err != nil {
return microerror.Mask(err)
}
}
return nil
}
// K8sClient returns a Kubernetes clienset for use in automated tests.
func (a *AppSetup) K8sClient() kubernetes.Interface {
return a.k8sClient
}
// CtrlClient returns a controller-runtime client for use in automated tests.
func (a *AppSetup) CtrlClient() client.Client {
return a.ctrlClient
}
// RESTConfig returns a Kubernetes REST config for use in automated tests.
func (a *AppSetup) RESTConfig() *rest.Config {
return a.restConfig
}
func (a *AppSetup) CleanUp(ctx context.Context, apps []App) error {
for _, app := range apps {
err := a.ctrlClient.Delete(ctx, &v1alpha1.App{
ObjectMeta: metav1.ObjectMeta{
Namespace: app.AppCRNamespace,
Name: app.Name,
},
Spec: v1alpha1.AppSpec{},
})
if apierrors.IsNotFound(err) {
// it's ok
} else if err != nil {
return microerror.Mask(err)
}
var appCRNamespace string
if app.AppCRNamespace != "" {
appCRNamespace = app.AppCRNamespace
} else {
appCRNamespace = defaultNamespace
}
kubeconfigSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: app.Name,
Namespace: appCRNamespace,
},
}
err = a.ctrlClient.Delete(ctx, kubeconfigSecret)
if apierrors.IsNotFound(err) {
// it's ok
} else if err != nil {
return microerror.Mask(err)
}
userValuesConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: app.Name,
Namespace: appCRNamespace,
},
}
err = a.ctrlClient.Delete(ctx, userValuesConfigMap)
if apierrors.IsNotFound(err) {
// it's ok
} else if err != nil {
return microerror.Mask(err)
}
}
return nil
}
func (a *AppSetup) createApps(ctx context.Context, apps []App) error {
for _, app := range apps {
// Get app version based on whether a commit SHA or a version was
// provided.
version, err := a.getVersionForApp(ctx, app)
if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "creating %#q app cr from catalog %#q with version %#q", app.Name, app.CatalogName, version)
var appOperatorVersion string
if app.AppOperatorVersion != "" {
appOperatorVersion = app.AppOperatorVersion
} else {
// Processed by app-operator-unique instance.
appOperatorVersion = uniqueAppCRVersion
}
var appCRName string
if app.AppCRName != "" {
appCRName = app.AppCRName
} else {
appCRName = app.Name
}
var appCRNamespace string
if app.AppCRNamespace != "" {
appCRNamespace = app.AppCRNamespace
} else {
appCRNamespace = defaultNamespace
}
var kubeConfig v1alpha1.AppSpecKubeConfig
if app.KubeConfig != "" {
kubeConfigName := fmt.Sprintf("%s-kubeconfig", app.Name)
err := a.createKubeConfigSecret(ctx, kubeConfigName, appCRNamespace, app.KubeConfig)
if err != nil {
return microerror.Mask(err)
}
kubeConfig = v1alpha1.AppSpecKubeConfig{
Context: v1alpha1.AppSpecKubeConfigContext{
Name: kubeConfigName,
},
InCluster: false,
Secret: v1alpha1.AppSpecKubeConfigSecret{
Name: kubeConfigName,
Namespace: appCRNamespace,
},
}
} else {
kubeConfig = v1alpha1.AppSpecKubeConfig{
InCluster: true,
}
}
var userValuesConfigMap string
if app.ValuesYAML != "" {
userValuesConfigMap = fmt.Sprintf("%s-user-values", app.Name)
err := a.ensureUserValuesConfigMap(ctx, userValuesConfigMap, appCRNamespace, app.ValuesYAML)
if err != nil {
return microerror.Mask(err)
}
}
labels := map[string]string{
label.AppKubernetesName: app.Name,
}
if app.ClusterID != "" {
labels[label.Cluster] = app.ClusterID
} else {
labels[label.AppOperatorVersion] = appOperatorVersion
}
appCR := &v1alpha1.App{
ObjectMeta: metav1.ObjectMeta{
Name: appCRName,
Namespace: appCRNamespace,
Labels: labels,
},
Spec: v1alpha1.AppSpec{
Catalog: app.CatalogName,
KubeConfig: kubeConfig,
Name: app.Name,
Namespace: app.Namespace,
Version: version,
},
}
if app.ValuesYAML != "" {
appCR.Spec.UserConfig.ConfigMap.Name = userValuesConfigMap
appCR.Spec.UserConfig.ConfigMap.Namespace = appCRNamespace
}
err = a.ctrlClient.Create(ctx, appCR)
if apierrors.IsAlreadyExists(err) {
a.logger.Debugf(ctx, "%#q app CR already exists", appCR.Name)
return nil
} else if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "created %#q app cr", appCR.Name)
}
return nil
}
func (a *AppSetup) createCatalogs(ctx context.Context, apps []App) error {
for _, app := range apps {
catalogURL, err := getCatalogURL(app)
if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "creating %#q catalog cr", app.CatalogName)
catalogCR := &v1alpha1.Catalog{
ObjectMeta: metav1.ObjectMeta{
Name: app.CatalogName,
Namespace: metav1.NamespaceDefault,
Labels: map[string]string{
// Processed by app-operator-unique.
label.AppOperatorVersion: uniqueAppCRVersion,
},
},
Spec: v1alpha1.CatalogSpec{
Description: app.CatalogName,
Title: app.CatalogName,
Storage: v1alpha1.CatalogSpecStorage{
Type: "helm",
URL: catalogURL,
},
Repositories: []v1alpha1.CatalogSpecRepository{
{
Type: "helm",
URL: catalogURL,
},
},
},
}
err = a.ctrlClient.Create(ctx, catalogCR)
if apierrors.IsAlreadyExists(err) {
a.logger.Debugf(ctx, "%#q catalog CR already exists", catalogCR.Name)
} else if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "created %#q catalog cr", app.CatalogName)
}
return nil
}
func (a *AppSetup) createKubeConfigSecret(ctx context.Context, name, namespace, kubeConfig string) error {
a.logger.Debugf(ctx, "creating secret '%s/%s'", namespace, name)
data := map[string][]byte{
"kubeConfig": []byte(kubeConfig),
"value": []byte(kubeConfig),
}
desired := &corev1.Secret{
Data: data,
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
_, err := a.k8sClient.CoreV1().Secrets(namespace).Get(ctx, name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
_, err := a.k8sClient.CoreV1().Secrets(namespace).Create(ctx, desired, metav1.CreateOptions{})
if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "created secret '%s/%s'", namespace, name)
} else {
_, err := a.k8sClient.CoreV1().Secrets(namespace).Update(ctx, desired, metav1.UpdateOptions{})
if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "updated existing secret '%s/%s'", namespace, name)
}
return nil
}
func (a *AppSetup) ensureUserValuesConfigMap(ctx context.Context, name, namespace, valuesYAML string) error {
values := map[string]string{
"values": valuesYAML,
}
configMap := &corev1.ConfigMap{
Data: values,
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
_, err := a.k8sClient.CoreV1().ConfigMaps(namespace).Get(ctx, name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
a.logger.Debugf(ctx, "creating configmap '%s/%s'", namespace, name)
_, err := a.k8sClient.CoreV1().ConfigMaps(namespace).Create(ctx, configMap, metav1.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
a.logger.Debugf(ctx, "already created configmap '%s/%s'", namespace, name)
} else if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "created configmap '%s/%s'", namespace, name)
return nil
}
a.logger.Debugf(ctx, "updating configmap '%s/%s'", namespace, name)
_, err = a.k8sClient.CoreV1().ConfigMaps(namespace).Update(ctx, configMap, metav1.UpdateOptions{})
if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "updated configmap '%s/%s'", namespace, name)
return nil
}
func (a *AppSetup) ensureCRD(ctx context.Context, crd *apiextensionsv1.CustomResourceDefinition) error {
var err error
err = a.ctrlClient.Create(ctx, crd)
if apierrors.IsAlreadyExists(err) {
// It's ok.
} else if err != nil {
return microerror.Mask(err)
}
updatedCRD := &apiextensionsv1.CustomResourceDefinition{}
o := func() error {
err = a.ctrlClient.Get(ctx, types.NamespacedName{Name: crd.Name}, updatedCRD)
if err != nil {
return microerror.Mask(err)
}
for _, condition := range updatedCRD.Status.Conditions {
if condition.Type == "Established" {
// Fall through.
return nil
}
}
return microerror.Maskf(executionFailedError, "CRD %#q is not established yet", crd.Name)
}
n := func(err error, t time.Duration) {
a.logger.Errorf(ctx, err, "failed to get CRD '%s': retrying in %s", crd.Name, t)
}
b := backoff.NewExponential(1*time.Minute, 10*time.Second)
err = backoff.RetryNotify(o, b, n)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (a *AppSetup) updateApp(ctx context.Context, desired App) error {
var err error
var currentApp v1alpha1.App
var appCRName string
if desired.AppCRName != "" {
appCRName = desired.AppCRName
} else {
appCRName = desired.Name
}
var appCRNamespace string
if desired.AppCRNamespace != "" {
appCRNamespace = desired.AppCRNamespace
} else {
appCRNamespace = defaultNamespace
}
a.logger.Debugf(ctx, "finding %#q app in namespace %#q", appCRName, appCRNamespace)
err = a.ctrlClient.Get(
ctx,
types.NamespacedName{Name: appCRName, Namespace: appCRNamespace},
¤tApp)
if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "found %#q app in namespace %#q", appCRName, appCRNamespace)
desiredApp := currentApp.DeepCopy()
var version string
{
catalogURL, err := getCatalogURL(desired)
if err != nil {
return microerror.Mask(err)
}
var appVersion string
if desired.SHA != "" {
appVersion = desired.SHA
} else {
appVersion = desired.Version
}
version, err = appcatalog.GetLatestVersion(ctx, catalogURL, desired.Name, appVersion)
if err != nil {
return microerror.Mask(err)
}
}
desiredApp.Spec.Version = version
desiredApp.Spec.Catalog = desired.CatalogName
a.logger.Debugf(ctx, "updating %#q app cr in namespace %#q", currentApp.Name, appCRNamespace)
a.logger.Debugf(ctx, "desired version: %#q", version)
a.logger.Debugf(ctx, "desired catalog: %#q", desired.CatalogName)
err = a.ctrlClient.Update(
ctx,
desiredApp)
if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "updated %#q app cr in namespace %#q", currentApp.Name, appCRNamespace)
return nil
}
func (a *AppSetup) waitForDeployedApps(ctx context.Context, apps []App) error {
for _, app := range apps {
if app.WaitForDeploy {
err := a.waitForDeployedApp(ctx, app)
if err != nil {
return microerror.Mask(err)
}
} else {
a.logger.Debugf(ctx, "skipping wait for deploy of %#q app cr", app.Name)
}
}
return nil
}
func (a *AppSetup) waitForDeployedApp(ctx context.Context, testApp App) error {
var err error
var appCRName string
if testApp.AppCRName != "" {
appCRName = testApp.AppCRName
} else {
appCRName = testApp.Name
}
var appCRNamespace string
if testApp.AppCRNamespace != "" {
appCRNamespace = testApp.AppCRNamespace
} else {
appCRNamespace = defaultNamespace
}
a.logger.Debugf(ctx, "ensuring '%s/%s' app CR is %#q", appCRNamespace, appCRName, deployedStatus)
var app v1alpha1.App
o := func() error {
err = a.ctrlClient.Get(
ctx,
types.NamespacedName{Name: appCRName, Namespace: appCRNamespace},
&app)
if err != nil {
return microerror.Mask(err)
}
switch app.Status.Release.Status {
case notInstalledStatus, failedStatus:
return backoff.Permanent(microerror.Maskf(executionFailedError, "status %#q, reason: %s", app.Status.Release.Status, app.Status.Release.Reason))
case deployedStatus:
if testApp.SHA != "" && strings.HasSuffix(app.Status.Version, testApp.SHA) {
return nil
}
if testApp.Version != "" && testApp.Version == app.Status.Version {
return nil
}
var appVersion string
if testApp.SHA != "" {
appVersion = testApp.SHA
} else {
appVersion = testApp.Version
}
return microerror.Maskf(executionFailedError, "waiting for version contains %#q, current version %#q", appVersion, app.Status.Version)
}
return microerror.Maskf(executionFailedError, "waiting for %#q, current %#q", deployedStatus, app.Status.Release.Status)
}
n := func(err error, t time.Duration) {
a.logger.Errorf(ctx, err, "failed to get app CR status '%s': retrying in %s", deployedStatus, t)
}
b := backoff.NewConstant(20*time.Minute, 10*time.Second)
err = backoff.RetryNotify(o, b, n)
if err != nil {
return microerror.Mask(err)
}
a.logger.Debugf(ctx, "ensured '%s/%s' app CR is deployed", appCRNamespace, testApp.Name)
return nil
}
func (a *AppSetup) getLatestVersionForApp(ctx context.Context, catalogURL, appName, appSHA string) (string, error) {
var version string
var err error
o := func() error {
version, err = appcatalog.GetLatestVersion(ctx, catalogURL, appName, appSHA)
if err != nil {
return microerror.Mask(err)
}
return nil
}
b := backoff.NewConstant(5*time.Minute, 10*time.Second)
n := backoff.NewNotifier(a.logger, ctx)
err = backoff.RetryNotify(o, b, n)
if err != nil {
return "", microerror.Mask(err)
}
return version, nil
}
// getVersionForApp checks whether a commit SHA or a version was provided.
// If a SHA was provided then we check the test catalog to get the latest version.
// As for test catalogs the version format used is [latest version]-[sha].
// e.g. 0.2.0-ad12c88111d7513114a1257994634e2ae81115a2
//
// If a version is provided then this is returned. This is to allow app
// dependencies to be installed.
func (a *AppSetup) getVersionForApp(ctx context.Context, app App) (version string, err error) {
catalogURL, err := getCatalogURL(app)
if err != nil {
return "", microerror.Mask(err)
}
if app.SHA == "" && app.Version != "" {
return app.Version, nil
} else if app.SHA != "" && app.Version == "" {
return a.getLatestVersionForApp(ctx, catalogURL, app.Name, app.SHA)
} else if app.SHA != "" && app.Version != "" {
return "", microerror.Maskf(executionFailedError, "both SHA and Version cannot be provided")
}
return "", microerror.Maskf(executionFailedError, "either SHA or Version must be provided")
}
// getCatalogURL returns the catalog URL for this app. If it is a Giant Swarm
// catalog no URL needs to be provided.
func getCatalogURL(app App) (string, error) {
if app.CatalogName == "" {
return "", microerror.Maskf(invalidConfigError, "catalog name must not be empty for app %#v", app)
}
if app.CatalogName != "" && app.CatalogURL != "" {
return app.CatalogURL, nil
}
catalogURL, exists := giantSwarmCatalogs[app.CatalogName]
if !exists {
return "", microerror.Maskf(invalidConfigError, "catalog %#q not found and no URL provided", app.CatalogName)
}
return catalogURL, nil
}