From 9eda8eb1bb985357080ba5b51732b4e533f595cf Mon Sep 17 00:00:00 2001 From: Anatolii Bazko Date: Thu, 25 Jan 2024 13:07:51 +0100 Subject: [PATCH] feat: Support HorizontalPodAutoscaler Signed-off-by: Anatolii Bazko --- pkg/deploy/deployment.go | 19 +++++++++++++++++-- pkg/deploy/gateway/gateway.go | 2 +- pkg/deploy/job.go | 1 - pkg/deploy/registry/registry_deployment.go | 5 +---- pkg/deploy/server/server_deployment.go | 3 +-- pkg/deploy/server/server_reconciler.go | 4 ++-- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/pkg/deploy/deployment.go b/pkg/deploy/deployment.go index 2156c49e9..6f175005c 100644 --- a/pkg/deploy/deployment.go +++ b/pkg/deploy/deployment.go @@ -67,6 +67,10 @@ func SyncDeploymentSpecToCluster( return false, err } + if err := setDesiredReplicas(deploymentSpec, deployContext); err != nil { + return false, err + } + done, err := Sync(deployContext, deploymentSpec, deploymentDiffOpts) if err != nil || !done { // Failed to sync (update), let's delete and create instead @@ -92,11 +96,11 @@ func SyncDeploymentSpecToCluster( return false, err } - if actual.Spec.Strategy.Type == appsv1.RollingUpdateDeploymentStrategyType && actual.Status.Replicas > 1 { + provisioned := actual.Status.UnavailableReplicas == 0 + if actual.Spec.Strategy.Type == appsv1.RollingUpdateDeploymentStrategyType && !provisioned { logrus.Infof("Deployment %s is in the rolling update state.", deploymentSpec.Name) } - provisioned := actual.Status.AvailableReplicas == 1 && actual.Status.Replicas == 1 return provisioned, nil } @@ -509,3 +513,14 @@ func MountConfigMaps(specDeployment *appsv1.Deployment, deployContext *chetypes. return nil } + +// setDesiredReplicas sets replicas count from the actual deployment. +func setDesiredReplicas(deployment *appsv1.Deployment, deployCtx *chetypes.DeployContext) error { + actual := &appsv1.Deployment{} + if exists, err := GetNamespacedObject(deployCtx, deployment.ObjectMeta.Name, actual); !exists { + return err + } + + deployment.Spec.Replicas = actual.Spec.Replicas + return nil +} diff --git a/pkg/deploy/gateway/gateway.go b/pkg/deploy/gateway/gateway.go index bf768d27f..169ef60da 100644 --- a/pkg/deploy/gateway/gateway.go +++ b/pkg/deploy/gateway/gateway.go @@ -147,7 +147,7 @@ func syncAll(deployContext *chetypes.DeployContext) error { if err != nil { return err } - if _, err := deploy.Sync(deployContext, depl, deploy.DefaultDeploymentDiffOpts); err != nil { + if _, err := deploy.SyncDeploymentSpecToCluster(deployContext, depl, deploy.DefaultDeploymentDiffOpts); err != nil { // Failed to sync (update), let's delete and create instead if strings.Contains(err.Error(), "field is immutable") { if _, err := deploy.DeleteNamespacedObject(deployContext, depl.Name, &appsv1.Deployment{}); err != nil { diff --git a/pkg/deploy/job.go b/pkg/deploy/job.go index c1bdb2fc6..ae922092b 100644 --- a/pkg/deploy/job.go +++ b/pkg/deploy/job.go @@ -95,7 +95,6 @@ func getJobSpec( }, Spec: corev1.PodSpec{ ServiceAccountName: serviceAccountName, - DeprecatedServiceAccount: serviceAccountName, RestartPolicy: "Never", TerminationGracePeriodSeconds: &terminationGracePeriodSeconds, Containers: []corev1.Container{ diff --git a/pkg/deploy/registry/registry_deployment.go b/pkg/deploy/registry/registry_deployment.go index 1ee2f3e73..4356b8477 100644 --- a/pkg/deploy/registry/registry_deployment.go +++ b/pkg/deploy/registry/registry_deployment.go @@ -19,7 +19,6 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" - "k8s.io/utils/pointer" ) func GetSpecRegistryDeployment( @@ -53,9 +52,7 @@ func GetSpecRegistryDeployment( Labels: labels, }, Spec: appsv1.DeploymentSpec{ - Replicas: pointer.Int32Ptr(1), - RevisionHistoryLimit: pointer.Int32Ptr(2), - Selector: &metav1.LabelSelector{MatchLabels: labelSelector}, + Selector: &metav1.LabelSelector{MatchLabels: labelSelector}, Strategy: appsv1.DeploymentStrategy{ Type: appsv1.RollingUpdateDeploymentStrategyType, RollingUpdate: &appsv1.RollingUpdateDeployment{ diff --git a/pkg/deploy/server/server_deployment.go b/pkg/deploy/server/server_deployment.go index ba88390c7..0998f4b04 100644 --- a/pkg/deploy/server/server_deployment.go +++ b/pkg/deploy/server/server_deployment.go @@ -118,8 +118,7 @@ func (s CheServerReconciler) getDeploymentSpec(ctx *chetypes.DeployContext) (*ap Labels: labels, }, Spec: corev1.PodSpec{ - ServiceAccountName: "che", - DeprecatedServiceAccount: "che", + ServiceAccountName: "che", Volumes: []corev1.Volume{ customPublicCertsVolume, }, diff --git a/pkg/deploy/server/server_reconciler.go b/pkg/deploy/server/server_reconciler.go index 8ea908c3c..d36ce9b88 100644 --- a/pkg/deploy/server/server_reconciler.go +++ b/pkg/deploy/server/server_reconciler.go @@ -102,13 +102,13 @@ func (s *CheServerReconciler) syncActiveChePhase(ctx *chetypes.DeployContext) (b } if exists { - if cheDeployment.Status.AvailableReplicas < 1 { + if cheDeployment.Status.AvailableReplicas == 0 { if ctx.CheCluster.Status.ChePhase != chev2.ClusterPhaseInactive { ctx.CheCluster.Status.ChePhase = chev2.ClusterPhaseInactive err := deploy.UpdateCheCRStatus(ctx, "Phase", chev2.ClusterPhaseInactive) return false, err } - } else if cheDeployment.Status.Replicas > 1 { + } else if cheDeployment.Status.Replicas != cheDeployment.Status.AvailableReplicas { if ctx.CheCluster.Status.ChePhase != chev2.RollingUpdate { ctx.CheCluster.Status.ChePhase = chev2.RollingUpdate err := deploy.UpdateCheCRStatus(ctx, "Phase", chev2.RollingUpdate)