Skip to content

Commit

Permalink
Add PodResources mount
Browse files Browse the repository at this point in the history
  • Loading branch information
gjulianm committed Feb 3, 2025
1 parent 8b4ebea commit b9e0baf
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 8 deletions.
5 changes: 3 additions & 2 deletions api/datadoghq/v2alpha1/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ const (
CriSocketVolumeName = "runtimesocketdir"
RuntimeDirVolumePath = "/var/run"

KubeletAgentCAPath = "/var/run/host-kubelet-ca.crt"
KubeletCAVolumeName = "kubelet-ca"
KubeletAgentCAPath = "/var/run/host-kubelet-ca.crt"
KubeletCAVolumeName = "kubelet-ca"
KubeletPodResourcesVolumeName = "kubelet-pod-resources"

APMSocketName = "apm.socket"

Expand Down
5 changes: 5 additions & 0 deletions api/datadoghq/v2alpha1/datadogagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,11 @@ type KubeletConfig struct {
// Default: '/var/run/host-kubelet-ca.crt' if hostCAPath is set, else '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
// +optional
AgentCAPath string `json:"agentCAPath,omitempty"`

// PodResourcesSocket is the path to the pod resources socket, to be used to read pod resource assignments
// Default: `/var/lib/kubelet/pod-resources/kubelet.sock`
// +optional
PodResourcesSocket string `json:"podResourcesSocket,omitempty"`
}

// HostPortConfig contains host port configuration.
Expand Down
1 change: 1 addition & 0 deletions api/datadoghq/v2alpha1/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
DDKubeResourcesNamespace = "DD_KUBE_RESOURCES_NAMESPACE"
DDKubernetesResourcesLabelsAsTags = "DD_KUBERNETES_RESOURCES_LABELS_AS_TAGS"
DDKubernetesResourcesAnnotationsAsTags = "DD_KUBERNETES_RESOURCES_ANNOTATIONS_AS_TAGS"
DDKubernetesPodResourcesSocket = "DD_KUBERNETES_KUBELET_PODRESOURCES_SOCKET"
DDLeaderElection = "DD_LEADER_ELECTION"
DDLogsEnabled = "DD_LOGS_ENABLED"
DDNamespaceLabelsAsTags = "DD_KUBERNETES_NAMESPACE_LABELS_AS_TAGS"
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/v1/datadoghq.com_datadogagents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,11 @@ spec:
hostCAPath:
description: HostCAPath is the host path where the kubelet CA certificate is stored.
type: string
podResourcesSocket:
description: |-
PodResourcesSocket is the path to the pod resources socket, to be used to read pod resource assignments
Default: `/var/lib/kubelet/pod-resources/kubelet.sock`
type: string
tlsVerify:
description: |-
TLSVerify toggles kubelet TLS verification.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/v1/datadoghq.com_datadogagents_v2alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,10 @@
"description": "HostCAPath is the host path where the kubelet CA certificate is stored.",
"type": "string"
},
"podResourcesSocket": {
"description": "PodResourcesSocket is the path to the pod resources socket, to be used to read pod resource assignments\nDefault: `/var/lib/kubelet/pod-resources/kubelet.sock`",
"type": "string"
},
"tlsVerify": {
"description": "TLSVerify toggles kubelet TLS verification.\nDefault: true",
"type": "boolean"
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.v2alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ spec:
| global.kubelet.host.secretKeyRef.name | Of the referent. This field is effectively required, but due to backwards compatibility is allowed to be empty. Instances of this type with an empty value here are almost certainly wrong. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names |
| global.kubelet.host.secretKeyRef.optional | Specify whether the Secret or its key must be defined |
| global.kubelet.hostCAPath | HostCAPath is the host path where the kubelet CA certificate is stored. |
| global.kubelet.podResourcesSocket | PodResourcesSocket is the path to the pod resources socket, to be used to read pod resource assignments Default: `/var/lib/kubelet/pod-resources/kubelet.sock` |
| global.kubelet.tlsVerify | TLSVerify toggles kubelet TLS verification. Default: true |
| global.kubernetesResourcesAnnotationsAsTags | Provide a mapping of Kubernetes Resource Groups to annotations mapping to Datadog Tags. <KUBERNETES_RESOURCE_GROUP>: <KUBERNETES_ANNOTATION>: <DATADOG_TAG_KEY> KUBERNETES_RESOURCE_GROUP should be in the form `{resource}.{group}` or `{resource}` (example: deployments.apps, pods) |
| global.kubernetesResourcesLabelsAsTags | Provide a mapping of Kubernetes Resource Groups to labels mapping to Datadog Tags. <KUBERNETES_RESOURCE_GROUP>: <KUBERNETES_LABEL>: <DATADOG_TAG_KEY> KUBERNETES_RESOURCE_GROUP should be in the form `{resource}.{group}` or `{resource}` (example: deployments.apps, pods) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const (

// defaultKubeletAgentCAPath = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
// defaultKubeletAgentCAPathHostPathSet = "/var/run/host-kubelet-ca.crt"
defaultKubeletPodResourcesSocket = "/var/lib/kubelet/pod-resources/kubelet.sock"

defaultContainerStrategy = v2alpha1.OptimizedContainerStrategy

Expand Down Expand Up @@ -192,6 +193,14 @@ func defaultGlobalConfig(ddaSpec *v2alpha1.DatadogAgentSpec) {
apiutils.DefaultBooleanIfUnset(&ddaSpec.Global.FIPS.UseHTTPS, defaultFIPSUseHTTPS)
}

if ddaSpec.Global.Kubelet == nil {
ddaSpec.Global.Kubelet = &v2alpha1.KubeletConfig{}
}

if ddaSpec.Global.Kubelet.PodResourcesSocket == "" {
ddaSpec.Global.Kubelet.PodResourcesSocket = defaultKubeletPodResourcesSocket
}

apiutils.DefaultBooleanIfUnset(&ddaSpec.Global.RunProcessChecksInCoreAgent, defaultRunProcessChecksInCoreAgent)
}

Expand Down
25 changes: 25 additions & 0 deletions internal/controller/datadogagent/override/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,31 @@ func applyGlobalSettings(logger logr.Logger, manager feature.PodTemplateManagers
Value: agentCAPath,
})
}
if config.Kubelet.PodResourcesSocket != "" {
manager.EnvVar().AddEnvVar(&corev1.EnvVar{
Name: v2alpha1.DDKubernetesPodResourcesSocket,
Value: config.Kubelet.PodResourcesSocket,
})

podResourcesVol, podResourcesMount := volume.GetVolumes(v2alpha1.KubeletPodResourcesVolumeName, config.Kubelet.PodResourcesSocket, config.Kubelet.PodResourcesSocket, false)
if singleContainerStrategyEnabled {
manager.VolumeMount().AddVolumeMountToContainers(
&podResourcesMount,
[]apicommon.AgentContainerName{
apicommon.UnprivilegedSingleAgentContainerName,
},
)
manager.Volume().AddVolume(&podResourcesVol)
} else {
manager.VolumeMount().AddVolumeMountToContainers(
&podResourcesMount,
[]apicommon.AgentContainerName{
apicommon.CoreAgentContainerName,
},
)
manager.Volume().AddVolume(&podResourcesVol)
}
}
}

var runtimeVol corev1.Volume
Expand Down
26 changes: 24 additions & 2 deletions internal/controller/datadogagent/override/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
const (
hostCAPath = "/host/ca/path/ca.crt"
agentCAPath = "/agent/ca/path/ca.crt"
podResourcesSocket = "/kubelet/podresources.sock"
dockerSocketPath = "/docker/socket/path/docker.sock"
secretBackendCommand = "foo.sh"
secretBackendArgs = "bar baz"
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestNodeAgentComponenGlobalSettings(t *testing.T) {
name: "Kubelet volume configured",
singleContainerStrategyEnabled: false,
dda: testutils.NewDatadogAgentBuilder().
WithGlobalKubeletConfig(hostCAPath, agentCAPath, true).
WithGlobalKubeletConfig(hostCAPath, agentCAPath, true, podResourcesSocket).
WithGlobalDockerSocketPath(dockerSocketPath).
BuildWithDefaults(),
wantEnvVars: getExpectedEnvVars([]*corev1.EnvVar{
Expand All @@ -85,6 +86,10 @@ func TestNodeAgentComponenGlobalSettings(t *testing.T) {
Name: v2alpha1.DockerHost,
Value: "unix:///host" + dockerSocketPath,
},
{
Name: v2alpha1.DDKubernetesPodResourcesSocket,
Value: podResourcesSocket,
},
}...),
wantVolumeMounts: getExpectedVolumeMounts(),
wantVolumes: getExpectedVolumes(),
Expand All @@ -94,7 +99,7 @@ func TestNodeAgentComponenGlobalSettings(t *testing.T) {
name: "Kubelet volume configured",
singleContainerStrategyEnabled: true,
dda: testutils.NewDatadogAgentBuilder().
WithGlobalKubeletConfig(hostCAPath, agentCAPath, true).
WithGlobalKubeletConfig(hostCAPath, agentCAPath, true, podResourcesSocket).
WithGlobalDockerSocketPath(dockerSocketPath).
BuildWithDefaults(),
wantEnvVars: getExpectedEnvVars([]*corev1.EnvVar{
Expand All @@ -110,6 +115,10 @@ func TestNodeAgentComponenGlobalSettings(t *testing.T) {
Name: v2alpha1.DockerHost,
Value: "unix:///host" + dockerSocketPath,
},
{
Name: v2alpha1.DDKubernetesPodResourcesSocket,
Value: podResourcesSocket,
},
}...),
wantVolumeMounts: getExpectedVolumeMounts(),
wantVolumes: getExpectedVolumes(),
Expand Down Expand Up @@ -314,6 +323,14 @@ func getExpectedVolumes() []*corev1.Volume {
},
},
},
{
Name: v2alpha1.KubeletPodResourcesVolumeName,
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: podResourcesSocket,
},
},
},
}
}

Expand All @@ -329,6 +346,11 @@ func getExpectedVolumeMounts() []*corev1.VolumeMount {
MountPath: "/host" + dockerSocketPath,
ReadOnly: true,
},
{
Name: v2alpha1.KubeletPodResourcesVolumeName,
MountPath: podResourcesSocket,
ReadOnly: false,
},
}
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/testutils/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,12 @@ func (builder *DatadogAgentBuilder) WithHelmCheckValuesAsTags(valuesAsTags map[s

// Global Kubelet

func (builder *DatadogAgentBuilder) WithGlobalKubeletConfig(hostCAPath, agentCAPath string, tlsVerify bool) *DatadogAgentBuilder {
func (builder *DatadogAgentBuilder) WithGlobalKubeletConfig(hostCAPath, agentCAPath string, tlsVerify bool, podResourcesSocket string) *DatadogAgentBuilder {
builder.datadogAgent.Spec.Global.Kubelet = &v2alpha1.KubeletConfig{
TLSVerify: apiutils.NewBoolPointer(tlsVerify),
HostCAPath: hostCAPath,
AgentCAPath: agentCAPath,
TLSVerify: apiutils.NewBoolPointer(tlsVerify),
HostCAPath: hostCAPath,
AgentCAPath: agentCAPath,
PodResourcesSocket: podResourcesSocket,
}
return builder
}
Expand Down

0 comments on commit b9e0baf

Please sign in to comment.