Skip to content

Commit

Permalink
[CONTP-266] Add namespaceAnnotationsAsTags to operator (#1253)
Browse files Browse the repository at this point in the history
* add namespaceAnnotationsAsTags

* Update docs/configuration.v2alpha1.md

Co-authored-by: Bryce Eadie <[email protected]>

* remove v1alpha and bundle/manifests

---------

Co-authored-by: Bryce Eadie <[email protected]>
  • Loading branch information
zhuminyi and buraizu authored Jul 11, 2024
1 parent a559a62 commit d0f739b
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 16 deletions.
1 change: 1 addition & 0 deletions apis/datadoghq/common/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const (
DDLogsContainerCollectUsingFiles = "DD_LOGS_CONFIG_K8S_CONTAINER_USE_FILE"
DDLogsEnabled = "DD_LOGS_ENABLED"
DDNamespaceLabelsAsTags = "DD_KUBERNETES_NAMESPACE_LABELS_AS_TAGS"
DDNamespaceAnnotationsAsTags = "DD_KUBERNETES_NAMESPACE_ANNOTATIONS_AS_TAGS"
DDNodeLabelsAsTags = "DD_KUBERNETES_NODE_LABELS_AS_TAGS"
DDOrchestratorExplorerEnabled = "DD_ORCHESTRATOR_EXPLORER_ENABLED"
DDOrchestratorExplorerExtraTags = "DD_ORCHESTRATOR_EXPLORER_EXTRA_TAGS"
Expand Down
5 changes: 5 additions & 0 deletions apis/datadoghq/v1alpha1/datadogagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,11 @@ type NodeAgentConfig struct {
// +optional
NamespaceLabelsAsTags map[string]string `json:"namespaceLabelsAsTags,omitempty"`

// Provide a mapping of Kubernetes Namespace Annotations to Datadog Tags.
// <KUBERNETES_LABEL>: <DATADOG_TAG_KEY>
// +optional
NamespaceAnnotationsAsTags map[string]string `json:"namespaceAnnotationsAsTags,omitempty"`

// Provide a mapping of Kubernetes Node Labels to Datadog Tags.
// <KUBERNETES_LABEL>: <DATADOG_TAG_KEY>
// +optional
Expand Down
7 changes: 7 additions & 0 deletions apis/datadoghq/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions apis/datadoghq/v1alpha1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions apis/datadoghq/v2alpha1/datadogagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,11 @@ type GlobalConfig struct {
// +optional
NamespaceLabelsAsTags map[string]string `json:"namespaceLabelsAsTags,omitempty"`

// Provide a mapping of Kubernetes Namespace Annotations to Datadog Tags.
// <KUBERNETES_LABEL>: <DATADOG_TAG_KEY>
// +optional
NamespaceAnnotationsAsTags map[string]string `json:"namespaceAnnotationsAsTags,omitempty"`

// NetworkPolicy contains the network configuration.
// +optional
NetworkPolicy *NetworkPolicyConfig `json:"networkPolicy,omitempty"`
Expand Down
7 changes: 7 additions & 0 deletions apis/datadoghq/v2alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions config/crd/bases/v1/datadoghq.com_datadogagents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,13 @@ spec:
Valid log levels are: trace, debug, info, warn, error, critical, and off.
Default: 'info'
type: string
namespaceAnnotationsAsTags:
additionalProperties:
type: string
description: |-
Provide a mapping of Kubernetes Namespace Annotations to Datadog Tags.
<KUBERNETES_LABEL>: <DATADOG_TAG_KEY>
type: object
namespaceLabelsAsTags:
additionalProperties:
type: string
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/v1beta1/datadoghq.com_datadogagents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,11 @@ spec:
type: string
description: 'Provide a mapping of Kubernetes Namespace Labels to Datadog Tags. <KUBERNETES_NAMESPACE_LABEL>: <DATADOG_TAG_KEY>'
type: object
namespaceAnnotationsAsTags:
additionalProperties:
type: string
description: 'Provide a mapping of Kubernetes Namespace Annotations to Datadog Tags. <KUBERNETES_NAMESPACE_ANNOTATIONS>: <DATADOG_TAG_KEY>'
type: object
networkPolicy:
description: NetworkPolicy contains the network configuration.
properties:
Expand Down
7 changes: 6 additions & 1 deletion controllers/datadogagent/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,9 @@ func TestReconcileDatadogAgent_Reconcile(t *testing.T) {
NamespaceLabelsAsTags: map[string]string{
"label": "test",
},
NamespaceAnnotationsAsTags: map[string]string{
"annotation": "test",
},
CollectEvents: apiutils.NewBoolPointer(true),
LeaderElection: apiutils.NewBoolPointer(true),
},
Expand Down Expand Up @@ -600,7 +603,9 @@ func TestReconcileDatadogAgent_Reconcile(t *testing.T) {
if !containsVolumeMounts(agentContainer.VolumeMounts, "volumeMount", "my/test/path") {
return errors.New("volumeMount hasn't been set correctly")
}

if !containsEnv(agentContainer.Env, "DD_KUBERNETES_NAMESPACE_ANNOTATIONS_AS_TAGS", "{\"annotation\":\"test\"}") {
return errors.New("DD_KUBERNETES_NAMESPACE_ANNOTATIONS_AS_TAGS hasn't been set correctly")
}
return nil
},
},
Expand Down
13 changes: 13 additions & 0 deletions controllers/datadogagent/override/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ func applyGlobalSettings(logger logr.Logger, manager feature.PodTemplateManagers
}
}

// Provide a mapping of Kubernetes Namespace Annotations to Datadog Tags.
if config.NamespaceAnnotationsAsTags != nil {
namespaceAnnotationsAsTags, err := json.Marshal(config.NamespaceAnnotationsAsTags)
if err != nil {
logger.Error(err, "Failed to unmarshal json input")
} else {
manager.EnvVar().AddEnvVar(&corev1.EnvVar{
Name: apicommon.DDNamespaceAnnotationsAsTags,
Value: string(namespaceAnnotationsAsTags),
})
}
}

if componentName == v2alpha1.NodeAgentComponentName {
// Kubelet contains the kubelet configuration parameters.
// The environment variable `DD_KUBERNETES_KUBELET_HOST` defaults to `status.hostIP` if not overriden.
Expand Down
10 changes: 10 additions & 0 deletions controllers/datadogagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,16 @@ func getEnvVarsForMetadataAsTags(agentConfig *datadoghqv1alpha1.NodeAgentConfig)
})
}

if agentConfig.NamespaceAnnotationsAsTags != nil {
namespaceAnnotationsAsTags, err := json.Marshal(agentConfig.NamespaceAnnotationsAsTags)
if err != nil {
return nil, err
}
envVars = append(envVars, corev1.EnvVar{
Name: apicommon.DDNamespaceAnnotationsAsTags,
Value: string(namespaceAnnotationsAsTags),
})
}
return envVars, nil
}

Expand Down
26 changes: 18 additions & 8 deletions controllers/datadogagent/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,11 @@ func Test_getEnvVarsForMetadataAsTags(t *testing.T) {
{
name: "Single mapping",
config: datadoghqv1alpha1.NodeAgentConfig{
NamespaceLabelsAsTags: singleMapping,
NodeLabelsAsTags: singleMapping,
PodLabelsAsTags: singleMapping,
PodAnnotationsAsTags: singleMapping,
NamespaceLabelsAsTags: singleMapping,
NamespaceAnnotationsAsTags: singleMapping,
NodeLabelsAsTags: singleMapping,
PodLabelsAsTags: singleMapping,
PodAnnotationsAsTags: singleMapping,
},
want: []v1.EnvVar{
{
Expand All @@ -473,15 +474,20 @@ func Test_getEnvVarsForMetadataAsTags(t *testing.T) {
Name: apicommon.DDNamespaceLabelsAsTags,
Value: singleMappingString,
},
{
Name: apicommon.DDNamespaceAnnotationsAsTags,
Value: singleMappingString,
},
},
},
{
name: "Multiple mappings",
config: datadoghqv1alpha1.NodeAgentConfig{
NamespaceLabelsAsTags: multipleMapping,
NodeLabelsAsTags: multipleMapping,
PodLabelsAsTags: multipleMapping,
PodAnnotationsAsTags: multipleMapping,
NamespaceLabelsAsTags: multipleMapping,
NamespaceAnnotationsAsTags: multipleMapping,
NodeLabelsAsTags: multipleMapping,
PodLabelsAsTags: multipleMapping,
PodAnnotationsAsTags: multipleMapping,
},
want: []v1.EnvVar{
{
Expand All @@ -500,6 +506,10 @@ func Test_getEnvVarsForMetadataAsTags(t *testing.T) {
Name: apicommon.DDNamespaceLabelsAsTags,
Value: multipleMappingString,
},
{
Name: apicommon.DDNamespaceAnnotationsAsTags,
Value: multipleMappingString,
},
},
},
}
Expand Down
15 changes: 8 additions & 7 deletions controllers/testutils/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,14 @@ func NewDatadogAgentWithGlobalConfigSettings(namespace string, name string) v2al
AppKey: apiutils.NewStringPointer("my-app-key"),
},
},
Registry: apiutils.NewStringPointer("my-custom-registry"),
LogLevel: apiutils.NewStringPointer("INFO"),
Tags: []string{"tagA:valA", "tagB:valB"},
PodLabelsAsTags: map[string]string{"some-label": "some-tag"},
PodAnnotationsAsTags: map[string]string{"some-annotation": "some-tag"},
NodeLabelsAsTags: map[string]string{"some-label": "some-tag"},
NamespaceLabelsAsTags: map[string]string{"some-label": "some-tag"},
Registry: apiutils.NewStringPointer("my-custom-registry"),
LogLevel: apiutils.NewStringPointer("INFO"),
Tags: []string{"tagA:valA", "tagB:valB"},
PodLabelsAsTags: map[string]string{"some-label": "some-tag"},
PodAnnotationsAsTags: map[string]string{"some-annotation": "some-tag"},
NodeLabelsAsTags: map[string]string{"some-label": "some-tag"},
NamespaceLabelsAsTags: map[string]string{"some-label": "some-tag"},
NamespaceAnnotationsAsTags: map[string]string{"some-annotation": "some-tag"},
NetworkPolicy: &v2alpha1.NetworkPolicyConfig{
Create: apiutils.NewBoolPointer(true),
Flavor: v2alpha1.NetworkPolicyFlavorKubernetes,
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.v2alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ spec:
| global.localService.forceEnableLocalService | ForceEnableLocalService forces the creation of the internal traffic policy service to target the agent running on the local node. This parameter only applies to Kubernetes 1.21, where the feature is in alpha and is disabled by default. (On Kubernetes 1.22+, the feature entered beta and the internal traffic service is created by default, so this parameter is ignored.) Default: false |
| global.localService.nameOverride | NameOverride defines the name of the internal traffic service to target the agent running on the local node. |
| global.logLevel | LogLevel sets logging verbosity. This can be overridden by container. Valid log levels are: trace, debug, info, warn, error, critical, and off. Default: 'info' |
| global.namespaceAnnotationsAsTags | Provide a mapping of Kubernetes Namespace Annotations to Datadog Tags. <KUBERNETES_LABEL>: <DATADOG_TAG_KEY> |
| global.namespaceLabelsAsTags | Provide a mapping of Kubernetes Namespace Labels to Datadog Tags. <KUBERNETES_NAMESPACE_LABEL>: <DATADOG_TAG_KEY> |
| global.networkPolicy.create | Create defines whether to create a NetworkPolicy for the current deployment. |
| global.networkPolicy.dnsSelectorEndpoints | DNSSelectorEndpoints defines the cilium selector of the DNS server entity. |
Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
Expand Down

0 comments on commit d0f739b

Please sign in to comment.