@@ -3,7 +3,6 @@ package generators
3
3
import (
4
4
"cmp"
5
5
"fmt"
6
- "maps"
7
6
"slices"
8
7
"strconv"
9
8
"strings"
@@ -28,15 +27,31 @@ import (
28
27
)
29
28
30
29
const (
31
- tlsCrtPath = "tls.crt"
32
- tlsKeyPath = "tls.key"
33
-
34
30
labelKubernetesNamespaceMetadataName = "kubernetes.io/metadata.name"
35
31
)
36
32
37
- // volume mount name -> mount path
38
- var certVolumeMounts = map [string ]string {
39
- "webhook-cert" : "/tmp/k8s-webhook-server/serving-certs" ,
33
+ type certVolumeConfig struct {
34
+ Name string
35
+ Path string
36
+ TLSCertPath string
37
+ TLSKeyPath string
38
+ }
39
+
40
+ // certVolumeConfigs contain the expected configurations for certificate volume/mounts
41
+ // that the generated Deployment resources for bundle containing webhooks and/or apiservices
42
+ // should contain.
43
+ var certVolumeConfigs = []certVolumeConfig {
44
+ {
45
+ Name : "webhook-cert" ,
46
+ Path : "/tmp/k8s-webhook-server/serving-certs" ,
47
+ TLSCertPath : "tls.crt" ,
48
+ TLSKeyPath : "tls.key" ,
49
+ }, {
50
+ Name : "apiservice-cert" ,
51
+ Path : "/apiserver.local.config/certificates" ,
52
+ TLSCertPath : "apiserver.crt" ,
53
+ TLSKeyPath : "apiserver.key" ,
54
+ },
40
55
}
41
56
42
57
// BundleCSVDeploymentGenerator generates all deployments defined in rv1's cluster service version (CSV). The generated
@@ -80,7 +95,7 @@ func BundleCSVDeploymentGenerator(rv1 *bundle.RegistryV1, opts render.Options) (
80
95
81
96
secretInfo := render .CertProvisionerFor (depSpec .Name , opts ).GetCertSecretInfo ()
82
97
if webhookDeployments .Has (depSpec .Name ) && secretInfo != nil {
83
- addCertVolumesToDeployment (deploymentResource , * secretInfo )
98
+ ensureCorrectDeploymentCertVolumes (deploymentResource , * secretInfo )
84
99
}
85
100
86
101
objs = append (objs , deploymentResource )
@@ -488,60 +503,67 @@ func getWebhookServicePort(wh v1alpha1.WebhookDescription) corev1.ServicePort {
488
503
}
489
504
}
490
505
491
- func addCertVolumesToDeployment (dep * appsv1.Deployment , certSecretInfo render.CertSecretInfo ) {
492
- volumeMountsToReplace := sets .New (slices .Collect (maps .Keys (certVolumeMounts ))... )
493
- certVolumeMountPaths := sets .New (slices .Collect (maps .Values (certVolumeMounts ))... )
506
+ // ensureCorrectDeploymentCertVolumes ensures the deployment has the correct certificate volume mounts by
507
+ // - removing all existing volumes with protected certificate volume names (i.e. webhook-cert and apiservice-cert)
508
+ // - removing all existing volumes that point to the protected certificate paths (e.g. /tmp/k8s-webhook-server/serving-certs)
509
+ // - adding the correct certificate volumes with the correct configuration
510
+ // - applying the same changes to all container volume mounts
511
+ func ensureCorrectDeploymentCertVolumes (dep * appsv1.Deployment , certSecretInfo render.CertSecretInfo ) {
512
+ // collect volumes and paths to replace
513
+ volumesToRemove := sets .New [string ]()
514
+ protectedVolumePaths := sets .New [string ]()
515
+ certVolumes := make ([]corev1.Volume , 0 , len (certVolumeConfigs ))
516
+ certVolumeMounts := make ([]corev1.VolumeMount , 0 , len (certVolumeConfigs ))
517
+ for _ , cfg := range certVolumeConfigs {
518
+ volumesToRemove .Insert (cfg .Name )
519
+ protectedVolumePaths .Insert (cfg .Path )
520
+ certVolumes = append (certVolumes , corev1.Volume {
521
+ Name : cfg .Name ,
522
+ VolumeSource : corev1.VolumeSource {
523
+ Secret : & corev1.SecretVolumeSource {
524
+ SecretName : certSecretInfo .SecretName ,
525
+ Items : []corev1.KeyToPath {
526
+ {
527
+ Key : certSecretInfo .CertificateKey ,
528
+ Path : cfg .TLSCertPath ,
529
+ },
530
+ {
531
+ Key : certSecretInfo .PrivateKeyKey ,
532
+ Path : cfg .TLSKeyPath ,
533
+ },
534
+ },
535
+ },
536
+ },
537
+ })
538
+ certVolumeMounts = append (certVolumeMounts , corev1.VolumeMount {
539
+ Name : cfg .Name ,
540
+ MountPath : cfg .Path ,
541
+ })
542
+ }
543
+
494
544
for _ , c := range dep .Spec .Template .Spec .Containers {
495
545
for _ , containerVolumeMount := range c .VolumeMounts {
496
- if certVolumeMountPaths .Has (containerVolumeMount .MountPath ) {
497
- volumeMountsToReplace .Insert (containerVolumeMount .Name )
546
+ if protectedVolumePaths .Has (containerVolumeMount .MountPath ) {
547
+ volumesToRemove .Insert (containerVolumeMount .Name )
498
548
}
499
549
}
500
550
}
501
551
502
552
// update pod volumes
503
553
dep .Spec .Template .Spec .Volumes = slices .Concat (
504
554
slices .DeleteFunc (dep .Spec .Template .Spec .Volumes , func (v corev1.Volume ) bool {
505
- return volumeMountsToReplace .Has (v .Name )
555
+ return volumesToRemove .Has (v .Name )
506
556
}),
507
- []corev1.Volume {
508
- {
509
- Name : "webhook-cert" ,
510
- VolumeSource : corev1.VolumeSource {
511
- Secret : & corev1.SecretVolumeSource {
512
- SecretName : certSecretInfo .SecretName ,
513
- Items : []corev1.KeyToPath {
514
- {
515
- Key : certSecretInfo .CertificateKey ,
516
- Path : tlsCrtPath ,
517
- },
518
- {
519
- Key : certSecretInfo .PrivateKeyKey ,
520
- Path : tlsKeyPath ,
521
- },
522
- },
523
- },
524
- },
525
- },
526
- },
557
+ certVolumes ,
527
558
)
528
559
529
560
// update container volume mounts
530
561
for i := range dep .Spec .Template .Spec .Containers {
531
562
dep .Spec .Template .Spec .Containers [i ].VolumeMounts = slices .Concat (
532
563
slices .DeleteFunc (dep .Spec .Template .Spec .Containers [i ].VolumeMounts , func (v corev1.VolumeMount ) bool {
533
- return volumeMountsToReplace .Has (v .Name )
564
+ return volumesToRemove .Has (v .Name )
534
565
}),
535
- func () []corev1.VolumeMount {
536
- volumeMounts := make ([]corev1.VolumeMount , 0 , len (certVolumeMounts ))
537
- for _ , name := range slices .Sorted (maps .Keys (certVolumeMounts )) {
538
- volumeMounts = append (volumeMounts , corev1.VolumeMount {
539
- Name : name ,
540
- MountPath : certVolumeMounts [name ],
541
- })
542
- }
543
- return volumeMounts
544
- }(),
566
+ certVolumeMounts ,
545
567
)
546
568
}
547
569
}
0 commit comments