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

Support autoscaling #39

Merged
merged 34 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
313f6df
Support autoscaling
okozachenko1203 Nov 11, 2022
5a7654e
Fix lint error
okozachenko1203 Nov 11, 2022
cd7e961
Merge remote-tracking branch 'origin/main' into add-autoscaler
okozachenko1203 Nov 11, 2022
0a08cc4
Merge remote-tracking branch 'origin/main' into add-autoscaler
okozachenko1203 Nov 11, 2022
166c5c4
incluster for mgmt, kubeconfig for workload
okozachenko1203 Nov 15, 2022
19a5bff
Define var for autoscaler helm chart version
okozachenko1203 Nov 15, 2022
915bec8
Update flux api version
okozachenko1203 Nov 15, 2022
4b75273
revert flux api version
okozachenko1203 Nov 15, 2022
b8e635b
Fix flux resource scope
okozachenko1203 Nov 15, 2022
4ebcf55
Fix param
okozachenko1203 Nov 15, 2022
0a401fc
Override delete method of ClusterResourcesConfigMap
okozachenko1203 Nov 15, 2022
490a110
Merge remote-tracking branch 'origin/delete-crconfigmap' into add-aut…
okozachenko1203 Nov 15, 2022
ee6da50
Fix autoscaler annotation value type
okozachenko1203 Nov 15, 2022
eaaef85
Undelete helmrepo
okozachenko1203 Nov 15, 2022
0bafd26
Enable autodiscovery in autoscaler
okozachenko1203 Nov 15, 2022
7b0bb1c
Fix helmrelease name
okozachenko1203 Nov 15, 2022
97f1d7e
resolve comments
okozachenko1203 Nov 16, 2022
306a22e
Add nodeselector
okozachenko1203 Nov 16, 2022
0dd1d48
Revert helmrelease name
okozachenko1203 Nov 16, 2022
b72c2fc
Fix helmrelease creation order
okozachenko1203 Nov 16, 2022
45d2cb6
fix typo
okozachenko1203 Nov 16, 2022
f9a7bf2
Update cluster name generation
okozachenko1203 Nov 16, 2022
bcd6797
Update hack.sh script
okozachenko1203 Nov 16, 2022
4fa6c5a
chore: use -autoscaler suffix for autoscaler
mnaser Nov 16, 2022
f75d03f
fix: added flux + node labels
mnaser Nov 16, 2022
525a419
test: improve hack/stack.sh
mnaser Nov 16, 2022
8d1cf90
Merge branch 'add-autoscaler' of github.com:vexxhost/magnum-cluster-a…
mnaser Nov 16, 2022
5ae83d1
test: fix merge
mnaser Nov 16, 2022
a79c352
Workaround for MD annotation propogation from cluster topology
okozachenko1203 Nov 16, 2022
35ef8b1
Use labelSelector instead of fieldSelector
okozachenko1203 Nov 16, 2022
2ed2d91
Use nodegroup instead of cluster
okozachenko1203 Nov 16, 2022
11efc23
Fix node count cal
okozachenko1203 Nov 16, 2022
7d0e532
Fix lint
okozachenko1203 Nov 16, 2022
c1a9a30
Fix param
okozachenko1203 Nov 16, 2022
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
8 changes: 8 additions & 0 deletions magnum_cluster_api/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
class BaseDriver(driver.Driver):
def __init__(self):
self.k8s_api = clients.get_pykube_api()
resources.ClusterAutoscalerHelmRepository(
self.k8s_api,
).apply()
okozachenko1203 marked this conversation as resolved.
Show resolved Hide resolved

def create_cluster(self, context, cluster, cluster_create_timeout):
osc = clients.get_openstack_api(context)
Expand All @@ -45,6 +48,9 @@ def create_cluster(self, context, cluster, cluster_create_timeout):
self.k8s_api, cluster
).apply()

if utils.get_cluster_label_as_bool(cluster, "auto_scaling_enabled", False):
resources.ClusterAutoscalerHelmRelease(self.k8s_api, cluster).apply()
okozachenko1203 marked this conversation as resolved.
Show resolved Hide resolved

resources.apply_cluster_from_magnum_cluster(context, self.k8s_api, cluster)

def update_cluster_status(self, context, cluster, use_admin_ctx=False):
Expand Down Expand Up @@ -186,6 +192,8 @@ def delete_cluster(self, context, cluster):
resources.ClusterResourceSet(self.k8s_api, cluster).delete()
resources.ClusterResourcesConfigMap(context, self.k8s_api, cluster).delete()
resources.Cluster(context, self.k8s_api, cluster).delete()
if utils.get_cluster_label_as_bool(cluster, "auto_scaling_enabled", False):
resources.ClusterAutoscalerHelmRelease(self.k8s_api, cluster).delete()
okozachenko1203 marked this conversation as resolved.
Show resolved Hide resolved

def create_nodegroup(self, context, cluster, nodegroup):
resources.apply_cluster_from_magnum_cluster(context, self.k8s_api, cluster)
Expand Down
12 changes: 12 additions & 0 deletions magnum_cluster_api/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ class StorageClass(pykube.objects.APIObject):
version = "storage.k8s.io/v1"
endpoint = "storageclasses"
kind = "StorageClass"


class HelmRelease(pykube.objects.NamespacedAPIObject):
version = "helm.toolkit.fluxcd.io/v2beta1"
endpoint = "helmreleases"
kind = "HelmRelease"


class HelmRepository(pykube.objects.NamespacedAPIObject):
version = "source.toolkit.fluxcd.io/v1beta2"
endpoint = "helmrepositories"
kind = "HelmRepository"
92 changes: 90 additions & 2 deletions magnum_cluster_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
KUBE_TAG = "v1.25.3"
CLOUD_PROVIDER_TAG = "v1.25.3"
CALICO_TAG = "v3.24.2"
AUTOSCALER_HELM_CHART_VERSION = "9.21.0"
CSI_TAG = "v1.25.3"

CLUSTER_CLASS_VERSION = pkg_resources.require("magnum_cluster_api")[0].version
Expand Down Expand Up @@ -85,6 +86,63 @@ def labels(self) -> dict:
}


class ClusterAutoscalerHelmRepository(Base):
def get_object(self) -> objects.HelmRepository:
return objects.HelmRepository(
self.api,
{
"apiVersion": objects.HelmRepository.version,
"kind": objects.HelmRepository.kind,
"metadata": {
"name": "autoscaler",
"namespace": "magnum-system",
},
"spec": {
"interval": "1m",
"url": "https://kubernetes.github.io/autoscaler",
},
},
)


class ClusterAutoscalerHelmRelease(ClusterBase):
def get_object(self) -> objects.HelmRelease:
cluster_name = utils.get_or_generate_cluster_api_name(self.api, self.cluster)
return objects.HelmRelease(
self.api,
{
"apiVersion": objects.HelmRelease.version,
"kind": objects.HelmRelease.kind,
"metadata": {
"name": f"autoscaler-{self.cluster.uuid}",
okozachenko1203 marked this conversation as resolved.
Show resolved Hide resolved
"namespace": "magnum-system",
},
"spec": {
"interval": "60s",
"chart": {
"spec": {
"chart": "cluster-autoscaler",
"version": AUTOSCALER_HELM_CHART_VERSION,
"sourceRef": {
"kind": objects.HelmRepository.kind,
"name": "autoscaler",
},
},
},
"values": {
"fullnameOverride": f"autoscaler-{self.cluster.uuid}",
"cloudProvider": "clusterapi",
"clusterAPIMode": "kubeconfig-incluster",
"clusterAPIKubeconfigSecret": f"{cluster_name}-kubeconfig",
"autoDiscovery": {
"clusterName": cluster_name,
},
},
okozachenko1203 marked this conversation as resolved.
Show resolved Hide resolved
},
},
)


class ClusterResourcesConfigMap(ClusterBase):
def __init__(
self,
Expand All @@ -103,7 +161,6 @@ def get_object(self) -> pykube.ConfigMap:
manifests_path = pkg_resources.resource_filename(
"magnum_cluster_api", "manifests"
)

calico_version = utils.get_cluster_label(self.cluster, "calico_tag", CALICO_TAG)
ccm_version = utils.get_cluster_label(
self.cluster, "cloud_provider_tag", CLOUD_PROVIDER_TAG
Expand Down Expand Up @@ -201,6 +258,16 @@ def get_object(self) -> pykube.ConfigMap:
},
)

def get_or_none(self) -> objects.Cluster:
return pykube.ConfigMap.objects(
self.api, namespace="magnum-system"
).get_or_none(name=self.cluster.uuid)

def delete(self):
cr_cm = self.get_or_none()
if cr_cm:
cr_cm.delete()


class ClusterResourceSet(ClusterBase):
def get_object(self) -> objects.ClusterResourceSet:
Expand Down Expand Up @@ -1073,6 +1140,9 @@ def get_or_none(self) -> objects.Cluster:
)

def get_object(self) -> objects.Cluster:
auto_scaling_enabled = utils.get_cluster_label_as_bool(
self.cluster, "auto_scaling_enabled", False
)
return objects.Cluster(
self.api,
{
Expand Down Expand Up @@ -1120,7 +1190,25 @@ def get_object(self) -> objects.Cluster:
{
"class": "default-worker",
"name": ng.name,
"replicas": ng.node_count,
"replicas": None
if auto_scaling_enabled
else ng.node_count,
"metadata": {
"annotations": {
"cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size": utils.get_cluster_label( # noqa: E501
self.cluster,
"min_node_count",
"0",
okozachenko1203 marked this conversation as resolved.
Show resolved Hide resolved
),
"cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size": utils.get_cluster_label( # noqa: E501
self.cluster,
"max_node_count",
"1",
),
}
}
if auto_scaling_enabled
else {},
"failureDomain": utils.get_cluster_label(
self.cluster, "availability_zone", ""
),
Expand Down