@@ -3,7 +3,6 @@ package generators
33import (
44 "cmp"
55 "fmt"
6- "maps"
76 "slices"
87 "strconv"
98 "strings"
@@ -28,15 +27,31 @@ import (
2827)
2928
3029const (
31- tlsCrtPath = "tls.crt"
32- tlsKeyPath = "tls.key"
33-
3430 labelKubernetesNamespaceMetadataName = "kubernetes.io/metadata.name"
3531)
3632
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+ },
4055}
4156
4257// 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) (
8095
8196 secretInfo := render .CertProvisionerFor (depSpec .Name , opts ).GetCertSecretInfo ()
8297 if webhookDeployments .Has (depSpec .Name ) && secretInfo != nil {
83- addCertVolumesToDeployment (deploymentResource , * secretInfo )
98+ ensureCorrectDeploymentCertVolumes (deploymentResource , * secretInfo )
8499 }
85100
86101 objs = append (objs , deploymentResource )
@@ -488,60 +503,67 @@ func getWebhookServicePort(wh v1alpha1.WebhookDescription) corev1.ServicePort {
488503 }
489504}
490505
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+
494544 for _ , c := range dep .Spec .Template .Spec .Containers {
495545 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 )
498548 }
499549 }
500550 }
501551
502552 // update pod volumes
503553 dep .Spec .Template .Spec .Volumes = slices .Concat (
504554 slices .DeleteFunc (dep .Spec .Template .Spec .Volumes , func (v corev1.Volume ) bool {
505- return volumeMountsToReplace .Has (v .Name )
555+ return volumesToRemove .Has (v .Name )
506556 }),
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 ,
527558 )
528559
529560 // update container volume mounts
530561 for i := range dep .Spec .Template .Spec .Containers {
531562 dep .Spec .Template .Spec .Containers [i ].VolumeMounts = slices .Concat (
532563 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 )
534565 }),
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 ,
545567 )
546568 }
547569}
0 commit comments