Skip to content

Commit

Permalink
feat: Support HorizontalPodAutoscaler
Browse files Browse the repository at this point in the history
Signed-off-by: Anatolii Bazko <abazko@redhat.com>
  • Loading branch information
tolusha committed Jan 25, 2024
1 parent 504f01e commit 9eda8eb
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
19 changes: 17 additions & 2 deletions pkg/deploy/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/deploy/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion pkg/deploy/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func getJobSpec(
},
Spec: corev1.PodSpec{
ServiceAccountName: serviceAccountName,
DeprecatedServiceAccount: serviceAccountName,
RestartPolicy: "Never",
TerminationGracePeriodSeconds: &terminationGracePeriodSeconds,
Containers: []corev1.Container{
Expand Down
5 changes: 1 addition & 4 deletions pkg/deploy/registry/registry_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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{
Expand Down
3 changes: 1 addition & 2 deletions pkg/deploy/server/server_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/deploy/server/server_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9eda8eb

Please sign in to comment.