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

PAASTA-18383: Update V2Beta2 HPA api references to V2 for 1.26 #4007

Merged
merged 1 commit into from
Jan 28, 2025
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
68 changes: 34 additions & 34 deletions paasta_tools/kubernetes_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@
from kubernetes.client import V1VolumeMount
from kubernetes.client import V1VolumeProjection
from kubernetes.client import V1WeightedPodAffinityTerm
from kubernetes.client import V2beta2CrossVersionObjectReference
from kubernetes.client import V2beta2HorizontalPodAutoscaler
from kubernetes.client import V2beta2HorizontalPodAutoscalerCondition
from kubernetes.client import V2beta2HorizontalPodAutoscalerSpec
from kubernetes.client import V2beta2MetricIdentifier
from kubernetes.client import V2beta2MetricSpec
from kubernetes.client import V2beta2MetricTarget
from kubernetes.client import V2beta2ObjectMetricSource
from kubernetes.client import V2beta2ResourceMetricSource
from kubernetes.client.models import V2beta2HorizontalPodAutoscalerStatus
from kubernetes.client import V2CrossVersionObjectReference
from kubernetes.client import V2HorizontalPodAutoscaler
from kubernetes.client import V2HorizontalPodAutoscalerCondition
from kubernetes.client import V2HorizontalPodAutoscalerSpec
from kubernetes.client import V2MetricIdentifier
from kubernetes.client import V2MetricSpec
from kubernetes.client import V2MetricTarget
from kubernetes.client import V2ObjectMetricSource
from kubernetes.client import V2ResourceMetricSource
from kubernetes.client.models import V2HorizontalPodAutoscalerStatus
from kubernetes.client.rest import ApiException
from mypy_extensions import TypedDict
from service_configuration_lib import read_soa_metadata
Expand Down Expand Up @@ -237,19 +237,19 @@
# For detail, https://github.com/kubernetes-client/python/issues/553
# This hack should be removed when the issue got fixed.
# This is no better way to work around rn.
class MonkeyPatchAutoScalingConditions(V2beta2HorizontalPodAutoscalerStatus):
class MonkeyPatchAutoScalingConditions(V2HorizontalPodAutoscalerStatus):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i kinda wonder if we still need this - the linked GH issue fizzled out a while back,
but hopefully it's actually been fixed since we added this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I will take a look at this and remove if we can!

@property
def conditions(self) -> Sequence[V2beta2HorizontalPodAutoscalerCondition]:
def conditions(self) -> Sequence[V2HorizontalPodAutoscalerCondition]:
return super().conditions()

@conditions.setter
def conditions(
self, conditions: Optional[Sequence[V2beta2HorizontalPodAutoscalerCondition]]
self, conditions: Optional[Sequence[V2HorizontalPodAutoscalerCondition]]
) -> None:
self._conditions = list() if conditions is None else conditions


models.V2beta2HorizontalPodAutoscalerStatus = MonkeyPatchAutoScalingConditions
models.V2HorizontalPodAutoscalerStatus = MonkeyPatchAutoScalingConditions


class KubeKind(NamedTuple):
Expand Down Expand Up @@ -607,7 +607,7 @@ def __init__(
self.apiextensions = kube_client.ApiextensionsV1Api(self.api_client)

self.custom = kube_client.CustomObjectsApi(self.api_client)
self.autoscaling = kube_client.AutoscalingV2beta2Api(self.api_client)
self.autoscaling = kube_client.AutoscalingV2Api(self.api_client)
self.rbac = kube_client.RbacAuthorizationV1Api(self.api_client)

self.request = self.api_client.request
Expand Down Expand Up @@ -801,18 +801,18 @@ def namespace_external_metric_name(self, metric_name: str) -> str:

def get_autoscaling_provider_spec(
self, name: str, namespace: str, provider: MetricsProviderDict
) -> Optional[V2beta2MetricSpec]:
) -> Optional[V2MetricSpec]:
target = provider["setpoint"]
prometheus_hpa_metric_name = (
f"{self.namespace_external_metric_name(provider['type'])}-prom"
)

if provider["type"] == METRICS_PROVIDER_CPU:
return V2beta2MetricSpec(
return V2MetricSpec(
type="Resource",
resource=V2beta2ResourceMetricSource(
resource=V2ResourceMetricSource(
name="cpu",
target=V2beta2MetricTarget(
target=V2MetricTarget(
type="Utilization",
average_utilization=int(target * 100),
),
Expand All @@ -824,14 +824,14 @@ def get_autoscaling_provider_spec(
METRICS_PROVIDER_GUNICORN,
METRICS_PROVIDER_ACTIVE_REQUESTS,
}:
return V2beta2MetricSpec(
return V2MetricSpec(
type="Object",
object=V2beta2ObjectMetricSource(
metric=V2beta2MetricIdentifier(name=prometheus_hpa_metric_name),
described_object=V2beta2CrossVersionObjectReference(
object=V2ObjectMetricSource(
metric=V2MetricIdentifier(name=prometheus_hpa_metric_name),
described_object=V2CrossVersionObjectReference(
api_version="apps/v1", kind="Deployment", name=name
),
target=V2beta2MetricTarget(
target=V2MetricTarget(
type="Value",
# we average the number of instances needed to handle the current (or
# averaged) load instead of the load itself as this leads to more
Expand All @@ -843,14 +843,14 @@ def get_autoscaling_provider_spec(
),
)
elif provider["type"] == METRICS_PROVIDER_PROMQL:
return V2beta2MetricSpec(
return V2MetricSpec(
type="Object",
object=V2beta2ObjectMetricSource(
metric=V2beta2MetricIdentifier(name=prometheus_hpa_metric_name),
described_object=V2beta2CrossVersionObjectReference(
object=V2ObjectMetricSource(
metric=V2MetricIdentifier(name=prometheus_hpa_metric_name),
described_object=V2CrossVersionObjectReference(
api_version="apps/v1", kind="Deployment", name=name
),
target=V2beta2MetricTarget(
target=V2MetricTarget(
# Use the setpoint specified by the user.
type="Value",
value=target,
Expand All @@ -870,7 +870,7 @@ def get_autoscaling_metric_spec(
cluster: str,
kube_client: KubeClient,
namespace: str,
) -> Optional[V2beta2HorizontalPodAutoscaler]:
) -> Optional[V2HorizontalPodAutoscaler]:
# Returns None if an HPA should not be attached based on the config,
# or the config is invalid.

Expand Down Expand Up @@ -909,17 +909,17 @@ def get_autoscaling_metric_spec(
paasta_prefixed("managed"): "true",
}

hpa = V2beta2HorizontalPodAutoscaler(
hpa = V2HorizontalPodAutoscaler(
kind="HorizontalPodAutoscaler",
metadata=V1ObjectMeta(
name=name, namespace=namespace, annotations=dict(), labels=labels
),
spec=V2beta2HorizontalPodAutoscalerSpec(
spec=V2HorizontalPodAutoscalerSpec(
behavior=scaling_policy,
max_replicas=max_replicas,
min_replicas=min_replicas,
metrics=metrics,
scale_target_ref=V2beta2CrossVersionObjectReference(
scale_target_ref=V2CrossVersionObjectReference(
api_version="apps/v1", kind="Deployment", name=name
),
),
Expand Down Expand Up @@ -3690,7 +3690,7 @@ async def get_hpa(
kube_client: KubeClient,
name: str,
namespace: str,
) -> V2beta2HorizontalPodAutoscaler:
) -> V2HorizontalPodAutoscaler:
async_get_hpa = a_sync.to_async(
kube_client.autoscaling.read_namespaced_horizontal_pod_autoscaler
)
Expand Down
62 changes: 31 additions & 31 deletions tests/test_kubernetes_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@
from kubernetes.client import V1Volume
from kubernetes.client import V1VolumeMount
from kubernetes.client import V1VolumeProjection
from kubernetes.client import V2beta2CrossVersionObjectReference
from kubernetes.client import V2beta2HorizontalPodAutoscaler
from kubernetes.client import V2beta2HorizontalPodAutoscalerSpec
from kubernetes.client import V2beta2MetricIdentifier
from kubernetes.client import V2beta2MetricSpec
from kubernetes.client import V2beta2MetricTarget
from kubernetes.client import V2beta2ResourceMetricSource
from kubernetes.client.models.v2beta2_object_metric_source import (
V2beta2ObjectMetricSource,
from kubernetes.client import V2CrossVersionObjectReference
from kubernetes.client import V2HorizontalPodAutoscaler
from kubernetes.client import V2HorizontalPodAutoscalerSpec
from kubernetes.client import V2MetricIdentifier
from kubernetes.client import V2MetricSpec
from kubernetes.client import V2MetricTarget
from kubernetes.client import V2ResourceMetricSource
from kubernetes.client.models.v2_object_metric_source import (
V2ObjectMetricSource,
)
from kubernetes.client.rest import ApiException
from requests.exceptions import ConnectionError
Expand Down Expand Up @@ -2348,34 +2348,34 @@ def test_get_autoscaling_metric_spec_cpu(self, metrics_provider):
"paasta",
)
annotations: Dict[Any, Any] = {}
expected_res = V2beta2HorizontalPodAutoscaler(
expected_res = V2HorizontalPodAutoscaler(
kind="HorizontalPodAutoscaler",
metadata=V1ObjectMeta(
name="fake_name",
namespace="paasta",
annotations=annotations,
labels=mock.ANY,
),
spec=V2beta2HorizontalPodAutoscalerSpec(
spec=V2HorizontalPodAutoscalerSpec(
behavior=mock_config.get_autoscaling_scaling_policy(
autoscaling_params={},
max_replicas=3,
),
max_replicas=3,
min_replicas=1,
metrics=[
V2beta2MetricSpec(
V2MetricSpec(
type="Resource",
resource=V2beta2ResourceMetricSource(
resource=V2ResourceMetricSource(
name="cpu",
target=V2beta2MetricTarget(
target=V2MetricTarget(
type="Utilization",
average_utilization=50.0,
),
),
)
],
scale_target_ref=V2beta2CrossVersionObjectReference(
scale_target_ref=V2CrossVersionObjectReference(
api_version="apps/v1",
kind="Deployment",
name="fake_name",
Expand Down Expand Up @@ -2424,41 +2424,41 @@ def test_get_autoscaling_metric_spec_uwsgi_prometheus(
KubeClient(),
"paasta",
)
expected_res = V2beta2HorizontalPodAutoscaler(
expected_res = V2HorizontalPodAutoscaler(
kind="HorizontalPodAutoscaler",
metadata=V1ObjectMeta(
name="fake_name",
namespace="paasta",
annotations={},
labels=mock.ANY,
),
spec=V2beta2HorizontalPodAutoscalerSpec(
spec=V2HorizontalPodAutoscalerSpec(
behavior=mock_config.get_autoscaling_scaling_policy(
autoscaling_params={},
max_replicas=3,
),
max_replicas=3,
min_replicas=1,
metrics=[
V2beta2MetricSpec(
V2MetricSpec(
type="Object",
object=V2beta2ObjectMetricSource(
metric=V2beta2MetricIdentifier(
object=V2ObjectMetricSource(
metric=V2MetricIdentifier(
name="service-instance-uwsgi-prom",
),
target=V2beta2MetricTarget(
target=V2MetricTarget(
type="Value",
value=1,
),
described_object=V2beta2CrossVersionObjectReference(
described_object=V2CrossVersionObjectReference(
api_version="apps/v1",
kind="Deployment",
name="fake_name",
),
),
),
],
scale_target_ref=V2beta2CrossVersionObjectReference(
scale_target_ref=V2CrossVersionObjectReference(
api_version="apps/v1",
kind="Deployment",
name="fake_name",
Expand Down Expand Up @@ -2508,41 +2508,41 @@ def test_get_autoscaling_metric_spec_gunicorn_prometheus(
KubeClient(),
"paasta",
)
expected_res = V2beta2HorizontalPodAutoscaler(
expected_res = V2HorizontalPodAutoscaler(
kind="HorizontalPodAutoscaler",
metadata=V1ObjectMeta(
name="fake_name",
namespace="paasta",
annotations={},
labels=mock.ANY,
),
spec=V2beta2HorizontalPodAutoscalerSpec(
spec=V2HorizontalPodAutoscalerSpec(
behavior=mock_config.get_autoscaling_scaling_policy(
autoscaling_params={},
max_replicas=3,
),
max_replicas=3,
min_replicas=1,
metrics=[
V2beta2MetricSpec(
V2MetricSpec(
type="Object",
object=V2beta2ObjectMetricSource(
metric=V2beta2MetricIdentifier(
object=V2ObjectMetricSource(
metric=V2MetricIdentifier(
name="service-instance-gunicorn-prom",
),
target=V2beta2MetricTarget(
target=V2MetricTarget(
type="Value",
value=1,
),
described_object=V2beta2CrossVersionObjectReference(
described_object=V2CrossVersionObjectReference(
api_version="apps/v1",
kind="Deployment",
name="fake_name",
),
),
),
],
scale_target_ref=V2beta2CrossVersionObjectReference(
scale_target_ref=V2CrossVersionObjectReference(
api_version="apps/v1",
kind="Deployment",
name="fake_name",
Expand Down