Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Tests For Scaled Objects Downscaling/Upscaling #92

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
jonathan-mayer marked this conversation as resolved.
Show resolved Hide resolved
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