From bbd2ab897bd68c1b56155ef0e6b7b6cdc1997c1b Mon Sep 17 00:00:00 2001 From: maskarb Date: Fri, 3 Jan 2025 13:57:20 -0500 Subject: [PATCH 1/2] manually scale down a deployment without going thru zero --- apis/cloud.redhat.com/v1alpha1/clowdapp_types.go | 4 ++++ .../cloud.redhat.com/providers/deployment/impl.go | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/apis/cloud.redhat.com/v1alpha1/clowdapp_types.go b/apis/cloud.redhat.com/v1alpha1/clowdapp_types.go index 7717eb9f2..e2aad7be7 100644 --- a/apis/cloud.redhat.com/v1alpha1/clowdapp_types.go +++ b/apis/cloud.redhat.com/v1alpha1/clowdapp_types.go @@ -251,6 +251,10 @@ func (d *Deployment) GetReplicaCount() *int32 { return &retVal } +func (d *Deployment) HasAutoScaler() bool { + return d.AutoScaler != nil || d.AutoScalerSimple != nil +} + type DeploymentStrategy struct { // PrivateStrategy allows a deployment that only uses a private port to set // the deployment strategy one of Recreate or Rolling, default for a diff --git a/controllers/cloud.redhat.com/providers/deployment/impl.go b/controllers/cloud.redhat.com/providers/deployment/impl.go index bcde8188a..7dc82cdc0 100644 --- a/controllers/cloud.redhat.com/providers/deployment/impl.go +++ b/controllers/cloud.redhat.com/providers/deployment/impl.go @@ -65,7 +65,14 @@ func setMinReplicas(deployment *crd.Deployment, d *apps.Deployment) { // No sense in running all these conditionals if desired state and observed state match if d.Spec.Replicas != nil && (*d.Spec.Replicas >= *replicaCount) { - return + // if deployment has an autoscaler, just keep the replica count the same it currently is + // since it has at least the desired count + if deployment.HasAutoScaler() { + return + } + // reset the replica count when there is no autoscaler. this will scale down a deployment that + // has more than desired count + d.Spec.Replicas = replicaCount } // If the spec has nil replicas or the spec replicas are less than the deployment replicas From 1fb1eab3c449b3fc38235fddf963f960e825b2a2 Mon Sep 17 00:00:00 2001 From: maskarb Date: Fri, 3 Jan 2025 14:10:31 -0500 Subject: [PATCH 2/2] add non-zero scaledown test --- .../00-install.yaml | 7 +++ .../01-assert.yaml | 21 +++++++++ .../01-pods.yaml | 47 +++++++++++++++++++ .../02-assert.yaml | 8 ++++ .../02-pods.yaml | 13 +++++ .../03-delete.yaml | 10 ++++ 6 files changed, 106 insertions(+) create mode 100644 tests/kuttl/test-replica-scaledown-nonzero/00-install.yaml create mode 100644 tests/kuttl/test-replica-scaledown-nonzero/01-assert.yaml create mode 100644 tests/kuttl/test-replica-scaledown-nonzero/01-pods.yaml create mode 100644 tests/kuttl/test-replica-scaledown-nonzero/02-assert.yaml create mode 100644 tests/kuttl/test-replica-scaledown-nonzero/02-pods.yaml create mode 100644 tests/kuttl/test-replica-scaledown-nonzero/03-delete.yaml diff --git a/tests/kuttl/test-replica-scaledown-nonzero/00-install.yaml b/tests/kuttl/test-replica-scaledown-nonzero/00-install.yaml new file mode 100644 index 000000000..182dee55b --- /dev/null +++ b/tests/kuttl/test-replica-scaledown-nonzero/00-install.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: test-replica-scaledown-nonzero +spec: + finalizers: + - kubernetes diff --git a/tests/kuttl/test-replica-scaledown-nonzero/01-assert.yaml b/tests/kuttl/test-replica-scaledown-nonzero/01-assert.yaml new file mode 100644 index 000000000..aeae8bc3e --- /dev/null +++ b/tests/kuttl/test-replica-scaledown-nonzero/01-assert.yaml @@ -0,0 +1,21 @@ +--- +apiVersion: v1 +kind: Secret +metadata: + name: puptoo + namespace: test-replica-scaledown-nonzero + labels: + app: puptoo + ownerReferences: + - apiVersion: cloud.redhat.com/v1alpha1 + kind: ClowdApp + name: puptoo +type: Opaque +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: puptoo-processor + namespace: test-replica-scaledown-nonzero +spec: + replicas: 4 diff --git a/tests/kuttl/test-replica-scaledown-nonzero/01-pods.yaml b/tests/kuttl/test-replica-scaledown-nonzero/01-pods.yaml new file mode 100644 index 000000000..993e5482b --- /dev/null +++ b/tests/kuttl/test-replica-scaledown-nonzero/01-pods.yaml @@ -0,0 +1,47 @@ +--- +apiVersion: cloud.redhat.com/v1alpha1 +kind: ClowdEnvironment +metadata: + name: test-replica-scaledown-nonzero +spec: + targetNamespace: test-replica-scaledown-nonzero + providers: + web: + port: 8000 + mode: operator + metrics: + port: 9000 + mode: operator + path: "/metrics" + kafka: + mode: none + db: + mode: none + logging: + mode: none + objectStore: + mode: none + inMemoryDb: + mode: none + featureFlags: + mode: none + resourceDefaults: + limits: + cpu: 400m + memory: 1024Mi + requests: + cpu: 30m + memory: 512Mi +--- +apiVersion: cloud.redhat.com/v1alpha1 +kind: ClowdApp +metadata: + name: puptoo + namespace: test-replica-scaledown-nonzero +spec: + envName: test-replica-scaledown-nonzero + deployments: + - name: processor + replicas: 4 + podSpec: + image: quay.io/psav/clowder-hello diff --git a/tests/kuttl/test-replica-scaledown-nonzero/02-assert.yaml b/tests/kuttl/test-replica-scaledown-nonzero/02-assert.yaml new file mode 100644 index 000000000..435bfadb1 --- /dev/null +++ b/tests/kuttl/test-replica-scaledown-nonzero/02-assert.yaml @@ -0,0 +1,8 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: puptoo-processor + namespace: test-replica-scaledown-nonzero +spec: + replicas: 1 diff --git a/tests/kuttl/test-replica-scaledown-nonzero/02-pods.yaml b/tests/kuttl/test-replica-scaledown-nonzero/02-pods.yaml new file mode 100644 index 000000000..b8186ad75 --- /dev/null +++ b/tests/kuttl/test-replica-scaledown-nonzero/02-pods.yaml @@ -0,0 +1,13 @@ +--- +apiVersion: cloud.redhat.com/v1alpha1 +kind: ClowdApp +metadata: + name: puptoo + namespace: test-replica-scaledown-nonzero +spec: + envName: test-replica-scaledown-nonzero + deployments: + - name: processor + replicas: 1 + podSpec: + image: quay.io/psav/clowder-hello diff --git a/tests/kuttl/test-replica-scaledown-nonzero/03-delete.yaml b/tests/kuttl/test-replica-scaledown-nonzero/03-delete.yaml new file mode 100644 index 000000000..e01bf4770 --- /dev/null +++ b/tests/kuttl/test-replica-scaledown-nonzero/03-delete.yaml @@ -0,0 +1,10 @@ +--- +apiVersion: kuttl.dev/v1beta1 +kind: TestStep +delete: +- apiVersion: v1 + kind: Namespace + name: test-replica-scaledown-nonzero +- apiVersion: cloud.redhat.com/v1alpha1 + kind: ClowdEnvironment + name: test-replica-scaledown-nonzero