Skip to content

Commit

Permalink
Fix Otterize credentials operator not deleting IAM roles on pod clean…
Browse files Browse the repository at this point in the history
…up due to the Otterize finalizer not being added to service accounts (#160)
  • Loading branch information
amitlicht authored Sep 15, 2024
1 parent 5a2fc0f commit 78fe3f2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func (r *ServiceAccountReconciler) handleServiceAccountUpdate(ctx context.Contex
return ctrl.Result{Requeue: true}, nil
}
if updated {
controllerutil.AddFinalizer(updatedServiceAccount, r.agent.FinalizerName())
err := r.Client.Patch(ctx, updatedServiceAccount, client.MergeFrom(&serviceAccount))
if err != nil {
if apierrors.IsConflict(err) {
Expand Down
1 change: 1 addition & 0 deletions src/operator/controllers/iam/webhooks/pod_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (w *ServiceAccountAnnotatingPodWebhook) handleOnce(ctx context.Context, pod

if !dryRun {
apiutils.AddLabel(updatedServiceAccount, w.agent.ServiceAccountLabel(), metadata.OtterizeServiceAccountHasPodsValue)
controllerutil.AddFinalizer(updatedServiceAccount, w.agent.FinalizerName())
err = w.client.Patch(ctx, updatedServiceAccount, client.MergeFrom(&serviceAccount))
if err != nil {
return corev1.Pod{}, false, "", errors.Errorf("could not patch service account: %w", err)
Expand Down
5 changes: 4 additions & 1 deletion src/operator/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/operator/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions src/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ func setupIAMAgents(ctx context.Context, mgr ctrl.Manager, client controllerrunt
azureIAMEnabled := viper.GetBool(operatorconfig.EnableAzureServiceAccountManagementKey)
gcpIAMEnabled := viper.GetBool(operatorconfig.EnableGCPServiceAccountManagementKey)

disableWebhookServer := viper.GetBool(operatorconfig.DisableWebhookServerKey)

if !awsIAMEnabled && !azureIAMEnabled && !gcpIAMEnabled {
return
}
Expand All @@ -356,8 +358,10 @@ func setupIAMAgents(ctx context.Context, mgr ctrl.Manager, client controllerrunt
awsCredentialsAgent := initAWSCredentialsAgent(ctx)
iamAgents = append(iamAgents, awsCredentialsAgent)

awsWebhookHandler := sa_pod_webhook_generic.NewServiceAccountAnnotatingPodWebhook(mgr, awsCredentialsAgent)
mgr.GetWebhookServer().Register("/mutate-aws-v1-pod", &webhook.Admission{Handler: awsWebhookHandler})
if !disableWebhookServer {
awsWebhookHandler := sa_pod_webhook_generic.NewServiceAccountAnnotatingPodWebhook(mgr, awsCredentialsAgent)
mgr.GetWebhookServer().Register("/mutate-aws-v1-pod", &webhook.Admission{Handler: awsWebhookHandler})
}
}

if gcpIAMEnabled {
Expand All @@ -369,8 +373,10 @@ func setupIAMAgents(ctx context.Context, mgr ctrl.Manager, client controllerrunt
azureCredentialsAgent := initAzureCredentialsAgent(ctx)
iamAgents = append(iamAgents, azureCredentialsAgent)

azureWebhookHandler := sa_pod_webhook_generic.NewServiceAccountAnnotatingPodWebhook(mgr, azureCredentialsAgent)
mgr.GetWebhookServer().Register("/mutate-azure-v1-pod", &webhook.Admission{Handler: azureWebhookHandler})
if !disableWebhookServer {
azureWebhookHandler := sa_pod_webhook_generic.NewServiceAccountAnnotatingPodWebhook(mgr, azureCredentialsAgent)
mgr.GetWebhookServer().Register("/mutate-azure-v1-pod", &webhook.Admission{Handler: azureWebhookHandler})
}
}

// setup service account reconciler
Expand Down
3 changes: 3 additions & 0 deletions src/operator/operatorconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const (
CertManagerIssuerDefault = "ca-issuer"
SelfSignedCertKey = "self-signed-cert"
SelfSignedCertDefault = true
DisableWebhookServerKey = "disable-webhook-server" // Disable webhook validator server
DisableWebhookServerDefault = false
CertManagerUseClustierIssuerKey = "cert-manager-use-cluster-issuer"
CertManagerUseClusterIssuerDefault = false
UseCertManagerApproverKey = "cert-manager-approve-requests"
Expand Down Expand Up @@ -76,6 +78,7 @@ func init() {
viper.SetDefault(SpireServerAddrKey, SpireServerAddrDefault)
viper.SetDefault(CertProviderKey, CertProviderDefault)
viper.SetDefault(SelfSignedCertKey, SelfSignedCertDefault)
viper.SetDefault(DisableWebhookServerKey, DisableWebhookServerDefault)
viper.SetDefault(EnableLeaderElectionKey, EnableLeaderElectionDefault)
viper.SetDefault(CertManagerIssuerKey, CertManagerIssuerDefault)
viper.SetDefault(CertManagerUseClustierIssuerKey, CertManagerUseClusterIssuerDefault)
Expand Down

0 comments on commit 78fe3f2

Please sign in to comment.