Skip to content

Commit

Permalink
feat: added cluster resize
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaser committed Oct 26, 2022
1 parent 898c818 commit fa1e300
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ steps to be able to test and develop the project.
## TODO

* audit all labels + options to make sure it works
* cluster resize
* cluster upgrade
* [autohealing](https://cluster-api.sigs.k8s.io/tasks/automated-machine-management/healthchecking.html)
with `auto_healing_enabled`
Expand Down
53 changes: 40 additions & 13 deletions magnum_cluster_api/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@

import keystoneauth1
import pykube
from magnum import objects
from magnum.common import clients
from magnum.drivers.common import driver, k8s_monitor

from magnum_cluster_api import resources
from magnum_cluster_api import objects, resources


class BaseDriver(driver.Driver):
Expand Down Expand Up @@ -65,8 +64,8 @@ def update_cluster_status(self, context, cluster, use_admin_ctx=False):
capi_cluster = resources.Cluster(k8s, cluster).get_object()

if cluster.status in (
objects.fields.ClusterStatus.CREATE_IN_PROGRESS,
objects.fields.ClusterStatus.UPDATE_IN_PROGRESS,
"CREATE_IN_PROGRESS",
"UPDATE_IN_PROGRESS",
):
capi_cluster.reload()
status_map = {
Expand All @@ -91,7 +90,10 @@ def update_cluster_status(self, context, cluster, use_admin_ctx=False):

for node_group in cluster.nodegroups:
ng = self.update_nodegroup_status(context, cluster, node_group)
if ng.status != objects.fields.ClusterStatus.CREATE_COMPLETE:
if ng.status not in (
"CREATE_COMPLETE",
"UPDATE_COMPLETE",
):
return

if node_group.role == "master":
Expand All @@ -102,15 +104,15 @@ def update_cluster_status(self, context, cluster, use_admin_ctx=False):

cluster.coe_version = kcp.obj["status"]["version"]

if cluster.status == objects.fields.ClusterStatus.CREATE_IN_PROGRESS:
cluster.status = objects.fields.ClusterStatus.CREATE_COMPLETE
if cluster.status == "CREATE_IN_PROGRESS":
cluster.status = "CREATE_COMPLETE"

if cluster.status == objects.fields.ClusterStatus.UPDATE_IN_PROGRESS:
cluster.status = objects.fields.ClusterStatus.UPDATE_COMPLETE
if cluster.status == "UPDATE_IN_PROGRESS":
cluster.status = "UPDATE_COMPLETE"

cluster.save()

if cluster.status == objects.fields.ClusterStatus.DELETE_IN_PROGRESS:
if cluster.status == "DELETE_IN_PROGRESS":
if capi_cluster.exists():
return

Expand All @@ -130,7 +132,7 @@ def update_cluster_status(self, context, cluster, use_admin_ctx=False):
resources.FrontProxyCertificateAuthoritySecret(k8s, cluster).delete()
resources.ServiceAccountCertificateAuthoritySecret(k8s, cluster).delete()

cluster.status = objects.fields.ClusterStatus.DELETE_COMPLETE
cluster.status = "DELETE_COMPLETE"
cluster.save()

def update_cluster(self, context, cluster, scale_manager=None, rollback=False):
Expand All @@ -145,7 +147,31 @@ def resize_cluster(
nodes_to_remove,
nodegroup=None,
):
raise NotImplementedError("Subclasses must implement " "'resize_cluster'.")
if nodegroup is None:
nodegroup = cluster.default_ng_worker

if nodes_to_remove:
k8s = pykube.HTTPClient(pykube.KubeConfig.from_env())
machines = objects.Machine.objects(k8s).filter(
namespace="magnum-system",
selector={
"cluster.x-k8s.io/deployment-name": resources.name_from_node_group(
cluster, nodegroup
)
},
)

for machine in machines:
instance_uuid = machine.obj["spec"]["providerID"].split("/")[-1]
if instance_uuid in nodes_to_remove:
machine.obj["metadata"].setdefault("annotations", {})
machine.obj["metadata"]["annotations"][
"cluster.x-k8s.io/delete-machine"
] = "yes"
machine.update()

nodegroup.node_count = node_count
self.update_nodegroup(context, cluster, nodegroup)

def upgrade_cluster(
self,
Expand Down Expand Up @@ -237,7 +263,8 @@ def update_nodegroup_status(self, context, cluster, nodegroup):
return nodegroup

def update_nodegroup(self, context, cluster, nodegroup):
raise NotImplementedError("Subclasses must implement " "'update_nodegroup'.")
k8s = pykube.HTTPClient(pykube.KubeConfig.from_env())
resources.MachineDeployment(k8s, cluster, nodegroup).apply()

def delete_nodegroup(self, context, cluster, nodegroup):
k8s = pykube.HTTPClient(pykube.KubeConfig.from_env())
Expand Down
6 changes: 6 additions & 0 deletions magnum_cluster_api/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class MachineDeployment(pykube.objects.NamespacedAPIObject):
kind = "MachineDeployment"


class Machine(pykube.objects.NamespacedAPIObject):
version = "cluster.x-k8s.io/v1beta1"
endpoint = "machines"
kind = "Machine"


class OpenStackCluster(pykube.objects.NamespacedAPIObject):
version = "infrastructure.cluster.x-k8s.io/v1alpha5"
endpoint = "openstackclusters"
Expand Down

0 comments on commit fa1e300

Please sign in to comment.