Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add service health tests #143

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pkg/health/health_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ func getContainerStatus(containerStatus corev1.ContainerStatus) (waiting *Health
terminated.Status = HealthStatusCode(state.Reason)
terminated.AppendMessage("exit=%d", state.ExitCode)
} else if state.Reason == string(HealthStatusCompleted) {
// completed with restart
terminated.Status = "RestartLoop"
if waiting != nil && waiting.Status == HealthStatusCrashLoopBackoff {
// completed with crash loop backoff
terminated.Status = "RestartLoopBackOff"
} else {
// completed with restart
terminated.Status = "RestartLoop"
}
} else {
terminated.Status = HealthStatusCode(state.Reason)
}
Expand All @@ -119,6 +124,7 @@ func getCorev1PodHealth(pod *corev1.Pod) (*HealthStatus, error) {
isStarting := age < deadline
hr := HealthStatus{
Health: lo.Ternary(isReady, HealthHealthy, HealthUnhealthy),
Ready: isReady,
}

if pod.ObjectMeta.DeletionTimestamp != nil && !pod.ObjectMeta.DeletionTimestamp.IsZero() {
Expand Down
5 changes: 4 additions & 1 deletion pkg/health/health_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 0 additions & 19 deletions pkg/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/health/testdata/Kubernetes/Pod/healthy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ metadata:
namespace: kube-system
annotations:
eks.amazonaws.com/compute-type: ec2
expected-ready: "true"
generateName: coredns-7448775847-
ownerReferences:
- uid: 3b4888e3-9330-4148-b0f4-559ceb4c6463
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
creationTimestamp: "2024-11-20T14:25:03Z"
generateName: test-unhealthy-69688db7d5-
annotations:
expected-status: RestartLoop
expected-status: RestartLoopBackOff
expected-health: unhealthy
labels:
app.kubernetes.io/name: test-unhealthy
Expand Down
25 changes: 25 additions & 0 deletions pkg/health/testdata/Kubernetes/Service/svc-clusterip.yaml
Original file line number Diff line number Diff line change
@@ -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: {}
38 changes: 38 additions & 0 deletions pkg/health/testdata/Kubernetes/Service/svc-loadbalancer.yaml
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions pkg/health/testdata/Kubernetes/Service/unhealthy.yaml
Original file line number Diff line number Diff line change
@@ -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: {}
29 changes: 29 additions & 0 deletions pkg/health/testdata/Kubernetes/Service/unknown.yaml
Original file line number Diff line number Diff line change
@@ -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: {}
Loading