scale down deployment before creating or updating job

[ZITADOPER-7]
This commit is contained in:
Haim Kortovich
2024-12-31 13:01:02 -05:00
parent 62fe61e5b9
commit 4df3494149

View File

@@ -51,6 +51,7 @@ import (
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue" "k8s.io/client-go/util/workqueue"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller"
@@ -315,6 +316,9 @@ func (r *ZitadelClusterReconciler) reconcileSetupJob(ctx context.Context, zitade
if !errors.IsNotFound(err) { if !errors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("error getting SetupJob: %v", err) return ctrl.Result{}, fmt.Errorf("error getting SetupJob: %v", err)
} }
if err := r.scaleDownDeployment(ctx, zitadel); err != nil {
return ctrl.Result{}, err
}
// If job is not found, create the job // If job is not found, create the job
if err := r.Create(ctx, desiredSetupJob); err != nil { if err := r.Create(ctx, desiredSetupJob); err != nil {
return ctrl.Result{}, fmt.Errorf("error creating SetupJob: %v", err) return ctrl.Result{}, fmt.Errorf("error creating SetupJob: %v", err)
@@ -328,6 +332,10 @@ func (r *ZitadelClusterReconciler) reconcileSetupJob(ctx context.Context, zitade
// If the images don't match, delete the existing job and wait for deletion // If the images don't match, delete the existing job and wait for deletion
if existingImage != desiredImage { if existingImage != desiredImage {
if err := r.scaleDownDeployment(ctx, zitadel); err != nil {
return ctrl.Result{}, err
}
if err := r.Delete(ctx, &existingJob); err != nil { if err := r.Delete(ctx, &existingJob); err != nil {
return ctrl.Result{}, fmt.Errorf("error deleting existing SetupJob: %v", err) return ctrl.Result{}, fmt.Errorf("error deleting existing SetupJob: %v", err)
} }
@@ -360,6 +368,25 @@ func (r *ZitadelClusterReconciler) reconcileSetupJob(ctx context.Context, zitade
// If the job exists and the image matches, no action is needed // If the job exists and the image matches, no action is needed
return ctrl.Result{}, nil 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) { func (r *ZitadelClusterReconciler) reconcileDeployment(ctx context.Context, zitadel *zitadelv1alpha1.ZitadelCluster) (ctrl.Result, error) {
// TODO: Reload on config changed // TODO: Reload on config changed