Skip to content

Commit

Permalink
add DatadogAgent v2alpha1 defaulting code (#477)
Browse files Browse the repository at this point in the history
* add v2alpha1 defaulting

* change token name, add apiutils helpers
  • Loading branch information
celenechang committed May 10, 2022
1 parent bb0d4f1 commit 6fea46c
Show file tree
Hide file tree
Showing 11 changed files with 863 additions and 60 deletions.
6 changes: 3 additions & 3 deletions apis/datadoghq/v1alpha1/datadogagent_conversion_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func convertDatadogAgentSpec(src *DatadogAgentSpecAgentSpec, dst *v2alpha1.Datad
if src.Config.CollectEvents != nil {
features := getV2Features(dst)
if features.EventCollection == nil {
features.EventCollection = &v2alpha1.EventCollectionConfig{}
features.EventCollection = &v2alpha1.EventCollectionFeatureConfig{}
}

features.EventCollection.CollectKubernetesEvents = src.Config.CollectEvents
Expand Down Expand Up @@ -105,7 +105,7 @@ func convertDatadogAgentSpec(src *DatadogAgentSpecAgentSpec, dst *v2alpha1.Datad
if src.Config.Dogstatsd != nil {
features := getV2Features(dst)
if features.Dogstatsd == nil {
features.Dogstatsd = &v2alpha1.DogstatsdConfig{}
features.Dogstatsd = &v2alpha1.DogstatsdFeatureConfig{}
}

features.Dogstatsd.OriginDetectionEnabled = src.Config.Dogstatsd.DogstatsdOriginDetection
Expand Down Expand Up @@ -330,7 +330,7 @@ func convertSystemProbeSpec(src *SystemProbeSpec, dst *v2alpha1.DatadogAgent) {
// TODO: BPFDebugEnabled, DebugPort skipped as not sure if should be exposed.

setBooleanPtrOR(src.Enabled, &features.NPM.Enabled)
features.NPM.UseConntrack = src.ConntrackEnabled
features.NPM.EnableConntrack = src.ConntrackEnabled
features.NPM.CollectDNSStats = src.CollectDNSStats

if src.EnableTCPQueueLength != nil {
Expand Down
2 changes: 1 addition & 1 deletion apis/datadoghq/v1alpha1/datadogagent_conversion_dca.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func convertClusterAgentSpec(src *DatadogAgentSpecClusterAgentSpec, dst *v2alpha
if src.Config.CollectEvents != nil {
features := getV2Features(dst)
if features.EventCollection == nil {
features.EventCollection = &v2alpha1.EventCollectionConfig{}
features.EventCollection = &v2alpha1.EventCollectionFeatureConfig{}
}

setBooleanPtrOR(src.Config.CollectEvents, &features.EventCollection.CollectKubernetesEvents)
Expand Down
254 changes: 254 additions & 0 deletions apis/datadoghq/v2alpha1/datadogagent_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
// 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 v2alpha1

import (
apiutils "github.com/DataDog/datadog-operator/apis/utils"
)

// Default configuration values. These are the recommended settings for monitoring with Datadog in Kubernetes.
// Note: many default values are set in the Datadog Agent and deliberately not set by the Operator.
const (
defaultSite string = "datadoghq.com"
defaultRegistry string = "gcr.io/datadoghq"
defaultLogLevel string = "info"

// defaultLogCollectionEnabled bool = false
defaultLogContainerCollectUsingFiles bool = true
defaultLogContainerLogsPath string = "/var/lib/docker/containers"
defaultLogPodLogsPath string = "/var/log/pods"
defaultLogContainerSymlinksPath string = "/var/log/containers"
defaultLogTempStoragePath string = "/var/lib/datadog-agent/logs"

// defaultLiveProcessCollectionEnabled bool = false
defaultLiveContainerCollectionEnabled bool = true

// defaultOOMKillEnabled bool = false
// defaultTCPQueueLengthEnabled bool = false

// defaultAPMEnabled bool = false
defaultAPMHostPortEnabled bool = false
defaultAPMHostPort int32 = 8126
defaultAPMSocketEnabled bool = true
defaultAPMSocketPath string = "/var/run/datadog/apm.sock"

// defaultCSPMEnabled bool = false
// defaultCWSEnabled bool = false
defaultCWSSyscallMonitorEnabled bool = false

// defaultNPMEnabled bool = false
defaultNPMEnableConntrack bool = true
defaultNPMCollectDNSStats bool = true

// defaultUSMEnabled bool = false

defaultDogstatsdOriginDetectionEnabled bool = false
defaultDogstatsdHostPortEnabled bool = false
defaultDogstatsdPort int32 = 8125
defaultDogstatsdUseSocketVolume bool = false
defaultDogstatsdSocketPath string = "/var/run/datadog/dsd.socket"

defaultCollectKubernetesEvents bool = true

// defaultAdmissionControllerEnabled bool = false
defaultAdmissionControllerMutateUnlabelled bool = false

defaultOrchestratorExplorerEnabled bool = true
DefaultOrchestratorExplorerConf string = "orchestrator-explorer-config"

// defaultExternalMetricsServerEnabled bool = false
defaultDatadogMetricsEnabled bool = true
// Cluster Agent versions < 1.20 should use 443
defaultMetricsProviderPort int32 = 8443

defaultKubeStateMetricsCoreEnabled bool = true
defaultKubeStateMetricsCoreConf string = "kube-state-metrics-core-config"

defaultClusterChecksEnabled bool = true
defaultUseClusterChecksRunners bool = true

// defaultPrometheusScrapeEnabled bool = false
defaultPrometheusScrapeEnableServiceEndpoints bool = false

// defaultDatadogMonitorEnabled bool = false
)

// DefaultDatadogAgent defaults the DatadogAgentSpec GlobalConfig and Features.
func DefaultDatadogAgent(dda *DatadogAgent) {
defaultGlobalConfig(&dda.Spec)

defaultFeaturesConfig(&dda.Spec)
}

// defaultGlobalConfig sets default values in DatadogAgentSpec.Global.
func defaultGlobalConfig(ddaSpec *DatadogAgentSpec) {
if ddaSpec.Global == nil {
ddaSpec.Global = &GlobalConfig{}
}

if ddaSpec.Global.Site == nil {
ddaSpec.Global.Site = apiutils.NewStringPointer(defaultSite)
}

if ddaSpec.Global.Registry == nil {
ddaSpec.Global.Registry = apiutils.NewStringPointer(defaultRegistry)
}

if ddaSpec.Global.LogLevel == nil {
ddaSpec.Global.LogLevel = apiutils.NewStringPointer(defaultLogLevel)
}
}

// defaultFeaturesConfig sets default values in DatadogAgentSpec.Features.
// Note: many default values are set in the Datadog Agent code and are not set here.
func defaultFeaturesConfig(ddaSpec *DatadogAgentSpec) {
if ddaSpec.Features == nil {
ddaSpec.Features = &DatadogFeatures{}
}

// LogsCollection Feature
if ddaSpec.Features.LogCollection != nil && *ddaSpec.Features.LogCollection.Enabled {
apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.LogCollection.ContainerCollectUsingFiles, defaultLogContainerCollectUsingFiles)

apiutils.DefaultStringIfUnset(&ddaSpec.Features.LogCollection.ContainerLogsPath, defaultLogContainerLogsPath)

apiutils.DefaultStringIfUnset(&ddaSpec.Features.LogCollection.PodLogsPath, defaultLogPodLogsPath)

apiutils.DefaultStringIfUnset(&ddaSpec.Features.LogCollection.ContainerSymlinksPath, defaultLogContainerSymlinksPath)

apiutils.DefaultStringIfUnset(&ddaSpec.Features.LogCollection.TempStoragePath, defaultLogTempStoragePath)
}

// LiveContainerCollection Feature
if ddaSpec.Features.LiveContainerCollection == nil {
ddaSpec.Features.LiveContainerCollection = &LiveContainerCollectionFeatureConfig{
Enabled: apiutils.NewBoolPointer(defaultLiveContainerCollectionEnabled),
}
}

// APM Feature
if ddaSpec.Features.APM != nil && *ddaSpec.Features.APM.Enabled {
if ddaSpec.Features.APM.HostPortConfig == nil {
ddaSpec.Features.APM.HostPortConfig = &HostPortConfig{}
}

apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.APM.HostPortConfig.Enabled, defaultAPMHostPortEnabled)

apiutils.DefaultInt32IfUnset(&ddaSpec.Features.APM.HostPortConfig.Port, defaultAPMHostPort)

if ddaSpec.Features.APM.UnixDomainSocketConfig == nil {
ddaSpec.Features.APM.UnixDomainSocketConfig = &UnixDomainSocketConfig{}
}

apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.APM.UnixDomainSocketConfig.Enabled, defaultAPMSocketEnabled)

apiutils.DefaultStringIfUnset(&ddaSpec.Features.APM.UnixDomainSocketConfig.Path, defaultAPMSocketPath)
}

// CWS (Cloud Workload Security) Feature
if ddaSpec.Features.CWS != nil && *ddaSpec.Features.CWS.Enabled {
apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.CWS.SyscallMonitorEnabled, defaultCWSSyscallMonitorEnabled)
}

// NPM (Network Performance Monitoring) Feature
if ddaSpec.Features.NPM != nil && *ddaSpec.Features.NPM.Enabled {
apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.NPM.EnableConntrack, defaultNPMEnableConntrack)

apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.NPM.CollectDNSStats, defaultNPMCollectDNSStats)
}

// Dogstatd Feature
if ddaSpec.Features.Dogstatsd == nil {
ddaSpec.Features.Dogstatsd = &DogstatsdFeatureConfig{}
}

apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.Dogstatsd.OriginDetectionEnabled, defaultDogstatsdOriginDetectionEnabled)

if ddaSpec.Features.Dogstatsd.HostPortConfig == nil {
ddaSpec.Features.Dogstatsd.HostPortConfig = &HostPortConfig{
Enabled: apiutils.NewBoolPointer(defaultDogstatsdHostPortEnabled),
}
}

if *ddaSpec.Features.Dogstatsd.HostPortConfig.Enabled {
apiutils.DefaultInt32IfUnset(&ddaSpec.Features.Dogstatsd.HostPortConfig.Port, defaultDogstatsdPort)
}

if ddaSpec.Features.Dogstatsd.UnixDomainSocketConfig == nil {
ddaSpec.Features.Dogstatsd.UnixDomainSocketConfig = &UnixDomainSocketConfig{}
}

apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.Dogstatsd.UnixDomainSocketConfig.Enabled, defaultDogstatsdUseSocketVolume)

apiutils.DefaultStringIfUnset(&ddaSpec.Features.Dogstatsd.UnixDomainSocketConfig.Path, defaultDogstatsdSocketPath)

// Cluster-level features

// EventCollection Feature
if ddaSpec.Features.EventCollection == nil {
ddaSpec.Features.EventCollection = &EventCollectionFeatureConfig{
CollectKubernetesEvents: apiutils.NewBoolPointer(defaultCollectKubernetesEvents),
}
}

// OrchestratorExplorer check Feature
if ddaSpec.Features.OrchestratorExplorer == nil {
ddaSpec.Features.OrchestratorExplorer = &OrchestratorExplorerFeatureConfig{
Enabled: apiutils.NewBoolPointer(defaultOrchestratorExplorerEnabled),
}
}

if *ddaSpec.Features.OrchestratorExplorer.Enabled {
if ddaSpec.Features.OrchestratorExplorer.Conf == nil {
ddaSpec.Features.OrchestratorExplorer.Conf = &CustomConfig{
ConfigData: apiutils.NewStringPointer(DefaultOrchestratorExplorerConf),
}
}
}

// KubeStateMetricsCore check Feature
if ddaSpec.Features.KubeStateMetricsCore == nil {
ddaSpec.Features.KubeStateMetricsCore = &KubeStateMetricsCoreFeatureConfig{
Enabled: apiutils.NewBoolPointer(defaultKubeStateMetricsCoreEnabled),
}
}

if *ddaSpec.Features.KubeStateMetricsCore.Enabled {
if ddaSpec.Features.KubeStateMetricsCore.Conf == nil {
ddaSpec.Features.KubeStateMetricsCore.Conf = &CustomConfig{
ConfigData: apiutils.NewStringPointer(defaultKubeStateMetricsCoreConf),
}
}
}

// AdmissionController Feature
if ddaSpec.Features.AdmissionController != nil && *ddaSpec.Features.AdmissionController.Enabled {
apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.AdmissionController.MutateUnlabelled, defaultAdmissionControllerMutateUnlabelled)
}

// ExternalMetricsServer Feature
if ddaSpec.Features.ExternalMetricsServer != nil && *ddaSpec.Features.ExternalMetricsServer.Enabled {
apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.ExternalMetricsServer.UseDatadogMetrics, defaultDatadogMetricsEnabled)

apiutils.DefaultInt32IfUnset(&ddaSpec.Features.ExternalMetricsServer.Port, defaultMetricsProviderPort)
}

// ClusterChecks Feature
if ddaSpec.Features.ClusterChecks == nil {
ddaSpec.Features.ClusterChecks = &ClusterChecksFeatureConfig{
Enabled: apiutils.NewBoolPointer(defaultClusterChecksEnabled),
}
}

if *ddaSpec.Features.ClusterChecks.Enabled {
apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.ClusterChecks.UseClusterChecksRunners, defaultUseClusterChecksRunners)
}

// PrometheusScrape Feature
if ddaSpec.Features.PrometheusScrape != nil && *ddaSpec.Features.PrometheusScrape.Enabled {
apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.PrometheusScrape.EnableServiceEndpoints, defaultPrometheusScrapeEnableServiceEndpoints)
}
}

0 comments on commit 6fea46c

Please sign in to comment.