-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathgitopsservice_controller_test.go
682 lines (546 loc) · 25 KB
/
gitopsservice_controller_test.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
/*
Copyright 2021.
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.
*/
package controllers
import (
"context"
"fmt"
"testing"
argoapp "github.com/argoproj-labs/argocd-operator/api/v1beta1"
argocommon "github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/controllers/argocd"
"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
configv1 "github.com/openshift/api/config/v1"
consolev1 "github.com/openshift/api/console/v1"
routev1 "github.com/openshift/api/route/v1"
pipelinesv1alpha1 "github.com/redhat-developer/gitops-operator/api/v1alpha1"
"github.com/redhat-developer/gitops-operator/common"
"github.com/redhat-developer/gitops-operator/controllers/util"
"gotest.tools/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
resourcev1 "k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
func TestImageFromEnvVariable(t *testing.T) {
ns := types.NamespacedName{Name: "test", Namespace: "test"}
t.Run("Image present as env variable", func(t *testing.T) {
image := "quay.io/org/test"
t.Setenv(backendImageEnvName, image)
deployment := newBackendDeployment(ns)
got := deployment.Spec.Template.Spec.Containers[0].Image
if got != image {
t.Errorf("Image mismatch: got %s, want %s", got, image)
}
})
t.Run("env variable for image not found", func(t *testing.T) {
deployment := newBackendDeployment(ns)
got := deployment.Spec.Template.Spec.Containers[0].Image
if got != backendImage {
t.Errorf("Image mismatch: got %s, want %s", got, backendImage)
}
})
}
func TestReconcileDefaultForArgoCDNodeplacement(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
var err error
gitopsService := &pipelinesv1alpha1.GitopsService{
ObjectMeta: v1.ObjectMeta{
Name: serviceName,
},
Spec: pipelinesv1alpha1.GitopsServiceSpec{
NodeSelector: map[string]string{
"foo": "bar",
},
},
}
fakeClient := fake.NewFakeClient(gitopsService)
reconciler := newReconcileGitOpsService(fakeClient, s)
existingArgoCD := &argoapp.ArgoCD{
ObjectMeta: v1.ObjectMeta{
Name: serviceNamespace,
Namespace: serviceNamespace,
},
Spec: argoapp.ArgoCDSpec{
Server: argoapp.ArgoCDServerSpec{
Route: argoapp.ArgoCDRouteSpec{
Enabled: true,
},
},
ApplicationSet: &argoapp.ArgoCDApplicationSet{},
SSO: &argoapp.ArgoCDSSOSpec{
Provider: "dex",
Dex: &argoapp.ArgoCDDexSpec{
Config: "test-config",
},
},
},
}
err = fakeClient.Create(context.TODO(), existingArgoCD)
assertNoError(t, err)
_, err = reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
// verify whether existingArgoCD NodePlacement is updated when user is patching nodePlacement through GitopsService CR
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: common.ArgoCDInstanceName, Namespace: serviceNamespace},
existingArgoCD)
assertNoError(t, err)
assert.Check(t, existingArgoCD.Spec.NodePlacement != nil)
assert.DeepEqual(t, existingArgoCD.Spec.NodePlacement.NodeSelector, gitopsService.Spec.NodeSelector)
}
// If the DISABLE_DEFAULT_ARGOCD_INSTANCE is set, ensure that the default ArgoCD instance is not created.
func TestReconcileDisableDefault(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
var err error
fakeClient := fake.NewFakeClient(newGitopsService())
reconciler := newReconcileGitOpsService(fakeClient, s)
reconciler.DisableDefaultInstall = true
_, err = reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
argoCD := &argoapp.ArgoCD{}
// ArgoCD instance SHOULD NOT created (in openshift-gitops namespace)
if err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: common.ArgoCDInstanceName, Namespace: serviceNamespace},
argoCD); err == nil || !errors.IsNotFound(err) {
t.Fatalf("ArgoCD instance should not exist in namespace, error: %v", err)
}
// openshift-gitops namespace SHOULD be created
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceNamespace}, &corev1.Namespace{})
assertNoError(t, err)
// backend Deployment SHOULD be created
deploy := &appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: serviceNamespace}, deploy)
assertNoError(t, err)
}
// If the DISABLE_DEFAULT_ARGOCD_INSTANCE is set, ensure that the default ArgoCD instance is deleted if it already exists.
func TestReconcileDisableDefault_DeleteIfAlreadyExists(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
var err error
fakeClient := fake.NewFakeClient(newGitopsService())
reconciler := newReconcileGitOpsService(fakeClient, s)
reconciler.DisableDefaultInstall = false
_, err = reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
argoCD := &argoapp.ArgoCD{}
// ArgoCD instance SHOULD be created (in openshift-gitops namespace)
if err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: common.ArgoCDInstanceName, Namespace: serviceNamespace},
argoCD); err != nil {
t.Fatalf("ArgoCD instance should exist in namespace, error: %v", err)
}
reconciler.DisableDefaultInstall = true
_, err = reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
// ArgoCD instance SHOULD be deleted from namespace (in openshift-gitops namespace)
if err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: common.ArgoCDInstanceName, Namespace: serviceNamespace},
argoCD); err == nil || !errors.IsNotFound(err) {
t.Fatalf("ArgoCD instance should not exist in namespace, error: %v", err)
}
// openshift-gitops namespace SHOULD still exist
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceNamespace}, &corev1.Namespace{})
assertNoError(t, err)
// backend Deployment SHOULD still exist
deploy := &appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: serviceNamespace}, deploy)
assertNoError(t, err)
}
func TestReconcile(t *testing.T) {
defer util.SetConsoleAPIFound(util.IsConsoleAPIFound())
util.SetConsoleAPIFound(true)
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.15.1"), newGitopsService())
reconciler := newReconcileGitOpsService(fakeClient, s)
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
// Check if backend resources are created in openshift-gitops namespace
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceNamespace}, &corev1.Namespace{})
assertNoError(t, err)
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsServicePrefix + serviceName, Namespace: serviceNamespace}, &corev1.ServiceAccount{})
assertNoError(t, err)
role := &rbacv1.ClusterRole{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsServicePrefix + serviceName}, role)
assertNoError(t, err)
assert.DeepEqual(t, role.Rules, policyRuleForBackendServiceClusterRole())
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsServicePrefix + serviceName}, &rbacv1.ClusterRoleBinding{})
assertNoError(t, err)
deploy := &appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: serviceNamespace}, deploy)
assertNoError(t, err)
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: serviceNamespace}, &corev1.Service{})
assertNoError(t, err)
// Check if argocd instance is created in openshift-gitops namespace
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: "openshift-gitops", Namespace: serviceNamespace}, &argoapp.ArgoCD{})
assertNoError(t, err)
// update Cluster Role and Backend Deployment
updatedPolicyRules := policyRuleForBackendServiceClusterRole()
updatedPolicyRules[0].Resources = append(updatedPolicyRules[0].Resources, "testResource")
role.Rules = updatedPolicyRules
err = fakeClient.Update(context.TODO(), role)
assertNoError(t, err)
deploy.Spec.Template.Spec.Containers[0].Image = "newTestImage:test"
err = fakeClient.Update(context.TODO(), deploy)
assertNoError(t, err)
// reconcile
_, err = reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsServicePrefix + serviceName}, role)
assertNoError(t, err)
assert.DeepEqual(t, role.Rules, policyRuleForBackendServiceClusterRole())
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: serviceNamespace}, deploy)
assertNoError(t, err)
assert.DeepEqual(t, deploy.Spec.Template.Spec.Containers[0].Image, backendImage)
// Check if plugin instance is created in openshift-gitops namespace
consolePlugin := &consolev1.ConsolePlugin{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName}, consolePlugin)
assertNoError(t, err)
assert.DeepEqual(t, consolePlugin.Spec.Backend.Service.Name, gitopsPluginName)
pluginDeploy := &appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, pluginDeploy)
assertNoError(t, err)
pluginService := &corev1.Service{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, pluginService)
assertNoError(t, err)
pluginConfigMap := &corev1.ConfigMap{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: httpdConfigMapName, Namespace: serviceNamespace}, pluginConfigMap)
assertNoError(t, err)
}
func TestReconcile_AppDeliveryNamespace(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.6.15"), newGitopsService())
reconciler := newReconcileGitOpsService(fakeClient, s)
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
// Check if both openshift-gitops and openshift-pipelines-app-delivey namespace is created
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: deprecatedServiceNamespace}, &corev1.Namespace{})
assertNoError(t, err)
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceNamespace}, &corev1.Namespace{})
assertNoError(t, err)
// Check if backend resources are created in openshift-pipelines-app-delivery namespace
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: deprecatedServiceNamespace}, &corev1.Namespace{})
assertNoError(t, err)
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: deprecatedServiceNamespace}, &appsv1.Deployment{})
assertNoError(t, err)
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: deprecatedServiceNamespace}, &corev1.Service{})
assertNoError(t, err)
// Check if argocd instance is created in openshift-gitops namespace
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: "openshift-gitops", Namespace: serviceNamespace}, &argoapp.ArgoCD{})
assertNoError(t, err)
}
func TestReconcile_consoleAPINotFound(t *testing.T) {
defer util.SetConsoleAPIFound(util.IsConsoleAPIFound())
util.SetConsoleAPIFound(false)
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
fakeClient := fake.NewFakeClient(newGitopsService())
reconciler := newReconcileGitOpsService(fakeClient, s)
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
// Check consolePlugin and other resources are not created
consolePlugin := &consolev1.ConsolePlugin{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName}, consolePlugin)
assert.Error(t, err, "consoleplugins.console.openshift.io \"gitops-plugin\" not found")
pluginDeploy := &appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, pluginDeploy)
assert.Error(t, err, "deployments.apps \"gitops-plugin\" not found")
pluginService := &corev1.Service{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, pluginService)
assert.Error(t, err, "services \"gitops-plugin\" not found")
pluginConfigMap := &corev1.ConfigMap{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: httpdConfigMapName, Namespace: serviceNamespace}, pluginConfigMap)
assert.Error(t, err, "configmaps \"httpd-cfg\" not found")
}
func TestReconcile_ocpVersionLowerThan4_15(t *testing.T) {
defer util.SetConsoleAPIFound(util.IsConsoleAPIFound())
util.SetConsoleAPIFound(false)
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.11.1"), newGitopsService())
reconciler := newReconcileGitOpsService(fakeClient, s)
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
// Check consolePlugin and other resources are not created
consolePlugin := &consolev1.ConsolePlugin{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName}, consolePlugin)
assert.Error(t, err, "consoleplugins.console.openshift.io \"gitops-plugin\" not found")
pluginDeploy := &appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, pluginDeploy)
assert.Error(t, err, "deployments.apps \"gitops-plugin\" not found")
pluginService := &corev1.Service{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, pluginService)
assert.Error(t, err, "services \"gitops-plugin\" not found")
pluginConfigMap := &corev1.ConfigMap{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: httpdConfigMapName, Namespace: serviceNamespace}, pluginConfigMap)
assert.Error(t, err, "configmaps \"httpd-cfg\" not found")
}
func TestReconcile_GitOpsNamespace(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.7.1"), newGitopsService())
reconciler := newReconcileGitOpsService(fakeClient, s)
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
// Check if only openshift-gitops namespace is created
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceNamespace}, &corev1.Namespace{})
assertNoError(t, err)
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: deprecatedServiceNamespace}, &corev1.Namespace{})
wantErr := `namespaces "openshift-pipelines-app-delivery" not found`
if err == nil {
t.Fatalf("was expecting an error %s, but got nil", wantErr)
}
}
func TestReconcile_BackendResourceLimits(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(util.NewClusterVersion("4.7.1"), newGitopsService()).Build()
reconciler := newReconcileGitOpsService(fakeClient, s)
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
deployment := appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: serviceNamespace}, &deployment)
assertNoError(t, err)
resources := deployment.Spec.Template.Spec.Containers[0].Resources
assert.Equal(t, resources.Requests[corev1.ResourceCPU], resourcev1.MustParse("250m"))
assert.Equal(t, resources.Requests[corev1.ResourceMemory], resourcev1.MustParse("128Mi"))
assert.Equal(t, resources.Limits[corev1.ResourceCPU], resourcev1.MustParse("500m"))
assert.Equal(t, resources.Limits[corev1.ResourceMemory], resourcev1.MustParse("256Mi"))
}
func TestReconcile_BackendSecurityContext(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
// Testing on OCP versions < 4.11.0
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(util.NewClusterVersion("4.10.1"), newGitopsService()).Build()
reconciler := newReconcileGitOpsService(fakeClient, s)
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
deployment := appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: serviceNamespace}, &deployment)
assertNoError(t, err)
// Testing on OCP versions < 4.11.0
fakeClient = fake.NewClientBuilder().WithScheme(s).WithObjects(util.NewClusterVersion("4.12.1"), newGitopsService()).Build()
reconciler = newReconcileGitOpsService(fakeClient, s)
_, err = reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
deployment = appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: serviceNamespace}, &deployment)
assertNoError(t, err)
securityContext := deployment.Spec.Template.Spec.Containers[0].SecurityContext
want := &corev1.SecurityContext{
AllowPrivilegeEscalation: util.BoolPtr(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
RunAsNonRoot: util.BoolPtr(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
assert.DeepEqual(t, securityContext, want)
}
func TestReconcile_testArgoCDForOperatorUpgrade(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(util.NewClusterVersion("4.7.1"), newGitopsService()).Build()
reconciler := newReconcileGitOpsService(fakeClient, s)
// Create a basic ArgoCD CR. ArgoCD created by Operator version >= v1.6.0
existingArgoCD := &argoapp.ArgoCD{
ObjectMeta: v1.ObjectMeta{
Name: serviceNamespace,
Namespace: serviceNamespace,
},
Spec: argoapp.ArgoCDSpec{
Server: argoapp.ArgoCDServerSpec{
Route: argoapp.ArgoCDRouteSpec{
Enabled: true,
},
},
ApplicationSet: &argoapp.ArgoCDApplicationSet{},
SSO: &argoapp.ArgoCDSSOSpec{
Provider: "dex",
Dex: &argoapp.ArgoCDDexSpec{
Config: "test-config",
},
},
},
}
err := fakeClient.Create(context.TODO(), existingArgoCD)
assertNoError(t, err)
_, err = reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
// ArgoCD instance SHOULD be updated with resource request/limits for each workload.
updateArgoCD := &argoapp.ArgoCD{}
if err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: common.ArgoCDInstanceName, Namespace: serviceNamespace},
updateArgoCD); err != nil {
t.Fatalf("ArgoCD instance should exist in namespace, error: %v", err)
}
assert.Check(t, updateArgoCD.Spec.ApplicationSet.Resources != nil)
assert.Check(t, updateArgoCD.Spec.Controller.Resources != nil)
assert.Check(t, updateArgoCD.Spec.SSO.Dex.Resources != nil)
assert.Check(t, updateArgoCD.Spec.Grafana.Resources != nil)
assert.Check(t, updateArgoCD.Spec.HA.Resources != nil)
assert.Check(t, updateArgoCD.Spec.Redis.Resources != nil)
assert.Check(t, updateArgoCD.Spec.Repo.Resources != nil)
assert.Check(t, updateArgoCD.Spec.Server.Resources != nil)
}
func TestReconcile_VerifyResourceQuotaDeletionForUpgrade(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
fakeClient := fake.NewClientBuilder().WithScheme(s).WithObjects(util.NewClusterVersion("4.7.1"), newGitopsService()).Build()
reconciler := newReconcileGitOpsService(fakeClient, s)
// Create namespace object for default ArgoCD instance and set resource quota to it.
defaultArgoNS := &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: serviceNamespace,
},
}
fakeClient.Create(context.TODO(), defaultArgoNS)
dummyResourceObj := &corev1.ResourceQuota{
ObjectMeta: v1.ObjectMeta{
Name: fmt.Sprintf("%s-compute-resources", serviceNamespace),
Namespace: serviceNamespace,
},
}
fakeClient.Create(context.TODO(), dummyResourceObj)
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
// Verify that resource quota object is deleted after reconciliation.
resourceQuota := corev1.ResourceQuota{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceNamespace + "-compute-resources", Namespace: serviceNamespace}, &resourceQuota)
assert.Error(t, err, "resourcequotas \"openshift-gitops-compute-resources\" not found")
}
func TestGetBackendNamespace(t *testing.T) {
logf.SetLogger(zap.New(zap.UseDevMode(true)))
s := scheme.Scheme
addKnownTypesToScheme(s)
assertNamespace := func(t *testing.T, err error, got, want string) {
t.Helper()
assertNoError(t, err)
if got != want {
t.Fatalf("namespace mismatch: got %s, want %s", got, want)
}
}
t.Run("Using a 4.7 Cluster", func(t *testing.T) {
fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.7.1"), newGitopsService())
namespace, err := GetBackendNamespace(fakeClient)
assertNamespace(t, err, namespace, serviceNamespace)
})
t.Run("Using a 4.6 Cluster", func(t *testing.T) {
fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.6.1"), newGitopsService())
namespace, err := GetBackendNamespace(fakeClient)
assertNamespace(t, err, namespace, deprecatedServiceNamespace)
})
t.Run("Using a 4.X Cluster", func(t *testing.T) {
fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.X.1"), newGitopsService())
namespace, err := GetBackendNamespace(fakeClient)
assertNamespace(t, err, namespace, serviceNamespace)
})
}
func TestReconcile_InfrastructureNode(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)
gitopsService := &pipelinesv1alpha1.GitopsService{
ObjectMeta: v1.ObjectMeta{
Name: serviceName,
},
Spec: pipelinesv1alpha1.GitopsServiceSpec{
RunOnInfra: true,
Tolerations: deploymentDefaultTolerations(),
},
}
fakeClient := fake.NewFakeClient(gitopsService)
reconciler := newReconcileGitOpsService(fakeClient, s)
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)
deployment := appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: serviceNamespace}, &deployment)
assertNoError(t, err)
nSelector := common.InfraNodeSelector()
argoutil.AppendStringMap(nSelector, argocommon.DefaultNodeSelector())
assert.DeepEqual(t, deployment.Spec.Template.Spec.NodeSelector, nSelector)
assert.DeepEqual(t, deployment.Spec.Template.Spec.Tolerations, deploymentDefaultTolerations())
argoCD := &argoapp.ArgoCD{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: common.ArgoCDInstanceName, Namespace: serviceNamespace},
argoCD)
assertNoError(t, err)
assert.DeepEqual(t, argoCD.Spec.NodePlacement.NodeSelector, common.InfraNodeSelector())
assert.DeepEqual(t, argoCD.Spec.NodePlacement.Tolerations, deploymentDefaultTolerations())
}
func addKnownTypesToScheme(scheme *runtime.Scheme) {
scheme.AddKnownTypes(configv1.GroupVersion, &configv1.ClusterVersion{})
scheme.AddKnownTypes(pipelinesv1alpha1.GroupVersion, &pipelinesv1alpha1.GitopsService{})
scheme.AddKnownTypes(argoapp.GroupVersion, &argoapp.ArgoCD{})
scheme.AddKnownTypes(consolev1.GroupVersion, &consolev1.ConsoleCLIDownload{})
scheme.AddKnownTypes(routev1.GroupVersion, &routev1.Route{})
scheme.AddKnownTypes(consolev1.GroupVersion, &consolev1.ConsolePlugin{})
}
func newReconcileGitOpsService(client client.Client, scheme *runtime.Scheme) *ReconcileGitopsService {
return &ReconcileGitopsService{
Client: client,
Scheme: scheme,
}
}
func newRequest(namespace, name string) reconcile.Request {
return reconcile.Request{
NamespacedName: types.NamespacedName{
Name: name,
Namespace: namespace,
},
}
}
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
func deploymentDefaultTolerations() []corev1.Toleration {
toleration := []corev1.Toleration{
{
Key: "test_key1",
Value: "test_value1",
Effect: corev1.TaintEffectNoSchedule,
},
{
Key: "test_key2",
Value: "test_value2",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoSchedule,
},
}
return toleration
}