From 842f91933ca5744f1f2f15825f178745e2ab1d56 Mon Sep 17 00:00:00 2001 From: Lukasz Szaszkiewicz Date: Wed, 3 Jul 2024 11:47:57 +0200 Subject: [PATCH] operator/certrotation: introduce an optional lock to CABundleConfigMap and RotatedSigningCASecret --- pkg/operator/certrotation/cabundle.go | 8 ++++++++ pkg/operator/certrotation/signer.go | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/pkg/operator/certrotation/cabundle.go b/pkg/operator/certrotation/cabundle.go index 58c10b7cad..4104d1667a 100644 --- a/pkg/operator/certrotation/cabundle.go +++ b/pkg/operator/certrotation/cabundle.go @@ -7,6 +7,7 @@ import ( "fmt" "reflect" "sort" + "sync" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/equality" @@ -34,6 +35,9 @@ type CABundleConfigMap struct { Owner *metav1.OwnerReference // AdditionalAnnotations is a collection of annotations set for the secret AdditionalAnnotations AdditionalAnnotations + // Lock is an optional mutex that protects the EnsureConfigMapCABundle method. + // Use it only when this instance is shared across multiple controllers. + Lock *sync.Mutex // Plumbing: Informer corev1informers.ConfigMapInformer Lister corev1listers.ConfigMapLister @@ -42,6 +46,10 @@ type CABundleConfigMap struct { } func (c CABundleConfigMap) EnsureConfigMapCABundle(ctx context.Context, signingCertKeyPair *crypto.CA) ([]*x509.Certificate, error) { + if c.Lock != nil { + c.Lock.Lock() + defer c.Lock.Unlock() + } // by this point we have current signing cert/key pair. We now need to make sure that the ca-bundle configmap has this cert and // doesn't have any expired certs modified := false diff --git a/pkg/operator/certrotation/signer.go b/pkg/operator/certrotation/signer.go index 36f3cf292d..5429b98456 100644 --- a/pkg/operator/certrotation/signer.go +++ b/pkg/operator/certrotation/signer.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "sync" "time" "github.com/openshift/library-go/pkg/crypto" @@ -47,6 +48,10 @@ type RotatedSigningCASecret struct { // AdditionalAnnotations is a collection of annotations set for the secret AdditionalAnnotations AdditionalAnnotations + // Lock is an optional mutex that protects the EnsureSigningCertKeyPair method. + // Use it only when this instance is shared across multiple controllers. + Lock *sync.Mutex + // Plumbing: Informer corev1informers.SecretInformer Lister corev1listers.SecretLister @@ -63,6 +68,10 @@ type RotatedSigningCASecret struct { // EnsureSigningCertKeyPair manages the entire lifecycle of a signer cert as a secret, from creation to continued rotation. // It always returns the currently used CA pair, a bool indicating whether it was created/updated within this function call and an error. func (c RotatedSigningCASecret) EnsureSigningCertKeyPair(ctx context.Context) (*crypto.CA, bool, error) { + if c.Lock != nil { + c.Lock.Lock() + defer c.Lock.Unlock() + } modified := false originalSigningCertKeyPairSecret, err := c.Lister.Secrets(c.Namespace).Get(c.Name) if err != nil && !apierrors.IsNotFound(err) {