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 28 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
21 changes: 11 additions & 10 deletions hack/stack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,26 @@ newgrp docker <<EOF
kind create cluster
EOF

# Install Go 1.19
curl -Lo /tmp/go1.19.3.linux-amd64.tar.gz https://go.dev/dl/go1.19.3.linux-amd64.tar.gz
rm -rf /usr/local/go && sudo tar -C /usr/local -xzf /tmp/go1.19.3.linux-amd64.tar.gz

# Install drone/envsubst
/usr/local/go/bin/go install github.com/drone/envsubst/v2/cmd/envsubst@latest
# Label a control plane node
kubectl label node kind-control-plane openstack-control-plane=enabled

# Initialize the `clusterctl` CLI
export EXP_CLUSTER_RESOURCE_SET=true
export CLUSTER_TOPOLOGY=true
curl -L https://github.com/kubernetes-sigs/cluster-api/releases/download/v1.3.0-beta.1/core-components.yaml | $HOME/go/bin/envsubst | kubectl apply -f-
curl -L https://github.com/kubernetes-sigs/cluster-api/releases/download/v1.3.0-beta.1/control-plane-components.yaml | $HOME/go/bin/envsubst | kubectl apply -f-
curl -L https://github.com/kubernetes-sigs/cluster-api/releases/download/v1.3.0-beta.1/bootstrap-components.yaml | $HOME/go/bin/envsubst | kubectl apply -f-
curl -L https://storage.googleapis.com/artifacts.k8s-staging-capi-openstack.appspot.com/components/nightly_main_20221109/infrastructure-components.yaml | $HOME/go/bin/envsubst | kubectl apply -f-
clusterctl init \
--core cluster-api:v1.3.0-rc.0 \
--bootstrap kubeadm:v1.3.0-rc.0 \
--control-plane kubeadm:v1.3.0-rc.0
curl -L https://storage.googleapis.com/artifacts.k8s-staging-capi-openstack.appspot.com/components/nightly_main_20221109/infrastructure-components.yaml | kubectl apply -f-

# Install Skopeo
sudo curl -Lo /usr/local/bin/skopeo https://github.com/lework/skopeo-binary/releases/download/v1.10.0/skopeo-linux-amd64
sudo chmod +x /usr/local/bin/skopeo

# Install Flux
curl -s https://fluxcd.io/install.sh | sudo bash
flux install

# Install `magnum-cluster-api`
pip install -U setuptools pip
$HOME/.local/bin/pip3 install -e .
Expand Down
1 change: 1 addition & 0 deletions magnum_cluster_api/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ 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()
resources.ClusterAutoscalerHelmRelease(self.k8s_api, cluster).delete()

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"
98 changes: 96 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,66 @@ 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": cluster_name,
"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"{cluster_name}-autoscaler",
"cloudProvider": "clusterapi",
"clusterAPIMode": "kubeconfig-incluster",
"clusterAPIKubeconfigSecret": f"{cluster_name}-kubeconfig",
"autoDiscovery": {
"clusterName": cluster_name,
},
"nodeSelector": {
"openstack-control-plane": "enabled",
},
},
okozachenko1203 marked this conversation as resolved.
Show resolved Hide resolved
},
},
)


class ClusterResourcesConfigMap(ClusterBase):
def __init__(
self,
Expand All @@ -103,7 +164,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 +261,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 +1143,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 +1193,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",
"1",
),
"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 Expand Up @@ -1320,6 +1411,9 @@ def apply_cluster_from_magnum_cluster(
ClusterResourcesConfigMap(context, api, cluster).apply()
ClusterResourceSet(api, cluster).apply()
Cluster(context, api, cluster).apply()
ClusterAutoscalerHelmRepository(api).apply()
if utils.get_cluster_label_as_bool(cluster, "auto_scaling_enabled", False):
ClusterAutoscalerHelmRelease(api, cluster).apply()


def get_kubeadm_control_plane(
Expand Down
2 changes: 1 addition & 1 deletion magnum_cluster_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def generate_cluster_api_name(
api: pykube.HTTPClient,
cluster: magnum_objects.Cluster,
) -> str:
name = f"{cluster.name}-{shortuuid.uuid()[:10].lower()}"
name = f"{cluster.name}-{shortuuid.uuid()[:10].lower()}".replace(".", "-")
if (
objects.Cluster.objects(api)
.filter(namespace="magnum-system")
Expand Down