Skip to content

Commit

Permalink
feat: use shorter cluster names
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaser committed Nov 4, 2022
1 parent 402b23b commit 7b58739
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 30 deletions.
58 changes: 34 additions & 24 deletions magnum_cluster_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pkg_resources
import pykube
import yaml
from magnum import objects as magnum_objects
from magnum.common import cert_manager, cinder, neutron
from magnum.common.x509 import operations as x509
from oslo_config import cfg
Expand Down Expand Up @@ -290,7 +291,7 @@ def get_object(self) -> pykube.Secret:
"kind": pykube.Secret.kind,
"type": "kubernetes.io/tls",
"metadata": {
"name": f"{name_from_cluster(self.cluster)}-{self.CERT}",
"name": f"{name_from_cluster(self.api, self.cluster)}-{self.CERT}",
"namespace": "magnum-system",
},
"stringData": {
Expand Down Expand Up @@ -347,7 +348,7 @@ def get_object(self) -> pykube.Secret:
"apiVersion": pykube.Secret.version,
"kind": pykube.Secret.kind,
"metadata": {
"name": f"{name_from_cluster(self.cluster)}-cloud-config",
"name": f"{name_from_cluster(self.api, self.cluster)}-cloud-config",
"namespace": "magnum-system",
"labels": self.labels,
},
Expand Down Expand Up @@ -386,7 +387,7 @@ def get_object(self) -> objects.OpenStackMachineTemplate:
"flavor": self.node_group.flavor_id,
"identityRef": {
"kind": pykube.Secret.kind,
"name": f"{name_from_cluster(self.cluster)}-cloud-config",
"name": f"{name_from_cluster(self.api, self.cluster)}-cloud-config",
},
"imageUUID": self.node_group.image_id,
}
Expand All @@ -413,7 +414,9 @@ def get_object(self) -> objects.OpenStackMachineTemplate:
"apiVersion": objects.OpenStackMachineTemplate.version,
"kind": objects.OpenStackMachineTemplate.kind,
"metadata": {
"name": name_from_node_group(self.cluster, self.node_group),
"name": name_from_node_group(
self.api, self.cluster, self.node_group
),
"namespace": "magnum-system",
"labels": self.labels,
},
Expand All @@ -430,17 +433,17 @@ def get_object(self) -> objects.MachineHealthCheck:
"apiVersion": objects.MachineHealthCheck.version,
"kind": objects.MachineHealthCheck.kind,
"metadata": {
"name": name_from_cluster(self.cluster),
"name": name_from_cluster(self.api, self.cluster),
"namespace": "magnum-system",
"labels": self.labels,
},
"spec": {
"clusterName": name_from_cluster(self.cluster),
"clusterName": name_from_cluster(self.api, self.cluster),
"maxUnhealthy": "40%",
"selector": {
"matchLabels": {
"cluster.x-k8s.io/cluster-name": name_from_cluster(
self.cluster
self.api, self.cluster
),
}
},
Expand Down Expand Up @@ -492,7 +495,7 @@ def get_object(self) -> objects.KubeadmConfigTemplate:
"apiVersion": objects.KubeadmConfigTemplate.version,
"kind": objects.KubeadmConfigTemplate.kind,
"metadata": {
"name": name_from_cluster(self.cluster),
"name": name_from_cluster(self.api, self.cluster),
"namespace": "magnum-system",
"labels": self.labels,
},
Expand Down Expand Up @@ -596,7 +599,9 @@ def get_object(self) -> objects.KubeadmControlPlane:
"infrastructureRef": {
"apiVersion": objects.OpenStackMachineTemplate.version,
"kind": objects.OpenStackMachineTemplate.kind,
"name": name_from_node_group(self.cluster, self.node_group),
"name": name_from_node_group(
self.api, self.cluster, self.node_group
),
},
},
}
Expand Down Expand Up @@ -655,7 +660,7 @@ def get_object(self) -> objects.KubeadmControlPlane:
"apiVersion": objects.KubeadmControlPlane.version,
"kind": objects.KubeadmControlPlane.kind,
"metadata": {
"name": name_from_cluster(self.cluster),
"name": name_from_cluster(self.api, self.cluster),
"namespace": "magnum-system",
"labels": self.labels,
},
Expand All @@ -672,24 +677,26 @@ def get_object(self) -> objects.MachineDeployment:
"apiVersion": objects.MachineDeployment.version,
"kind": objects.MachineDeployment.kind,
"metadata": {
"name": name_from_node_group(self.cluster, self.node_group),
"name": name_from_node_group(
self.api, self.cluster, self.node_group
),
"namespace": "magnum-system",
"labels": self.labels,
},
"spec": {
"clusterName": name_from_cluster(self.cluster),
"clusterName": name_from_cluster(self.api, self.cluster),
"replicas": self.node_group.node_count,
"selector": {
"matchLabels": None,
},
"template": {
"spec": {
"clusterName": name_from_cluster(self.cluster),
"clusterName": name_from_cluster(self.api, self.cluster),
"bootstrap": {
"configRef": {
"apiVersion": objects.KubeadmConfigTemplate.version,
"kind": objects.KubeadmConfigTemplate.kind,
"name": name_from_cluster(self.cluster),
"name": name_from_cluster(self.api, self.cluster),
},
},
"version": utils.get_cluster_label(
Expand All @@ -702,7 +709,7 @@ def get_object(self) -> objects.MachineDeployment:
"apiVersion": objects.OpenStackMachineTemplate.version,
"kind": objects.OpenStackMachineTemplate.kind,
"name": name_from_node_group(
self.cluster, self.node_group
self.api, self.cluster, self.node_group
),
},
}
Expand All @@ -726,7 +733,7 @@ def get_object(self) -> objects.OpenStackCluster:
"apiVersion": objects.OpenStackCluster.version,
"kind": objects.OpenStackCluster.kind,
"metadata": {
"name": name_from_cluster(self.cluster),
"name": name_from_cluster(self.api, self.cluster),
"namespace": "magnum-system",
"labels": self.labels,
},
Expand All @@ -743,7 +750,7 @@ def get_object(self) -> objects.OpenStackCluster:
),
"identityRef": {
"kind": pykube.Secret.kind,
"name": f"{name_from_cluster(self.cluster)}-cloud-config",
"name": f"{name_from_cluster(self.api, self.cluster)}-cloud-config",
},
"managedSecurityGroups": True,
"nodeCidr": utils.get_cluster_label(
Expand Down Expand Up @@ -782,7 +789,7 @@ def get_object(self) -> objects.Cluster:
"apiVersion": objects.Cluster.version,
"kind": objects.Cluster.kind,
"metadata": {
"name": name_from_cluster(self.cluster),
"name": name_from_cluster(self.api, self.cluster),
"namespace": "magnum-system",
"labels": self.labels,
},
Expand All @@ -802,21 +809,24 @@ def get_object(self) -> objects.Cluster:
"controlPlaneRef": {
"apiVersion": objects.KubeadmControlPlane.version,
"kind": objects.KubeadmControlPlane.kind,
"name": name_from_cluster(self.cluster),
"name": name_from_cluster(self.api, self.cluster),
},
"infrastructureRef": {
"apiVersion": objects.OpenStackCluster.version,
"kind": objects.OpenStackCluster.kind,
"name": name_from_cluster(self.cluster),
"name": name_from_cluster(self.api, self.cluster),
},
},
},
)


def name_from_cluster(cluster: any) -> str:
return cluster.uuid
def name_from_cluster(api: pykube.HTTPClient, cluster: magnum_objects.Cluster) -> str:
if cluster.stack_id is None:
cluster.stack_id = utils.generate_cluster_api_name(api, cluster)
cluster.save()
return cluster.stack_id


def name_from_node_group(cluster: any, node_group: any) -> str:
return f"{name_from_cluster(cluster)}-{node_group.name}"
def name_from_node_group(api: pykube.HTTPClient, cluster: any, node_group: any) -> str:
return f"{name_from_cluster(api, cluster)}-{node_group.name}"
30 changes: 25 additions & 5 deletions magnum_cluster_api/utils.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
from magnum import objects
import shortuuid
from magnum import objects as magnum_objects
from magnum.common import exception
from oslo_utils import strutils
from tenacity import retry, retry_if_exception_type

from magnum_cluster_api import objects


@retry(retry=retry_if_exception_type(exception.Conflict))
def generate_cluster_api_name(api, cluster):
name = f"{cluster.name}-{shortuuid.uuid()[:10].lower()}"
if (
objects.Cluster.objects(api)
.filter(namespace="magnum-system")
.get_or_none(name=name)
is not None
):
raise exception.Conflict("Generated name already exists")
return name


def get_cluster_label_as_bool(
cluster: objects.Cluster, key: str, default: bool
cluster: magnum_objects.Cluster, key: str, default: bool
) -> bool:
value = get_cluster_label(cluster, key, default)
return strutils.bool_from_string(value, strict=True)


def get_cluster_label_as_int(cluster: objects.Cluster, key: str, default: int) -> int:
def get_cluster_label_as_int(
cluster: magnum_objects.Cluster, key: str, default: int
) -> int:
value = get_cluster_label(cluster, key, default)
return strutils.validate_integer(value, key)


def get_cluster_label(cluster: objects.Cluster, key: str, default: str) -> str:
def get_cluster_label(cluster: magnum_objects.Cluster, key: str, default: str) -> str:
return cluster.labels.get(
key, get_cluster_template_label(cluster.cluster_template, key, default)
)


def get_cluster_template_label(
cluster_template: objects.ClusterTemplate, key: str, default: str
cluster_template: magnum_objects.ClusterTemplate, key: str, default: str
) -> str:
return cluster_template.labels.get(key, default)
14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ python = "^3.8"
pykube-ng = "^22.9.0"
click = "*"
requests = "*"
shortuuid = "^1.0.9"

[build-system]
requires = ["setuptools", "poetry-core"]
Expand Down

0 comments on commit 7b58739

Please sign in to comment.