-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathstatefulset.go
More file actions
1138 lines (1034 loc) · 34.7 KB
/
statefulset.go
File metadata and controls
1138 lines (1034 loc) · 34.7 KB
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 ArgoCD Operator Developers
//
// 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 argocd
import (
"context"
"fmt"
"reflect"
"strconv"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
)
func getRedisHAReplicas() *int32 {
replicas := common.ArgoCDDefaultRedisHAReplicas
// TODO: Allow override of this value through CR?
return &replicas
}
// newStatefulSet returns a new StatefulSet instance for the given ArgoCD instance.
func newStatefulSet(cr *argoproj.ArgoCD) *appsv1.StatefulSet {
return &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: argoutil.LabelsForCluster(cr),
},
}
}
// newStatefulSetWithName returns a new StatefulSet instance for the given ArgoCD using the given name.
func newStatefulSetWithName(name string, component string, cr *argoproj.ArgoCD) *appsv1.StatefulSet {
ss := newStatefulSet(cr)
// The name is already truncated by nameWithSuffix, so use it directly
ss.Name = name
lbls := ss.Labels
lbls[common.ArgoCDKeyName] = name
lbls[common.ArgoCDKeyComponent] = component
ss.Labels = lbls
ss.Spec = appsv1.StatefulSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
common.ArgoCDKeyName: name,
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
common.ArgoCDKeyName: name,
},
Annotations: make(map[string]string),
},
Spec: corev1.PodSpec{
NodeSelector: common.DefaultNodeSelector(),
},
},
}
if cr.Spec.NodePlacement != nil {
ss.Spec.Template.Spec.NodeSelector = argoutil.AppendStringMap(ss.Spec.Template.Spec.NodeSelector, cr.Spec.NodePlacement.NodeSelector)
ss.Spec.Template.Spec.Tolerations = cr.Spec.NodePlacement.Tolerations
}
ss.Spec.ServiceName = name
return ss
}
// newStatefulSetWithSuffix returns a new StatefulSet instance for the given ArgoCD using the given suffix.
func newStatefulSetWithSuffix(suffix string, component string, cr *argoproj.ArgoCD) *appsv1.StatefulSet {
return newStatefulSetWithName(nameWithSuffix(suffix, cr), component, cr)
}
func (r *ReconcileArgoCD) reconcileRedisStatefulSet(cr *argoproj.ArgoCD) error {
ss := newStatefulSetWithSuffix("redis-ha-server", "redis", cr)
redisEnv := append(proxyEnvVars(), corev1.EnvVar{
Name: "AUTH",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: argoutil.GetSecretNameWithSuffix(cr, "redis-initial-password"),
},
Key: "admin.password",
},
},
})
ss.Spec.PodManagementPolicy = appsv1.OrderedReadyPodManagement
ss.Spec.Replicas = getRedisHAReplicas()
ss.Spec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{
common.ArgoCDKeyName: nameWithSuffix("redis-ha", cr),
},
}
ss.Spec.ServiceName = nameWithSuffix("redis-ha", cr)
ss.Spec.Template.ObjectMeta = metav1.ObjectMeta{
Annotations: map[string]string{
"checksum/init-config": "7128bfbb51eafaffe3c33b1b463e15f0cf6514cec570f9d9c4f2396f28c724ac", // TODO: Should this be hard-coded?
},
Labels: map[string]string{
common.ArgoCDKeyName: nameWithSuffix("redis-ha", cr),
},
}
ss.Spec.Template.Spec.Affinity = &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
common.ArgoCDKeyName: nameWithSuffix("redis-ha", cr),
},
},
TopologyKey: common.ArgoCDKeyHostname,
}},
},
}
f := false
ss.Spec.Template.Spec.AutomountServiceAccountToken = &f
ss.Spec.Template.Spec.Containers = []corev1.Container{
{
Args: []string{
"/data/conf/redis.conf",
},
Command: []string{
"redis-server",
},
Env: redisEnv,
Image: getRedisHAContainerImage(cr),
ImagePullPolicy: argoutil.GetImagePullPolicy(cr.Spec.ImagePullPolicy),
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{
"sh",
"-c",
"/health/redis_liveness.sh",
},
},
},
FailureThreshold: int32(5),
InitialDelaySeconds: int32(30),
PeriodSeconds: int32(15),
SuccessThreshold: int32(1),
TimeoutSeconds: int32(15),
},
Name: "redis",
Ports: []corev1.ContainerPort{{
ContainerPort: common.ArgoCDDefaultRedisPort,
Name: "redis",
}},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{
"sh",
"-c",
"/health/redis_readiness.sh",
},
},
},
FailureThreshold: int32(5),
InitialDelaySeconds: int32(30),
PeriodSeconds: int32(15),
SuccessThreshold: int32(1),
TimeoutSeconds: int32(15),
},
Resources: getRedisHAResources(cr),
SecurityContext: argoutil.DefaultSecurityContext(),
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/data",
Name: "data",
},
{
MountPath: "/health",
Name: "health",
},
{
Name: common.ArgoCDRedisServerTLSSecretName,
MountPath: "/app/config/redis/tls",
},
},
},
{
Args: []string{
"/data/conf/sentinel.conf",
},
Command: []string{
"redis-sentinel",
},
Env: redisEnv,
Image: getRedisHAContainerImage(cr),
ImagePullPolicy: argoutil.GetImagePullPolicy(cr.Spec.ImagePullPolicy),
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{
"sh",
"-c",
"/health/sentinel_liveness.sh",
},
},
},
FailureThreshold: int32(5),
InitialDelaySeconds: int32(30),
PeriodSeconds: int32(15),
SuccessThreshold: int32(1),
TimeoutSeconds: int32(15),
},
Name: "sentinel",
Ports: []corev1.ContainerPort{{
ContainerPort: common.ArgoCDDefaultRedisSentinelPort,
Name: "sentinel",
}},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{
"sh",
"-c",
"/health/sentinel_liveness.sh",
},
},
},
FailureThreshold: int32(5),
InitialDelaySeconds: int32(30),
PeriodSeconds: int32(15),
SuccessThreshold: int32(1),
TimeoutSeconds: int32(15),
},
Resources: getRedisHAResources(cr),
SecurityContext: argoutil.DefaultSecurityContext(),
Lifecycle: &corev1.Lifecycle{
PostStart: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: []string{
"/bin/sh",
"-c",
func() string {
// Check if TLS is enabled for Redis
useTLS := r.redisShouldUseTLS(cr)
if useTLS {
// Use TLS for redis-cli when connecting to sentinel
return "sleep 30; redis-cli -p 26379 --tls --cert /app/config/redis/tls/tls.crt --key /app/config/redis/tls/tls.key --insecure sentinel reset argocd"
}
return "sleep 30; redis-cli -p 26379 sentinel reset argocd"
}(),
},
},
},
},
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/data",
Name: "data",
},
{
MountPath: "/health",
Name: "health",
},
{
Name: common.ArgoCDRedisServerTLSSecretName,
MountPath: "/app/config/redis/tls",
},
},
},
}
ss.Spec.Template.Spec.InitContainers = []corev1.Container{{
Args: []string{
"/readonly-config/init.sh",
},
Command: []string{
"sh",
},
Env: []corev1.EnvVar{
{
Name: "SENTINEL_ID_0",
Value: "3c0d9c0320bb34888c2df5757c718ce6ca992ce6", // TODO: Should this be hard-coded?
},
{
Name: "SENTINEL_ID_1",
Value: "40000915ab58c3fa8fd888fb8b24711944e6cbb4", // TODO: Should this be hard-coded?
},
{
Name: "SENTINEL_ID_2",
Value: "2bbec7894d954a8af3bb54d13eaec53cb024e2ca", // TODO: Should this be hard-coded?
},
{
Name: "AUTH",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: argoutil.GetSecretNameWithSuffix(cr, "redis-initial-password"),
},
Key: "admin.password",
},
},
},
},
Image: getRedisHAContainerImage(cr),
ImagePullPolicy: argoutil.GetImagePullPolicy(cr.Spec.ImagePullPolicy),
Name: "config-init",
Resources: getRedisHAResources(cr),
SecurityContext: argoutil.DefaultSecurityContext(),
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/readonly-config",
Name: "config",
ReadOnly: true,
},
{
MountPath: "/data",
Name: "data",
},
{
Name: common.ArgoCDRedisServerTLSSecretName,
MountPath: "/app/config/redis/tls",
},
},
}}
if IsOpenShiftCluster() {
var runAsNonRoot = true
ss.Spec.Template.Spec.SecurityContext = &corev1.PodSecurityContext{
RunAsNonRoot: &runAsNonRoot,
}
} else {
var fsGroup int64 = 1000
var runAsNonRoot = true
var runAsUser int64 = 1000
ss.Spec.Template.Spec.SecurityContext = &corev1.PodSecurityContext{
FSGroup: &fsGroup,
RunAsNonRoot: &runAsNonRoot,
RunAsUser: &runAsUser,
}
}
AddSeccompProfileForOpenShift(r.Client, &ss.Spec.Template.Spec)
ss.Spec.Template.Spec.ServiceAccountName = nameWithSuffix("argocd-redis-ha", cr)
var terminationGracePeriodSeconds int64 = 60
ss.Spec.Template.Spec.TerminationGracePeriodSeconds = &terminationGracePeriodSeconds
var defaultMode int32 = 493
ss.Spec.Template.Spec.Volumes = []corev1.Volume{
{
Name: "config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: common.ArgoCDRedisHAConfigMapName,
},
},
},
},
{
Name: "health",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
DefaultMode: &defaultMode,
LocalObjectReference: corev1.LocalObjectReference{
Name: common.ArgoCDRedisHAHealthConfigMapName,
},
},
},
},
{
Name: "data",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: common.ArgoCDRedisServerTLSSecretName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: common.ArgoCDRedisServerTLSSecretName,
Optional: boolPtr(true),
},
},
},
}
ss.Spec.UpdateStrategy = appsv1.StatefulSetUpdateStrategy{
Type: appsv1.RollingUpdateStatefulSetStrategyType,
}
if err := applyReconcilerHook(cr, ss, ""); err != nil {
return err
}
existing := newStatefulSetWithSuffix("redis-ha-server", "redis", cr)
ssExists, err := argoutil.IsObjectFound(r.Client, cr.Namespace, existing.Name, existing)
if err != nil {
return err
}
if ssExists {
if !cr.Spec.HA.Enabled || !cr.Spec.Redis.IsEnabled() {
// StatefulSet exists but either HA or component enabled flag has been set to false, delete the StatefulSet
var explanation string
if !cr.Spec.HA.Enabled {
explanation = "ha is disabled"
} else {
explanation = "redis is disabled"
}
argoutil.LogResourceDeletion(log, existing, explanation)
return r.Delete(context.TODO(), existing)
}
desiredImage := getRedisHAContainerImage(cr)
changed := false
explanation := ""
updateNodePlacementStateful(existing, ss, &changed, &explanation)
for i, container := range existing.Spec.Template.Spec.Containers {
if container.Image != desiredImage {
existing.Spec.Template.Spec.Containers[i].Image = getRedisHAContainerImage(cr)
existing.Spec.Template.Labels["image.upgraded"] = time.Now().UTC().Format("01022006-150406-MST")
if changed {
explanation += ", "
}
explanation += fmt.Sprintf("container '%s' image", container.Name)
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[i].VolumeMounts, existing.Spec.Template.Spec.Containers[i].VolumeMounts) {
existing.Spec.Template.Spec.Containers[i].VolumeMounts = ss.Spec.Template.Spec.Containers[i].VolumeMounts
if changed {
explanation += ", "
}
explanation += fmt.Sprintf("container '%s' VolumeMounts", container.Name)
changed = true
}
if existing.Spec.Template.Spec.Containers[i].ImagePullPolicy != ss.Spec.Template.Spec.Containers[i].ImagePullPolicy {
existing.Spec.Template.Spec.Containers[0].ImagePullPolicy = ss.Spec.Template.Spec.Containers[i].ImagePullPolicy
if changed {
explanation += ", "
}
explanation += "image pull policy"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[i].Resources, existing.Spec.Template.Spec.Containers[i].Resources) {
existing.Spec.Template.Spec.Containers[i].Resources = ss.Spec.Template.Spec.Containers[i].Resources
if changed {
explanation += ", "
}
explanation += fmt.Sprintf("container '%s' resources", container.Name)
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[i].SecurityContext, existing.Spec.Template.Spec.Containers[i].SecurityContext) {
existing.Spec.Template.Spec.Containers[i].SecurityContext = ss.Spec.Template.Spec.Containers[i].SecurityContext
if changed {
explanation += ", "
}
explanation += fmt.Sprintf("container '%s' security context", container.Name)
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[i].Env, existing.Spec.Template.Spec.Containers[i].Env) {
existing.Spec.Template.Spec.Containers[i].Env = ss.Spec.Template.Spec.Containers[i].Env
if changed {
explanation += ", "
}
explanation += fmt.Sprintf("container '%s' env", container.Name)
changed = true
}
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.SecurityContext, existing.Spec.Template.Spec.SecurityContext) {
existing.Spec.Template.Spec.SecurityContext = ss.Spec.Template.Spec.SecurityContext
if changed {
explanation += ", "
}
explanation += "security context"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.Volumes, existing.Spec.Template.Spec.Volumes) {
existing.Spec.Template.Spec.Volumes = ss.Spec.Template.Spec.Volumes
if changed {
explanation += ", "
}
explanation += "volumes"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.InitContainers, existing.Spec.Template.Spec.InitContainers) {
existing.Spec.Template.Spec.InitContainers = ss.Spec.Template.Spec.InitContainers
if changed {
explanation += ", "
}
explanation += "init containers"
changed = true
}
if changed {
argoutil.LogResourceUpdate(log, existing, "updating", explanation)
return r.Update(context.TODO(), existing)
}
return nil // StatefulSet found, do nothing
}
if cr.Spec.Redis.IsEnabled() && cr.Spec.Redis.Remote != nil && *cr.Spec.Redis.Remote != "" {
log.Info("Custom Redis Endpoint. Skipping starting redis.")
return nil
}
if !cr.Spec.Redis.IsEnabled() {
log.Info("Redis disabled. Skipping starting Redis.") // Redis not enabled, do nothing.
return nil
}
if !cr.Spec.HA.Enabled {
return nil // HA not enabled, do nothing.
}
if err := controllerutil.SetControllerReference(cr, ss, r.Scheme); err != nil {
return err
}
argoutil.LogResourceCreation(log, ss)
return r.Create(context.TODO(), ss)
}
func getArgoControllerContainerEnv(cr *argoproj.ArgoCD, replicas int32) []corev1.EnvVar {
env := make([]corev1.EnvVar, 0)
env = append(env, corev1.EnvVar{
Name: "HOME",
Value: "/home/argocd",
})
env = append(env, corev1.EnvVar{
Name: "REDIS_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: argoutil.GetSecretNameWithSuffix(cr, "redis-initial-password"),
},
Key: "admin.password",
},
},
})
if cr.Spec.Controller.Sharding.Enabled || (cr.Spec.Controller.Sharding.DynamicScalingEnabled != nil && *cr.Spec.Controller.Sharding.DynamicScalingEnabled) {
env = append(env, corev1.EnvVar{
Name: "ARGOCD_CONTROLLER_REPLICAS",
Value: fmt.Sprint(replicas),
})
}
if cr.Spec.Controller.AppSync != nil {
env = append(env, corev1.EnvVar{
Name: "ARGOCD_RECONCILIATION_TIMEOUT",
Value: strconv.FormatInt(int64(cr.Spec.Controller.AppSync.Seconds()), 10) + "s",
})
}
env = append(env, corev1.EnvVar{
Name: "ARGOCD_CONTROLLER_RESOURCE_HEALTH_PERSIST",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: common.ArgoCDCmdParamsConfigMapName,
},
Key: "controller.resource.health.persist",
},
},
},
)
return env
}
func (r *ReconcileArgoCD) getApplicationControllerReplicaCount(cr *argoproj.ArgoCD) int32 {
var replicas int32 = common.ArgocdApplicationControllerDefaultReplicas
var minShards = cr.Spec.Controller.Sharding.MinShards
var maxShards = cr.Spec.Controller.Sharding.MaxShards
if cr.Spec.Controller.Sharding.DynamicScalingEnabled != nil && *cr.Spec.Controller.Sharding.DynamicScalingEnabled {
// TODO: add the same validations to Validation Webhook once webhook has been introduced
if minShards < 1 {
log.Info("Minimum number of shards cannot be less than 1. Setting default value to 1")
minShards = 1
}
if maxShards < minShards {
log.Info("Maximum number of shards cannot be less than minimum number of shards. Setting maximum shards same as minimum shards")
maxShards = minShards
}
clustersPerShard := cr.Spec.Controller.Sharding.ClustersPerShard
if clustersPerShard < 1 {
log.Info("clustersPerShard cannot be less than 1. Defaulting to 1.")
clustersPerShard = 1
}
clusterSecrets, err := r.getClusterSecrets(cr)
if err != nil {
// If we were not able to query cluster secrets, return the default count of replicas (ArgocdApplicationControllerDefaultReplicas)
log.Error(err, "Error retreiving cluster secrets for ArgoCD instance %s", cr.Name)
return replicas
}
replicas = int32(len(clusterSecrets.Items)) / clustersPerShard // #nosec G115
if replicas < minShards {
replicas = minShards
}
if replicas > maxShards {
replicas = maxShards
}
return replicas
} else if cr.Spec.Controller.Sharding.Replicas != 0 && cr.Spec.Controller.Sharding.Enabled {
return cr.Spec.Controller.Sharding.Replicas
}
return replicas
}
func (r *ReconcileArgoCD) reconcileApplicationControllerStatefulSet(cr *argoproj.ArgoCD, useTLSForRedis bool) error {
replicas := r.getApplicationControllerReplicaCount(cr)
ss := newStatefulSetWithSuffix("application-controller", "application-controller", cr)
ss.Spec.Replicas = &replicas
controllerEnv := cr.Spec.Controller.Env
// Sharding setting explicitly overrides a value set in the env
controllerEnv = argoutil.EnvMerge(controllerEnv, getArgoControllerContainerEnv(cr, replicas), true)
// Let user specify their own environment first
controllerEnv = argoutil.EnvMerge(controllerEnv, proxyEnvVars(), false)
if cr.Spec.Controller.InitContainers != nil {
ss.Spec.Template.Spec.InitContainers = append(ss.Spec.Template.Spec.InitContainers, cr.Spec.Controller.InitContainers...)
}
controllerVolumeMounts := []corev1.VolumeMount{
{
Name: "argocd-repo-server-tls",
MountPath: "/app/config/controller/tls",
},
{
Name: common.ArgoCDRedisServerTLSSecretName,
MountPath: "/app/config/controller/tls/redis",
},
{
Name: "argocd-home",
MountPath: "/home/argocd",
},
{
Name: "argocd-cmd-params-cm",
MountPath: "/home/argocd/params",
},
{
Name: "argocd-application-controller-tmp",
MountPath: "/tmp",
},
}
if cr.Spec.Controller.VolumeMounts != nil {
controllerVolumeMounts = append(controllerVolumeMounts, cr.Spec.Controller.VolumeMounts...)
}
podSpec := &ss.Spec.Template.Spec
podSpec.Containers = []corev1.Container{{
Command: getArgoApplicationControllerCommand(cr, useTLSForRedis),
Image: getArgoContainerImage(cr),
ImagePullPolicy: argoutil.GetImagePullPolicy(cr.Spec.ImagePullPolicy),
Name: "argocd-application-controller",
Env: controllerEnv,
Ports: []corev1.ContainerPort{
{
ContainerPort: 8082,
},
},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
Port: intstr.FromInt(8082),
},
},
InitialDelaySeconds: 5,
PeriodSeconds: 10,
},
Resources: getArgoApplicationControllerResources(cr),
SecurityContext: argoutil.DefaultSecurityContext(),
VolumeMounts: controllerVolumeMounts,
}}
if cr.Spec.Controller.SidecarContainers != nil {
ss.Spec.Template.Spec.Containers = append(ss.Spec.Template.Spec.Containers, cr.Spec.Controller.SidecarContainers...)
}
AddSeccompProfileForOpenShift(r.Client, podSpec)
podSpec.ServiceAccountName = nameWithSuffix("argocd-application-controller", cr)
controllerVolumes := []corev1.Volume{
{
Name: "argocd-repo-server-tls",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: common.ArgoCDRepoServerTLSSecretName,
Optional: boolPtr(true),
},
},
},
{
Name: common.ArgoCDRedisServerTLSSecretName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: common.ArgoCDRedisServerTLSSecretName,
Optional: boolPtr(true),
},
},
},
{
Name: "argocd-home",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: "argocd-cmd-params-cm",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "argocd-cmd-params-cm",
},
Optional: boolPtr(true),
Items: []corev1.KeyToPath{
{
Key: "controller.profile.enabled",
Path: "profiler.enabled",
},
{
Key: "controller.resource.health.persist",
Path: "controller.resource.health.persist",
},
},
},
},
},
{
Name: "argocd-application-controller-tmp",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
}
if cr.Spec.Controller.Volumes != nil {
controllerVolumes = append(controllerVolumes, cr.Spec.Controller.Volumes...)
}
podSpec.Volumes = controllerVolumes
ss.Spec.Template.Spec.Affinity = &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{{
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
common.ArgoCDKeyName: nameWithSuffix("argocd-application-controller", cr),
},
},
TopologyKey: common.ArgoCDKeyHostname,
},
Weight: int32(100),
},
{
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
common.ArgoCDKeyPartOf: common.ArgoCDAppName,
},
},
TopologyKey: common.ArgoCDKeyHostname,
},
Weight: int32(5),
}},
},
}
// Handle import/restore from ArgoCDExport
export, err := r.getArgoCDExport(cr)
if err != nil {
return err
}
if export == nil {
log.Info("existing argocd export not found, skipping import")
} else {
containerCommand, err := getArgoImportCommand(r.Client, cr)
if err != nil {
return err
}
podSpec.InitContainers = []corev1.Container{{
Command: containerCommand,
Env: proxyEnvVars(getArgoImportContainerEnv(export)...),
Resources: getArgoApplicationControllerResources(cr),
Image: getArgoImportContainerImage(export),
ImagePullPolicy: argoutil.GetImagePullPolicy(cr.Spec.ImagePullPolicy),
Name: "argocd-import",
SecurityContext: argoutil.DefaultSecurityContext(),
VolumeMounts: getArgoImportVolumeMounts(),
}}
podSpec.Volumes = getArgoImportVolumes(export)
}
invalidImagePod, err := containsInvalidImage(*cr, *r)
if err != nil {
return err
} else if invalidImagePod {
argoutil.LogResourceDeletion(log, ss, "one or more pods has an invalid image")
if err := r.Delete(context.TODO(), ss); err != nil {
return err
}
}
if cr.Spec.Controller.Annotations != nil {
for key, value := range cr.Spec.Controller.Annotations {
ss.Spec.Template.Annotations[key] = value
}
}
if cr.Spec.Controller.Labels != nil {
for key, value := range cr.Spec.Controller.Labels {
ss.Spec.Template.Labels[key] = value
}
}
existing := newStatefulSetWithSuffix("application-controller", "application-controller", cr)
ssExists, err := argoutil.IsObjectFound(r.Client, cr.Namespace, existing.Name, existing)
if err != nil {
return err
}
if ssExists {
if !cr.Spec.Controller.IsEnabled() {
// Delete existing deployment for Application Controller, if any ..
argoutil.LogResourceDeletion(log, existing, "application controller is disabled")
return r.Delete(context.TODO(), existing)
}
actualImage := existing.Spec.Template.Spec.Containers[0].Image
desiredImage := getArgoContainerImage(cr)
actualImagePullPolicy := existing.Spec.Template.Spec.Containers[0].ImagePullPolicy
desiredImagePullPolicy := argoutil.GetImagePullPolicy(cr.Spec.ImagePullPolicy)
changed := false
explanation := ""
if actualImage != desiredImage {
existing.Spec.Template.Spec.Containers[0].Image = desiredImage
existing.Spec.Template.Labels["image.upgraded"] = time.Now().UTC().Format("01022006-150406-MST")
explanation = "container image"
changed = true
}
if actualImagePullPolicy != desiredImagePullPolicy {
existing.Spec.Template.Spec.Containers[0].ImagePullPolicy = desiredImagePullPolicy
if changed {
explanation += ", "
}
explanation += "image pull policy"
changed = true
}
desiredCommand := getArgoApplicationControllerCommand(cr, useTLSForRedis)
if isRepoServerTLSVerificationRequested(cr) {
desiredCommand = append(desiredCommand, "--repo-server-strict-tls")
}
updateNodePlacementStateful(existing, ss, &changed, &explanation)
if !reflect.DeepEqual(desiredCommand, existing.Spec.Template.Spec.Containers[0].Command) {
existing.Spec.Template.Spec.Containers[0].Command = desiredCommand
if changed {
explanation += ", "
}
explanation += "container command"
changed = true
}
if !reflect.DeepEqual(existing.Spec.Template.Spec.InitContainers, ss.Spec.Template.Spec.InitContainers) {
existing.Spec.Template.Spec.InitContainers = ss.Spec.Template.Spec.InitContainers
if changed {
explanation += ", "
}
explanation += "init containers"
changed = true
}
if !reflect.DeepEqual(existing.Spec.Template.Spec.Containers[0].Env,
ss.Spec.Template.Spec.Containers[0].Env) {
existing.Spec.Template.Spec.Containers[0].Env = ss.Spec.Template.Spec.Containers[0].Env
if changed {
explanation += ", "
}
explanation += "container env"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.Volumes, existing.Spec.Template.Spec.Volumes) {
existing.Spec.Template.Spec.Volumes = ss.Spec.Template.Spec.Volumes
if changed {
explanation += ", "
}
explanation += "volumes"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[0].VolumeMounts,
existing.Spec.Template.Spec.Containers[0].VolumeMounts) {
existing.Spec.Template.Spec.Containers[0].VolumeMounts = ss.Spec.Template.Spec.Containers[0].VolumeMounts
if changed {
explanation += ", "
}
explanation += "container volume mounts"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[0].Resources, existing.Spec.Template.Spec.Containers[0].Resources) {
existing.Spec.Template.Spec.Containers[0].Resources = ss.Spec.Template.Spec.Containers[0].Resources
if changed {
explanation += ", "
}
explanation += "container resources"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[0].SecurityContext, existing.Spec.Template.Spec.Containers[0].SecurityContext) {
existing.Spec.Template.Spec.Containers[0].SecurityContext = ss.Spec.Template.Spec.Containers[0].SecurityContext
if changed {
explanation += ", "
}
explanation += "container security context"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Replicas, existing.Spec.Replicas) {
existing.Spec.Replicas = ss.Spec.Replicas
if changed {
explanation += ", "
}
explanation += "replicas"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.SecurityContext, existing.Spec.Template.Spec.SecurityContext) {
existing.Spec.Template.Spec.SecurityContext = ss.Spec.Template.Spec.SecurityContext
if changed {
explanation += ", "
}
explanation += "security context"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Spec.Containers[1:],
existing.Spec.Template.Spec.Containers[1:]) {
existing.Spec.Template.Spec.Containers = append(existing.Spec.Template.Spec.Containers[0:1],
ss.Spec.Template.Spec.Containers[1:]...)
if changed {
explanation += ", "
}
explanation += "additional containers"
changed = true
}
//Check if labels/annotations have changed
UpdateMapValues(&existing.Spec.Template.Labels, ss.Spec.Template.Labels)
UpdateMapValues(&existing.Spec.Template.Annotations, ss.Spec.Template.Annotations)
if !reflect.DeepEqual(ss.Spec.Template.Annotations, existing.Spec.Template.Annotations) {
existing.Spec.Template.Annotations = ss.Spec.Template.Annotations
if changed {
explanation += ", "
}
explanation += "annotations"
changed = true
}
if !reflect.DeepEqual(ss.Spec.Template.Labels, existing.Spec.Template.Labels) {