From 189c6f808db3dd3057ec723b96b44977fc54efd8 Mon Sep 17 00:00:00 2001 From: Amit Sahastrabuddhe Date: Wed, 27 Mar 2024 18:16:13 +0530 Subject: [PATCH 1/2] PCP-2735: EKS cluster deletion stuck with error failed to delete OIDC provider --- pkg/cloud/services/eks/oidc.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/cloud/services/eks/oidc.go b/pkg/cloud/services/eks/oidc.go index ab23395b1f..efbdcd6c0c 100644 --- a/pkg/cloud/services/eks/oidc.go +++ b/pkg/cloud/services/eks/oidc.go @@ -36,6 +36,13 @@ import ( "sigs.k8s.io/cluster-api/controllers/remote" ) +const ( + // OidcProviderArnAnnotation set/unset this annotation to managed control plane. + // This is required in case of force pivot control plane status do not have ARN in status. + // In that cases annotation will be used to delete oidc resource. + OidcProviderArnAnnotation = "aws.spectrocloud.com/oidcProviderArn" +) + func (s *Service) reconcileOIDCProvider(cluster *eks.Cluster) error { if !s.scope.ControlPlane.Spec.AssociateOIDCProvider { return nil @@ -53,7 +60,10 @@ func (s *Service) reconcileOIDCProvider(cluster *eks.Cluster) error { } s.scope.ControlPlane.Status.OIDCProvider.ARN = oidcProvider anno := s.scope.ControlPlane.GetAnnotations() - anno["aws.spectrocloud.com/oidcProviderArn"] = oidcProvider + if anno == nil { + anno = make(map[string]string) + } + anno[OidcProviderArnAnnotation] = oidcProvider s.scope.ControlPlane.SetAnnotations(anno) if err := s.scope.PatchObject(); err != nil { return errors.Wrap(err, "failed to update control plane with OIDC provider ARN") @@ -137,10 +147,15 @@ func (s *Service) reconcileTrustPolicy() error { } func (s *Service) deleteOIDCProvider() error { - anno := s.scope.ControlPlane.GetAnnotations() - arn := anno["aws.spectrocloud.com/oidcProviderArn"] + + // In case of force pivot managed control plane do not have ARN in status, that lead to oidcProvider not getting cleaned up during delete. + // OidcProviderArnAnnotation will be used to avoid it. + + annotations := s.scope.ControlPlane.GetAnnotations() + arn := annotations[OidcProviderArnAnnotation] if arn == "" { + // Upgrade support for cluster without OidcProviderArnAnnotation set arn = s.scope.ControlPlane.Status.OIDCProvider.ARN } @@ -158,6 +173,10 @@ func (s *Service) deleteOIDCProvider() error { return errors.Wrap(err, "failed to update control plane with OIDC provider ARN") } + // Remove OidcProviderArnAnnotation after successfully deleting oidc provider + annotations[OidcProviderArnAnnotation] = "" + s.scope.ControlPlane.SetAnnotations(annotations) + return nil } From b8cf9dbd1afe885010861f238eca8b40203e4026 Mon Sep 17 00:00:00 2001 From: Amit Sahastrabuddhe Date: Wed, 27 Mar 2024 19:51:50 +0530 Subject: [PATCH 2/2] code refactor --- pkg/cloud/services/eks/oidc.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/cloud/services/eks/oidc.go b/pkg/cloud/services/eks/oidc.go index efbdcd6c0c..784c9ce67a 100644 --- a/pkg/cloud/services/eks/oidc.go +++ b/pkg/cloud/services/eks/oidc.go @@ -37,10 +37,10 @@ import ( ) const ( - // OidcProviderArnAnnotation set/unset this annotation to managed control plane. + // OIDCProviderARNAnnotation set/unset this annotation to managed control plane. // This is required in case of force pivot control plane status do not have ARN in status. // In that cases annotation will be used to delete oidc resource. - OidcProviderArnAnnotation = "aws.spectrocloud.com/oidcProviderArn" + OIDCProviderARNAnnotation = "aws.spectrocloud.com/oidcProviderArn" ) func (s *Service) reconcileOIDCProvider(cluster *eks.Cluster) error { @@ -63,7 +63,7 @@ func (s *Service) reconcileOIDCProvider(cluster *eks.Cluster) error { if anno == nil { anno = make(map[string]string) } - anno[OidcProviderArnAnnotation] = oidcProvider + anno[OIDCProviderARNAnnotation] = oidcProvider s.scope.ControlPlane.SetAnnotations(anno) if err := s.scope.PatchObject(); err != nil { return errors.Wrap(err, "failed to update control plane with OIDC provider ARN") @@ -149,13 +149,13 @@ func (s *Service) reconcileTrustPolicy() error { func (s *Service) deleteOIDCProvider() error { // In case of force pivot managed control plane do not have ARN in status, that lead to oidcProvider not getting cleaned up during delete. - // OidcProviderArnAnnotation will be used to avoid it. + // OIDCProviderARNAnnotation will be used to avoid it. annotations := s.scope.ControlPlane.GetAnnotations() - arn := annotations[OidcProviderArnAnnotation] + arn := annotations[OIDCProviderARNAnnotation] if arn == "" { - // Upgrade support for cluster without OidcProviderArnAnnotation set + // Upgrade support for cluster without OIDCProviderARNAnnotation set arn = s.scope.ControlPlane.Status.OIDCProvider.ARN } @@ -173,8 +173,8 @@ func (s *Service) deleteOIDCProvider() error { return errors.Wrap(err, "failed to update control plane with OIDC provider ARN") } - // Remove OidcProviderArnAnnotation after successfully deleting oidc provider - annotations[OidcProviderArnAnnotation] = "" + // Remove OIDCProviderARNAnnotation after successfully deleting oidc provider + annotations[OIDCProviderARNAnnotation] = "" s.scope.ControlPlane.SetAnnotations(annotations) return nil