Skip to content

Commit

Permalink
Use existing HPA methods
Browse files Browse the repository at this point in the history
  • Loading branch information
siadat committed Aug 27, 2024
1 parent 2a79c17 commit 7cbbb00
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 70 deletions.
9 changes: 6 additions & 3 deletions paasta_tools/kubernetes_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,11 @@ def get_autoscaling_provider_spec(
)
return None

def get_autoscaling_target(self, name: str) -> V2beta2CrossVersionObjectReference:
return V2beta2CrossVersionObjectReference(
api_version="apps/v1", kind="Deployment", name=name
)

def get_autoscaling_metric_spec(
self,
name: str,
Expand Down Expand Up @@ -926,9 +931,7 @@ def get_autoscaling_metric_spec(
max_replicas=max_replicas,
min_replicas=min_replicas,
metrics=metrics,
scale_target_ref=V2beta2CrossVersionObjectReference(
api_version="apps/v1", kind="Deployment", name=name
),
scale_target_ref=self.get_autoscaling_target(name),
),
)

Expand Down
85 changes: 18 additions & 67 deletions paasta_tools/vitesscell_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,11 @@
from typing import Union

import service_configuration_lib
from kubernetes.client import V1ObjectMeta
from kubernetes.client import V1OwnerReference
from kubernetes.client import V2beta2CrossVersionObjectReference
from kubernetes.client import V2beta2HorizontalPodAutoscaler
from kubernetes.client import V2beta2HorizontalPodAutoscalerSpec

from paasta_tools.kubernetes_tools import get_cr
from paasta_tools.kubernetes_tools import KubeClient
from paasta_tools.kubernetes_tools import KubernetesDeploymentConfigDict
from paasta_tools.kubernetes_tools import paasta_prefixed
from paasta_tools.kubernetes_tools import sanitised_cr_name
from paasta_tools.long_running_service_tools import DEFAULT_AUTOSCALING_SETPOINT
from paasta_tools.long_running_service_tools import METRICS_PROVIDER_CPU
from paasta_tools.utils import BranchDictV2
from paasta_tools.utils import deep_merge_dictionaries
from paasta_tools.utils import DEFAULT_SOA_DIR
Expand Down Expand Up @@ -187,57 +179,19 @@ def get_global_lock_server(self) -> Dict[str, str]:
"rootPath": TOPO_GLOBAL_ROOT,
}

def get_desired_hpa(
self,
kube_client: KubeClient,
owner_uid: str,
min_replicas: Optional[int],
max_replicas: Optional[int],
) -> Optional[V2beta2HorizontalPodAutoscaler]:
name = sanitised_cr_name(self.service, self.instance)
return V2beta2HorizontalPodAutoscaler(
kind="HorizontalPodAutoscaler",
metadata=V1ObjectMeta(
name=name,
namespace=self.get_namespace(),
annotations=dict(),
labels={
paasta_prefixed("service"): self.service,
paasta_prefixed("instance"): self.instance,
paasta_prefixed("pool"): self.get_pool(),
paasta_prefixed("managed"): "true",
},
owner_references=[
V1OwnerReference(
api_version="planetscale.com/v2",
kind="VitessCell",
name=name,
uid=owner_uid,
controller=True,
block_owner_deletion=True,
),
],
),
spec=V2beta2HorizontalPodAutoscalerSpec(
behavior=self.get_autoscaling_scaling_policy(max_replicas, {}),
max_replicas=max_replicas,
min_replicas=min_replicas,
metrics=[
self.get_autoscaling_provider_spec(
name=name,
namespace=self.get_namespace(),
provider={
"type": METRICS_PROVIDER_CPU,
"setpoint": DEFAULT_AUTOSCALING_SETPOINT,
},
),
],
scale_target_ref=V2beta2CrossVersionObjectReference(
api_version="planetscale.com/v2", kind="VitessCell", name=name
),
),
def get_autoscaling_target(self, name: str) -> V2beta2CrossVersionObjectReference:
return V2beta2CrossVersionObjectReference(
api_version="planetscale.com/v2", kind="VitessCell", name=name
)

def get_min_instances(self) -> Optional[int]:
vtgate_resources = self.config_dict.get("vtgate_resources")
return vtgate_resources.get("min_instances", 1)

def get_max_instances(self) -> Optional[int]:
vtgate_resources = self.config_dict.get("vtgate_resources")
return vtgate_resources.get("max_instances")

def update_related_api_objects(
self,
kube_client: KubeClient,
Expand All @@ -261,18 +215,15 @@ def update_related_api_objects(
)

if should_exist:
vitesscell_cr = get_cr(kube_client, cr_id(self.service, self.instance))
if not vitesscell_cr:
# We need the uid of the VitessCell CR to set the owner reference on the HPA.
return

owner_uid = vitesscell_cr["metadata"]["uid"]
hpa = self.get_desired_hpa(
hpa = self.get_autoscaling_metric_spec(
name=name,
cluster=self.get_cluster(),
kube_client=kube_client,
owner_uid=owner_uid,
min_replicas=min_instances,
max_replicas=max_instances,
namespace=self.get_namespace(),
)
if not hpa:
return

if exists:
kube_client.autoscaling.replace_namespaced_horizontal_pod_autoscaler(
name=name,
Expand Down

0 comments on commit 7cbbb00

Please sign in to comment.