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

Provide default probe handler httpGet values if not configured in override #998

Merged
merged 3 commits into from
Dec 27, 2023
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
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
Loading