Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DatadogAgent][Flare] Additional environment variables and RBAC to permit user manifests retrieval #1477

Merged
merged 10 commits into from
Nov 6, 2024
12 changes: 12 additions & 0 deletions internal/controller/datadogagent/feature/enabledefault/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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 enabledefault

const (
DDAgentDaemonSet = "AGENT_DAEMONSET"
DDClusterAgentDeployment = "CLUSTER_AGENT_DEPLOYMENT"
DDDatadogAgentCustomResource = "DATADOGAGENT_CR_NAME"
)
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,18 @@ func (f *defaultFeature) ManageClusterAgent(managers feature.PodTemplateManagers
Name: apicommon.DDClusterAgentServiceAccountName,
Value: f.clusterAgent.serviceAccountName,
})
managers.EnvVar().AddEnvVar(&corev1.EnvVar{
Name: DDAgentDaemonSet,
Value: getDaemonSetNameFromDatadogAgent(f.owner.(*v2alpha1.DatadogAgent)),
})
managers.EnvVar().AddEnvVar(&corev1.EnvVar{
Name: DDClusterAgentDeployment,
Value: getDeploymentNameFromDatadogAgent(f.owner.(*v2alpha1.DatadogAgent)),
})
managers.EnvVar().AddEnvVar(&corev1.EnvVar{
Name: DDDatadogAgentCustomResource,
Value: f.owner.GetName(),
})
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func getDefaultClusterAgentRolePolicyRules(dda metav1.Object) []rbacv1.PolicyRul
},
Verbs: []string{rbac.GetVerb, rbac.UpdateVerb, rbac.CreateVerb},
})
rules = append(rules, rbacv1.PolicyRule{
APIGroups: []string{rbac.DatadogAPIGroup},
Resources: []string{rbac.DatadogAgentsResource},
ResourceNames: []string{
dda.GetName(),
},
Verbs: []string{rbac.GetVerb},
})
return rules
}

Expand Down
36 changes: 36 additions & 0 deletions internal/controller/datadogagent/feature/enabledefault/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 enabledefault

import (
"github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
componentagent "github.com/DataDog/datadog-operator/internal/controller/datadogagent/component/agent"
componentdca "github.com/DataDog/datadog-operator/internal/controller/datadogagent/component/clusteragent"
)

// getDaemonSetNameFromDatadogAgent returns the expected node Agent DS/EDS name based on
// the DDA name and nodeAgent name override
func getDaemonSetNameFromDatadogAgent(dda *v2alpha1.DatadogAgent) string {
dsName := componentagent.GetAgentName(dda)
if componentOverride, ok := dda.Spec.Override[v2alpha1.NodeAgentComponentName]; ok {
if componentOverride.Name != nil && *componentOverride.Name != "" {
dsName = *componentOverride.Name
}
}
return dsName
}

// getDeploymentNameFromDatadogAgent returns the expected Cluster Agent Deployment name based on
// the DDA name and clusterAgent name override
func getDeploymentNameFromDatadogAgent(dda *v2alpha1.DatadogAgent) string {
deployName := componentdca.GetClusterAgentName(dda)
if componentOverride, ok := dda.Spec.Override[v2alpha1.ClusterAgentComponentName]; ok {
if componentOverride.Name != nil && *componentOverride.Name != "" {
deployName = *componentOverride.Name
}
}
return deployName
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 enabledefault

import (
"testing"

"github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
v2alpha1test "github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1/test"
"github.com/stretchr/testify/assert"
)

func Test_getDaemonSetNameFromDatadogAgent(t *testing.T) {
tests := []struct {
name string
ddaName string
overrideAgentName string
expectedName string
}{
{
name: "No override",
ddaName: "foo",
overrideAgentName: "",
expectedName: "foo-agent",
},
{
name: "With override",
ddaName: "bar",
overrideAgentName: "node",
expectedName: "node",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dda := v2alpha1test.NewDatadogAgentBuilder().
WithName(tt.ddaName).
WithComponentOverride(v2alpha1.NodeAgentComponentName, v2alpha1.DatadogAgentComponentOverride{
Name: &tt.overrideAgentName,
}).
Build()
dsName := getDaemonSetNameFromDatadogAgent(dda)
assert.Equal(t, tt.expectedName, dsName)
})
}
}

func Test_getDeploymentNameFromDatadogAgent(t *testing.T) {
tests := []struct {
name string
ddaName string
overrideClusterAgentName string
expectedName string
}{
{
name: "No override",
ddaName: "foo",
overrideClusterAgentName: "",
expectedName: "foo-cluster-agent",
},
{
name: "With override",
ddaName: "bar",
overrideClusterAgentName: "dca",
expectedName: "dca",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dda := v2alpha1test.NewDatadogAgentBuilder().
WithName(tt.ddaName).
WithComponentOverride(v2alpha1.ClusterAgentComponentName, v2alpha1.DatadogAgentComponentOverride{
Name: &tt.overrideClusterAgentName,
}).
Build()
deployName := getDeploymentNameFromDatadogAgent(dda)
assert.Equal(t, tt.expectedName, deployName)
})
}
}
1 change: 1 addition & 0 deletions pkg/kubernetes/rbac/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
CronjobsResource = "cronjobs"
CustomResourceDefinitionsResource = "customresourcedefinitions"
DaemonsetsResource = "daemonsets"
DatadogAgentsResource = "datadogagents"
DatadogMetricsResource = "datadogmetrics"
DatadogMetricsStatusResource = "datadogmetrics/status"
DatadogPodAutoscalersResource = "datadogpodautoscalers"
Expand Down
Loading