From 1b0f581712eb4e116832c446cd4c53a841d7b728 Mon Sep 17 00:00:00 2001 From: Haim Kortovich Date: Tue, 31 Dec 2024 16:31:34 -0500 Subject: [PATCH] only update smtp if there is change [ZITADOPER-7] --- .../controller/zitadelcluster_controller.go | 73 +++++++------------ 1 file changed, 27 insertions(+), 46 deletions(-) diff --git a/src/internal/controller/zitadelcluster_controller.go b/src/internal/controller/zitadelcluster_controller.go index 24836dc..141d009 100644 --- a/src/internal/controller/zitadelcluster_controller.go +++ b/src/internal/controller/zitadelcluster_controller.go @@ -362,26 +362,6 @@ func (r *ZitadelClusterReconciler) reconcileSetupJob(ctx context.Context, zitade return ctrl.Result{}, nil } -// func (r *ZitadelClusterReconciler) scaleDownDeployment(ctx context.Context, zitadel *zitadelv1alpha1.ZitadelCluster) error { -// // scale down deployment first -// deploymentKey := client.ObjectKeyFromObject(zitadel) -// var existingDep appsv1.Deployment -// err := r.Get(ctx, deploymentKey, &existingDep) -// if err != nil { -// if errors.IsNotFound(err) { -// } else { -// return fmt.Errorf("error getting Deployment: %v", err) -// } -// } else { -// patch := client.MergeFrom(existingDep.DeepCopy()) -// existingDep.Spec.Replicas = ptr.To(int32(0)) -// if err := r.Patch(ctx, &existingDep, patch); err != nil { -// return fmt.Errorf("Error scaling down deployment: %v", err) -// } -// } -// return nil -// } - func (r *ZitadelClusterReconciler) reconcileDeployment(ctx context.Context, zitadel *zitadelv1alpha1.ZitadelCluster) (ctrl.Result, error) { // TODO: Reload on config changed key := client.ObjectKeyFromObject(zitadel) @@ -580,36 +560,37 @@ func (r *ZitadelClusterReconciler) reconcileSMTPConfig(ctx context.Context, zita return ctrl.Result{}, fmt.Errorf("Error getting SMTP config: %v", err) } } else { - adminRequest := &adm.UpdateEmailProviderSMTPRequest{ - SenderAddress: zitadel.Spec.SMTPConfig.SenderAddress, - SenderName: zitadel.Spec.SMTPConfig.SenderName, - Tls: zitadel.Spec.SMTPConfig.TLS, - Host: zitadel.Spec.SMTPConfig.Host, - Id: resp.Config.Id, - Password: "test", - Description: "autogenerated by k8s-operator", - } - if zitadel.Spec.SMTPConfig.User != nil && zitadel.Spec.SMTPConfig.Password != nil { - - passwordSecret, err := r.RefResolver.SecretKeyRef(ctx, zitadel.Spec.SMTPConfig.Password.SecretKeyRef, zitadel.Namespace) - if err != nil { - return ctrl.Result{}, err + if zitadel.Spec.SMTPConfig.SenderAddress != resp.Config.GetSmtp().GetSenderAddress() || zitadel.Spec.SMTPConfig.SenderName != resp.Config.GetSmtp().SenderName || zitadel.Spec.SMTPConfig.TLS != resp.Config.GetSmtp().Tls || zitadel.Spec.SMTPConfig.Host != resp.Config.GetSmtp().Host { + adminRequest := &adm.UpdateEmailProviderSMTPRequest{ + SenderAddress: zitadel.Spec.SMTPConfig.SenderAddress, + SenderName: zitadel.Spec.SMTPConfig.SenderName, + Tls: zitadel.Spec.SMTPConfig.TLS, + Host: zitadel.Spec.SMTPConfig.Host, + Id: resp.Config.Id, + Password: "test", + Description: "autogenerated by k8s-operator", } - adminRequest.Password = passwordSecret - adminRequest.User = *zitadel.Spec.SMTPConfig.User - } - if zitadel.Spec.SMTPConfig.ReplyToAddress != nil { - adminRequest.ReplyToAddress = *zitadel.Spec.SMTPConfig.ReplyToAddress - } + if zitadel.Spec.SMTPConfig.User != nil && zitadel.Spec.SMTPConfig.Password != nil { - if _, err = adminClient.UpdateEmailProviderSMTP(ctx, adminRequest); err != nil { - if !strings.Contains(err.Error(), "No changes") { - return ctrl.Result{}, fmt.Errorf("Could not update SMTP config: %v", err) + passwordSecret, err := r.RefResolver.SecretKeyRef(ctx, zitadel.Spec.SMTPConfig.Password.SecretKeyRef, zitadel.Namespace) + if err != nil { + return ctrl.Result{}, err + } + adminRequest.Password = passwordSecret + adminRequest.User = *zitadel.Spec.SMTPConfig.User + } + if zitadel.Spec.SMTPConfig.ReplyToAddress != nil { + adminRequest.ReplyToAddress = *zitadel.Spec.SMTPConfig.ReplyToAddress + } + if _, err = adminClient.UpdateEmailProviderSMTP(ctx, adminRequest); err != nil { + if !strings.Contains(err.Error(), "No changes") { + return ctrl.Result{}, fmt.Errorf("Could not update SMTP config: %v", err) + } } - } - patch := client.MergeFrom(zitadel.DeepCopy()) - return ctrl.Result{}, r.Status().Patch(ctx, zitadel, patch) + // patch := client.MergeFrom(zitadel.DeepCopy()) + // return ctrl.Result{}, r.Status().Patch(ctx, zitadel, patch) + } } return ctrl.Result{}, nil }