Skip to content

Commit

Permalink
Propagate custom check definitions to CLC Runners
Browse files Browse the repository at this point in the history
It's already possible to define `.Spec.Agent.Config.Checksd`, but that
option is not propagated to the Clusterchecks Runner. As a runner is
basically an agent, we're just reusing the same configuration.

This applies the same changes as this proposed change to
stable/datadog's Helm chart: helm/charts#23139
  • Loading branch information
juliogreff committed Aug 4, 2020
1 parent 02a5073 commit fe75253
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 85 deletions.
9 changes: 5 additions & 4 deletions docs/custom_check.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ spec:
agent:
image:
name: "datadog/agent:latest"
confd:
configMapName: "confd-config"
checksd:
configMapName: "checksd-config"
config:
confd:
configMapName: "confd-config"
checksd:
configMapName: "checksd-config"
```

**Note**: Any ConfigMaps you create need to be in the same `DD_NAMESPACE` as the `DatadogAgent` resource.
Expand Down
15 changes: 13 additions & 2 deletions pkg/controller/datadogagent/clusterchecksrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,26 @@ func newClusterChecksRunnerPodTemplate(dda *datadoghqv1alpha1.DatadogAgent, labe
// copy Spec to configure the Cluster Checks Runner Pod Template
clusterChecksRunnerSpec := dda.Spec.ClusterChecksRunner.DeepCopy()

spec := &dda.Spec
volumeMounts := getVolumeMountsForClusterChecksRunner(dda)
envVars := getEnvVarsForClusterChecksRunner(dda)
initContainers := getConfigInitContainers(spec, volumeMounts, envVars)

newPodTemplate := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Annotations: annotations,
},
Spec: corev1.PodSpec{
ServiceAccountName: getClusterChecksRunnerServiceAccount(dda),
InitContainers: initContainers,
Containers: []corev1.Container{
{
Name: "cluster-checks-runner",
Image: clusterChecksRunnerSpec.Image.Name,
ImagePullPolicy: *clusterChecksRunnerSpec.Image.PullPolicy,
Env: getEnvVarsForClusterChecksRunner(dda),
VolumeMounts: getVolumeMountsForClusterChecksRunner(dda),
Env: envVars,
VolumeMounts: volumeMounts,
LivenessProbe: getDefaultLivenessProbe(),
},
},
Expand Down Expand Up @@ -397,6 +403,8 @@ func getClusterChecksRunnerName(dda *datadoghqv1alpha1.DatadogAgent) string {
// getVolumesForClusterChecksRunner defines volumes for the Cluster Checks Runner
func getVolumesForClusterChecksRunner(dda *datadoghqv1alpha1.DatadogAgent) []corev1.Volume {
volumes := []corev1.Volume{
getVolumeForChecksd(dda),
getVolumeForConfig(),
{
Name: "s6-run",
VolumeSource: corev1.VolumeSource{
Expand All @@ -421,6 +429,8 @@ func getVolumesForClusterChecksRunner(dda *datadoghqv1alpha1.DatadogAgent) []cor
// getVolumeMountsForClusterChecksRunner defines volume mounts for the Cluster Checks Runner
func getVolumeMountsForClusterChecksRunner(dda *datadoghqv1alpha1.DatadogAgent) []corev1.VolumeMount {
volumeMounts := []corev1.VolumeMount{
getVolumeMountForChecksd(),
getVolumeMountForConfig(),
{
Name: "s6-run",
MountPath: "/var/run/s6",
Expand All @@ -430,6 +440,7 @@ func getVolumeMountsForClusterChecksRunner(dda *datadoghqv1alpha1.DatadogAgent)
MountPath: fmt.Sprintf("%s/%s", datadoghqv1alpha1.ConfigVolumePath, "conf.d"),
},
}

if dda.Spec.ClusterChecksRunner.CustomConfig != nil {
volumeMount := getVolumeMountFromCustomConfigSpec(dda.Spec.ClusterChecksRunner.CustomConfig, datadoghqv1alpha1.AgentCustomConfigVolumeName, datadoghqv1alpha1.AgentCustomConfigVolumePath, datadoghqv1alpha1.AgentCustomConfigVolumeSubPath)
volumeMounts = append(volumeMounts, volumeMount)
Expand Down
49 changes: 49 additions & 0 deletions pkg/controller/datadogagent/clusterchecksrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ func clusterChecksRunnerDefaultPodSpec() corev1.PodSpec {
return corev1.PodSpec{
Affinity: getPodAffinity(nil, "foo-cluster-checks-runner"),
ServiceAccountName: "foo-cluster-checks-runner",
InitContainers: []corev1.Container{
{
Name: "init-volume",
Image: "datadog/agent:latest",
ImagePullPolicy: corev1.PullIfNotPresent,
Resources: corev1.ResourceRequirements{},
Command: []string{"bash", "-c"},
Args: []string{"cp -r /etc/datadog-agent /opt"},
VolumeMounts: []corev1.VolumeMount{
{
Name: datadoghqv1alpha1.ConfigVolumeName,
MountPath: "/opt/datadog-agent",
},
},
},
{
Name: "init-config",
Image: "datadog/agent:latest",
ImagePullPolicy: corev1.PullIfNotPresent,
Resources: corev1.ResourceRequirements{},
Command: []string{"bash", "-c"},
Args: []string{"for script in $(find /etc/cont-init.d/ -type f -name '*.sh' | sort) ; do bash $script ; done"},
Env: clusterChecksRunnerDefaultEnvVars(),
VolumeMounts: clusterChecksRunnerDefaultVolumeMounts(),
},
},
Containers: []corev1.Container{
{
Name: "cluster-checks-runner",
Expand All @@ -40,6 +66,15 @@ func clusterChecksRunnerDefaultPodSpec() corev1.PodSpec {

func clusterChecksRunnerDefaultVolumeMounts() []corev1.VolumeMount {
return []corev1.VolumeMount{
{
Name: datadoghqv1alpha1.ChecksdVolumeName,
MountPath: datadoghqv1alpha1.ChecksdVolumePath,
ReadOnly: true,
},
{
Name: datadoghqv1alpha1.ConfigVolumeName,
MountPath: datadoghqv1alpha1.ConfigVolumePath,
},
{
Name: "s6-run",
MountPath: "/var/run/s6",
Expand All @@ -53,6 +88,18 @@ func clusterChecksRunnerDefaultVolumeMounts() []corev1.VolumeMount {

func clusterChecksRunnerDefaultVolumes() []corev1.Volume {
return []corev1.Volume{
{
Name: datadoghqv1alpha1.ChecksdVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: datadoghqv1alpha1.ConfigVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: "s6-run",
VolumeSource: corev1.VolumeSource{
Expand Down Expand Up @@ -188,6 +235,7 @@ func Test_newClusterChecksRunnerDeploymentFromInstance_UserVolumes(t *testing.T)
}
userMountsPodSpec := clusterChecksRunnerDefaultPodSpec()
userMountsPodSpec.Volumes = append(userMountsPodSpec.Volumes, userVolumes...)
userMountsPodSpec.InitContainers[1].VolumeMounts = append(userMountsPodSpec.InitContainers[1].VolumeMounts, userVolumeMounts...)
userMountsPodSpec.Containers[0].VolumeMounts = append(userMountsPodSpec.Containers[0].VolumeMounts, userVolumeMounts...)

envVarsAgentDeployment := test.NewDefaultedDatadogAgent(
Expand Down Expand Up @@ -266,6 +314,7 @@ func Test_newClusterChecksRunnerDeploymentFromInstance_EnvVars(t *testing.T) {
},
}
podSpec := clusterChecksRunnerDefaultPodSpec()
podSpec.InitContainers[1].Env = append(podSpec.InitContainers[1].Env, envVars...)
podSpec.Containers[0].Env = append(podSpec.Containers[0].Env, envVars...)

envVarsAgentDeployment := test.NewDefaultedDatadogAgent(
Expand Down
190 changes: 111 additions & 79 deletions pkg/controller/datadogagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,32 +308,9 @@ func getInitContainers(dda *datadoghqv1alpha1.DatadogAgent) ([]corev1.Container,
if err != nil {
return nil, err
}
containers := []corev1.Container{
{
Name: "init-volume",
Image: spec.Agent.Image.Name,
ImagePullPolicy: *spec.Agent.Image.PullPolicy,
Resources: *spec.Agent.Config.Resources,
Command: []string{"bash", "-c"},
Args: []string{"cp -r /etc/datadog-agent /opt"},
VolumeMounts: []corev1.VolumeMount{
{
Name: datadoghqv1alpha1.ConfigVolumeName,
MountPath: "/opt/datadog-agent",
},
},
},
{
Name: "init-config",
Image: spec.Agent.Image.Name,
ImagePullPolicy: *spec.Agent.Image.PullPolicy,
Resources: *spec.Agent.Config.Resources,
Command: []string{"bash", "-c"},
Args: []string{"for script in $(find /etc/cont-init.d/ -type f -name '*.sh' | sort) ; do bash $script ; done"},
Env: envVars,
VolumeMounts: volumeMounts,
},
}

containers := getConfigInitContainers(spec, volumeMounts, envVars)

if isSystemProbeEnabled(dda) {
if getSeccompProfileName(&dda.Spec.Agent.SystemProbe) == datadoghqv1alpha1.DefaultSeccompProfileName || dda.Spec.Agent.SystemProbe.SecCompCustomProfileConfigMap != "" {
systemProbeInit := corev1.Container{
Expand Down Expand Up @@ -364,6 +341,37 @@ func getInitContainers(dda *datadoghqv1alpha1.DatadogAgent) ([]corev1.Container,
return containers, nil
}

// getConfigInitContainers returns the init containers necessary to set up the
// agent's configuration volume.
func getConfigInitContainers(spec *datadoghqv1alpha1.DatadogAgentSpec, volumeMounts []corev1.VolumeMount, envVars []corev1.EnvVar) []corev1.Container {
return []corev1.Container{
{
Name: "init-volume",
Image: spec.Agent.Image.Name,
ImagePullPolicy: *spec.Agent.Image.PullPolicy,
Resources: *spec.Agent.Config.Resources,
Command: []string{"bash", "-c"},
Args: []string{"cp -r /etc/datadog-agent /opt"},
VolumeMounts: []corev1.VolumeMount{
{
Name: datadoghqv1alpha1.ConfigVolumeName,
MountPath: "/opt/datadog-agent",
},
},
},
{
Name: "init-config",
Image: spec.Agent.Image.Name,
ImagePullPolicy: *spec.Agent.Image.PullPolicy,
Resources: *spec.Agent.Config.Resources,
Command: []string{"bash", "-c"},
Args: []string{"for script in $(find /etc/cont-init.d/ -type f -name '*.sh' | sort) ; do bash $script ; done"},
Env: envVars,
VolumeMounts: volumeMounts,
},
}
}

// getEnvVarsForAPMAgent converts APM Agent Config into container env vars
func getEnvVarsForAPMAgent(dda *datadoghqv1alpha1.DatadogAgent) ([]corev1.EnvVar, error) {
envVars := []corev1.EnvVar{
Expand Down Expand Up @@ -592,46 +600,10 @@ func getEnvVarsForAgent(dda *datadoghqv1alpha1.DatadogAgent) ([]corev1.EnvVar, e

// getVolumesForAgent defines volumes for the Agent
func getVolumesForAgent(dda *datadoghqv1alpha1.DatadogAgent) []corev1.Volume {
confdVolumeSource := corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
}
if dda.Spec.Agent.Config.Confd != nil {
confdVolumeSource = corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: dda.Spec.Agent.Config.Confd.ConfigMapName,
},
},
}
}
checksdVolumeSource := corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
}
if dda.Spec.Agent.Config.Checksd != nil {
checksdVolumeSource = corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: dda.Spec.Agent.Config.Checksd.ConfigMapName,
},
},
}
}

volumes := []corev1.Volume{
{
Name: datadoghqv1alpha1.ConfdVolumeName,
VolumeSource: confdVolumeSource,
},
{
Name: datadoghqv1alpha1.ChecksdVolumeName,
VolumeSource: checksdVolumeSource,
},
{
Name: datadoghqv1alpha1.ConfigVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
getVolumeForConfd(dda),
getVolumeForChecksd(dda),
getVolumeForConfig(),
{
Name: datadoghqv1alpha1.ProcVolumeName,
VolumeSource: corev1.VolumeSource{
Expand Down Expand Up @@ -771,6 +743,55 @@ func getVolumesForAgent(dda *datadoghqv1alpha1.DatadogAgent) []corev1.Volume {
return volumes
}

func getVolumeForConfd(dda *datadoghqv1alpha1.DatadogAgent) corev1.Volume {
source := corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
}
if dda.Spec.Agent.Config.Confd != nil {
source = corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: dda.Spec.Agent.Config.Confd.ConfigMapName,
},
},
}
}

return corev1.Volume{
Name: datadoghqv1alpha1.ConfdVolumeName,
VolumeSource: source,
}
}

func getVolumeForChecksd(dda *datadoghqv1alpha1.DatadogAgent) corev1.Volume {
source := corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
}
if dda.Spec.Agent.Config.Checksd != nil {
source = corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: dda.Spec.Agent.Config.Checksd.ConfigMapName,
},
},
}
}

return corev1.Volume{
Name: datadoghqv1alpha1.ChecksdVolumeName,
VolumeSource: source,
}
}

func getVolumeForConfig() corev1.Volume {
return corev1.Volume{
Name: datadoghqv1alpha1.ConfigVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
}
}

func getSecCompRootPath(spec *datadoghqv1alpha1.SystemProbeSpec) string {
if spec.SecCompRootPath != "" {
return spec.SecCompRootPath
Expand Down Expand Up @@ -828,20 +849,9 @@ func getVolumeMountFromCustomConfigSpec(cfcm *datadoghqv1alpha1.CustomConfigSpec
func getVolumeMountsForAgent(spec *datadoghqv1alpha1.DatadogAgentSpec) []corev1.VolumeMount {
// Default mounted volumes
volumeMounts := []corev1.VolumeMount{
{
Name: datadoghqv1alpha1.ConfdVolumeName,
MountPath: datadoghqv1alpha1.ConfdVolumePath,
ReadOnly: true,
},
{
Name: datadoghqv1alpha1.ChecksdVolumeName,
MountPath: datadoghqv1alpha1.ChecksdVolumePath,
ReadOnly: true,
},
{
Name: datadoghqv1alpha1.ConfigVolumeName,
MountPath: datadoghqv1alpha1.ConfigVolumePath,
},
getVolumeMountForConfd(),
getVolumeMountForChecksd(),
getVolumeMountForConfig(),
{
Name: datadoghqv1alpha1.ProcVolumeName,
MountPath: datadoghqv1alpha1.ProcVolumePath,
Expand Down Expand Up @@ -907,6 +917,28 @@ func getVolumeMountsForAgent(spec *datadoghqv1alpha1.DatadogAgentSpec) []corev1.
return append(volumeMounts, spec.Agent.Config.VolumeMounts...)
}

func getVolumeMountForConfig() corev1.VolumeMount {
return corev1.VolumeMount{
Name: datadoghqv1alpha1.ConfigVolumeName,
MountPath: datadoghqv1alpha1.ConfigVolumePath,
}
}

func getVolumeMountForConfd() corev1.VolumeMount {
return corev1.VolumeMount{
Name: datadoghqv1alpha1.ConfdVolumeName,
MountPath: datadoghqv1alpha1.ConfdVolumePath,
ReadOnly: true,
}
}
func getVolumeMountForChecksd() corev1.VolumeMount {
return corev1.VolumeMount{
Name: datadoghqv1alpha1.ChecksdVolumeName,
MountPath: datadoghqv1alpha1.ChecksdVolumePath,
ReadOnly: true,
}
}

// getVolumeMountsForAgent defines mounted volumes for the Process Agent
func getVolumeMountsForProcessAgent(spec *datadoghqv1alpha1.DatadogAgentSpec) []corev1.VolumeMount {
// Default mounted volumes
Expand Down

0 comments on commit fe75253

Please sign in to comment.