Skip to content

Commit

Permalink
kie-kogito-serverless-operator-559: Add the ability to scale the Jobs…
Browse files Browse the repository at this point in the history
… Service to 0 (#563)
wmedvede authored Oct 31, 2024
1 parent 957ca3d commit ae7dde3
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/controller/platform/k8s.go
Original file line number Diff line number Diff line change
@@ -159,6 +159,7 @@ func createOrUpdateDeployment(ctx context.Context, client client.Client, platfor
MatchLabels: selectorLbl,
},
Replicas: &replicas,
Strategy: psh.GetDeploymentStrategy(),
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: lbl,
@@ -200,6 +201,9 @@ func createOrUpdateDeployment(ctx context.Context, client client.Client, platfor
if op, err := controllerutil.CreateOrUpdate(ctx, client, serviceDeployment, func() error {
knative.SaveKnativeData(&serviceDeploymentSpec.Template.Spec, &serviceDeployment.Spec.Template.Spec)
err := mergo.Merge(&(serviceDeployment.Spec), serviceDeploymentSpec, mergo.WithOverride)
// mergo.Merge algorithm is not setting the serviceDeployment.Spec.Replicas when the
// *serviceDeploymentSpec.Replicas is 0. Making impossible to scale to zero. Ensure the value.
serviceDeployment.Spec.Replicas = serviceDeploymentSpec.Replicas
if err != nil {
return err
}
18 changes: 18 additions & 0 deletions internal/controller/platform/services/services.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ package services
import (
"fmt"

appsv1 "k8s.io/api/apps/v1"

"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/cfg"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/knative"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/profiles"
@@ -70,6 +72,8 @@ type PlatformServiceHandler interface {
GetPodResourceRequirements() corev1.ResourceRequirements
// GetReplicaCount Returns the default pod replica count for the given service
GetReplicaCount() int32
// GetDeploymentStrategy Returns the deployment strategy for the service
GetDeploymentStrategy() appsv1.DeploymentStrategy

// MergeContainerSpec performs a merge with override using the containerSpec argument and the expected values based on the service's pod template specifications. The returning
// object is the merged result
@@ -255,6 +259,10 @@ func (d *DataIndexHandler) GetReplicaCount() int32 {
return 1
}

func (d *DataIndexHandler) GetDeploymentStrategy() appsv1.DeploymentStrategy {
return appsv1.DeploymentStrategy{}
}

func (d *DataIndexHandler) GetServiceCmName() string {
return fmt.Sprintf("%s-props", d.GetServiceName())
}
@@ -383,9 +391,19 @@ func (j *JobServiceHandler) GetPodResourceRequirements() corev1.ResourceRequirem
}

func (j *JobServiceHandler) GetReplicaCount() int32 {
if j.platform.Spec.Services.JobService.PodTemplate.Replicas != nil && *j.platform.Spec.Services.JobService.PodTemplate.Replicas == 0 {
return 0
}
return 1
}

func (j *JobServiceHandler) GetDeploymentStrategy() appsv1.DeploymentStrategy {
return appsv1.DeploymentStrategy{
Type: appsv1.RecreateDeploymentStrategyType,
RollingUpdate: nil,
}
}

func (j JobServiceHandler) MergeContainerSpec(containerSpec *corev1.Container) (*corev1.Container, error) {
return mergeContainerSpec(containerSpec, &j.platform.Spec.Services.JobService.PodTemplate.Container)
}

0 comments on commit ae7dde3

Please sign in to comment.