From 7934e9d5c6969b556ec6ce4777d628ee1c38c764 Mon Sep 17 00:00:00 2001 From: Liliya Belaus <59583867+liliyadd@users.noreply.github.com> Date: Mon, 18 Dec 2023 14:26:57 -0500 Subject: [PATCH] [APMON-406] Set Datadog namespace env variable on Cluster Agent (#1012) * [APMON-406] Set env variable for the Datadog resources namespace * DCA namespace resources env var added for V2 --------- Co-authored-by: julia-elsammak Co-authored-by: Julia-elsammak <109159652+Julia-elsammak@users.noreply.github.com> --- apis/datadoghq/common/envvar.go | 1 + controllers/datadogagent/agent_test.go | 5 +++-- controllers/datadogagent/clusteragent_test.go | 4 ++++ controllers/datadogagent/component/clusteragent/default.go | 4 ++++ pkg/controller/utils/shared_utils.go | 5 +++++ 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/apis/datadoghq/common/envvar.go b/apis/datadoghq/common/envvar.go index 1d4f86d383..ef39c77768 100644 --- a/apis/datadoghq/common/envvar.go +++ b/apis/datadoghq/common/envvar.go @@ -68,6 +68,7 @@ const ( DDKubeletCAPath = "DD_KUBELET_CLIENT_CA" DDKubeletHost = "DD_KUBERNETES_KUBELET_HOST" DDKubeletTLSVerify = "DD_KUBELET_TLS_VERIFY" + DDKubeResourcesNamespace = "DD_KUBE_RESOURCES_NAMESPACE" DDKubeStateMetricsCoreConfigMap = "DD_KUBE_STATE_METRICS_CORE_CONFIGMAP_NAME" DDKubeStateMetricsCoreEnabled = "DD_KUBE_STATE_METRICS_CORE_ENABLED" DDLeaderElection = "DD_LEADER_ELECTION" diff --git a/controllers/datadogagent/agent_test.go b/controllers/datadogagent/agent_test.go index 3d6cd2b2ae..1c9d1833b2 100644 --- a/controllers/datadogagent/agent_test.go +++ b/controllers/datadogagent/agent_test.go @@ -34,8 +34,9 @@ import ( ) const ( - testDdaName = "foo" - agentConfigFile = "/etc/datadog-agent/datadog.yaml" + testDdaName = "foo" + testDdaNamespace = "bar" + agentConfigFile = "/etc/datadog-agent/datadog.yaml" ) func apiKeyValue() *corev1.EnvVarSource { diff --git a/controllers/datadogagent/clusteragent_test.go b/controllers/datadogagent/clusteragent_test.go index ec9d9197d8..13706ea1ad 100644 --- a/controllers/datadogagent/clusteragent_test.go +++ b/controllers/datadogagent/clusteragent_test.go @@ -201,6 +201,10 @@ func clusterAgentDefaultEnvVars() []corev1.EnvVar { Name: "DD_CLUSTER_AGENT_TOKEN_NAME", Value: fmt.Sprintf("%stoken", testDdaName), }, + { + Name: "DD_KUBE_RESOURCES_NAMESPACE", + Value: testDdaNamespace, + }, } } diff --git a/controllers/datadogagent/component/clusteragent/default.go b/controllers/datadogagent/component/clusteragent/default.go index 1e8b796ff0..e3124f3351 100644 --- a/controllers/datadogagent/component/clusteragent/default.go +++ b/controllers/datadogagent/component/clusteragent/default.go @@ -132,6 +132,10 @@ func defaultEnvVars(dda metav1.Object) []corev1.EnvVar { Name: apicommon.DDClusterAgentKubeServiceName, Value: GetClusterAgentServiceName(dda), }, + { + Name: apicommon.DDKubeResourcesNamespace, + Value: utils.GetDatadogAgentResourceNamespace(dda), + }, { Name: apicommon.DDLeaderElection, Value: "true", diff --git a/pkg/controller/utils/shared_utils.go b/pkg/controller/utils/shared_utils.go index 38e8754e3d..15d9384200 100644 --- a/pkg/controller/utils/shared_utils.go +++ b/pkg/controller/utils/shared_utils.go @@ -23,6 +23,11 @@ func GetDatadogLeaderElectionResourceName(dda metav1.Object) string { return fmt.Sprintf("%s-leader-election", dda.GetName()) } +// GetDatadogAgentResourceNamespace returns the namespace of the Datadog Agent Resource +func GetDatadogAgentResourceNamespace(dda metav1.Object) string { + return dda.GetNamespace() +} + // GetDatadogTokenResourceName returns the name of the ConfigMap used by the cluster agent to store token func GetDatadogTokenResourceName(dda metav1.Object) string { return fmt.Sprintf("%stoken", dda.GetName())