Skip to content

Commit

Permalink
Provide default probe handler httpGet values if not configured in ove…
Browse files Browse the repository at this point in the history
…rride (#998)

* Provide default readinessProbe.httpGet if not configured

* apply review suggestions
  • Loading branch information
fanny-jiang authored Dec 27, 2023
1 parent 371e021 commit 3eec094
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 2 deletions.
25 changes: 23 additions & 2 deletions controllers/datadogagent/override/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
115 changes: 115 additions & 0 deletions controllers/datadogagent/override/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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)
})
Expand Down

0 comments on commit 3eec094

Please sign in to comment.