Skip to content

Commit e2211d6

Browse files
authored
Add PodResources mount to agent (#1650)
* Add PodResources mount * Fix tests * Add missing definition * Fix target containers * Fix const definition
1 parent 3b49a53 commit e2211d6

File tree

10 files changed

+166
-41
lines changed

10 files changed

+166
-41
lines changed

api/datadoghq/v2alpha1/const.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ const (
178178
CriSocketVolumeName = "runtimesocketdir"
179179
RuntimeDirVolumePath = "/var/run"
180180

181-
KubeletAgentCAPath = "/var/run/host-kubelet-ca.crt"
182-
KubeletCAVolumeName = "kubelet-ca"
181+
KubeletAgentCAPath = "/var/run/host-kubelet-ca.crt"
182+
KubeletCAVolumeName = "kubelet-ca"
183+
KubeletPodResourcesVolumeName = "kubelet-pod-resources"
183184

184185
APMSocketName = "apm.socket"
185186

api/datadoghq/v2alpha1/datadogagent_types.go

+5
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,11 @@ type KubeletConfig struct {
10791079
// Default: '/var/run/host-kubelet-ca.crt' if hostCAPath is set, else '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
10801080
// +optional
10811081
AgentCAPath string `json:"agentCAPath,omitempty"`
1082+
1083+
// PodResourcesSocket is the path to the pod resources socket, to be used to read pod resource assignments
1084+
// Default: `/var/lib/kubelet/pod-resources/kubelet.sock`
1085+
// +optional
1086+
PodResourcesSocket string `json:"podResourcesSocket,omitempty"`
10821087
}
10831088

10841089
// HostPortConfig contains host port configuration.

api/datadoghq/v2alpha1/envvar.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
DDKubeResourcesNamespace = "DD_KUBE_RESOURCES_NAMESPACE"
4545
DDKubernetesResourcesLabelsAsTags = "DD_KUBERNETES_RESOURCES_LABELS_AS_TAGS"
4646
DDKubernetesResourcesAnnotationsAsTags = "DD_KUBERNETES_RESOURCES_ANNOTATIONS_AS_TAGS"
47+
DDKubernetesPodResourcesSocket = "DD_KUBERNETES_KUBELET_PODRESOURCES_SOCKET"
4748
DDLeaderElection = "DD_LEADER_ELECTION"
4849
DDLogsEnabled = "DD_LOGS_ENABLED"
4950
DDNamespaceLabelsAsTags = "DD_KUBERNETES_NAMESPACE_LABELS_AS_TAGS"

config/crd/bases/v1/datadoghq.com_datadogagents.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,11 @@ spec:
21492149
hostCAPath:
21502150
description: HostCAPath is the host path where the kubelet CA certificate is stored.
21512151
type: string
2152+
podResourcesSocket:
2153+
description: |-
2154+
PodResourcesSocket is the path to the pod resources socket, to be used to read pod resource assignments
2155+
Default: `/var/lib/kubelet/pod-resources/kubelet.sock`
2156+
type: string
21522157
tlsVerify:
21532158
description: |-
21542159
TLSVerify toggles kubelet TLS verification.

config/crd/bases/v1/datadoghq.com_datadogagents_v2alpha1.json

+4
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,10 @@
23002300
"description": "HostCAPath is the host path where the kubelet CA certificate is stored.",
23012301
"type": "string"
23022302
},
2303+
"podResourcesSocket": {
2304+
"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`",
2305+
"type": "string"
2306+
},
23032307
"tlsVerify": {
23042308
"description": "TLSVerify toggles kubelet TLS verification.\nDefault: true",
23052309
"type": "boolean"

docs/configuration.v2alpha1.md

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ spec:
228228
| 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 |
229229
| global.kubelet.host.secretKeyRef.optional | Specify whether the Secret or its key must be defined |
230230
| global.kubelet.hostCAPath | HostCAPath is the host path where the kubelet CA certificate is stored. |
231+
| 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` |
231232
| global.kubelet.tlsVerify | TLSVerify toggles kubelet TLS verification. Default: true |
232233
| 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) |
233234
| 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) |

internal/controller/datadogagent/defaults/datadogagent_default.go

+9
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const (
115115

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

119120
defaultContainerStrategy = v2alpha1.OptimizedContainerStrategy
120121

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

196+
if ddaSpec.Global.Kubelet == nil {
197+
ddaSpec.Global.Kubelet = &v2alpha1.KubeletConfig{}
198+
}
199+
200+
if ddaSpec.Global.Kubelet.PodResourcesSocket == "" {
201+
ddaSpec.Global.Kubelet.PodResourcesSocket = defaultKubeletPodResourcesSocket
202+
}
203+
195204
apiutils.DefaultBooleanIfUnset(&ddaSpec.Global.RunProcessChecksInCoreAgent, defaultRunProcessChecksInCoreAgent)
196205
}
197206

internal/controller/datadogagent/override/global.go

+30
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,36 @@ func applyGlobalSettings(logger logr.Logger, manager feature.PodTemplateManagers
285285
Value: agentCAPath,
286286
})
287287
}
288+
if config.Kubelet.PodResourcesSocket != "" {
289+
manager.EnvVar().AddEnvVar(&corev1.EnvVar{
290+
Name: v2alpha1.DDKubernetesPodResourcesSocket,
291+
Value: config.Kubelet.PodResourcesSocket,
292+
})
293+
294+
podResourcesVol, podResourcesMount := volume.GetVolumes(v2alpha1.KubeletPodResourcesVolumeName, config.Kubelet.PodResourcesSocket, config.Kubelet.PodResourcesSocket, false)
295+
if singleContainerStrategyEnabled {
296+
manager.VolumeMount().AddVolumeMountToContainers(
297+
&podResourcesMount,
298+
[]apicommon.AgentContainerName{
299+
apicommon.UnprivilegedSingleAgentContainerName,
300+
},
301+
)
302+
manager.Volume().AddVolume(&podResourcesVol)
303+
} else {
304+
manager.VolumeMount().AddVolumeMountToContainers(
305+
&podResourcesMount,
306+
[]apicommon.AgentContainerName{
307+
apicommon.CoreAgentContainerName,
308+
apicommon.ProcessAgentContainerName,
309+
apicommon.TraceAgentContainerName,
310+
apicommon.SecurityAgentContainerName,
311+
apicommon.AgentDataPlaneContainerName,
312+
apicommon.SystemProbeContainerName,
313+
},
314+
)
315+
manager.Volume().AddVolume(&podResourcesVol)
316+
}
317+
}
288318
}
289319

290320
var runtimeVol corev1.Volume

0 commit comments

Comments
 (0)