From fdf539dc6a027ef975fde23bf734f880570ccdc3 Mon Sep 17 00:00:00 2001 From: 1102 <90682513+nueavv@users.noreply.github.com> Date: Mon, 16 Dec 2024 05:56:34 +0900 Subject: [PATCH] feat: add health check for ClusterResourceSet (#20746) Signed-off-by: nueavv --- .../ClusterResourceSet/health.lua | 31 +++++++++++++++++++ .../ClusterResourceSet/health_test.yaml | 13 ++++++++ .../testdata/degraded_resourceapplied.yaml | 27 ++++++++++++++++ .../testdata/healthy_resourceapplied.yaml | 24 ++++++++++++++ .../testdata/progressing_resourceapplied.yaml | 18 +++++++++++ 5 files changed, 113 insertions(+) create mode 100644 resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/health.lua create mode 100644 resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/health_test.yaml create mode 100644 resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/degraded_resourceapplied.yaml create mode 100644 resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/healthy_resourceapplied.yaml create mode 100644 resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/progressing_resourceapplied.yaml diff --git a/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/health.lua b/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/health.lua new file mode 100644 index 0000000000000..9cc7edc368b33 --- /dev/null +++ b/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/health.lua @@ -0,0 +1,31 @@ +function getStatus(obj) + local hs = {} + hs.status = "Progressing" + hs.message = "Initializing cluster resource set" + + if obj.status ~= nil then + if obj.status.conditions ~= nil then + for i, condition in ipairs(obj.status.conditions) do + + -- Ready + if condition.type == "ResourcesApplied" and condition.status == "True" then + hs.status = "Healthy" + hs.message = "cluster resource set is applied" + return hs + end + + -- Resources Applied + if condition.type == "ResourcesApplied" and condition.status == "False" then + hs.status = "Degraded" + hs.message = condition.message + return hs + end + + end + end + end + return hs +end + +local hs = getStatus(obj) +return hs \ No newline at end of file diff --git a/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/health_test.yaml b/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/health_test.yaml new file mode 100644 index 0000000000000..373d143f9bc26 --- /dev/null +++ b/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/health_test.yaml @@ -0,0 +1,13 @@ +tests: +- healthStatus: + status: Progressing + message: 'Initializing cluster resource set' + inputPath: testdata/progressing_resourceapplied.yaml +- healthStatus: + status: Degraded + message: 'Failed to apply resources' + inputPath: testdata/degraded_resourceapplied.yaml +- healthStatus: + status: Healthy + message: 'cluster resource set is applied' + inputPath: testdata/healthy_resourceapplied.yaml \ No newline at end of file diff --git a/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/degraded_resourceapplied.yaml b/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/degraded_resourceapplied.yaml new file mode 100644 index 0000000000000..ec27b67ed4be5 --- /dev/null +++ b/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/degraded_resourceapplied.yaml @@ -0,0 +1,27 @@ +apiVersion: addons.cluster.x-k8s.io/v1beta1 +kind: ClusterResourceSet +metadata: + finalizers: + - addons.cluster.x-k8s.io + generation: 1 + labels: + app.argocd.io/instance: clustername + name: clustername-resource-set + namespace: capi-managed-cluster +spec: + clusterSelector: + matchLabels: + clusterName: clustername + resources: + - kind: ConfigMap + name: clustername-default-rbac + strategy: ApplyOnce +status: + conditions: + - lastTransitionTime: '2024-11-11T03:28:48Z' + message: "Failed to apply resources" + reason: RemoteClusterClientFailed + severity: Error + status: 'False' + type: ResourcesApplied + observedGeneration: 1 diff --git a/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/healthy_resourceapplied.yaml b/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/healthy_resourceapplied.yaml new file mode 100644 index 0000000000000..4aaf8c7b36104 --- /dev/null +++ b/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/healthy_resourceapplied.yaml @@ -0,0 +1,24 @@ +apiVersion: addons.cluster.x-k8s.io/v1beta1 +kind: ClusterResourceSet +metadata: + finalizers: + - addons.cluster.x-k8s.io + generation: 2 + labels: + app.argocd.io/instance: clustername + name: clustername-resource-set + namespace: capi-managed-cluster +spec: + clusterSelector: + matchLabels: + clusterName: clustername + resources: + - kind: ConfigMap + name: clustername-default-rbac + strategy: ApplyOnce +status: + conditions: + - lastTransitionTime: '2024-11-08T08:49:13Z' + status: 'True' + type: ResourcesApplied + observedGeneration: 2 diff --git a/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/progressing_resourceapplied.yaml b/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/progressing_resourceapplied.yaml new file mode 100644 index 0000000000000..f6e499abbc8a6 --- /dev/null +++ b/resource_customizations/addons.cluster.x-k8s.io/ClusterResourceSet/testdata/progressing_resourceapplied.yaml @@ -0,0 +1,18 @@ +apiVersion: addons.cluster.x-k8s.io/v1beta1 +kind: ClusterResourceSet +metadata: + finalizers: + - addons.cluster.x-k8s.io + generation: 2 + labels: + app.argocd.io/instance: clustername + name: clustername-resource-set + namespace: capi-managed-cluster +spec: + clusterSelector: + matchLabels: + clusterName: clustername + resources: + - kind: ConfigMap + name: clustername-default-rbac + strategy: ApplyOnce