From 4d2b36656d85685b06067cb0077da5689411f807 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 9 Oct 2024 12:41:16 +0545 Subject: [PATCH] feat: initial delay seconds support on deployment, replicaset & STS --- pkg/health/health_deployment.go | 20 ++++++++++++++++++++ pkg/health/health_replicaset.go | 20 ++++++++++++++++++++ pkg/health/health_statefulset.go | 20 ++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/pkg/health/health_deployment.go b/pkg/health/health_deployment.go index ad0fb96..ea88eef 100644 --- a/pkg/health/health_deployment.go +++ b/pkg/health/health_deployment.go @@ -2,6 +2,8 @@ package health import ( "fmt" + "strings" + "time" appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -24,6 +26,24 @@ func getDeploymentHealth(obj *unstructured.Unstructured) (*HealthStatus, error) } func getAppsv1DeploymentHealth(deployment *appsv1.Deployment, obj *unstructured.Unstructured) (*HealthStatus, error) { + var containersWaitingForReadiness []string + for _, container := range deployment.Spec.Template.Spec.Containers { + if container.ReadinessProbe != nil && container.ReadinessProbe.InitialDelaySeconds > 0 { + deadline := deployment.CreationTimestamp.Add(time.Second * time.Duration(container.ReadinessProbe.InitialDelaySeconds)) + if time.Now().Before(deadline) { + containersWaitingForReadiness = append(containersWaitingForReadiness, container.Name) + } + } + } + + if len(containersWaitingForReadiness) > 0 { + return &HealthStatus{ + Health: HealthUnknown, + Status: HealthStatusStarting, + Message: fmt.Sprintf("Container(s) %s is waiting for readiness probe", strings.Join(containersWaitingForReadiness, ",")), + }, nil + } + status, err := GetDefaultHealth(obj) if err != nil { return status, err diff --git a/pkg/health/health_replicaset.go b/pkg/health/health_replicaset.go index 55ac099..1e53ca6 100644 --- a/pkg/health/health_replicaset.go +++ b/pkg/health/health_replicaset.go @@ -2,6 +2,8 @@ package health import ( "fmt" + "strings" + "time" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -25,6 +27,24 @@ func getReplicaSetHealth(obj *unstructured.Unstructured) (*HealthStatus, error) } func getAppsv1ReplicaSetHealth(replicaSet *appsv1.ReplicaSet) (*HealthStatus, error) { + var containersWaitingForReadiness []string + for _, container := range replicaSet.Spec.Template.Spec.Containers { + if container.ReadinessProbe != nil && container.ReadinessProbe.InitialDelaySeconds > 0 { + deadline := replicaSet.CreationTimestamp.Add(time.Second * time.Duration(container.ReadinessProbe.InitialDelaySeconds)) + if time.Now().Before(deadline) { + containersWaitingForReadiness = append(containersWaitingForReadiness, container.Name) + } + } + } + + if len(containersWaitingForReadiness) > 0 { + return &HealthStatus{ + Health: HealthUnknown, + Status: HealthStatusStarting, + Message: fmt.Sprintf("Container(s) %s is waiting for readiness probe", strings.Join(containersWaitingForReadiness, ",")), + }, nil + } + health := HealthUnknown if (replicaSet.Spec.Replicas == nil || *replicaSet.Spec.Replicas == 0) && replicaSet.Status.Replicas == 0 { return &HealthStatus{ diff --git a/pkg/health/health_statefulset.go b/pkg/health/health_statefulset.go index 890339d..c3c613d 100644 --- a/pkg/health/health_statefulset.go +++ b/pkg/health/health_statefulset.go @@ -2,6 +2,8 @@ package health import ( "fmt" + "strings" + "time" appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -36,6 +38,24 @@ func getAppsv1StatefulSetHealth(sts *appsv1.StatefulSet) (*HealthStatus, error) }, nil } + var containersWaitingForReadiness []string + for _, container := range sts.Spec.Template.Spec.Containers { + if container.ReadinessProbe != nil && container.ReadinessProbe.InitialDelaySeconds > 0 { + deadline := sts.CreationTimestamp.Add(time.Second * time.Duration(container.ReadinessProbe.InitialDelaySeconds)) + if time.Now().Before(deadline) { + containersWaitingForReadiness = append(containersWaitingForReadiness, container.Name) + } + } + } + + if len(containersWaitingForReadiness) > 0 { + return &HealthStatus{ + Health: HealthUnknown, + Status: HealthStatusStarting, + Message: fmt.Sprintf("Container(s) %s is waiting for readiness probe", strings.Join(containersWaitingForReadiness, ",")), + }, nil + } + health := HealthHealthy if sts.Status.ReadyReplicas == 0 { health = HealthUnhealthy