From cda0e780c80898f3f8036b8fe1ab8d7c2569ea17 Mon Sep 17 00:00:00 2001 From: Moshe Immermam Date: Thu, 28 Nov 2024 11:25:47 +0200 Subject: [PATCH] chore: add service health tests --- pkg/health/health_service.go | 5 ++- pkg/health/health_test.go | 19 ---------- .../Kubernetes/Service/svc-clusterip.yaml | 25 ++++++++++++ .../Kubernetes/Service/svc-loadbalancer.yaml | 38 +++++++++++++++++++ .../Kubernetes/Service/unhealthy.yaml | 29 ++++++++++++++ .../testdata/Kubernetes/Service/unknown.yaml | 29 ++++++++++++++ 6 files changed, 125 insertions(+), 20 deletions(-) create mode 100644 pkg/health/testdata/Kubernetes/Service/svc-clusterip.yaml create mode 100644 pkg/health/testdata/Kubernetes/Service/svc-loadbalancer.yaml create mode 100644 pkg/health/testdata/Kubernetes/Service/unhealthy.yaml create mode 100644 pkg/health/testdata/Kubernetes/Service/unknown.yaml diff --git a/pkg/health/health_service.go b/pkg/health/health_service.go index 5ea9b53..1aa2479 100644 --- a/pkg/health/health_service.go +++ b/pkg/health/health_service.go @@ -2,7 +2,9 @@ package health import ( "fmt" + "time" + "github.com/samber/lo" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) @@ -30,8 +32,9 @@ func getCorev1ServiceHealth(service *corev1.Service) (*HealthStatus, error) { health.Health = HealthHealthy health.Ready = true } else { + age := time.Since(service.CreationTimestamp.Time) health.Status = HealthStatusCreating - health.Health = HealthUnknown + health.Health = lo.Ternary(age < time.Hour, HealthUnknown, HealthUnhealthy) } } else { health.Ready = true diff --git a/pkg/health/health_test.go b/pkg/health/health_test.go index e17b26a..2b9f147 100644 --- a/pkg/health/health_test.go +++ b/pkg/health/health_test.go @@ -451,25 +451,6 @@ func TestPVCHealth(t *testing.T) { assertAppHealthMsg(t, "./testdata/pvc-pending.yaml", health.HealthStatusProgressing, health.HealthHealthy, false) } -func TestServiceHealth(t *testing.T) { - assertAppHealthMsg(t, "./testdata/svc-clusterip.yaml", health.HealthStatusUnknown, health.HealthUnknown, true) - assertAppHealthMsg(t, "./testdata/svc-loadbalancer.yaml", health.HealthStatusRunning, health.HealthHealthy, true) - assertAppHealthMsg( - t, - "./testdata/svc-loadbalancer-unassigned.yaml", - health.HealthStatusCreating, - health.HealthUnknown, - false, - ) - assertAppHealthMsg( - t, - "./testdata/svc-loadbalancer-nonemptylist.yaml", - health.HealthStatusRunning, - health.HealthHealthy, - true, - ) -} - func TestIngressHealth(t *testing.T) { assertAppHealthMsg(t, "./testdata/ingress.yaml", health.HealthStatusHealthy, health.HealthHealthy, true) assertAppHealthMsg(t, "./testdata/ingress-unassigned.yaml", health.HealthStatusPending, health.HealthHealthy, false) diff --git a/pkg/health/testdata/Kubernetes/Service/svc-clusterip.yaml b/pkg/health/testdata/Kubernetes/Service/svc-clusterip.yaml new file mode 100644 index 0000000..5b24aa2 --- /dev/null +++ b/pkg/health/testdata/Kubernetes/Service/svc-clusterip.yaml @@ -0,0 +1,25 @@ +apiVersion: v1 +kind: Service +metadata: + annotations: + expected-ready: "true" + expected-health: unknown + creationTimestamp: 2018-10-27T06:36:27Z + name: argocd-metrics + namespace: argocd + resourceVersion: "1131" + selfLink: /api/v1/namespaces/argocd/services/argocd-metrics + uid: a1f65069-d9b2-11e8-b3c1-9ae2f452bd03 +spec: + clusterIP: 10.96.199.2 + ports: + - name: http + port: 8082 + protocol: TCP + targetPort: 8082 + selector: + app: argocd-server + sessionAffinity: None + type: ClusterIP +status: + loadBalancer: {} diff --git a/pkg/health/testdata/Kubernetes/Service/svc-loadbalancer.yaml b/pkg/health/testdata/Kubernetes/Service/svc-loadbalancer.yaml new file mode 100644 index 0000000..2edea77 --- /dev/null +++ b/pkg/health/testdata/Kubernetes/Service/svc-loadbalancer.yaml @@ -0,0 +1,38 @@ +apiVersion: v1 +kind: Service +metadata: + annotations: + expected-status: Running + expected-health: healthy + expected-ready: "true" + service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout: "600" + creationTimestamp: 2018-06-05T23:34:58Z + labels: + app.kubernetes.io/instance: argocd-cdp + name: argocd-server + namespace: argocd + resourceVersion: "32559487" + selfLink: /api/v1/namespaces/argocd/services/argocd-server + uid: 0f5885a9-6919-11e8-ad29-020124679688 +spec: + clusterIP: 100.69.46.185 + externalTrafficPolicy: Cluster + ports: + - name: http + nodePort: 30354 + port: 80 + protocol: TCP + targetPort: 8080 + - name: https + nodePort: 31866 + port: 443 + protocol: TCP + targetPort: 8080 + selector: + app: argocd-server + sessionAffinity: None + type: LoadBalancer +status: + loadBalancer: + ingress: + - hostname: abc123.us-west-2.elb.amazonaws.com diff --git a/pkg/health/testdata/Kubernetes/Service/unhealthy.yaml b/pkg/health/testdata/Kubernetes/Service/unhealthy.yaml new file mode 100644 index 0000000..502e317 --- /dev/null +++ b/pkg/health/testdata/Kubernetes/Service/unhealthy.yaml @@ -0,0 +1,29 @@ +apiVersion: v1 +kind: Service +metadata: + annotations: + expected-status: Creating + expected-health: unhealthy + expected-ready: "false" + creationTimestamp: "@now-1h" + name: argo-artifacts + namespace: argo + resourceVersion: "346792" + selfLink: /api/v1/namespaces/argo/services/argo-artifacts + uid: 586f5e57-e160-11e8-b3c1-9ae2f452bd03 +spec: + clusterIP: 10.105.70.181 + externalTrafficPolicy: Cluster + ports: + - name: service + nodePort: 32667 + port: 9000 + protocol: TCP + targetPort: 9000 + selector: + app: minio + release: argo-artifacts + sessionAffinity: None + type: LoadBalancer +status: + loadBalancer: {} diff --git a/pkg/health/testdata/Kubernetes/Service/unknown.yaml b/pkg/health/testdata/Kubernetes/Service/unknown.yaml new file mode 100644 index 0000000..5c5b619 --- /dev/null +++ b/pkg/health/testdata/Kubernetes/Service/unknown.yaml @@ -0,0 +1,29 @@ +apiVersion: v1 +kind: Service +metadata: + annotations: + expected-status: Creating + expected-health: unknown + expected-ready: "false" + creationTimestamp: "@now-5m" + name: argo-artifacts + namespace: argo + resourceVersion: "346792" + selfLink: /api/v1/namespaces/argo/services/argo-artifacts + uid: 586f5e57-e160-11e8-b3c1-9ae2f452bd03 +spec: + clusterIP: 10.105.70.181 + externalTrafficPolicy: Cluster + ports: + - name: service + nodePort: 32667 + port: 9000 + protocol: TCP + targetPort: 9000 + selector: + app: minio + release: argo-artifacts + sessionAffinity: None + type: LoadBalancer +status: + loadBalancer: {}