Skip to content

Commit

Permalink
add replicas.idle to set idleReplicaCount on SO
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Kuendig <[email protected]>
  • Loading branch information
lucakuendig committed Jan 31, 2023
1 parent 7cea10c commit fd57b33
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 4 deletions.
7 changes: 7 additions & 0 deletions config/crd/bases/http.keda.sh_httpscaledobjects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ spec:
- jsonPath: .spec.replicas.max
name: MaxReplicas
type: integer
- jsonPath: .spec.replicas.idle
name: IdleReplicas
type: integer
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
Expand Down Expand Up @@ -76,6 +79,10 @@ spec:
(Default 0)
format: int32
type: integer
idle:
description: Amount of replicas to have in the deployment while beeing idle
format: int32
type: integer
type: object
scaleTargetRef:
description: The name of the deployment to route HTTP requests to
Expand Down
1 change: 1 addition & 0 deletions e2e/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func getScaledObject(
"",
1,
2,
0,
30,
)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions examples/xkcd/templates/httpscaledobject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ spec:
replicas:
min: {{ .Values.autoscaling.http.minReplicas }}
max: {{ .Values.autoscaling.http.maxReplicas }}
idle: {{ .Values.autoscaling.http.idleReplicas }}
1 change: 1 addition & 0 deletions examples/xkcd/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ autoscaling:
http:
minReplicas: 0
maxReplicas: 10
idleReplicas: 0
3 changes: 3 additions & 0 deletions operator/api/v1alpha1/httpscaledobject_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ type ReplicaStruct struct {
Min int32 `json:"min,omitempty" description:"Minimum amount of replicas to have in the deployment (Default 0)"`
// Maximum amount of replicas to have in the deployment (Default 100)
Max int32 `json:"max,omitempty" description:"Maximum amount of replicas to have in the deployment (Default 100)"`
// Amount of replicas to have in the deployment while beeing idle
Idle int32 `json:"idle,omitempty" description:"Amount of replicas to have in the deployment while beeing idle"`
}

// HTTPScaledObjectSpec defines the desired state of HTTPScaledObject
Expand Down Expand Up @@ -133,6 +135,7 @@ type HTTPScaledObjectStatus struct {
// +kubebuilder:printcolumn:name="ScaleTargetPort",type="integer",JSONPath=".spec.scaleTargetRef"
// +kubebuilder:printcolumn:name="MinReplicas",type="integer",JSONPath=".spec.replicas.min"
// +kubebuilder:printcolumn:name="MaxReplicas",type="integer",JSONPath=".spec.replicas.max"
// +kubebuilder:printcolumn:name="IdleReplicas",type="integer",JSONPath=".spec.replicas.idle"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:printcolumn:name="Active",type="string",JSONPath=".status.conditions[?(@.type==\"HTTPScaledObjectIsReady\")].status"

Expand Down
3 changes: 2 additions & 1 deletion operator/controllers/scaled_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func createOrUpdateScaledObject(
httpso *v1alpha1.HTTPScaledObject,
) error {
logger.Info("Creating scaled objects", "external scaler host name", externalScalerHostName)

logger.Info("This is the current httpso object", "output", httpso.Spec)
appScaledObject, appErr := k8s.NewScaledObject(
httpso.GetNamespace(),
fmt.Sprintf("%s-app", httpso.GetName()), // HTTPScaledObject name is the same as the ScaledObject name
Expand All @@ -34,6 +34,7 @@ func createOrUpdateScaledObject(
httpso.Spec.Host,
httpso.Spec.Replicas.Min,
httpso.Spec.Replicas.Max,
httpso.Spec.Replicas.Idle,
httpso.Spec.CooldownPeriod,
)
if appErr != nil {
Expand Down
11 changes: 10 additions & 1 deletion operator/controllers/scaled_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestCreateOrUpdateScaledObject(t *testing.T) {
metadata["name"],
)

// HTTPScaledObject min/max replicas are int32s,
// HTTPScaledObject min/max/idle replicas are int32s,
// but the ScaledObject's spec is decoded into
// an *unsructured.Unstructured (basically a map[string]interface{})
// which is an int64. we need to convert the
Expand All @@ -73,11 +73,16 @@ func TestCreateOrUpdateScaledObject(t *testing.T) {
int64(testInfra.httpso.Spec.Replicas.Max),
spec["maxReplicaCount"],
)
r.Equal(
int64(testInfra.httpso.Spec.Replicas.Idle),
spec["idleReplicaCount"],
)

// now update the min and max replicas on the httpso
// and call createOrUpdateScaledObject again
testInfra.httpso.Spec.Replicas.Min++
testInfra.httpso.Spec.Replicas.Max++
testInfra.httpso.Spec.Replicas.Idle++
r.NoError(createOrUpdateScaledObject(
testInfra.ctx,
testInfra.cl,
Expand All @@ -104,6 +109,10 @@ func TestCreateOrUpdateScaledObject(t *testing.T) {
int64(testInfra.httpso.Spec.Replicas.Max),
spec["maxReplicaCount"],
)
r.Equal(
int64(testInfra.httpso.Spec.Replicas.Idle),
spec["idleReplicaCount"],
)
}

func getSO(
Expand Down
5 changes: 3 additions & 2 deletions operator/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ func newCommonTestInfra(namespace, appName string) *commonTestInfra {
Port: 8081,
},
Replicas: v1alpha1.ReplicaStruct{
Min: 0,
Max: 20,
Min: 0,
Max: 20,
Idle: 0,
},
},
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/k8s/scaledobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewScaledObject(
host string,
minReplicas,
maxReplicas int32,
idleReplicas int32,
cooldownPeriod int32,
) (*unstructured.Unstructured, error) {
// https://keda.sh/docs/1.5/faq/
Expand All @@ -79,6 +80,7 @@ func NewScaledObject(
"Labels": labels,
"MinReplicas": minReplicas,
"MaxReplicas": maxReplicas,
"IdleReplicas": idleReplicas,
"DeploymentName": deploymentName,
"ScalerAddress": scalerAddress,
"Host": host,
Expand Down
1 change: 1 addition & 0 deletions pkg/k8s/templates/scaledobject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ metadata:
spec:
minReplicaCount: {{ .MinReplicas }}
maxReplicaCount: {{ .MaxReplicas }}
idleReplicaCount: {{ .IdleReplicas }}
cooldownPeriod: {{ .CooldownPeriod }}
pollingInterval: 1
scaleTargetRef:
Expand Down

0 comments on commit fd57b33

Please sign in to comment.