@@ -34,6 +34,7 @@ const (
34
34
TLSCertFile Prefix = "--tls-cert-file="
35
35
TLSPrivateKeyFile Prefix = "--tls-private-key-file="
36
36
CertRotation Prefix = "--enable-cert-rotation="
37
+ CADir Prefix = "--ca-dir="
37
38
)
38
39
39
40
func (p Prefix ) String () string {
@@ -45,6 +46,7 @@ const (
45
46
containerNameKedaOperator = "keda-operator"
46
47
containerNameMetricsServer = "keda-metrics-apiserver"
47
48
containerNameAdmissionWebhooks = "keda-admission-webhooks"
49
+ caCertVolPrefix = "cabundle"
48
50
)
49
51
50
52
// ReplaceAllNamespaces returns a transformer which will traverse the unstructured content looking for map entries with
@@ -406,7 +408,7 @@ func ensureCertificatesVolumeForDeployment(containerName, configMapName, secretN
406
408
var cabundleVolume corev1.Volume
407
409
if containerName == containerNameKedaOperator {
408
410
cabundleVolume = corev1.Volume {
409
- Name : "cabundle" ,
411
+ Name : caCertVolPrefix ,
410
412
VolumeSource : corev1.VolumeSource {
411
413
ConfigMap : & corev1.ConfigMapVolumeSource {
412
414
LocalObjectReference : corev1.LocalObjectReference {
@@ -462,7 +464,7 @@ func ensureCertificatesVolumeForDeployment(containerName, configMapName, secretN
462
464
certificatesVolumeFound := false
463
465
for i := range volumes {
464
466
if containerName == containerNameKedaOperator {
465
- if volumes [i ].Name == "cabundle" {
467
+ if volumes [i ].Name == caCertVolPrefix {
466
468
volumes [i ] = cabundleVolume
467
469
cabundleVolumeFound = true
468
470
}
@@ -486,7 +488,7 @@ func ensureCertificatesVolumeForDeployment(containerName, configMapName, secretN
486
488
var cabundleVolumeMount corev1.VolumeMount
487
489
if containerName == containerNameKedaOperator {
488
490
cabundleVolumeMount = corev1.VolumeMount {
489
- Name : "cabundle" ,
491
+ Name : caCertVolPrefix ,
490
492
MountPath : "/custom/ca" ,
491
493
}
492
494
}
@@ -504,7 +506,7 @@ func ensureCertificatesVolumeForDeployment(containerName, configMapName, secretN
504
506
for j := range volumeMounts {
505
507
// add custom CA to Operator
506
508
if containerName == containerNameKedaOperator {
507
- if volumeMounts [j ].Name == "cabundle" {
509
+ if volumeMounts [j ].Name == caCertVolPrefix {
508
510
volumeMounts [j ] = cabundleVolumeMount
509
511
cabundleVolumeMountFound = true
510
512
}
@@ -534,59 +536,83 @@ func ensureCertificatesVolumeForDeployment(containerName, configMapName, secretN
534
536
}
535
537
}
536
538
537
- //nolint:dupl
538
- func EnsureOpenshiftCABundleForOperatorDeployment (configMapName string , scheme * runtime.Scheme ) mf.Transformer {
539
- return func (u * unstructured.Unstructured ) error {
539
+ // Add configmap volumes for configMapNames named cabundle0, cabundle1, etc. as /custom/ca0, /custom/ca1, etc. with
540
+ // container args --ca-dir=/custom/ca0, --ca-dir=/custom/ca1, etc.
541
+ func EnsureCACertsForOperatorDeployment (configMapNames []string , scheme * runtime.Scheme , logger logr.Logger ) []mf.Transformer {
542
+ var retval []mf.Transformer
543
+
544
+ var caDirs []string
545
+ for i := range configMapNames {
546
+ caDirs = append (caDirs , "/custom/ca" + strconv .Itoa (i ))
547
+ }
548
+ retval = append (retval , replaceContainerArgs (caDirs , CADir , containerNameKedaOperator , scheme , logger ))
549
+
550
+ retval = append (retval , func (u * unstructured.Unstructured ) error {
540
551
if u .GetKind () == "Deployment" {
541
552
deploy := & appsv1.Deployment {}
542
553
if err := scheme .Convert (u , deploy , nil ); err != nil {
543
554
return err
544
555
}
545
556
546
557
// add Volumes referencing certs in ConfigMap
547
- cabundleVolume := corev1.Volume {
548
- Name : "cabundle" ,
549
- VolumeSource : corev1.VolumeSource {
550
- ConfigMap : & corev1.ConfigMapVolumeSource {
551
- LocalObjectReference : corev1.LocalObjectReference {
552
- Name : configMapName ,
558
+ cabundleVolumes := map [string ]corev1.Volume {}
559
+ cabundleVolumeMounts := map [string ]corev1.VolumeMount {}
560
+ for i , configMapName := range configMapNames {
561
+ cabundleVolumes [configMapName ] = corev1.Volume {
562
+ Name : caCertVolPrefix + strconv .Itoa (i ),
563
+ VolumeSource : corev1.VolumeSource {
564
+ ConfigMap : & corev1.ConfigMapVolumeSource {
565
+ LocalObjectReference : corev1.LocalObjectReference {
566
+ Name : configMapName ,
567
+ },
553
568
},
554
569
},
555
- },
556
- }
570
+ }
557
571
558
- volumes := deploy .Spec .Template .Spec .Volumes
559
- cabundleVolumeFound := false
560
- for i := range volumes {
561
- if volumes [i ].Name == "cabundle" {
562
- volumes [i ] = cabundleVolume
563
- cabundleVolumeFound = true
572
+ cabundleVolumeMounts [caCertVolPrefix + strconv .Itoa (i )] = corev1.VolumeMount {
573
+ Name : caCertVolPrefix + strconv .Itoa (i ),
574
+ MountPath : "/custom/ca" + strconv .Itoa (i ),
564
575
}
565
576
}
566
- if ! cabundleVolumeFound {
567
- deploy .Spec .Template .Spec .Volumes = append (deploy .Spec .Template .Spec .Volumes , cabundleVolume )
577
+
578
+ cabundleVolumeFound := map [string ]bool {}
579
+ var volumes []corev1.Volume
580
+ for _ , vol := range deploy .Spec .Template .Spec .Volumes {
581
+ if caVol , ok := cabundleVolumes [vol .Name ]; ok {
582
+ volumes = append (volumes , caVol )
583
+ cabundleVolumeFound [vol .Name ] = true
584
+ } else if ! strings .HasPrefix (vol .Name , caCertVolPrefix ) {
585
+ volumes = append (volumes , vol )
586
+ } // else don't copy it over since it shouldn't be there
568
587
}
588
+
589
+ for name , vol := range cabundleVolumes {
590
+ if ! cabundleVolumeFound [name ] {
591
+ volumes = append (volumes , vol )
592
+ }
593
+ }
594
+ deploy .Spec .Template .Spec .Volumes = volumes
595
+
569
596
containers := deploy .Spec .Template .Spec .Containers
570
597
for i := range containers {
571
598
if containers [i ].Name == containerNameKedaOperator {
572
599
// mount Volumes referencing certs in ConfigMap
573
- cabundleVolumeMount := corev1.VolumeMount {
574
- Name : "cabundle" ,
575
- MountPath : "/custom/ca" ,
600
+ var volumeMounts []corev1.VolumeMount
601
+ cabundleVolumeMountFound := map [string ]bool {}
602
+ for _ , volMount := range containers [i ].VolumeMounts {
603
+ if caVolMount , ok := cabundleVolumeMounts [volMount .Name ]; ok {
604
+ volumeMounts = append (volumeMounts , caVolMount )
605
+ cabundleVolumeMountFound [volMount .Name ] = true
606
+ } else if ! strings .HasPrefix (volMount .Name , caCertVolPrefix ) {
607
+ volumeMounts = append (volumeMounts , volMount )
608
+ } // else don't copy it over since it shouldn't be there
576
609
}
577
-
578
- volumeMounts := containers [i ].VolumeMounts
579
- cabundleVolumeMountFound := false
580
- for j := range volumeMounts {
581
- if volumeMounts [j ].Name == "cabundle" {
582
- volumeMounts [j ] = cabundleVolumeMount
583
- cabundleVolumeMountFound = true
610
+ for name , volmount := range cabundleVolumeMounts {
611
+ if ! cabundleVolumeMountFound [name ] {
612
+ volumeMounts = append (volumeMounts , volmount )
584
613
}
585
614
}
586
- if ! cabundleVolumeMountFound {
587
- containers [i ].VolumeMounts = append (containers [i ].VolumeMounts , cabundleVolumeMount )
588
- }
589
-
615
+ containers [i ].VolumeMounts = volumeMounts
590
616
break
591
617
}
592
618
}
@@ -596,7 +622,8 @@ func EnsureOpenshiftCABundleForOperatorDeployment(configMapName string, scheme *
596
622
}
597
623
}
598
624
return nil
599
- }
625
+ })
626
+ return retval
600
627
}
601
628
602
629
func EnsurePathsToCertsInDeployment (values []string , prefixes []Prefix , scheme * runtime.Scheme , logger logr.Logger ) []mf.Transformer {
@@ -607,7 +634,6 @@ func EnsurePathsToCertsInDeployment(values []string, prefixes []Prefix, scheme *
607
634
return transforms
608
635
}
609
636
610
- //nolint:dupl
611
637
func EnsureAuditPolicyConfigMapMountsVolume (configMapName string , scheme * runtime.Scheme ) mf.Transformer {
612
638
return func (u * unstructured.Unstructured ) error {
613
639
if u .GetKind () == "Deployment" {
@@ -936,6 +962,62 @@ func replaceContainerArg(value string, prefix Prefix, containerName string, sche
936
962
}
937
963
}
938
964
965
+ func replaceContainerArgs (values []string , prefix Prefix , containerName string , scheme * runtime.Scheme , logger logr.Logger ) mf.Transformer {
966
+ return func (u * unstructured.Unstructured ) error {
967
+ // this function only supports flags with a prefix
968
+ if prefix == "" {
969
+ return nil
970
+ }
971
+ changed := false
972
+ if u .GetKind () == "Deployment" {
973
+ deploy := & appsv1.Deployment {}
974
+ if err := scheme .Convert (u , deploy , nil ); err != nil {
975
+ return err
976
+ }
977
+ containers := deploy .Spec .Template .Spec .Containers
978
+ for i , container := range containers {
979
+ if container .Name == containerName {
980
+ argFound := false
981
+ var newArgs []string
982
+ for _ , arg := range container .Args {
983
+ if ! strings .HasPrefix (arg , prefix .String ()) {
984
+ newArgs = append (newArgs , arg )
985
+ } else {
986
+ if argFound {
987
+ continue
988
+ }
989
+ argFound = true
990
+ for _ , value := range values {
991
+ newArgs = append (newArgs , prefix .String ()+ value )
992
+ }
993
+ }
994
+ }
995
+ if argFound {
996
+ changed = ! reflect .DeepEqual (containers [i ].Args , newArgs )
997
+ if changed {
998
+ logger .Info ("Updating args" , "deployment" , container .Name , "prefix" , prefix .String (), "values" , values )
999
+ containers [i ].Args = newArgs
1000
+ }
1001
+ } else if len (values ) > 0 {
1002
+ logger .Info ("Adding args" , "deployment" , container .Name , "prefix" , prefix .String (), "value" , values )
1003
+ for _ , value := range values {
1004
+ containers [i ].Args = append (containers [i ].Args , prefix .String ()+ value )
1005
+ }
1006
+ changed = true
1007
+ }
1008
+ break
1009
+ }
1010
+ }
1011
+ if changed {
1012
+ if err := scheme .Convert (deploy , u , nil ); err != nil {
1013
+ return err
1014
+ }
1015
+ }
1016
+ }
1017
+ return nil
1018
+ }
1019
+ }
1020
+
939
1021
func AddServiceAccountAnnotations (annotations map [string ]string , scheme * runtime.Scheme ) mf.Transformer {
940
1022
return func (u * unstructured.Unstructured ) error {
941
1023
if u .GetKind () == "ServiceAccount" {
0 commit comments