From 326cc4a06b2cb5ac99797d3f04c2d4c48b8692e2 Mon Sep 17 00:00:00 2001 From: Andreas <49288593+asamonik@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:26:06 +0200 Subject: [PATCH] feat(health): Added KeptnMetric (#19210) Signed-off-by: asamonik <49288593+asamonik@users.noreply.github.com> --- .../metrics.keptn.sh/KeptnMetric/health.lua | 14 +++++++++++ .../KeptnMetric/health_test.yaml | 17 +++++++++++++ .../KeptnMetric/testdata/degraded.yaml | 24 +++++++++++++++++++ .../KeptnMetric/testdata/healthy.yaml | 22 +++++++++++++++++ .../testdata/healthy_empty_error.yaml | 23 ++++++++++++++++++ .../KeptnMetric/testdata/progressing.yaml | 21 ++++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 resource_customizations/metrics.keptn.sh/KeptnMetric/health.lua create mode 100644 resource_customizations/metrics.keptn.sh/KeptnMetric/health_test.yaml create mode 100644 resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/degraded.yaml create mode 100644 resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/healthy.yaml create mode 100644 resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/healthy_empty_error.yaml create mode 100644 resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/progressing.yaml diff --git a/resource_customizations/metrics.keptn.sh/KeptnMetric/health.lua b/resource_customizations/metrics.keptn.sh/KeptnMetric/health.lua new file mode 100644 index 0000000000..0275f503c4 --- /dev/null +++ b/resource_customizations/metrics.keptn.sh/KeptnMetric/health.lua @@ -0,0 +1,14 @@ +local hs = {} +if (obj.status.errMsg == nil or obj.status.errMsg == "") and obj.status.value ~= nil then + hs.status = "Healthy" + hs.message = "KeptnMetric is healthy" + return hs +end +if obj.status.errMsg ~= nil and obj.status.errMsg ~= "" then + hs.status = "Degraded" + hs.message = "KeptnMetric is degraded" + return hs +end +hs.status = "Progressing" +hs.message = "KeptnMetric is progressing" +return hs diff --git a/resource_customizations/metrics.keptn.sh/KeptnMetric/health_test.yaml b/resource_customizations/metrics.keptn.sh/KeptnMetric/health_test.yaml new file mode 100644 index 0000000000..0f170f6f5f --- /dev/null +++ b/resource_customizations/metrics.keptn.sh/KeptnMetric/health_test.yaml @@ -0,0 +1,17 @@ +tests: + - healthStatus: + status: Progressing + message: "KeptnMetric is progressing" + inputPath: testdata/progressing.yaml + - healthStatus: + status: Degraded + message: "KeptnMetric is degraded" + inputPath: testdata/degraded.yaml + - healthStatus: + status: Healthy + message: "KeptnMetric is healthy" + inputPath: testdata/healthy.yaml + - healthStatus: + status: Healthy + message: "KeptnMetric is healthy" + inputPath: testdata/healthy_empty_error.yaml diff --git a/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/degraded.yaml b/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/degraded.yaml new file mode 100644 index 0000000000..cdd429bb82 --- /dev/null +++ b/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/degraded.yaml @@ -0,0 +1,24 @@ +apiVersion: metrics.keptn.sh/v1 +kind: KeptnMetric +metadata: + annotations: + kubectl.kubernetes.io/last-applied-configuration: > + {"apiVersion":"metrics.keptn.sh/v1","kind":"KeptnMetric","metadata":{"annotations":{},"labels":{"app.kubernetes.io/instance":"podtato-head"},"name":"available-cpus","namespace":"podtato-kubectl"},"spec":{"fetchIntervalSeconds":10,"provider":{"name":"my-provider"},"query":"sum(kube_node_status_capacity{resource='cpu'})"}} + creationTimestamp: '2024-07-16T07:34:42Z' + generation: 1 + labels: + app.kubernetes.io/instance: podtato-head + name: available-cpus + namespace: podtato-kubectl + resourceVersion: '405403' + uid: c448a014-b6b6-45a4-91ff-89949b9d0fce +spec: + fetchIntervalSeconds: 10 + provider: + name: my-provider + query: sum(kube_node_status_capacity{resource='cpu'}) +status: + errMsg: >- + Post "http://prometheus-k8s.monitoring.svc.cluster.local:9090/api/v1/query": + dial tcp: lookup prometheus-k8s.monitoring.svc.cluster.local on + lastUpdated: '2024-07-23T12:49:44Z' diff --git a/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/healthy.yaml b/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/healthy.yaml new file mode 100644 index 0000000000..2c5ecad045 --- /dev/null +++ b/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/healthy.yaml @@ -0,0 +1,22 @@ +apiVersion: metrics.keptn.sh/v1 +kind: KeptnMetric +metadata: + annotations: + kubectl.kubernetes.io/last-applied-configuration: > + {"apiVersion":"metrics.keptn.sh/v1","kind":"KeptnMetric","metadata":{"annotations":{},"labels":{"app.kubernetes.io/instance":"podtato-head"},"name":"available-cpus","namespace":"podtato-kubectl"},"spec":{"fetchIntervalSeconds":10,"provider":{"name":"my-provider"},"query":"sum(kube_node_status_capacity{resource='cpu'})"}} + creationTimestamp: '2024-07-16T07:34:42Z' + generation: 1 + labels: + app.kubernetes.io/instance: podtato-head + name: available-cpus + namespace: podtato-kubectl + resourceVersion: '405403' + uid: c448a014-b6b6-45a4-91ff-89949b9d0fce +spec: + fetchIntervalSeconds: 10 + provider: + name: my-provider + query: sum(kube_node_status_capacity{resource='cpu'}) +status: + value: '100' + lastUpdated: '2024-07-23T12:49:44Z' diff --git a/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/healthy_empty_error.yaml b/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/healthy_empty_error.yaml new file mode 100644 index 0000000000..758ccf9170 --- /dev/null +++ b/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/healthy_empty_error.yaml @@ -0,0 +1,23 @@ +apiVersion: metrics.keptn.sh/v1 +kind: KeptnMetric +metadata: + annotations: + kubectl.kubernetes.io/last-applied-configuration: > + {"apiVersion":"metrics.keptn.sh/v1","kind":"KeptnMetric","metadata":{"annotations":{},"labels":{"app.kubernetes.io/instance":"podtato-head"},"name":"available-cpus","namespace":"podtato-kubectl"},"spec":{"fetchIntervalSeconds":10,"provider":{"name":"my-provider"},"query":"sum(kube_node_status_capacity{resource='cpu'})"}} + creationTimestamp: '2024-07-16T07:34:42Z' + generation: 1 + labels: + app.kubernetes.io/instance: podtato-head + name: available-cpus + namespace: podtato-kubectl + resourceVersion: '405403' + uid: c448a014-b6b6-45a4-91ff-89949b9d0fce +spec: + fetchIntervalSeconds: 10 + provider: + name: my-provider + query: sum(kube_node_status_capacity{resource='cpu'}) +status: + errMsg: "" + value: 100 + lastUpdated: '2024-07-23T12:49:44Z' diff --git a/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/progressing.yaml b/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/progressing.yaml new file mode 100644 index 0000000000..71f219fbae --- /dev/null +++ b/resource_customizations/metrics.keptn.sh/KeptnMetric/testdata/progressing.yaml @@ -0,0 +1,21 @@ +apiVersion: metrics.keptn.sh/v1 +kind: KeptnMetric +metadata: + annotations: + kubectl.kubernetes.io/last-applied-configuration: > + {"apiVersion":"metrics.keptn.sh/v1","kind":"KeptnMetric","metadata":{"annotations":{},"labels":{"app.kubernetes.io/instance":"podtato-head"},"name":"available-cpus","namespace":"podtato-kubectl"},"spec":{"fetchIntervalSeconds":10,"provider":{"name":"my-provider"},"query":"sum(kube_node_status_capacity{resource='cpu'})"}} + creationTimestamp: '2024-07-16T07:34:42Z' + generation: 1 + labels: + app.kubernetes.io/instance: podtato-head + name: available-cpus + namespace: podtato-kubectl + resourceVersion: '405403' + uid: c448a014-b6b6-45a4-91ff-89949b9d0fce +spec: + fetchIntervalSeconds: 10 + provider: + name: my-provider + query: sum(kube_node_status_capacity{resource='cpu'}) +status: + lastUpdated: '2024-07-23T12:49:44Z'