Skip to content

Commit

Permalink
Fix cluster-agent defaulting to enable it by default (#349)
Browse files Browse the repository at this point in the history
In a specific when the section "spec.clusterAgent" is not provided
in the DatadogAgent instance, the cluster-agent is not defaulted to
`spec.clusterAgent.enabled:true`.

This change fix it, and update the tests according to this new behaviour.
  • Loading branch information
clamoriniere committed Aug 5, 2021
1 parent 1e43add commit 11ad61d
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 16 deletions.
9 changes: 5 additions & 4 deletions api/v1alpha1/datadogagent_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func DefaultDatadogAgent(dda *DatadogAgent) *DatadogAgentStatus {
DefaultOverride: &DatadogAgentSpec{},
}

// Features
// default features because it might have an impact on the other defaulting
dso.DefaultOverride.Features = *DefaultFeatures(dda)

// Cluster Agent
dso.DefaultOverride.ClusterAgent = *DefaultDatadogAgentSpecClusterAgent(&dda.Spec.ClusterAgent)

Expand All @@ -116,9 +120,6 @@ func DefaultDatadogAgent(dda *DatadogAgent) *DatadogAgentStatus {
// CLC
dso.DefaultOverride.ClusterChecksRunner = *DefaultDatadogAgentSpecClusterChecksRunner(&dda.Spec.ClusterChecksRunner)

// Features
dso.DefaultOverride.Features = *DefaultFeatures(dda)

// Creds
if dda.Spec.Credentials == nil {
dda.Spec.Credentials = &AgentCredentials{}
Expand Down Expand Up @@ -903,7 +904,7 @@ func DefaultDatadogFeatureNetworkMonitoring(ft *DatadogFeatures) *NetworkMonitor
// return the defaulted DatadogAgentSpecClusterAgentSpec to update the status
func DefaultDatadogAgentSpecClusterAgent(clusterAgent *DatadogAgentSpecClusterAgentSpec) *DatadogAgentSpecClusterAgentSpec {
if IsEqualStruct(*clusterAgent, DatadogAgentSpecClusterAgentSpec{}) {
clusterAgent.Enabled = NewBoolPointer(false)
clusterAgent.Enabled = NewBoolPointer(true)
return clusterAgent
}

Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/datadogagent_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ func TestDefaultDatadogAgentSpecClusterAgent(t *testing.T) {
name: "empty field",
dca: DatadogAgentSpecClusterAgentSpec{},
overrideExpected: &DatadogAgentSpecClusterAgentSpec{
Enabled: NewBoolPointer(false),
Enabled: NewBoolPointer(true),
},
internalDefaulted: DatadogAgentSpecClusterAgentSpec{
Enabled: NewBoolPointer(false),
Enabled: NewBoolPointer(true),
},
},
{
Expand Down
4 changes: 4 additions & 0 deletions api/v1alpha1/test/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func NewDefaultedDatadogAgent(ns, name string, options *NewDatadogAgentOptions)
if len(options.ClusterAgentEnvVars) != 0 {
ad.Spec.ClusterAgent.Config.Env = options.ClusterAgentEnvVars
}
} else {
ad.Spec.ClusterAgent = datadoghqv1alpha1.DatadogAgentSpecClusterAgentSpec{
Enabled: datadoghqv1alpha1.NewBoolPointer(false),
}
}

if options.ClusterChecksRunnerEnabled {
Expand Down
2 changes: 1 addition & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ spec:
- --enable-leader-election
- --pprof
image: controller:latest
imagePullPolicy: Always
imagePullPolicy: IfNotPresent
name: manager
env:
- name: POD_NAME
Expand Down
13 changes: 13 additions & 0 deletions controllers/datadogagent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,18 @@ func customKubeletConfigPodSpec(kubeletConfig *datadoghqv1alpha1.KubeletConfig)
Name: "DD_API_KEY",
ValueFrom: apiKeyValue(),
},
{
Name: "DD_CLUSTER_AGENT_AUTH_TOKEN",
ValueFrom: authTokenValue(),
},
{
Name: "DD_CLUSTER_AGENT_ENABLED",
Value: "true",
},
{
Name: "DD_CLUSTER_AGENT_KUBERNETES_SERVICE_NAME",
Value: "foo-cluster-agent",
},
}

return corev1.PodSpec{
Expand Down Expand Up @@ -3526,6 +3538,7 @@ func Test_newExtendedDaemonSetFromInstance_SecurityAgent_Runtime(t *testing.T) {
func Test_newExtendedDaemonSetFromInstance_KubeletConfiguration(t *testing.T) {
dda := test.NewDefaultedDatadogAgent("bar", "foo", &test.NewDatadogAgentOptions{
UseEDS: true,
ClusterAgentEnabled: true,
OrchestratorExplorerDisabled: true,
})

Expand Down
5 changes: 0 additions & 5 deletions controllers/datadogagent/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func init() {
const resourcesName = "foo"
const resourcesNamespace = "bar"
}

func TestReconcileDatadogAgent_createNewExtendedDaemonSet(t *testing.T) {
eventBroadcaster := record.NewBroadcaster()
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "TestReconcileDatadogAgent_createNewExtendedDaemonSet"})
Expand Down
2 changes: 1 addition & 1 deletion controllers/datadogagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ func getEnvVarsForAgent(logger logr.Logger, dda *datadoghqv1alpha1.DatadogAgent)

if isClusterAgentEnabled(dda.Spec.ClusterAgent) {
clusterEnv := envForClusterAgentConnection(dda)
if datadoghqv1alpha1.BoolValue(spec.ClusterAgent.Config.ClusterChecksEnabled) {
if spec.ClusterAgent.Config != nil && datadoghqv1alpha1.BoolValue(spec.ClusterAgent.Config.ClusterChecksEnabled) {
if !datadoghqv1alpha1.BoolValue(dda.Spec.ClusterChecksRunner.Enabled) {
clusterEnv = append(clusterEnv, corev1.EnvVar{
Name: datadoghqv1alpha1.DDExtraConfigProviders,
Expand Down
3 changes: 2 additions & 1 deletion controllers/datadogagent_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ var _ = Describe("DatadogAgent Controller", func() {
It("It should create DaemonSet", func() {
options := &testutils.NewDatadogAgentOptions{
UseEDS: false,
ClusterAgentDisabled: true,
APIKey: "xnfdsjgdjcxlg42rqmzxzvdsgjdfklg",
OrchestratorExplorerDisabled: true,
}
Expand Down Expand Up @@ -216,7 +217,7 @@ var _ = Describe("DatadogAgent Controller", func() {
}

It("It should create Deployment", func() {
agent := testutils.NewDatadogAgent(namespace, name, "datadog/agent:7.22.0", &testutils.NewDatadogAgentOptions{ClusterAgentEnabled: true, APIKey: "xnfdsjgdjcxlg42rqmzxzvdsgjdfklg"})
agent := testutils.NewDatadogAgent(namespace, name, "datadog/agent:7.22.0", &testutils.NewDatadogAgentOptions{APIKey: "xnfdsjgdjcxlg42rqmzxzvdsgjdfklg"})
Expect(k8sClient.Create(context.Background(), agent)).Should(Succeed())

var agentClusterAgentHash string
Expand Down
8 changes: 6 additions & 2 deletions controllers/testutils/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type NewDatadogAgentOptions struct {
ExtraLabels map[string]string
ExtraAnnotations map[string]string
ClusterAgentEnabled bool
ClusterAgentDisabled bool
OrchestratorExplorerDisabled bool
UseEDS bool
APIKey string
Expand Down Expand Up @@ -112,7 +112,7 @@ func NewDatadogAgent(ns, name, image string, options *NewDatadogAgentOptions) *d
}
}

if options.ClusterAgentEnabled {
if !options.ClusterAgentDisabled {
ad.Spec.ClusterAgent = datadoghqv1alpha1.DatadogAgentSpecClusterAgentSpec{
Config: &datadoghqv1alpha1.ClusterAgentConfig{},
Image: &datadoghqv1alpha1.ImageConfig{
Expand All @@ -121,6 +121,10 @@ func NewDatadogAgent(ns, name, image string, options *NewDatadogAgentOptions) *d
PullSecrets: &[]v1.LocalObjectReference{},
},
}
} else {
ad.Spec.ClusterAgent = datadoghqv1alpha1.DatadogAgentSpecClusterAgentSpec{
Enabled: datadoghqv1alpha1.NewBoolPointer(false),
}
}

ad.Spec.Agent.Config.VolumeMounts = options.VolumeMounts
Expand Down
20 changes: 20 additions & 0 deletions examples/datadogagent/datadog-agent-with-credential-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ----------------------------------------------------------#
# Example about how to use an existing secret to store the #
# Datadog credentials #
# ----------------------------------------------------------#
# First, create the datadog-secret with the folling command:
# kubectl create secret generic datadog-secret --from-literal api-key=$DD_API_KEY --from-literal app-key=$DD_APP_KEY

apiVersion: datadoghq.com/v1alpha1
kind: DatadogAgent
metadata:
name: datadog
spec:
clusterName: foo
credentials:
apiSecret:
secretName: datadog-secret
keyName: api-key
appSecret:
secretName: datadog-secret
keyName: app-key

0 comments on commit 11ad61d

Please sign in to comment.