Skip to content

Commit

Permalink
Merge pull request #122 from vexxhost/reflect-cluster-status
Browse files Browse the repository at this point in the history
feat: Expose underlying k8s resource status to coe cluster status reason
  • Loading branch information
mnaser committed Jun 27, 2023
2 parents e188cbe + 4157d16 commit 71a9d1e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
35 changes: 35 additions & 0 deletions magnum_cluster_api/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ def create_cluster(self, context, cluster, cluster_create_timeout):

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

def _get_cluster_status_reason(self, capi_cluster):
capi_cluster_status_reason = ""
capi_ops_cluster_status_reason = ""

# Get the latest event message of the CAPI Cluster
capi_cluster_events = capi_cluster.events
if capi_cluster_events:
capi_cluster_status_reason += utils.format_event_message(
list(capi_cluster_events)[-1]
)

# Get the latest event message of the CAPI OpenstackCluster
capi_ops_cluster_events = []
capi_ops_cluster = capi_cluster.openstack_cluster
if capi_ops_cluster:
capi_ops_cluster_events = capi_ops_cluster.events
if capi_ops_cluster_events:
capi_ops_cluster_status_reason += utils.format_event_message(
list(capi_ops_cluster_events)[-1]
)

return "CAPI Cluster status: %s. CAPI OpenstackCluster status reason: %s" % (
capi_cluster_status_reason,
capi_ops_cluster_status_reason,
)

def update_cluster_status(self, context, cluster, use_admin_ctx=False):
node_groups = [
self.update_nodegroup_status(context, cluster, node_group)
Expand Down Expand Up @@ -87,6 +113,10 @@ def update_cluster_status(self, context, cluster, use_admin_ctx=False):

for condition in ("ControlPlaneReady", "InfrastructureReady", "Ready"):
if status_map.get(condition) != "True":
cluster.status_reason = self._get_cluster_status_reason(
capi_cluster
)
cluster.save()
return

api_endpoint = capi_cluster.obj["spec"]["controlPlaneEndpoint"]
Expand All @@ -102,14 +132,18 @@ def update_cluster_status(self, context, cluster, use_admin_ctx=False):
ng.destroy()

if cluster.status == "CREATE_IN_PROGRESS":
cluster.status_reason = None
cluster.status = "CREATE_COMPLETE"
if cluster.status == "UPDATE_IN_PROGRESS":
cluster.status_reason = None
cluster.status = "UPDATE_COMPLETE"

cluster.save()

if cluster.status == "DELETE_IN_PROGRESS":
if capi_cluster and capi_cluster.exists():
cluster.status_reason = self._get_cluster_status_reason(capi_cluster)
cluster.save()
return

# NOTE(mnaser): We delete the application credentials at this stage
Expand All @@ -132,6 +166,7 @@ def update_cluster_status(self, context, cluster, use_admin_ctx=False):
self.k8s_api, cluster
).delete()

cluster.status_reason = None
cluster.status = "DELETE_COMPLETE"
cluster.save()

Expand Down
38 changes: 25 additions & 13 deletions magnum_cluster_api/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,73 @@
from magnum_cluster_api import exceptions


class EndpointSlice(pykube.objects.NamespacedAPIObject):
class NamespacedAPIObject(pykube.objects.NamespacedAPIObject):
@property
def events(self):
return pykube.Event.objects(self.api, namespace=self.namespace).filter(
field_selector={
"involvedObject.name": self.name,
"involvedObject.apiVersion": self.version,
"involvedObject.kind": self.kind,
},
)


class EndpointSlice(NamespacedAPIObject):
version = "discovery.k8s.io/v1"
endpoint = "endpointslices"
kind = "EndpointSlice"


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


class OpenStackMachineTemplate(pykube.objects.NamespacedAPIObject):
class OpenStackMachineTemplate(NamespacedAPIObject):
version = "infrastructure.cluster.x-k8s.io/v1alpha6"
endpoint = "openstackmachinetemplates"
kind = "OpenStackMachineTemplate"


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


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


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


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


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


class OpenStackClusterTemplate(pykube.objects.NamespacedAPIObject):
class OpenStackClusterTemplate(NamespacedAPIObject):
version = "infrastructure.cluster.x-k8s.io/v1alpha6"
endpoint = "openstackclustertemplates"
kind = "OpenStackClusterTemplate"


class OpenStackCluster(pykube.objects.NamespacedAPIObject):
class OpenStackCluster(NamespacedAPIObject):
version = "infrastructure.cluster.x-k8s.io/v1alpha6"
endpoint = "openstackclusters"
kind = "OpenStackCluster"
Expand Down Expand Up @@ -148,13 +160,13 @@ def cloud_controller_manager_config(self):
return fd.getvalue()


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


class Cluster(pykube.objects.NamespacedAPIObject):
class Cluster(NamespacedAPIObject):
version = "cluster.x-k8s.io/v1beta1"
endpoint = "clusters"
kind = "Cluster"
Expand All @@ -170,7 +182,7 @@ def openstack_cluster(self):
if len(filtered_clusters) == 0:
raise exceptions.OpenStackClusterNotCreated()

return filtered_clusters[0]
return list(filtered_clusters)[0]


class StorageClass(pykube.objects.APIObject):
Expand Down
7 changes: 7 additions & 0 deletions magnum_cluster_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ def is_manila_csi_enabled(cluster: magnum_objects.Cluster) -> bool:
)


def format_event_message(event: pykube.Event):
return "%s: %s" % (
event.obj["reason"],
event.obj["message"],
)


def validate_cluster(cluster: magnum_objects.Cluster):
# Check master count
if (cluster.master_count % 2) == 0:
Expand Down

0 comments on commit 71a9d1e

Please sign in to comment.