Skip to content

Commit

Permalink
Merge pull request #92 from samuel-esp/enhancing-scaled-objects-tests
Browse files Browse the repository at this point in the history
Enhanced Tests For Scaled Objects Downscaling/Upscaling
  • Loading branch information
samuel-esp authored Aug 26, 2024
2 parents c3dd270 + f1872fd commit 6cd190f
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 20 deletions.
4 changes: 2 additions & 2 deletions chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ name: py-kube-downscaler
description: A Helm chart for deploying py-kube-downscaler

type: application
version: 0.2.6
appVersion: "24.8.1"
version: 0.2.7
appVersion: "24.8.2"
1 change: 1 addition & 0 deletions chart/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ rules:
- batch
resources:
- cronjobs
- jobs
verbs:
- get
- watch
Expand Down
6 changes: 4 additions & 2 deletions kube_downscaler/resources/keda.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ class ScaledObject(NamespacedAPIObject):
@property
def replicas(self):
if ScaledObject.keda_pause_annotation in self.annotations:
if self.annotations[ScaledObject.keda_pause_annotation] == "0":
if self.annotations[ScaledObject.keda_pause_annotation] is None:
replicas = 1
elif self.annotations[ScaledObject.keda_pause_annotation] == "0":
replicas = 0
elif self.annotations[ScaledObject.keda_pause_annotation] != "0":
elif self.annotations[ScaledObject.keda_pause_annotation] != "0" and self.annotations[ScaledObject.keda_pause_annotation] is not None:
replicas = int(self.annotations[ScaledObject.keda_pause_annotation])
else:
replicas = 1
Expand Down
29 changes: 14 additions & 15 deletions tests/test_autoscale_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ def test_upscale_hpa_with_autoscaling():
assert hpa.obj["spec"]["minReplicas"] == 4
assert hpa.obj["metadata"]["annotations"][ORIGINAL_REPLICAS_ANNOTATION] is None


def test_downscale_pdb_minavailable_with_autoscaling():
pdb = PodDisruptionBudget(
None,
Expand Down Expand Up @@ -1002,6 +1002,7 @@ def test_upscale_pdb_minavailable_with_autoscaling():
assert pdb.obj["spec"]["minAvailable"] == 4
assert pdb.obj["metadata"]["annotations"][ORIGINAL_REPLICAS_ANNOTATION] is None


def test_downscale_pdb_maxunavailable_with_autoscaling():
pdb = PodDisruptionBudget(
None,
Expand Down Expand Up @@ -1068,7 +1069,7 @@ def test_upscale_pdb_maxunavailable_with_autoscaling():
assert pdb.obj["spec"]["maxUnavailable"] == 4
assert pdb.obj["metadata"]["annotations"][ORIGINAL_REPLICAS_ANNOTATION] is None


def test_downscale_daemonset_with_autoscaling():
ds = DaemonSet(
None,
Expand Down Expand Up @@ -1168,7 +1169,6 @@ def test_downscale_scaledobject_with_pause_annotation_already_present():
tzinfo=timezone.utc
)


autoscale_resource(
so,
upscale_target_only=False,
Expand All @@ -1185,7 +1185,7 @@ def test_downscale_scaledobject_with_pause_annotation_already_present():

# Check if the annotations have been correctly updated
assert so.annotations[ScaledObject.keda_pause_annotation] == "0"
assert so.annotations[ScaledObject.last_keda_pause_annotation_if_present] == "3"
assert so.replicas == 0


def test_upscale_scaledobject_with_pause_annotation_already_present():
Expand All @@ -1206,12 +1206,10 @@ def test_upscale_scaledobject_with_pause_annotation_already_present():
}
)


now = datetime.strptime("2023-08-21T10:30:00Z", "%Y-%m-%dT%H:%M:%SZ").replace(
tzinfo=timezone.utc
)


autoscale_resource(
so,
upscale_target_only=False,
Expand All @@ -1228,18 +1226,20 @@ def test_upscale_scaledobject_with_pause_annotation_already_present():

# Check if the annotations have been correctly updated for the upscale operation
assert so.annotations[ScaledObject.keda_pause_annotation] == "3"
assert so.replicas == 3
assert so.annotations.get(ScaledObject.last_keda_pause_annotation_if_present) is None

def test_downscale_scaledobject_without_pause_annotation():

def test_downscale_scaledobject_without_keda_pause_annotation():
so = ScaledObject(
None,
{
"metadata": {
"name": "scaledobject-1",
"namespace": "default",
"creationTimestamp": "2023-08-21T10:00:00Z",
"annotations": {}
},
"spec": {}
}
)

Expand All @@ -1263,9 +1263,11 @@ def test_downscale_scaledobject_without_pause_annotation():

# Check if the annotations have been correctly updated
assert so.annotations[ScaledObject.keda_pause_annotation] == "0"
assert so.annotations.get(ScaledObject.last_keda_pause_annotation_if_present) is None
assert so.replicas == 0


def test_upscale_scaledobject_without_pause_annotation():
def test_upscale_scaledobject_without_keda_pause_annotation():
so = ScaledObject(
None,
{
Expand All @@ -1275,20 +1277,16 @@ def test_upscale_scaledobject_without_pause_annotation():
"creationTimestamp": "2023-08-21T10:00:00Z",
"annotations": {
"autoscaling.keda.sh/paused-replicas": "0",
"downscaler/original-pause-replicas": "3",
"downscaler/original-replicas": "3",
}
},
"spec": {}
}
)


now = datetime.strptime("2023-08-21T10:30:00Z", "%Y-%m-%dT%H:%M:%SZ").replace(
tzinfo=timezone.utc
)


autoscale_resource(
so,
upscale_target_only=False,
Expand All @@ -1304,5 +1302,6 @@ def test_upscale_scaledobject_without_pause_annotation():
)

# Check if the annotations have been correctly updated for the upscale operation
assert so.annotations[ScaledObject.keda_pause_annotation] == "3"
assert so.annotations.get(ScaledObject.last_keda_pause_annotation_if_present) is None
assert so.annotations[ScaledObject.keda_pause_annotation] is None
assert so.annotations.get(ScaledObject.last_keda_pause_annotation_if_present) is None
assert so.replicas == 1
Loading

0 comments on commit 6cd190f

Please sign in to comment.