Skip to content
Merged
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
23 changes: 17 additions & 6 deletions pkg/clusteragent/admission/common/label_selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type LabelSelectorsConfig struct {
// IncludeNamespaces lists namespaces to explicitly include (mutually exclusive with ExcludeNamespaces)
// If set, only these namespaces will be targeted
IncludeNamespaces []string

// ShouldAcceptAllFunc the condition to accept all pods except the ones explicitly
// excluded. If empty, defaultShouldAcceptAllFunc is used.
ShouldAcceptAllFunc func() bool
}

var defaultShouldAcceptAllFunc = func() bool {
return pkgconfigsetup.Datadog().GetBool("admission_controller.mutate_unlabelled")
}

// DefaultLabelSelectors returns namespace and object selectors for admission webhooks.
Expand All @@ -30,10 +38,10 @@ type LabelSelectorsConfig struct {
func DefaultLabelSelectors(useNamespaceSelector bool, config LabelSelectorsConfig) (nsSelector *metav1.LabelSelector, objSelector *metav1.LabelSelector) {
nsSelector = &metav1.LabelSelector{}
if useNamespaceSelector {
applyAdmissionEnabledSelectors(nsSelector)
applyAdmissionEnabledSelectors(nsSelector, config)
} else {
objSelector = &metav1.LabelSelector{}
applyAdmissionEnabledSelectors(objSelector)
applyAdmissionEnabledSelectors(objSelector, config)
}

applySelectorConfig(nsSelector, config)
Expand Down Expand Up @@ -67,10 +75,13 @@ func applySelectorConfig(nsSelector *metav1.LabelSelector, config LabelSelectors
}
}

func applyAdmissionEnabledSelectors(selector *metav1.LabelSelector) {
if pkgconfigsetup.Datadog().GetBool("admission_controller.mutate_unlabelled") ||
pkgconfigsetup.Datadog().GetBool("apm_config.instrumentation.enabled") ||
len(pkgconfigsetup.Datadog().GetStringSlice("apm_config.instrumentation.enabled_namespaces")) > 0 {
func applyAdmissionEnabledSelectors(selector *metav1.LabelSelector, config LabelSelectorsConfig) {
shouldAcceptAllFunc := config.ShouldAcceptAllFunc
if shouldAcceptAllFunc == nil {
shouldAcceptAllFunc = defaultShouldAcceptAllFunc
}

if shouldAcceptAllFunc() {
// Accept all, ignore pods explicitly filtered-out
selector.MatchExpressions = []metav1.LabelSelectorRequirement{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/clusteragent/admission/common"
mutatecommon "github.com/DataDog/datadog-agent/pkg/clusteragent/admission/mutate/common"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

Expand Down Expand Up @@ -111,6 +112,11 @@ func (w *Webhook) Operations() []admissionregistrationv1.OperationType {
func (w *Webhook) LabelSelectors(useNamespaceSelector bool) (*metav1.LabelSelector, *metav1.LabelSelector) {
return common.DefaultLabelSelectors(useNamespaceSelector, common.LabelSelectorsConfig{
ExcludeNamespaces: mutatecommon.DefaultDisabledNamespaces(),
ShouldAcceptAllFunc: func() bool {
return pkgconfigsetup.Datadog().GetBool("admission_controller.mutate_unlabelled") ||
pkgconfigsetup.Datadog().GetBool("apm_config.instrumentation.enabled") ||
len(pkgconfigsetup.Datadog().GetStringSlice("apm_config.instrumentation.enabled_namespaces")) > 0
},
})
}

Expand Down