Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions comp/core/workloadfilter/catalog/filter_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"fmt"
"strings"

"gopkg.in/yaml.v2"

"github.com/DataDog/datadog-agent/comp/core/config"
workloadfilter "github.com/DataDog/datadog-agent/comp/core/workloadfilter/def"
"github.com/DataDog/datadog-agent/comp/core/workloadfilter/impl/parse"
Expand Down Expand Up @@ -131,19 +129,7 @@ func loadCELConfig(cfg config.Component) ([]workloadfilter.RuleBundle, error) {
var celConfig []workloadfilter.RuleBundle

// First try the standard UnmarshalKey method (input defined in datadog.yaml)
err := structure.UnmarshalKey(cfg, "cel_workload_exclude", &celConfig)
if err == nil {
return celConfig, nil
}

// Fallback: try to get raw value and unmarshal manually
rawValue := cfg.GetString("cel_workload_exclude")
if rawValue == "" {
return nil, nil
}

// handles both yaml and json input
err = yaml.Unmarshal([]byte(rawValue), &celConfig)
err := structure.UnmarshalKey(cfg, "cel_workload_exclude", &celConfig, structure.EnableStringUnmarshal)
if err == nil {
return celConfig, nil
}
Expand Down
41 changes: 8 additions & 33 deletions comp/core/workloadfilter/catalog/filter_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
)

func TestNewFilterConfig_CELFallback(t *testing.T) {
t.Run("successful unmarshal - no fallback needed", func(t *testing.T) {
// When configuration option is defined through datadog.yaml,
// the config component loads the value as an object.
t.Run("unmarshal cel workload exclude object", func(t *testing.T) {

mockConfig := configmock.New(t)

Expand All @@ -40,36 +42,9 @@ func TestNewFilterConfig_CELFallback(t *testing.T) {
assert.Contains(t, filterConfig.CELProductRules, workloadfilter.ProductMetrics)
})

t.Run("unmarshal fails but YAML string fallback succeeds", func(t *testing.T) {
mockConfig := configmock.New(t)

// Set up YAML string like what would come from configuration files
yamlConfig := `- products:
- metrics
rules:
kube_services:
- service.name == 'yaml-service'
pods:
- pod.namespace == 'yaml-ns'
containers:
- container.name == 'yaml-test'`
mockConfig.SetWithoutSource("cel_workload_exclude", yamlConfig)

filterConfig, err := NewFilterConfig(mockConfig)
require.NoError(t, err)
assert.NotNil(t, filterConfig)
assert.NotNil(t, filterConfig.CELProductRules)
assert.Contains(t, filterConfig.CELProductRules, workloadfilter.ProductMetrics)

containerRules := filterConfig.GetCELRulesForProduct(workloadfilter.ProductMetrics, workloadfilter.ContainerType)
assert.Equal(t, "container.name == 'yaml-test'", containerRules)
podRules := filterConfig.GetCELRulesForProduct(workloadfilter.ProductMetrics, workloadfilter.PodType)
assert.Equal(t, "pod.namespace == 'yaml-ns'", podRules)
serviceRules := filterConfig.GetCELRulesForProduct(workloadfilter.ProductMetrics, workloadfilter.ServiceType)
assert.Equal(t, "service.name == 'yaml-service'", serviceRules)
})

t.Run("unmarshal fails but JSON string fallback succeeds", func(t *testing.T) {
// When configuration option is defined through envvar,
// the config component loads the value as a string.
t.Run("unmarshal cel workload exclude JSON string", func(t *testing.T) {
mockConfig := configmock.New(t)
jsonConfig := `[
{
Expand Down Expand Up @@ -97,7 +72,7 @@ func TestNewFilterConfig_CELFallback(t *testing.T) {
assert.Equal(t, "service.name == 'test-service'", serviceRules)
})

t.Run("both unmarshal and fallback fail", func(t *testing.T) {
t.Run("unmarshal cel workload exclude invalid string", func(t *testing.T) {
mockConfig := configmock.New(t)
mockConfig.SetWithoutSource("cel_workload_exclude", "invalid string data")

Expand All @@ -106,7 +81,7 @@ func TestNewFilterConfig_CELFallback(t *testing.T) {
assert.Nil(t, filterConfig)
})

t.Run("no cel_workload_exclude config", func(t *testing.T) {
t.Run("unmarshal cel workload exclude empty config", func(t *testing.T) {
mockConfig := configmock.New(t)

filterConfig, err := NewFilterConfig(mockConfig)
Expand Down