diff --git a/resource_customizations/openfaas.com/Function/health.lua b/resource_customizations/openfaas.com/Function/health.lua new file mode 100644 index 0000000000..df72e228b0 --- /dev/null +++ b/resource_customizations/openfaas.com/Function/health.lua @@ -0,0 +1,31 @@ +hs = {} +if obj.status ~= nil then + if obj.status.conditions ~= nil then + for i, condition in ipairs(obj.status.conditions) do + if condition.type == "Ready" and condition.status == "False" then + hs.status = "Degraded" + hs.message = condition.message + return hs + end + if condition.type == "Stalled" and condition.status == "True" then + hs.status = "Degraded" + hs.message = condition.message + return hs + end + if condition.type == "Ready" and condition.status == "True" then + if obj.status.replicas ~= nil and obj.status.replicas > 0 then + hs.status = "Healthy" + hs.message = condition.message + else + hs.status = "Suspended" + hs.message = "No replicas available" + end + return hs + end + end + end +end + +hs.status = "Progressing" +hs.message = "Waiting for Function" +return hs diff --git a/resource_customizations/openfaas.com/Function/health_test.yaml b/resource_customizations/openfaas.com/Function/health_test.yaml new file mode 100644 index 0000000000..750089fac4 --- /dev/null +++ b/resource_customizations/openfaas.com/Function/health_test.yaml @@ -0,0 +1,17 @@ +tests: +- healthStatus: + status: Progressing + message: "Waiting for Function" + inputPath: testdata/progressing.yaml +- healthStatus: + status: Degraded + message: "Secret missing: secrets \"missing-secret\" not found" + inputPath: testdata/degraded_no_secret.yaml +- healthStatus: + status: Healthy + message: "Deployment and service reconciled" + inputPath: testdata/healthy.yaml +- healthStatus: + status: Suspended + message: "No replicas available" + inputPath: testdata/suspended_zero_replicas.yaml diff --git a/resource_customizations/openfaas.com/Function/testdata/degraded_no_secret.yaml b/resource_customizations/openfaas.com/Function/testdata/degraded_no_secret.yaml new file mode 100644 index 0000000000..a1c0c981f1 --- /dev/null +++ b/resource_customizations/openfaas.com/Function/testdata/degraded_no_secret.yaml @@ -0,0 +1,48 @@ +{ + "apiVersion": "openfaas.com/v1", + "kind": "Function", + "metadata": { + "annotations": { + "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"openfaas.com/v1\",\"kind\":\"Function\",\"metadata\":{\"annotations\":{},\"name\":\"env\",\"namespace\":\"openfaas-fn\"},\"spec\":{\"annotations\":{},\"environment\":{\"fprocess\":\"env\",\"test\":\"yes\"},\"image\":\"ghcr.io/openfaas/alpine:latest\",\"labels\":{},\"name\":\"env\",\"secrets\":[\"missing-secret\"]}}\n" + }, + "creationTimestamp": "2024-04-29T13:42:46Z", + "generation": 1, + "name": "env", + "namespace": "openfaas-fn", + "resourceVersion": "580675", + "uid": "7a00bc7b-eb01-4f6a-b5f7-7893422ace7d" + }, + "spec": { + "annotations": {}, + "environment": { + "fprocess": "env", + "test": "yes" + }, + "image": "ghcr.io/openfaas/alpine:latest", + "labels": {}, + "name": "env", + "secrets": [ + "missing-secret" + ] + }, + "status": { + "conditions": [ + { + "lastTransitionTime": "2024-04-29T13:42:46Z", + "message": "Function queued for creation", + "observedGeneration": 1, + "reason": "Reconciling", + "status": "True", + "type": "Reconciling" + }, + { + "lastTransitionTime": "2024-04-29T13:42:46Z", + "message": "Secret missing: secrets \"missing-secret\" not found", + "observedGeneration": 1, + "reason": "SecretMissing", + "status": "True", + "type": "Stalled" + } + ] + } +} diff --git a/resource_customizations/openfaas.com/Function/testdata/healthy.yaml b/resource_customizations/openfaas.com/Function/testdata/healthy.yaml new file mode 100644 index 0000000000..7d09972561 --- /dev/null +++ b/resource_customizations/openfaas.com/Function/testdata/healthy.yaml @@ -0,0 +1,36 @@ +apiVersion: openfaas.com/v1 +kind: Function +metadata: + annotations: + kubectl.kubernetes.io/last-applied-configuration: | + {"apiVersion":"openfaas.com/v1","kind":"Function","metadata":{"annotations":{},"name":"env","namespace":"openfaas-fn"},"spec":{"annotations":{},"environment":{"fprocess":"env","test":"yes"},"image":"ghcr.io/openfaas/alpine:latest","labels":{},"name":"env"}} + creationTimestamp: "2024-04-29T13:38:50Z" + generation: 1 + name: env + namespace: openfaas-fn + resourceVersion: "580323" + uid: 865f74b9-cbc5-455a-abd7-4a1cdeae22d1 +spec: + annotations: {} + environment: + fprocess: env + test: "yes" + image: ghcr.io/openfaas/alpine:latest + labels: {} + name: env +status: + availableReplicas: 1 + conditions: + - lastTransitionTime: "2024-04-29T13:38:50Z" + message: Deployment and service reconciled + observedGeneration: 1 + reason: Ready + status: "True" + type: Ready + - lastTransitionTime: "2024-04-29T13:38:53Z" + message: At least one replica available + observedGeneration: 1 + reason: ReplicaAvailable + status: "True" + type: Healthy + replicas: 1 diff --git a/resource_customizations/openfaas.com/Function/testdata/progressing.yaml b/resource_customizations/openfaas.com/Function/testdata/progressing.yaml new file mode 100644 index 0000000000..4b70ad08c1 --- /dev/null +++ b/resource_customizations/openfaas.com/Function/testdata/progressing.yaml @@ -0,0 +1,30 @@ +--- +apiVersion: openfaas.com/v1 +kind: Function +metadata: + annotations: + kubectl.kubernetes.io/last-applied-configuration: | + {"apiVersion":"openfaas.com/v1","kind":"Function","metadata":{"annotations":{},"name":"env","namespace":"openfaas-fn"},"spec":{"annotations":{},"environment":{"fprocess":"env","test":"yes"},"image":"ghcr.io/openfaas/alpine:latest","labels":{},"name":"env"}} + creationTimestamp: "2024-04-29T13:38:50Z" + generation: 1 + name: env + namespace: openfaas-fn + resourceVersion: "580277" + uid: 865f74b9-cbc5-455a-abd7-4a1cdeae22d1 +spec: + annotations: {} + environment: + fprocess: env + test: "yes" + image: ghcr.io/openfaas/alpine:latest + labels: {} + name: env +status: + conditions: + - lastTransitionTime: "2024-04-29T13:38:50Z" + message: Function queued for creation + observedGeneration: 1 + reason: Reconciling + status: "True" + type: Reconciling +--- diff --git a/resource_customizations/openfaas.com/Function/testdata/suspended_zero_replicas.yaml b/resource_customizations/openfaas.com/Function/testdata/suspended_zero_replicas.yaml new file mode 100644 index 0000000000..3307dfd8e4 --- /dev/null +++ b/resource_customizations/openfaas.com/Function/testdata/suspended_zero_replicas.yaml @@ -0,0 +1,35 @@ +--- +apiVersion: openfaas.com/v1 +kind: Function +metadata: + annotations: + kubectl.kubernetes.io/last-applied-configuration: | + {"apiVersion":"openfaas.com/v1","kind":"Function","metadata":{"annotations":{},"name":"env","namespace":"openfaas-fn"},"spec":{"annotations":{},"environment":{"fprocess":"env","test":"yes"},"image":"ghcr.io/openfaas/alpine:latest","labels":{},"name":"env"}} + creationTimestamp: "2024-04-29T13:38:50Z" + generation: 1 + name: env + namespace: openfaas-fn + resourceVersion: "580543" + uid: 865f74b9-cbc5-455a-abd7-4a1cdeae22d1 +spec: + annotations: {} + environment: + fprocess: env + test: "yes" + image: ghcr.io/openfaas/alpine:latest + labels: {} + name: env +status: + conditions: + - lastTransitionTime: "2024-04-29T13:38:50Z" + message: Deployment and service reconciled + observedGeneration: 1 + reason: Ready + status: "True" + type: Ready + - lastTransitionTime: "2024-04-29T13:41:27Z" + message: At least one replica available + observedGeneration: 1 + reason: ReplicaAvailable + status: "False" + type: Healthy