Skip to content

Commit

Permalink
add watch env vars for dashboard and genericresource (#1649)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbavelier authored Feb 3, 2025
1 parent 0a7ed51 commit 8b4ebea
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 57 deletions.
12 changes: 7 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,13 @@ func run(opts *options) error {
RenewDeadline: &renewDeadline,
RetryPeriod: &retryPeriod,
Cache: config.CacheOptions(setupLog, config.WatchOptions{
DatadogAgentEnabled: opts.datadogAgentEnabled,
DatadogMonitorEnabled: opts.datadogMonitorEnabled,
DatadogSLOEnabled: opts.datadogSLOEnabled,
DatadogAgentProfileEnabled: opts.datadogAgentProfileEnabled,
IntrospectionEnabled: opts.introspectionEnabled,
DatadogAgentEnabled: opts.datadogAgentEnabled,
DatadogMonitorEnabled: opts.datadogMonitorEnabled,
DatadogSLOEnabled: opts.datadogSLOEnabled,
DatadogAgentProfileEnabled: opts.datadogAgentProfileEnabled,
IntrospectionEnabled: opts.introspectionEnabled,
DatadogDashboardEnabled: opts.datadogDashboardEnabled,
DatadogGenericResourceEnabled: opts.datadogGenericResourceEnabled,
}),
})
if err != nil {
Expand Down
50 changes: 37 additions & 13 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,40 @@ import (
const (
// AgentWatchNamespaceEnvVar is a comma-separated list of namespaces watched by the DatadogAgent controller.
agentWatchNamespaceEnvVar = "DD_AGENT_WATCH_NAMESPACE"
// SLOWatchNamespaceEnvVar is a comma-separated list of namespaces watched by the DatadogSLO controller.
sloWatchNamespaceEnvVar = "DD_SLO_WATCH_NAMESPACE"
// DashboardWatchNamespaceEnvVar is a comma-separated list of namespaces watched by the DatadogDashboard controller.
dashboardWatchNamespaceEnvVar = "DD_DASHBOARD_WATCH_NAMESPACE"
// GenericResourceWatchNamespaceEnvVar is a comma-separated list of namespaces watched by the DatadogGenericResource controller.
genericResourceWatchNamespaceEnvVar = "DD_GENERIC_RESOURCE_WATCH_NAMESPACE"
// MonitorWatchNamespaceEnvVar is a comma-separated list of namespaces watched by the DatadogMonitor controller.
monitorWatchNamespaceEnvVar = "DD_MONITOR_WATCH_NAMESPACE"
// ProfilesWatchNamespaceEnvVar is a comma-separated list of namespaces watched by the DatadogAgentProfile controller.
profileWatchNamespaceEnvVar = "DD_AGENT_PROFILE_WATCH_NAMESPACE"
// SLOWatchNamespaceEnvVar is a comma-separated list of namespaces watched by the DatadogSLO controller.
sloWatchNamespaceEnvVar = "DD_SLO_WATCH_NAMESPACE"
// WatchNamespaceEnvVar is a comma-separated list of namespaces watched by all controllers, unless a controller-specific configuration is provided.
// An empty value means the operator is running with cluster scope.
watchNamespaceEnvVar = "WATCH_NAMESPACE"
)

var (
agentObj = &datadoghqv2alpha1.DatadogAgent{}
monitorObj = &datadoghqv1alpha1.DatadogMonitor{}
sloObj = &datadoghqv1alpha1.DatadogSLO{}
profileObj = &datadoghqv1alpha1.DatadogAgentProfile{}
podObj = &corev1.Pod{}
nodeObj = &corev1.Node{}
agentObj = &datadoghqv2alpha1.DatadogAgent{}
dashboardObj = &datadoghqv1alpha1.DatadogDashboard{}
genericResourceObj = &datadoghqv1alpha1.DatadogGenericResource{}
monitorObj = &datadoghqv1alpha1.DatadogMonitor{}
sloObj = &datadoghqv1alpha1.DatadogSLO{}
profileObj = &datadoghqv1alpha1.DatadogAgentProfile{}
podObj = &corev1.Pod{}
nodeObj = &corev1.Node{}
)

type WatchOptions struct {
DatadogAgentEnabled bool
DatadogMonitorEnabled bool
DatadogSLOEnabled bool
DatadogAgentProfileEnabled bool
IntrospectionEnabled bool
DatadogAgentEnabled bool
DatadogMonitorEnabled bool
DatadogSLOEnabled bool
DatadogAgentProfileEnabled bool
IntrospectionEnabled bool
DatadogDashboardEnabled bool
DatadogGenericResourceEnabled bool
}

// CacheOptions function configures Controller Runtime cache options on a resource level (supported in v0.16+).
Expand All @@ -72,6 +80,22 @@ func CacheOptions(logger logr.Logger, opts WatchOptions) cache.Options {
}
}

if opts.DatadogDashboardEnabled {
dashboardNamespaces := getWatchNamespacesFromEnv(logger, dashboardWatchNamespaceEnvVar)
logger.Info("DatadogDashboard Enabled", "watching namespaces", maps.Keys(dashboardNamespaces))
byObject[dashboardObj] = cache.ByObject{
Namespaces: dashboardNamespaces,
}
}

if opts.DatadogGenericResourceEnabled {
genericResourceNamespaces := getWatchNamespacesFromEnv(logger, genericResourceWatchNamespaceEnvVar)
logger.Info("DatadogGenericResource Enabled", "watching namespaces", maps.Keys(genericResourceNamespaces))
byObject[genericResourceObj] = cache.ByObject{
Namespaces: genericResourceNamespaces,
}
}

if opts.DatadogMonitorEnabled {
monitorNamespaces := getWatchNamespacesFromEnv(logger, monitorWatchNamespaceEnvVar)
logger.Info("DatadogMonitor Enabled", "watching namespaces", maps.Keys(monitorNamespaces))
Expand Down
92 changes: 53 additions & 39 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,35 @@ func Test_CacheConfig(t *testing.T) {
{
name: "All envs non empty, all CRDs enabled",
watchOptions: WatchOptions{
DatadogAgentEnabled: true,
DatadogMonitorEnabled: true,
DatadogSLOEnabled: true,
DatadogAgentProfileEnabled: true,
DatadogAgentEnabled: true,
DatadogMonitorEnabled: true,
DatadogSLOEnabled: true,
DatadogAgentProfileEnabled: true,
DatadogDashboardEnabled: true,
DatadogGenericResourceEnabled: true,
},

envConfig: map[string]string{
watchNamespaceEnvVar: "datadog",
agentWatchNamespaceEnvVar: "agentNs",
monitorWatchNamespaceEnvVar: "monitorNs, monitorNs2",
sloWatchNamespaceEnvVar: " nsWithSpace ",
profileWatchNamespaceEnvVar: "profileNs",
watchNamespaceEnvVar: "datadog",
agentWatchNamespaceEnvVar: "agentNs",
monitorWatchNamespaceEnvVar: "monitorNs, monitorNs2",
sloWatchNamespaceEnvVar: " nsWithSpace ",
profileWatchNamespaceEnvVar: "profileNs",
dashboardWatchNamespaceEnvVar: "dashboardNs",
genericResourceWatchNamespaceEnvVar: "genericNs",
},

wantDefaultNamepsace: objectConfig{configured: true, namespaces: []string{"agentNs"}},

wantObjectConfig: map[client.Object]objectConfig{
agentObj: {configured: true, namespaces: []string{"agentNs"}},
monitorObj: {configured: true, namespaces: []string{"monitorNs", "monitorNs2"}},
sloObj: {configured: true, namespaces: []string{"nsWithSpace"}},
profileObj: {configured: true, namespaces: []string{"profileNs"}},
podObj: {configured: true, namespaces: []string{"agentNs"}},
nodeObj: {configured: true, namespaces: nil},
agentObj: {configured: true, namespaces: []string{"agentNs"}},
dashboardObj: {configured: true, namespaces: []string{"dashboardNs"}},
genericResourceObj: {configured: true, namespaces: []string{"genericNs"}},
monitorObj: {configured: true, namespaces: []string{"monitorNs", "monitorNs2"}},
sloObj: {configured: true, namespaces: []string{"nsWithSpace"}},
profileObj: {configured: true, namespaces: []string{"profileNs"}},
podObj: {configured: true, namespaces: []string{"agentNs"}},
nodeObj: {configured: true, namespaces: nil},
},
},
{
Expand All @@ -72,12 +78,14 @@ func Test_CacheConfig(t *testing.T) {

wantDefaultNamepsace: objectConfig{configured: true, namespaces: []string{"datadog"}},
wantObjectConfig: map[client.Object]objectConfig{
agentObj: {configured: true, namespaces: []string{"datadog"}},
monitorObj: {configured: false},
sloObj: {configured: false},
profileObj: {configured: true, namespaces: []string{"profileNs"}},
podObj: {configured: true, namespaces: []string{"datadog"}},
nodeObj: {configured: true, namespaces: nil},
agentObj: {configured: true, namespaces: []string{"datadog"}},
dashboardObj: {configured: false},
genericResourceObj: {configured: false},
monitorObj: {configured: false},
sloObj: {configured: false},
profileObj: {configured: true, namespaces: []string{"profileNs"}},
podObj: {configured: true, namespaces: []string{"datadog"}},
nodeObj: {configured: true, namespaces: nil},
},
},

Expand All @@ -98,12 +106,14 @@ func Test_CacheConfig(t *testing.T) {
// Expected
wantDefaultNamepsace: objectConfig{configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
wantObjectConfig: map[client.Object]objectConfig{
agentObj: {configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
monitorObj: {configured: false},
sloObj: {configured: false},
profileObj: {configured: true, namespaces: []string{"profileNs"}},
podObj: {configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
nodeObj: {configured: true, namespaces: nil},
agentObj: {configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
dashboardObj: {configured: false},
genericResourceObj: {configured: false},
monitorObj: {configured: false},
sloObj: {configured: false},
profileObj: {configured: true, namespaces: []string{"profileNs"}},
podObj: {configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
nodeObj: {configured: true, namespaces: nil},
},
},
{
Expand All @@ -123,12 +133,14 @@ func Test_CacheConfig(t *testing.T) {
// Expected
wantDefaultNamepsace: objectConfig{configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
wantObjectConfig: map[client.Object]objectConfig{
agentObj: {configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
monitorObj: {configured: true, namespaces: []string{"datadog"}},
sloObj: {configured: false},
profileObj: {configured: false},
podObj: {configured: false},
nodeObj: {configured: false},
agentObj: {configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
dashboardObj: {configured: false},
genericResourceObj: {configured: false},
monitorObj: {configured: true, namespaces: []string{"datadog"}},
sloObj: {configured: false},
profileObj: {configured: false},
podObj: {configured: false},
nodeObj: {configured: false},
},
},
{
Expand All @@ -149,12 +161,14 @@ func Test_CacheConfig(t *testing.T) {
// Expected
wantDefaultNamepsace: objectConfig{configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
wantObjectConfig: map[client.Object]objectConfig{
agentObj: {configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
monitorObj: {configured: false},
sloObj: {configured: false},
profileObj: {configured: false},
podObj: {configured: false},
nodeObj: {configured: true, namespaces: nil},
agentObj: {configured: true, namespaces: []string{"agentNs1", "agentNs2"}},
dashboardObj: {configured: false},
genericResourceObj: {configured: false},
monitorObj: {configured: false},
sloObj: {configured: false},
profileObj: {configured: false},
podObj: {configured: false},
nodeObj: {configured: true, namespaces: nil},
},
},
}
Expand Down

0 comments on commit 8b4ebea

Please sign in to comment.