diff --git a/controllers/datadogagent/override/container.go b/controllers/datadogagent/override/container.go index 082e2f15d..abd723598 100644 --- a/controllers/datadogagent/override/container.go +++ b/controllers/datadogagent/override/container.go @@ -15,6 +15,7 @@ import ( "github.com/DataDog/datadog-operator/controllers/datadogagent/feature" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/intstr" ) // Container use to override a corev1.Container with a 2alpha1.DatadogAgentGenericContainer. @@ -121,11 +122,11 @@ func overrideContainer(container *corev1.Container, override *v2alpha1.DatadogAg } if override.ReadinessProbe != nil { - container.ReadinessProbe = override.ReadinessProbe + container.ReadinessProbe = overrideReadinessProbe(override.ReadinessProbe) } if override.LivenessProbe != nil { - container.LivenessProbe = override.LivenessProbe + container.LivenessProbe = overrideLivenessProbe(override.LivenessProbe) } if override.SecurityContext != nil { @@ -206,3 +207,23 @@ func overrideAppArmorProfile(containerName commonv1.AgentContainerName, manager manager.Annotation().AddAnnotation(annotation, *override.AppArmorProfileName) } } + +func overrideReadinessProbe(readinessProbeOverride *corev1.Probe) *corev1.Probe { + // Add default httpGet.path and httpGet.port if not present in readinessProbe override + if readinessProbeOverride.HTTPGet == nil { + readinessProbeOverride.HTTPGet = &corev1.HTTPGetAction{ + Path: common.DefaultReadinessProbeHTTPPath, + Port: intstr.IntOrString{IntVal: common.DefaultAgentHealthPort}} + } + return readinessProbeOverride +} + +func overrideLivenessProbe(livenessProbeOverride *corev1.Probe) *corev1.Probe { + // Add default httpGet.path and httpGet.port if not present in livenessProbe override + if livenessProbeOverride.HTTPGet == nil { + livenessProbeOverride.HTTPGet = &corev1.HTTPGetAction{ + Path: common.DefaultLivenessProbeHTTPPath, + Port: intstr.IntOrString{IntVal: common.DefaultAgentHealthPort}} + } + return livenessProbeOverride +} diff --git a/controllers/datadogagent/override/container_test.go b/controllers/datadogagent/override/container_test.go index 068534137..b5db482af 100644 --- a/controllers/datadogagent/override/container_test.go +++ b/controllers/datadogagent/override/container_test.go @@ -10,6 +10,7 @@ import ( "github.com/DataDog/datadog-operator/apis/datadoghq/common" apiutils "github.com/DataDog/datadog-operator/apis/utils" "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/apimachinery/pkg/util/intstr" "reflect" "testing" @@ -272,6 +273,47 @@ func TestContainer(t *testing.T) { assert.Equal(t, expectedEnvs, envs) }, }, + { + name: "override readiness probe with default HTTPGet", + containerName: commonv1.CoreAgentContainerName, + existingManager: func() *fake.PodTemplateManagers { + return fake.NewPodTemplateManagers(t, corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{*agentContainer}, + }, + }) + }, + override: v2alpha1.DatadogAgentGenericContainer{ + ReadinessProbe: &corev1.Probe{ + InitialDelaySeconds: 10, + TimeoutSeconds: 5, + PeriodSeconds: 30, + SuccessThreshold: 1, + FailureThreshold: 5, + }, + }, + validateManager: func(t *testing.T, manager *fake.PodTemplateManagers, containerName string) { + assertContainerMatch(t, manager.PodTemplateSpec().Spec.Containers, containerName, func(container corev1.Container) bool { + return reflect.DeepEqual( + &corev1.Probe{ + InitialDelaySeconds: 10, + TimeoutSeconds: 5, + PeriodSeconds: 30, + SuccessThreshold: 1, + FailureThreshold: 5, + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/ready", + Port: intstr.IntOrString{ + IntVal: 5555, + }, + }, + }, + }, + container.ReadinessProbe) + }) + }, + }, { name: "override readiness probe", containerName: commonv1.CoreAgentContainerName, @@ -289,12 +331,28 @@ func TestContainer(t *testing.T) { PeriodSeconds: 30, SuccessThreshold: 1, FailureThreshold: 5, + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/some/path", + Port: intstr.IntOrString{ + IntVal: 1234, + }, + }, + }, }, }, validateManager: func(t *testing.T, manager *fake.PodTemplateManagers, containerName string) { assertContainerMatch(t, manager.PodTemplateSpec().Spec.Containers, containerName, func(container corev1.Container) bool { return reflect.DeepEqual( &corev1.Probe{ + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/some/path", + Port: intstr.IntOrString{ + IntVal: 1234, + }, + }, + }, InitialDelaySeconds: 10, TimeoutSeconds: 5, PeriodSeconds: 30, @@ -305,6 +363,47 @@ func TestContainer(t *testing.T) { }) }, }, + { + name: "override liveness probe with default HTTPGet", + containerName: commonv1.CoreAgentContainerName, + existingManager: func() *fake.PodTemplateManagers { + return fake.NewPodTemplateManagers(t, corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{*agentContainer}, + }, + }) + }, + override: v2alpha1.DatadogAgentGenericContainer{ + LivenessProbe: &corev1.Probe{ + InitialDelaySeconds: 10, + TimeoutSeconds: 5, + PeriodSeconds: 30, + SuccessThreshold: 1, + FailureThreshold: 5, + }, + }, + validateManager: func(t *testing.T, manager *fake.PodTemplateManagers, containerName string) { + assertContainerMatch(t, manager.PodTemplateSpec().Spec.Containers, containerName, func(container corev1.Container) bool { + return reflect.DeepEqual( + &corev1.Probe{ + InitialDelaySeconds: 10, + TimeoutSeconds: 5, + PeriodSeconds: 30, + SuccessThreshold: 1, + FailureThreshold: 5, + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/live", + Port: intstr.IntOrString{ + IntVal: 5555, + }, + }, + }, + }, + container.LivenessProbe) + }) + }, + }, { name: "override liveness probe", containerName: commonv1.CoreAgentContainerName, @@ -322,6 +421,14 @@ func TestContainer(t *testing.T) { PeriodSeconds: 30, SuccessThreshold: 1, FailureThreshold: 5, + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/some/path", + Port: intstr.IntOrString{ + IntVal: 1234, + }, + }, + }, }, }, validateManager: func(t *testing.T, manager *fake.PodTemplateManagers, containerName string) { @@ -333,6 +440,14 @@ func TestContainer(t *testing.T) { PeriodSeconds: 30, SuccessThreshold: 1, FailureThreshold: 5, + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/some/path", + Port: intstr.IntOrString{ + IntVal: 1234, + }, + }, + }, }, container.LivenessProbe) })