-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
298 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
package oomkill | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/DataDog/datadog-operator/apis/datadoghq/v1alpha1" | ||
"github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1" | ||
apiutils "github.com/DataDog/datadog-operator/apis/utils" | ||
|
||
apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common" | ||
apicommonv1 "github.com/DataDog/datadog-operator/apis/datadoghq/common/v1" | ||
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature" | ||
"github.com/DataDog/datadog-operator/controllers/datadogagent/object/volume" | ||
) | ||
|
||
func init() { | ||
err := feature.Register(feature.OOMKillIDType, buildOOMKillFeature) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func buildOOMKillFeature(options *feature.Options) feature.Feature { | ||
oomKillFeat := &oomKillFeature{} | ||
|
||
return oomKillFeat | ||
} | ||
|
||
type oomKillFeature struct { | ||
enable bool | ||
} | ||
|
||
// Configure is used to configure the feature from a v2alpha1.DatadogAgent instance. | ||
func (f *oomKillFeature) Configure(dda *v2alpha1.DatadogAgent) (reqComp feature.RequiredComponents) { | ||
if dda.Spec.Features.OOMKill != nil && apiutils.BoolValue(dda.Spec.Features.OOMKill.Enabled) { | ||
f.enable = true | ||
reqComp.Agent = feature.RequiredComponent{ | ||
IsRequired: &f.enable, | ||
Containers: []apicommonv1.AgentContainerName{apicommonv1.CoreAgentContainerName, apicommonv1.SystemProbeContainerName}, | ||
} | ||
} | ||
|
||
return reqComp | ||
} | ||
|
||
// ConfigureV1 use to configure the feature from a v1alpha1.DatadogAgent instance. | ||
func (f *oomKillFeature) ConfigureV1(dda *v1alpha1.DatadogAgent) (reqComp feature.RequiredComponents) { | ||
if dda.Spec.Agent.SystemProbe != nil && *dda.Spec.Agent.SystemProbe.EnableOOMKill { | ||
f.enable = true | ||
reqComp.Agent = feature.RequiredComponent{ | ||
IsRequired: &f.enable, | ||
Containers: []apicommonv1.AgentContainerName{apicommonv1.CoreAgentContainerName, apicommonv1.SystemProbeContainerName}, | ||
} | ||
} | ||
|
||
return reqComp | ||
} | ||
|
||
// ManageDependencies allows a feature to manage its dependencies. | ||
// Feature's dependencies should be added in the store. | ||
func (f *oomKillFeature) ManageDependencies(managers feature.ResourceManagers) error { | ||
return nil | ||
} | ||
|
||
// ManageClusterAgent allows a feature to configure the ClusterAgent's corev1.PodTemplateSpec | ||
// It should do nothing if the feature doesn't need to configure it. | ||
func (f *oomKillFeature) ManageClusterAgent(managers feature.PodTemplateManagers) error { | ||
return nil | ||
} | ||
|
||
// ManageNodeAgent allows a feature to configure the Node Agent's corev1.PodTemplateSpec | ||
// It should do nothing if the feature doesn't need to configure it. | ||
func (f *oomKillFeature) ManageNodeAgent(managers feature.PodTemplateManagers) error { | ||
// modules volume mount | ||
modulesVol, modulesVolMount := volume.GetVolumes(apicommon.ModulesVolumeName, apicommon.ModulesVolumePath, apicommon.ModulesVolumePath) | ||
managers.Volume().AddVolumeToContainer(&modulesVol, &modulesVolMount, apicommonv1.SystemProbeContainerName) | ||
|
||
// src volume mount | ||
srcVol, srcVolMount := volume.GetVolumes(apicommon.SrcVolumeName, apicommon.SrcVolumePath, apicommon.SrcVolumePath) | ||
managers.Volume().AddVolumeToContainer(&srcVol, &srcVolMount, apicommonv1.SystemProbeContainerName) | ||
|
||
enableEnvVar := &corev1.EnvVar{ | ||
Name: apicommon.DDEnableOOMKillEnvVar, | ||
Value: "true", | ||
} | ||
|
||
managers.EnvVar().AddEnvVarToContainer(apicommonv1.CoreAgentContainerName, enableEnvVar) | ||
managers.EnvVar().AddEnvVarToContainer(apicommonv1.SystemProbeContainerName, enableEnvVar) | ||
|
||
return nil | ||
} | ||
|
||
// ManageClusterChecksRunner allows a feature to configure the ClusterChecksRunner's corev1.PodTemplateSpec | ||
// It should do nothing if the feature doesn't need to configure it. | ||
func (f *oomKillFeature) ManageClusterChecksRunner(managers feature.PodTemplateManagers) error { | ||
return nil | ||
} |
155 changes: 155 additions & 0 deletions
155
controllers/datadogagent/feature/oom_kill/feature_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
package oomkill | ||
|
||
import ( | ||
"testing" | ||
|
||
apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common" | ||
apicommonv1 "github.com/DataDog/datadog-operator/apis/datadoghq/common/v1" | ||
"github.com/DataDog/datadog-operator/apis/datadoghq/v1alpha1" | ||
"github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1" | ||
apiutils "github.com/DataDog/datadog-operator/apis/utils" | ||
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature" | ||
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature/fake" | ||
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature/test" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func createEmptyFakeManager(t testing.TB) feature.PodTemplateManagers { | ||
mgr := fake.NewPodTemplateManagers(t) | ||
return mgr | ||
} | ||
|
||
func Test_oomKillFeature_Configure(t *testing.T) { | ||
ddav1OOMKillDisabled := v1alpha1.DatadogAgent{ | ||
Spec: v1alpha1.DatadogAgentSpec{ | ||
Agent: v1alpha1.DatadogAgentSpecAgentSpec{ | ||
SystemProbe: &v1alpha1.SystemProbeSpec{ | ||
EnableOOMKill: apiutils.NewBoolPointer(false), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
ddav1OOMKillEnabled := ddav1OOMKillDisabled.DeepCopy() | ||
{ | ||
ddav1OOMKillEnabled.Spec.Agent.SystemProbe.EnableOOMKill = apiutils.NewBoolPointer(true) | ||
} | ||
|
||
ddav2OOMKillDisabled := v2alpha1.DatadogAgent{ | ||
Spec: v2alpha1.DatadogAgentSpec{ | ||
Features: &v2alpha1.DatadogFeatures{ | ||
OOMKill: &v2alpha1.OOMKillFeatureConfig{ | ||
Enabled: apiutils.NewBoolPointer(false), | ||
}, | ||
}, | ||
}, | ||
} | ||
ddav2OOMKillEnabled := ddav2OOMKillDisabled.DeepCopy() | ||
{ | ||
ddav2OOMKillEnabled.Spec.Features.OOMKill.Enabled = apiutils.NewBoolPointer(true) | ||
} | ||
|
||
oomKillAgentNodeWantFunc := func(t testing.TB, mgrInterface feature.PodTemplateManagers) { | ||
mgr := mgrInterface.(*fake.PodTemplateManagers) | ||
|
||
// check volume mounts | ||
wantVolumeMounts := []corev1.VolumeMount{ | ||
{ | ||
Name: apicommon.ModulesVolumeName, | ||
MountPath: apicommon.ModulesVolumePath, | ||
ReadOnly: true, | ||
}, | ||
{ | ||
Name: apicommon.SrcVolumeName, | ||
MountPath: apicommon.SrcVolumePath, | ||
ReadOnly: true, | ||
}, | ||
} | ||
|
||
systemProbeVolumeMounts := mgr.VolumeMgr.VolumeMountByC[apicommonv1.SystemProbeContainerName] | ||
assert.True(t, apiutils.IsEqualStruct(systemProbeVolumeMounts, wantVolumeMounts), "System Probe volume mounts \ndiff = %s", cmp.Diff(systemProbeVolumeMounts, wantVolumeMounts)) | ||
|
||
// check volumes | ||
wantVolumes := []corev1.Volume{ | ||
{ | ||
Name: apicommon.ModulesVolumeName, | ||
VolumeSource: corev1.VolumeSource{ | ||
HostPath: &corev1.HostPathVolumeSource{ | ||
Path: apicommon.ModulesVolumePath, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: apicommon.SrcVolumeName, | ||
VolumeSource: corev1.VolumeSource{ | ||
HostPath: &corev1.HostPathVolumeSource{ | ||
Path: apicommon.SrcVolumePath, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
volumes := mgr.VolumeMgr.Volumes | ||
assert.True(t, apiutils.IsEqualStruct(volumes, wantVolumes), "Volumes \ndiff = %s", cmp.Diff(volumes, wantVolumes)) | ||
|
||
// check env vars | ||
wantEnvVars := []*corev1.EnvVar{ | ||
{ | ||
Name: apicommon.DDEnableOOMKillEnvVar, | ||
Value: "true", | ||
}, | ||
} | ||
agentEnvVars := mgr.EnvVarMgr.EnvVarsByC[apicommonv1.CoreAgentContainerName] | ||
assert.True(t, apiutils.IsEqualStruct(agentEnvVars, wantEnvVars), "Agent envvars \ndiff = %s", cmp.Diff(agentEnvVars, wantEnvVars)) | ||
|
||
systemProbeEnvVars := mgr.EnvVarMgr.EnvVarsByC[apicommonv1.SystemProbeContainerName] | ||
assert.True(t, apiutils.IsEqualStruct(systemProbeEnvVars, wantEnvVars), "System Probe envvars \ndiff = %s", cmp.Diff(systemProbeEnvVars, wantEnvVars)) | ||
} | ||
|
||
tests := test.FeatureTestSuite{ | ||
/////////////////////////// | ||
// v1alpha1.DatadogAgent // | ||
/////////////////////////// | ||
{ | ||
Name: "v1alpha1 oom kill not enabled", | ||
DDAv1: ddav1OOMKillDisabled.DeepCopy(), | ||
WantConfigure: false, | ||
}, | ||
{ | ||
Name: "v1alpha1 oom kill enabled", | ||
DDAv1: ddav1OOMKillEnabled, | ||
WantConfigure: true, | ||
Agent: &test.ComponentTest{ | ||
CreateFunc: createEmptyFakeManager, | ||
WantFunc: oomKillAgentNodeWantFunc, | ||
}, | ||
}, | ||
/////////////////////////// | ||
// v2alpha1.DatadogAgent // | ||
/////////////////////////// | ||
{ | ||
Name: "v2alpha1 oom kill not enabled", | ||
DDAv2: ddav2OOMKillDisabled.DeepCopy(), | ||
WantConfigure: false, | ||
}, | ||
{ | ||
Name: "v2alpha1 oom kill enabled", | ||
DDAv2: ddav2OOMKillEnabled, | ||
WantConfigure: true, | ||
Agent: &test.ComponentTest{ | ||
CreateFunc: createEmptyFakeManager, | ||
WantFunc: oomKillAgentNodeWantFunc, | ||
}, | ||
}, | ||
} | ||
|
||
tests.Run(t, buildOOMKillFeature) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters