From c4a12d9149a808c9ead3ea233f6fba5815f0a50e Mon Sep 17 00:00:00 2001 From: Gabriel Dos Santos Date: Thu, 9 Jan 2025 22:15:32 +0000 Subject: [PATCH 1/3] Base impl static tag migration --- .../tagger/collectors/workloadmeta_main.go | 39 ++------ comp/core/tagger/common/static_tags.go | 92 +++++++++++++++++++ comp/core/tagger/common/static_tags_test.go | 89 ++++++++++++++++++ comp/core/tagger/impl/local_tagger.go | 2 +- comp/core/tagger/impl/tagger.go | 2 +- comp/core/tagger/mock/fake_tagger.go | 26 ++++-- comp/dogstatsd/server/server.go | 15 ++- comp/dogstatsd/server/server_util_test.go | 9 +- comp/dogstatsd/server/serverless.go | 5 +- comp/otelcol/otlp/collector.go | 2 +- comp/otelcol/otlp/config.go | 9 +- .../kubernetesapiserver/events_common_test.go | 2 +- pkg/serverless/metrics/metric.go | 8 +- pkg/serverless/metrics/metric_test.go | 6 +- 14 files changed, 247 insertions(+), 59 deletions(-) create mode 100644 comp/core/tagger/common/static_tags.go create mode 100644 comp/core/tagger/common/static_tags_test.go diff --git a/comp/core/tagger/collectors/workloadmeta_main.go b/comp/core/tagger/collectors/workloadmeta_main.go index 527b35b0ece0f..d5b012ea8f636 100644 --- a/comp/core/tagger/collectors/workloadmeta_main.go +++ b/comp/core/tagger/collectors/workloadmeta_main.go @@ -12,16 +12,14 @@ import ( "github.com/gobwas/glob" "github.com/DataDog/datadog-agent/comp/core/config" + "github.com/DataDog/datadog-agent/comp/core/tagger/common" k8smetadata "github.com/DataDog/datadog-agent/comp/core/tagger/k8s_metadata" "github.com/DataDog/datadog-agent/comp/core/tagger/taglist" "github.com/DataDog/datadog-agent/comp/core/tagger/types" workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def" configutils "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/status/health" - "github.com/DataDog/datadog-agent/pkg/util/flavor" - "github.com/DataDog/datadog-agent/pkg/util/kubernetes/clustername" "github.com/DataDog/datadog-agent/pkg/util/log" - tagutil "github.com/DataDog/datadog-agent/pkg/util/tags" ) const ( @@ -90,37 +88,17 @@ func (c *WorkloadMetaCollector) initK8sResourcesMetaAsTags(resourcesLabelsAsTags // Run runs the continuous event watching loop and sends new tags to the // tagger based on the events sent by the workloadmeta. -func (c *WorkloadMetaCollector) Run(ctx context.Context, datadogConfig config.Component) { - c.collectStaticGlobalTags(ctx, datadogConfig) +func (c *WorkloadMetaCollector) Run(ctx context.Context) { c.stream(ctx) } func (c *WorkloadMetaCollector) collectStaticGlobalTags(ctx context.Context, datadogConfig config.Component) { - c.staticTags = tagutil.GetStaticTags(ctx, datadogConfig) - if _, exists := c.staticTags[clusterTagNamePrefix]; flavor.GetFlavor() == flavor.ClusterAgent && !exists { - // If we are running the cluster agent, we want to set the kube_cluster_name tag as a global tag if we are able - // to read it, for the instances where we are running in an environment where hostname cannot be detected. - if cluster := clustername.GetClusterNameTagValue(ctx, ""); cluster != "" { - if c.staticTags == nil { - c.staticTags = make(map[string][]string, 1) - } - if _, exists := c.staticTags[clusterTagNamePrefix]; !exists { - c.staticTags[clusterTagNamePrefix] = []string{} - } - c.staticTags[clusterTagNamePrefix] = append(c.staticTags[clusterTagNamePrefix], cluster) - } - } - // These are the global tags that should only be applied to the internal global entity on DCA. - // Whereas the static tags are applied to containers and pods directly as well. - globalEnvTags := tagutil.GetGlobalEnvTags(datadogConfig) + c.staticTags = common.GetStaticTags(ctx, datadogConfig) tagList := taglist.NewTagList() - - for _, tags := range []map[string][]string{c.staticTags, globalEnvTags} { - for tagKey, valueList := range tags { - for _, value := range valueList { - tagList.AddLow(tagKey, value) - } + for tagKey, valueList := range c.staticTags { + for _, value := range valueList { + tagList.AddLow(tagKey, value) } } @@ -172,7 +150,7 @@ func (c *WorkloadMetaCollector) stream(ctx context.Context) { } // NewWorkloadMetaCollector returns a new WorkloadMetaCollector. -func NewWorkloadMetaCollector(_ context.Context, cfg config.Component, store workloadmeta.Component, p processor) *WorkloadMetaCollector { +func NewWorkloadMetaCollector(ctx context.Context, cfg config.Component, store workloadmeta.Component, p processor) *WorkloadMetaCollector { c := &WorkloadMetaCollector{ tagProcessor: p, store: store, @@ -196,6 +174,9 @@ func NewWorkloadMetaCollector(_ context.Context, cfg config.Component, store wor metadataAsTags := configutils.GetMetadataAsTags(cfg) c.initK8sResourcesMetaAsTags(metadataAsTags.GetResourcesLabelsAsTags(), metadataAsTags.GetResourcesAnnotationsAsTags()) + // initializes with the static global tags + c.collectStaticGlobalTags(ctx, cfg) + return c } diff --git a/comp/core/tagger/common/static_tags.go b/comp/core/tagger/common/static_tags.go new file mode 100644 index 0000000000000..bdb6fe19cc372 --- /dev/null +++ b/comp/core/tagger/common/static_tags.go @@ -0,0 +1,92 @@ +// 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 2024-present Datadog, Inc. + +// Package common provides common utilities that are useful when interacting with the tagger. +package common + +import ( + "context" + "strings" + + "github.com/DataDog/datadog-agent/comp/core/config" + "github.com/DataDog/datadog-agent/pkg/config/env" + configUtils "github.com/DataDog/datadog-agent/pkg/config/utils" + "github.com/DataDog/datadog-agent/pkg/util/fargate" + "github.com/DataDog/datadog-agent/pkg/util/flavor" + "github.com/DataDog/datadog-agent/pkg/util/kubernetes/clustername" + "github.com/DataDog/datadog-agent/pkg/util/log" +) + +// GetStaticTags gets the "static tags" for this agent. These are tags +// that are attached directly to everything the agent produces, but _not_ +// included in host tags. In environments with no host metadata (such as where +// the hostname is empty), tags that would otherwise be included in host +// metadata are generated by this function. +func GetStaticTags(ctx context.Context, datadogConfig config.Reader) map[string][]string { + tags := []string{} + + if flavor.GetFlavor() == flavor.ClusterAgent { + // DD_CLUSTER_CHECKS_EXTRA_TAGS / DD_ORCHESTRATOR_EXPLORER_EXTRA_TAGS + tags = append(tags, configUtils.GetConfiguredDCATags(datadogConfig)...) + } + + if flavor.GetFlavor() == flavor.ClusterAgent || fargate.IsFargateInstance() { + // Fargate (ECS/EKS) + Cluster Agent does not have host tag resolution so + // we must manually add the following tags that are typically host tags. + + // DD_TAGS / DD_EXTRA_TAGS + tags = append(tags, configUtils.GetConfiguredTags(datadogConfig, false)...) + + // kube_cluster_name + clusterTagNamePrefix := "kube_cluster_name:" + var tag string + var found bool + for _, tag = range tags { + if strings.HasPrefix(tag, clusterTagNamePrefix) { + found = true + break + } + } + if found { + log.Infof("'%s' was set manually via DD_TAGS, not changing it", clusterTagNamePrefix+tag) + } else { + cluster := clustername.GetClusterNameTagValue(ctx, "") + if cluster == "" { + log.Infof("Couldn't build the %q.. tag, DD_CLUSTER_NAME can be used to set it", clusterTagNamePrefix) + } else { + tags = append(tags, clusterTagNamePrefix+cluster) + } + } + } + + // EKS Fargate specific tags + if env.IsFeaturePresent(env.EKSFargate) { + // eks_fargate_node + node, err := fargate.GetEKSFargateNodename() + if err != nil { + log.Infof("Couldn't build the 'eks_fargate_node' tag: %v", err) + } else { + tags = append(tags, "eks_fargate_node:"+node) + } + } + + return ConvertTagSliceToMap(tags) +} + +// ConvertTagSliceToMap converts a slice of tags to a map of tags +// eg. ["key1:value1", "key2:value2", "key1:value3"] -> {"key1": ["value1", "value3"], "key2": ["value2"]} +func ConvertTagSliceToMap(tags []string) map[string][]string { + tagsMap := make(map[string][]string) + for _, tag := range tags { + parts := strings.SplitN(tag, ":", 2) + if len(parts) != 2 { + continue + } + key := parts[0] + value := parts[1] + tagsMap[key] = append(tagsMap[key], value) + } + return tagsMap +} diff --git a/comp/core/tagger/common/static_tags_test.go b/comp/core/tagger/common/static_tags_test.go new file mode 100644 index 0000000000000..261b6b8fdb43b --- /dev/null +++ b/comp/core/tagger/common/static_tags_test.go @@ -0,0 +1,89 @@ +// 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 2022-present Datadog, Inc. + +package common + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/DataDog/datadog-agent/pkg/config/env" + configmock "github.com/DataDog/datadog-agent/pkg/config/mock" + "github.com/DataDog/datadog-agent/pkg/util/flavor" +) + +func TestStaticTags(t *testing.T) { + mockConfig := configmock.New(t) + mockConfig.SetWithoutSource("kubernetes_kubelet_nodename", "eksnode") + defer mockConfig.SetWithoutSource("kubernetes_kubelet_nodename", "") + + env.SetFeatures(t, env.EKSFargate) + + t.Run("just tags", func(t *testing.T) { + mockConfig.SetWithoutSource("tags", []string{"some:tag", "another:tag", "nocolon"}) + defer mockConfig.SetWithoutSource("tags", []string{}) + staticTags := GetStaticTags(context.Background(), mockConfig) + assert.Equal(t, map[string][]string{ + "some": {"tag"}, + "another": {"tag"}, + "eks_fargate_node": {"eksnode"}, + }, staticTags) + }) + + t.Run("tags and extra_tags", func(t *testing.T) { + mockConfig.SetWithoutSource("tags", []string{"some:tag", "nocolon"}) + mockConfig.SetWithoutSource("extra_tags", []string{"extra:tag", "missingcolon"}) + defer mockConfig.SetWithoutSource("tags", []string{}) + defer mockConfig.SetWithoutSource("extra_tags", []string{}) + staticTags := GetStaticTags(context.Background(), mockConfig) + assert.Equal(t, map[string][]string{ + "some": {"tag"}, + "extra": {"tag"}, + "eks_fargate_node": {"eksnode"}, + }, staticTags) + }) + + t.Run("cluster name already set", func(t *testing.T) { + mockConfig.SetWithoutSource("tags", []string{"kube_cluster_name:foo"}) + defer mockConfig.SetWithoutSource("tags", []string{}) + staticTags := GetStaticTags(context.Background(), mockConfig) + assert.Equal(t, map[string][]string{ + "eks_fargate_node": {"eksnode"}, + "kube_cluster_name": {"foo"}, + }, staticTags) + }) +} + +func TestExtraGlobalEnvTags(t *testing.T) { + mockConfig := configmock.New(t) + mockConfig.SetWithoutSource("tags", []string{"some:tag", "nocolon"}) + mockConfig.SetWithoutSource("extra_tags", []string{"extra:tag", "missingcolon"}) + mockConfig.SetWithoutSource("cluster_checks.extra_tags", []string{"cluster:tag", "nocolon"}) + mockConfig.SetWithoutSource("orchestrator_explorer.extra_tags", []string{"orch:tag", "missingcolon"}) + + recordFlavor := flavor.GetFlavor() + defer func() { + flavor.SetFlavor(recordFlavor) + }() + + t.Run("Agent extraGlobalTags", func(t *testing.T) { + flavor.SetFlavor(flavor.DefaultAgent) + globalTags := GetStaticTags(context.Background(), mockConfig) + assert.Equal(t, map[string][]string{}, globalTags) + }) + + t.Run("ClusterAgent extraGlobalTags", func(t *testing.T) { + flavor.SetFlavor(flavor.ClusterAgent) + globalTags := GetStaticTags(context.Background(), mockConfig) + assert.Equal(t, map[string][]string{ + "some": {"tag"}, + "extra": {"tag"}, + "cluster": {"tag"}, + "orch": {"tag"}, + }, globalTags) + }) +} diff --git a/comp/core/tagger/impl/local_tagger.go b/comp/core/tagger/impl/local_tagger.go index 5c9dda19b539c..222d03f1222d5 100644 --- a/comp/core/tagger/impl/local_tagger.go +++ b/comp/core/tagger/impl/local_tagger.go @@ -63,7 +63,7 @@ func (t *localTagger) Start(ctx context.Context) error { ) go t.tagStore.Run(t.ctx) - go t.collector.Run(t.ctx, t.cfg) + go t.collector.Run(t.ctx) return nil } diff --git a/comp/core/tagger/impl/tagger.go b/comp/core/tagger/impl/tagger.go index 612fdca2bb9d3..779e0c5768264 100644 --- a/comp/core/tagger/impl/tagger.go +++ b/comp/core/tagger/impl/tagger.go @@ -126,7 +126,7 @@ func NewTaggerClient(params tagger.Params, cfg config.Component, wmeta workloadm var err error telemetryStore := telemetry.NewStore(telemetryComp) if params.UseFakeTagger { - defaultTagger = taggermock.New().Comp + defaultTagger = taggermock.New(taggermock.Dependencies{Config: cfg}).Comp } else { defaultTagger, err = newLocalTagger(cfg, wmeta, telemetryStore) } diff --git a/comp/core/tagger/mock/fake_tagger.go b/comp/core/tagger/mock/fake_tagger.go index 8a609d052c766..1a7b735ebb020 100644 --- a/comp/core/tagger/mock/fake_tagger.go +++ b/comp/core/tagger/mock/fake_tagger.go @@ -9,6 +9,8 @@ import ( "context" "strconv" + "github.com/DataDog/datadog-agent/comp/core/config" + "github.com/DataDog/datadog-agent/comp/core/tagger/collectors" tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def" "github.com/DataDog/datadog-agent/comp/core/tagger/origindetection" "github.com/DataDog/datadog-agent/comp/core/tagger/tagstore" @@ -35,19 +37,27 @@ type FakeTagger struct { store *tagstore.TagStore } -// Provides is a struct containing the mock and the endpoint +// Dependencies is the mock dependencies for the tagger component +type Dependencies struct { + Config config.Component +} + +// Provides is a struct containing the mock type Provides struct { - Comp Mock + Comp tagger.Component } // New instantiates a new fake tagger -func New() Provides { - return Provides{ - Comp: &FakeTagger{ - errors: make(map[string]error), - store: tagstore.NewTagStore(nil), - }, +func New(deps Dependencies) Provides { + fakeTagger := &FakeTagger{ + errors: make(map[string]error), + store: tagstore.NewTagStore(nil), } + + // Initialize the fakeTagger similar to localTagger start() + _ = collectors.NewWorkloadMetaCollector(context.Background(), deps.Config, nil, fakeTagger.store) + + return Provides{Comp: fakeTagger} } // SetTags allows to set tags in store for a given source, entity diff --git a/comp/dogstatsd/server/server.go b/comp/dogstatsd/server/server.go index bd0f0f08b585f..27bab3b493a28 100644 --- a/comp/dogstatsd/server/server.go +++ b/comp/dogstatsd/server/server.go @@ -20,6 +20,8 @@ import ( api "github.com/DataDog/datadog-agent/comp/api/api/def" configComponent "github.com/DataDog/datadog-agent/comp/core/config" log "github.com/DataDog/datadog-agent/comp/core/log/def" + tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def" + "github.com/DataDog/datadog-agent/comp/core/tagger/types" "github.com/DataDog/datadog-agent/comp/core/telemetry" workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def" "github.com/DataDog/datadog-agent/comp/dogstatsd/listeners" @@ -40,7 +42,6 @@ import ( "github.com/DataDog/datadog-agent/pkg/util/option" "github.com/DataDog/datadog-agent/pkg/util/sort" statutil "github.com/DataDog/datadog-agent/pkg/util/stat" - tagutil "github.com/DataDog/datadog-agent/pkg/util/tags" ) var ( @@ -79,6 +80,7 @@ type dependencies struct { Params Params WMeta option.Option[workloadmeta.Component] Telemetry telemetry.Component + Tagger tagger.Component } type provides struct { @@ -184,7 +186,7 @@ func initTelemetry() { // TODO: (components) - merge with newServerCompat once NewServerlessServer is removed func newServer(deps dependencies) provides { - s := newServerCompat(deps.Config, deps.Log, deps.Replay, deps.Debug, deps.Params.Serverless, deps.Demultiplexer, deps.WMeta, deps.PidMap, deps.Telemetry) + s := newServerCompat(deps.Config, deps.Log, deps.Replay, deps.Debug, deps.Params.Serverless, deps.Demultiplexer, deps.WMeta, deps.PidMap, deps.Telemetry, deps.Tagger) if deps.Config.GetBool("use_dogstatsd") { deps.Lc.Append(fx.Hook{ @@ -199,7 +201,7 @@ func newServer(deps dependencies) provides { } } -func newServerCompat(cfg model.Reader, log log.Component, capture replay.Component, debug serverdebug.Component, serverless bool, demux aggregator.Demultiplexer, wmeta option.Option[workloadmeta.Component], pidMap pidmap.Component, telemetrycomp telemetry.Component) *server { +func newServerCompat(cfg model.Reader, log log.Component, capture replay.Component, debug serverdebug.Component, serverless bool, demux aggregator.Demultiplexer, wmeta option.Option[workloadmeta.Component], pidMap pidmap.Component, telemetrycomp telemetry.Component, tagger tagger.Component) *server { // This needs to be done after the configuration is loaded once.Do(func() { initTelemetry() }) var stats *statutil.Stats @@ -237,9 +239,12 @@ func newServerCompat(cfg model.Reader, log log.Component, capture replay.Compone // if the server is running in a context where static tags are required, add those // to extraTags. - if staticTags := tagutil.GetStaticTagsSlice(context.TODO(), cfg); staticTags != nil { - extraTags = append(extraTags, staticTags...) + staticTags, err := tagger.GlobalTags(types.LowCardinality) + log.Errorf("GABE: got staticTags from tagger: %v", staticTags) + if err != nil { + log.Errorf("Dogstatsd: unable to get static tags: %s", err) } + extraTags = append(extraTags, staticTags...) sort.UniqInPlace(extraTags) entityIDPrecedenceEnabled := cfg.GetBool("dogstatsd_entity_id_precedence") diff --git a/comp/dogstatsd/server/server_util_test.go b/comp/dogstatsd/server/server_util_test.go index a84bdbb0f0741..660b21428dc83 100644 --- a/comp/dogstatsd/server/server_util_test.go +++ b/comp/dogstatsd/server/server_util_test.go @@ -24,6 +24,8 @@ import ( "github.com/DataDog/datadog-agent/comp/core/hostname/hostnameimpl" log "github.com/DataDog/datadog-agent/comp/core/log/def" logmock "github.com/DataDog/datadog-agent/comp/core/log/mock" + tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def" + taggermock "github.com/DataDog/datadog-agent/comp/core/tagger/mock" "github.com/DataDog/datadog-agent/comp/core/telemetry" "github.com/DataDog/datadog-agent/comp/core/telemetry/telemetryimpl" workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def" @@ -54,6 +56,7 @@ type depsWithoutServer struct { Debug serverdebug.Component WMeta option.Option[workloadmeta.Component] Telemetry telemetry.Component + Tagger tagger.Component } type serverDeps struct { @@ -67,6 +70,7 @@ type serverDeps struct { Debug serverdebug.Component WMeta option.Option[workloadmeta.Component] Telemetry telemetry.Component + Tagger tagger.Component Server Component } @@ -86,6 +90,7 @@ func fulfillDepsWithConfigOverride(t testing.TB, overrides map[string]interface{ pidmapimpl.Module(), demultiplexerimpl.FakeSamplerMockModule(), workloadmetafxmock.MockModule(workloadmeta.NewParams()), + taggermock.Module(), Module(Params{Serverless: false}), )) } @@ -102,6 +107,7 @@ func fulfillDepsWithConfigYaml(t testing.TB, yaml string) serverDeps { pidmapimpl.Module(), demultiplexerimpl.FakeSamplerMockModule(), workloadmetafxmock.MockModule(workloadmeta.NewParams()), + taggermock.Module(), Module(Params{Serverless: false}), )) } @@ -121,9 +127,10 @@ func fulfillDepsWithInactiveServer(t *testing.T, cfg map[string]interface{}) (de pidmapimpl.Module(), demultiplexerimpl.FakeSamplerMockModule(), workloadmetafxmock.MockModule(workloadmeta.NewParams()), + taggermock.Module(), )) - s := newServerCompat(deps.Config, deps.Log, deps.Replay, deps.Debug, false, deps.Demultiplexer, deps.WMeta, deps.PidMap, deps.Telemetry) + s := newServerCompat(deps.Config, deps.Log, deps.Replay, deps.Debug, false, deps.Demultiplexer, deps.WMeta, deps.PidMap, deps.Telemetry, deps.Tagger) return deps, s } diff --git a/comp/dogstatsd/server/serverless.go b/comp/dogstatsd/server/serverless.go index ba4bb5e9f6ad5..07698fed1834d 100644 --- a/comp/dogstatsd/server/serverless.go +++ b/comp/dogstatsd/server/serverless.go @@ -10,6 +10,7 @@ import ( "context" logComponentImpl "github.com/DataDog/datadog-agent/comp/core/log/impl" + tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def" telemetry "github.com/DataDog/datadog-agent/comp/core/telemetry/noopsimpl" workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def" "github.com/DataDog/datadog-agent/comp/dogstatsd/pidmap/pidmapimpl" @@ -29,9 +30,9 @@ type ServerlessDogstatsd interface { } //nolint:revive // TODO(AML) Fix revive linter -func NewServerlessServer(demux aggregator.Demultiplexer) (ServerlessDogstatsd, error) { +func NewServerlessServer(demux aggregator.Demultiplexer, tagger tagger.Component) (ServerlessDogstatsd, error) { wmeta := option.None[workloadmeta.Component]() - s := newServerCompat(pkgconfigsetup.Datadog(), logComponentImpl.NewTemporaryLoggerWithoutInit(), replay.NewNoopTrafficCapture(), serverdebugimpl.NewServerlessServerDebug(), true, demux, wmeta, pidmapimpl.NewServerlessPidMap(), telemetry.GetCompatComponent()) + s := newServerCompat(pkgconfigsetup.Datadog(), logComponentImpl.NewTemporaryLoggerWithoutInit(), replay.NewNoopTrafficCapture(), serverdebugimpl.NewServerlessServerDebug(), true, demux, wmeta, pidmapimpl.NewServerlessPidMap(), telemetry.GetCompatComponent(), tagger) err := s.start(context.TODO()) if err != nil { diff --git a/comp/otelcol/otlp/collector.go b/comp/otelcol/otlp/collector.go index 099dce19ac85c..512777d93426b 100644 --- a/comp/otelcol/otlp/collector.go +++ b/comp/otelcol/otlp/collector.go @@ -269,7 +269,7 @@ func (p *Pipeline) Stop() { // NewPipelineFromAgentConfig creates a new pipeline from the given agent configuration, metric serializer and logs channel. It returns // any potential failure. func NewPipelineFromAgentConfig(cfg config.Component, s serializer.MetricSerializer, logsAgentChannel chan *message.Message, tagger tagger.Component) (*Pipeline, error) { - pcfg, err := FromAgentConfig(cfg) + pcfg, err := FromAgentConfig(cfg, tagger) if err != nil { pipelineError.Store(fmt.Errorf("config error: %w", err)) return nil, pipelineError.Load() diff --git a/comp/otelcol/otlp/config.go b/comp/otelcol/otlp/config.go index 1ee62509c467a..2353848239da1 100644 --- a/comp/otelcol/otlp/config.go +++ b/comp/otelcol/otlp/config.go @@ -8,7 +8,6 @@ package otlp import ( - "context" "fmt" "strings" @@ -17,10 +16,11 @@ import ( "github.com/go-viper/mapstructure/v2" "github.com/DataDog/datadog-agent/comp/core/config" + tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def" + "github.com/DataDog/datadog-agent/comp/core/tagger/types" "github.com/DataDog/datadog-agent/comp/otelcol/otlp/components/exporter/serializerexporter" "github.com/DataDog/datadog-agent/comp/otelcol/otlp/configcheck" coreconfig "github.com/DataDog/datadog-agent/pkg/config/setup" - tagutil "github.com/DataDog/datadog-agent/pkg/util/tags" ) func portToUint(v int) (port uint, err error) { @@ -32,7 +32,7 @@ func portToUint(v int) (port uint, err error) { } // FromAgentConfig builds a pipeline configuration from an Agent configuration. -func FromAgentConfig(cfg config.Reader) (PipelineConfig, error) { +func FromAgentConfig(cfg config.Reader, tagger tagger.Component) (PipelineConfig, error) { var errs []error otlpConfig := configcheck.ReadConfigSection(cfg, coreconfig.OTLPReceiverSection) tracePort, err := portToUint(cfg.GetInt(coreconfig.OTLPTracePort)) @@ -52,7 +52,8 @@ func FromAgentConfig(cfg config.Reader) (PipelineConfig, error) { metricsConfigMap["apm_stats_receiver_addr"] = fmt.Sprintf("http://localhost:%s/v0.6/stats", coreconfig.Datadog().GetString("apm_config.receiver_port")) } - tags := strings.Join(tagutil.GetStaticTagsSlice(context.TODO(), cfg), ",") + tagSlice, _ := tagger.GlobalTags(types.LowCardinality) + tags := strings.Join(tagSlice, ",") if tags != "" { metricsConfigMap["tags"] = tags } diff --git a/pkg/collector/corechecks/cluster/kubernetesapiserver/events_common_test.go b/pkg/collector/corechecks/cluster/kubernetesapiserver/events_common_test.go index af0efc2196462..64d4d69d62694 100644 --- a/pkg/collector/corechecks/cluster/kubernetesapiserver/events_common_test.go +++ b/pkg/collector/corechecks/cluster/kubernetesapiserver/events_common_test.go @@ -53,7 +53,7 @@ func TestGetDDAlertType(t *testing.T) { } func Test_getInvolvedObjectTags(t *testing.T) { - taggerInstance := mockTagger.New().Comp + taggerInstance := mockTagger.SetupFakeTagger(t) taggerInstance.SetTags(types.NewEntityID(types.KubernetesPodUID, "nginx"), "workloadmeta-kubernetes_pod", nil, []string{"additional_pod_tag:nginx"}, nil, nil) taggerInstance.SetTags(types.NewEntityID(types.KubernetesDeployment, "workload-redis/my-deployment-1"), "workloadmeta-kubernetes_deployment", nil, []string{"deployment_tag:redis-1"}, nil, nil) taggerInstance.SetTags(types.NewEntityID(types.KubernetesDeployment, "default/my-deployment-2"), "workloadmeta-kubernetes_deployment", nil, []string{"deployment_tag:redis-2"}, nil, nil) diff --git a/pkg/serverless/metrics/metric.go b/pkg/serverless/metrics/metric.go index 75c99f9dbf91e..1ecb5690b75b3 100644 --- a/pkg/serverless/metrics/metric.go +++ b/pkg/serverless/metrics/metric.go @@ -44,7 +44,7 @@ type MultipleEndpointConfig interface { // DogStatsDFactory allows create a new DogStatsD server type DogStatsDFactory interface { - NewServer(aggregator.Demultiplexer) (dogstatsdServer.ServerlessDogstatsd, error) + NewServer(aggregator.Demultiplexer, tagger.Component) (dogstatsdServer.ServerlessDogstatsd, error) } const ( @@ -58,8 +58,8 @@ func (m *MetricConfig) GetMultipleEndpoints() (map[string][]string, error) { } // NewServer returns a running DogStatsD server -func (m *MetricDogStatsD) NewServer(demux aggregator.Demultiplexer) (dogstatsdServer.ServerlessDogstatsd, error) { - return dogstatsdServer.NewServerlessServer(demux) +func (m *MetricDogStatsD) NewServer(demux aggregator.Demultiplexer, tagger tagger.Component) (dogstatsdServer.ServerlessDogstatsd, error) { + return dogstatsdServer.NewServerlessServer(demux, tagger) } // Start starts the DogStatsD agent @@ -81,7 +81,7 @@ func (c *ServerlessMetricAgent) Start(forwarderTimeout time.Duration, multipleEn demux := buildDemultiplexer(multipleEndpointConfig, forwarderTimeout, c.Tagger) if demux != nil { - statsd, err := dogstatFactory.NewServer(demux) + statsd, err := dogstatFactory.NewServer(demux, c.Tagger) if err != nil { log.Errorf("Unable to start the DogStatsD server: %s", err) } else { diff --git a/pkg/serverless/metrics/metric_test.go b/pkg/serverless/metrics/metric_test.go index dc3df4790609c..e9be1f0a65230 100644 --- a/pkg/serverless/metrics/metric_test.go +++ b/pkg/serverless/metrics/metric_test.go @@ -21,6 +21,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def" nooptagger "github.com/DataDog/datadog-agent/comp/core/tagger/impl-noop" dogstatsdServer "github.com/DataDog/datadog-agent/comp/dogstatsd/server" "github.com/DataDog/datadog-agent/pkg/aggregator" @@ -77,7 +78,7 @@ func TestStartInvalidConfig(t *testing.T) { type MetricDogStatsDMocked struct{} //nolint:revive // TODO(SERV) Fix revive linter -func (m *MetricDogStatsDMocked) NewServer(_ aggregator.Demultiplexer) (dogstatsdServer.ServerlessDogstatsd, error) { +func (m *MetricDogStatsDMocked) NewServer(_ aggregator.Demultiplexer, _ tagger.Component) (dogstatsdServer.ServerlessDogstatsd, error) { return nil, fmt.Errorf("error") } @@ -217,8 +218,9 @@ func TestRaceFlushVersusParsePacket(t *testing.T) { pkgconfigsetup.Datadog().SetDefault("dogstatsd_port", port) demux := aggregator.InitAndStartServerlessDemultiplexer(nil, time.Second*1000, nooptagger.NewComponent()) + tagger := nooptagger.NewComponent() - s, err := dogstatsdServer.NewServerlessServer(demux) + s, err := dogstatsdServer.NewServerlessServer(demux, tagger) require.NoError(t, err, "cannot start DSD") defer s.Stop() From 89c1a19187b97c5f58d0412d7ea2f139b37818f2 Mon Sep 17 00:00:00 2001 From: Gabriel Dos Santos Date: Fri, 10 Jan 2025 15:51:26 +0000 Subject: [PATCH 2/3] Fix fx test imports --- .../core/tagger/collectors/workloadmeta_main.go | 2 -- comp/core/tagger/mock/fake_tagger.go | 2 +- comp/core/tagger/mock/mock.go | 6 +++++- comp/dogstatsd/server/server.go | 1 - comp/otelcol/otlp/collector_test.go | 4 +++- comp/otelcol/otlp/config_test.go | 17 +++++++++++++---- comp/trace/config/config_test.go | 2 +- 7 files changed, 23 insertions(+), 11 deletions(-) diff --git a/comp/core/tagger/collectors/workloadmeta_main.go b/comp/core/tagger/collectors/workloadmeta_main.go index d5b012ea8f636..ca304beb55acb 100644 --- a/comp/core/tagger/collectors/workloadmeta_main.go +++ b/comp/core/tagger/collectors/workloadmeta_main.go @@ -33,8 +33,6 @@ const ( processSource = workloadmetaCollectorName + "-" + string(workloadmeta.KindProcess) kubeMetadataSource = workloadmetaCollectorName + "-" + string(workloadmeta.KindKubernetesMetadata) deploymentSource = workloadmetaCollectorName + "-" + string(workloadmeta.KindKubernetesDeployment) - - clusterTagNamePrefix = "kube_cluster_name" ) // CollectorPriorities holds collector priorities diff --git a/comp/core/tagger/mock/fake_tagger.go b/comp/core/tagger/mock/fake_tagger.go index 1a7b735ebb020..c85897bd273d8 100644 --- a/comp/core/tagger/mock/fake_tagger.go +++ b/comp/core/tagger/mock/fake_tagger.go @@ -44,7 +44,7 @@ type Dependencies struct { // Provides is a struct containing the mock type Provides struct { - Comp tagger.Component + Comp Mock } // New instantiates a new fake tagger diff --git a/comp/core/tagger/mock/mock.go b/comp/core/tagger/mock/mock.go index 26b9781c871ec..a24162cb78d5e 100644 --- a/comp/core/tagger/mock/mock.go +++ b/comp/core/tagger/mock/mock.go @@ -11,6 +11,7 @@ package mock import ( "testing" + "github.com/DataDog/datadog-agent/comp/core/config" "github.com/DataDog/datadog-agent/pkg/util/fxutil" ) @@ -23,5 +24,8 @@ func Module() fxutil.Module { // SetupFakeTagger calls fxutil.Test to create a mock tagger for testing func SetupFakeTagger(t testing.TB) Mock { - return fxutil.Test[Mock](t, Module()) + return fxutil.Test[Mock](t, + config.MockModule(), + Module(), + ) } diff --git a/comp/dogstatsd/server/server.go b/comp/dogstatsd/server/server.go index 27bab3b493a28..b8662d9a45563 100644 --- a/comp/dogstatsd/server/server.go +++ b/comp/dogstatsd/server/server.go @@ -240,7 +240,6 @@ func newServerCompat(cfg model.Reader, log log.Component, capture replay.Compone // if the server is running in a context where static tags are required, add those // to extraTags. staticTags, err := tagger.GlobalTags(types.LowCardinality) - log.Errorf("GABE: got staticTags from tagger: %v", staticTags) if err != nil { log.Errorf("Dogstatsd: unable to get static tags: %s", err) } diff --git a/comp/otelcol/otlp/collector_test.go b/comp/otelcol/otlp/collector_test.go index 6134d8a6a9a47..070178f8ef3a1 100644 --- a/comp/otelcol/otlp/collector_test.go +++ b/comp/otelcol/otlp/collector_test.go @@ -78,6 +78,8 @@ func TestStartPipeline(t *testing.T) { } func TestStartPipelineFromConfig(t *testing.T) { + fakeTagger := mock.SetupFakeTagger(t) + pkgconfigsetup.Datadog().SetWithoutSource("hostname", "otlp-testhostname") defer pkgconfigsetup.Datadog().SetWithoutSource("hostname", "") @@ -101,7 +103,7 @@ func TestStartPipelineFromConfig(t *testing.T) { t.Run(testInstance.path, func(t *testing.T) { cfg, err := testutil.LoadConfig(t, "./testdata/"+testInstance.path) require.NoError(t, err) - pcfg, err := FromAgentConfig(cfg) + pcfg, err := FromAgentConfig(cfg, fakeTagger) require.NoError(t, err) if testInstance.err == "" { AssertSucessfulRun(t, pcfg) diff --git a/comp/otelcol/otlp/config_test.go b/comp/otelcol/otlp/config_test.go index 64ba24ff772e0..7929e3fcb9aae 100644 --- a/comp/otelcol/otlp/config_test.go +++ b/comp/otelcol/otlp/config_test.go @@ -13,6 +13,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/DataDog/datadog-agent/comp/core/tagger/mock" "github.com/DataDog/datadog-agent/comp/otelcol/otlp/configcheck" "github.com/DataDog/datadog-agent/comp/otelcol/otlp/testutil" ) @@ -46,6 +47,8 @@ func TestIsEnabledEnv(t *testing.T) { } func TestFromAgentConfigReceiver(t *testing.T) { + fakeTagger := mock.SetupFakeTagger(t) + tests := []struct { path string cfg PipelineConfig @@ -189,7 +192,7 @@ func TestFromAgentConfigReceiver(t *testing.T) { t.Run(testInstance.path, func(t *testing.T) { cfg, err := testutil.LoadConfig(t, "./testdata/"+testInstance.path) require.NoError(t, err) - pcfg, err := FromAgentConfig(cfg) + pcfg, err := FromAgentConfig(cfg, fakeTagger) if err != nil || testInstance.err != "" { assert.Equal(t, testInstance.err, err.Error()) return @@ -202,6 +205,8 @@ func TestFromAgentConfigReceiver(t *testing.T) { } func TestFromEnvironmentVariables(t *testing.T) { + fakeTagger := mock.SetupFakeTagger(t) + tests := []struct { name string env map[string]string @@ -458,7 +463,7 @@ func TestFromEnvironmentVariables(t *testing.T) { } cfg, err := testutil.LoadConfig(t, "./testdata/empty.yaml") require.NoError(t, err) - pcfg, err := FromAgentConfig(cfg) + pcfg, err := FromAgentConfig(cfg, fakeTagger) if err != nil || testInstance.err != "" { assert.Equal(t, testInstance.err, err.Error()) return @@ -471,6 +476,8 @@ func TestFromEnvironmentVariables(t *testing.T) { } func TestFromAgentConfigMetrics(t *testing.T) { + fakeTagger := mock.SetupFakeTagger(t) + tests := []struct { path string cfg PipelineConfig @@ -510,7 +517,7 @@ func TestFromAgentConfigMetrics(t *testing.T) { t.Run(testInstance.path, func(t *testing.T) { cfg, err := testutil.LoadConfig(t, "./testdata/"+testInstance.path) require.NoError(t, err) - pcfg, err := FromAgentConfig(cfg) + pcfg, err := FromAgentConfig(cfg, fakeTagger) if err != nil || testInstance.err != "" { assert.Equal(t, testInstance.err, err.Error()) return @@ -523,6 +530,8 @@ func TestFromAgentConfigMetrics(t *testing.T) { } func TestFromAgentConfigDebug(t *testing.T) { + fakeTagger := mock.SetupFakeTagger(t) + tests := []struct { path string cfg PipelineConfig @@ -606,7 +615,7 @@ func TestFromAgentConfigDebug(t *testing.T) { t.Run(testInstance.path, func(t *testing.T) { cfg, err := testutil.LoadConfig(t, "./testdata/"+testInstance.path) require.NoError(t, err) - pcfg, err := FromAgentConfig(cfg) + pcfg, err := FromAgentConfig(cfg, fakeTagger) if err != nil || testInstance.err != "" { assert.Equal(t, testInstance.err, err.Error()) return diff --git a/comp/trace/config/config_test.go b/comp/trace/config/config_test.go index 9706e2c784f4c..ec05be92044a8 100644 --- a/comp/trace/config/config_test.go +++ b/comp/trace/config/config_test.go @@ -2304,7 +2304,7 @@ func buildConfigComponent(t *testing.T, setHostnameInConfig bool, coreConfigOpti } taggerComponent := fxutil.Test[taggermock.Mock](t, - fx.Replace(coreConfig), + corecomp.MockModule(), taggermock.Module(), ) From 8530b0e248076eca59fbd1ee30c9de1f3c9bbf27 Mon Sep 17 00:00:00 2001 From: Gabriel Dos Santos Date: Fri, 10 Jan 2025 20:39:23 +0000 Subject: [PATCH 3/3] Consider nil tag processor in wmeta collector for tests --- .github/CODEOWNERS | 1 - .../tagger/collectors/workloadmeta_main.go | 4 +- comp/dogstatsd/server/server_util_test.go | 3 + pkg/util/tags/static_tags.go | 117 ----------------- pkg/util/tags/static_tags_test.go | 124 ------------------ 5 files changed, 6 insertions(+), 243 deletions(-) delete mode 100644 pkg/util/tags/static_tags.go delete mode 100644 pkg/util/tags/static_tags_test.go diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 3f00a7c544bf9..80bc9f2de8c43 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -466,7 +466,6 @@ /pkg/util/kubernetes/ @DataDog/container-integrations @DataDog/container-platform @DataDog/container-app /pkg/util/podman/ @DataDog/container-integrations /pkg/util/prometheus @DataDog/container-integrations -/pkg/util/tags/ @DataDog/container-platform /pkg/util/trivy/ @DataDog/container-integrations @DataDog/agent-security /pkg/util/uuid/ @DataDog/agent-shared-components /pkg/util/cgroups/ @DataDog/container-integrations diff --git a/comp/core/tagger/collectors/workloadmeta_main.go b/comp/core/tagger/collectors/workloadmeta_main.go index ca304beb55acb..279696bcfdd74 100644 --- a/comp/core/tagger/collectors/workloadmeta_main.go +++ b/comp/core/tagger/collectors/workloadmeta_main.go @@ -173,7 +173,9 @@ func NewWorkloadMetaCollector(ctx context.Context, cfg config.Component, store w c.initK8sResourcesMetaAsTags(metadataAsTags.GetResourcesLabelsAsTags(), metadataAsTags.GetResourcesAnnotationsAsTags()) // initializes with the static global tags - c.collectStaticGlobalTags(ctx, cfg) + if c.tagProcessor != nil { + c.collectStaticGlobalTags(ctx, cfg) + } return c } diff --git a/comp/dogstatsd/server/server_util_test.go b/comp/dogstatsd/server/server_util_test.go index 660b21428dc83..f9e3e1bb6669a 100644 --- a/comp/dogstatsd/server/server_util_test.go +++ b/comp/dogstatsd/server/server_util_test.go @@ -91,6 +91,7 @@ func fulfillDepsWithConfigOverride(t testing.TB, overrides map[string]interface{ demultiplexerimpl.FakeSamplerMockModule(), workloadmetafxmock.MockModule(workloadmeta.NewParams()), taggermock.Module(), + fx.Provide(func(mock taggermock.Mock) tagger.Component { return mock }), Module(Params{Serverless: false}), )) } @@ -108,6 +109,7 @@ func fulfillDepsWithConfigYaml(t testing.TB, yaml string) serverDeps { demultiplexerimpl.FakeSamplerMockModule(), workloadmetafxmock.MockModule(workloadmeta.NewParams()), taggermock.Module(), + fx.Provide(func(mock taggermock.Mock) tagger.Component { return mock }), Module(Params{Serverless: false}), )) } @@ -128,6 +130,7 @@ func fulfillDepsWithInactiveServer(t *testing.T, cfg map[string]interface{}) (de demultiplexerimpl.FakeSamplerMockModule(), workloadmetafxmock.MockModule(workloadmeta.NewParams()), taggermock.Module(), + fx.Provide(func(mock taggermock.Mock) tagger.Component { return mock }), )) s := newServerCompat(deps.Config, deps.Log, deps.Replay, deps.Debug, false, deps.Demultiplexer, deps.WMeta, deps.PidMap, deps.Telemetry, deps.Tagger) diff --git a/pkg/util/tags/static_tags.go b/pkg/util/tags/static_tags.go deleted file mode 100644 index 681da68b43073..0000000000000 --- a/pkg/util/tags/static_tags.go +++ /dev/null @@ -1,117 +0,0 @@ -// 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 2022-present Datadog, Inc. - -// Package tags provides utilities for working with tags. -package tags - -import ( - "context" - "strings" - - "github.com/DataDog/datadog-agent/comp/core/config" - "github.com/DataDog/datadog-agent/pkg/config/env" - configUtils "github.com/DataDog/datadog-agent/pkg/config/utils" - "github.com/DataDog/datadog-agent/pkg/util/fargate" - "github.com/DataDog/datadog-agent/pkg/util/flavor" - "github.com/DataDog/datadog-agent/pkg/util/kubernetes/clustername" - "github.com/DataDog/datadog-agent/pkg/util/log" -) - -// GetStaticTagsSlice gets the "static tags" for this agent. These are tags -// that are attached directly to everything the agent produces, but _not_ -// included in host tags. In environments with no host metadata (such as where -// the hostname is empty), tags that would otherwise be included in host -// metadata are generated by this function. -func GetStaticTagsSlice(ctx context.Context, datadogConfig config.Reader) []string { - // fargate (ECS or EKS) does not have host tags, so we need to - // add static tags to each container manually - - if !fargate.IsFargateInstance() { - return nil - } - - tags := []string{} - - // DD_TAGS / DD_EXTRA_TAGS - tags = append(tags, configUtils.GetConfiguredTags(datadogConfig, false)...) - - // EKS Fargate specific tags - if env.IsFeaturePresent(env.EKSFargate) { - // eks_fargate_node - node, err := fargate.GetEKSFargateNodename() - if err != nil { - log.Infof("Couldn't build the 'eks_fargate_node' tag: %v", err) - } else { - tags = append(tags, "eks_fargate_node:"+node) - } - - // kube_cluster_name - clusterTagNamePrefix := "kube_cluster_name:" - var tag string - var found bool - for _, tag = range tags { - if strings.HasPrefix(tag, clusterTagNamePrefix) { - found = true - break - } - } - if found { - log.Infof("'%s' was set manually via DD_TAGS, not changing it", clusterTagNamePrefix+tag) - } else { - cluster := clustername.GetClusterNameTagValue(ctx, "") - if cluster == "" { - log.Infof("Couldn't build the %q.. tag, DD_CLUSTER_NAME can be used to set it", clusterTagNamePrefix) - } else { - tags = append(tags, clusterTagNamePrefix+cluster) - } - } - } - - return tags -} - -// GetStaticTags is similar to GetStaticTagsSlice, but returning a map[string][]string containing -// : pairs for tags. Tags not matching this pattern are omitted. -func GetStaticTags(ctx context.Context, datadogConfig config.Component) map[string][]string { - tags := GetStaticTagsSlice(ctx, datadogConfig) - if tags == nil { - return nil - } - return sliceToMap(tags) -} - -// GetGlobalEnvTags is similar to GetStaticTags, but returning a map[string][]string containing -// : pairs for all global environment tags on the cluster agent. This includes: -// DD_TAGS, DD_EXTRA_TAGS, DD_CLUSTER_CHECKS_EXTRA_TAGS, and DD_ORCHESTRATOR_EXPLORER_EXTRA_TAGS -func GetGlobalEnvTags(config config.Reader) map[string][]string { - if flavor.GetFlavor() != flavor.ClusterAgent { - return nil - } - - // DD_TAGS / DD_EXTRA_TAGS - tags := configUtils.GetConfiguredTags(config, false) - - // DD_CLUSTER_CHECKS_EXTRA_TAGS / DD_ORCHESTRATOR_EXPLORER_EXTRA_TAGS - tags = append(tags, configUtils.GetConfiguredDCATags(config)...) - - if tags == nil { - return nil - } - return sliceToMap(tags) -} - -func sliceToMap(tags []string) map[string][]string { - rv := make(map[string][]string, len(tags)) - for _, t := range tags { - tagParts := strings.SplitN(t, ":", 2) - if len(tagParts) == 2 { - if _, ok := rv[tagParts[0]]; !ok { - rv[tagParts[0]] = []string{} - } - rv[tagParts[0]] = append(rv[tagParts[0]], tagParts[1]) - } - } - return rv -} diff --git a/pkg/util/tags/static_tags_test.go b/pkg/util/tags/static_tags_test.go deleted file mode 100644 index c418a032db6e7..0000000000000 --- a/pkg/util/tags/static_tags_test.go +++ /dev/null @@ -1,124 +0,0 @@ -// 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 2022-present Datadog, Inc. - -package tags - -import ( - "context" - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/DataDog/datadog-agent/pkg/config/env" - configmock "github.com/DataDog/datadog-agent/pkg/config/mock" - "github.com/DataDog/datadog-agent/pkg/util/flavor" -) - -func TestStaticTags(t *testing.T) { - mockConfig := configmock.New(t) - mockConfig.SetWithoutSource("kubernetes_kubelet_nodename", "eksnode") - defer mockConfig.SetWithoutSource("kubernetes_kubelet_nodename", "") - - env.SetFeatures(t, env.EKSFargate) - - t.Run("just tags", func(t *testing.T) { - mockConfig.SetWithoutSource("tags", []string{"some:tag", "another:tag", "nocolon"}) - defer mockConfig.SetWithoutSource("tags", []string{}) - staticTags := GetStaticTags(context.Background(), mockConfig) - assert.Equal(t, map[string][]string{ - "some": {"tag"}, - "another": {"tag"}, - "eks_fargate_node": {"eksnode"}, - }, staticTags) - }) - - t.Run("tags and extra_tags", func(t *testing.T) { - mockConfig.SetWithoutSource("tags", []string{"some:tag", "nocolon"}) - mockConfig.SetWithoutSource("extra_tags", []string{"extra:tag", "missingcolon"}) - defer mockConfig.SetWithoutSource("tags", []string{}) - defer mockConfig.SetWithoutSource("extra_tags", []string{}) - staticTags := GetStaticTags(context.Background(), mockConfig) - assert.Equal(t, map[string][]string{ - "some": {"tag"}, - "extra": {"tag"}, - "eks_fargate_node": {"eksnode"}, - }, staticTags) - }) - - t.Run("cluster name already set", func(t *testing.T) { - mockConfig.SetWithoutSource("tags", []string{"kube_cluster_name:foo"}) - defer mockConfig.SetWithoutSource("tags", []string{}) - staticTags := GetStaticTags(context.Background(), mockConfig) - assert.Equal(t, map[string][]string{ - "eks_fargate_node": {"eksnode"}, - "kube_cluster_name": {"foo"}, - }, staticTags) - }) -} - -func TestStaticTagsSlice(t *testing.T) { - mockConfig := configmock.New(t) - mockConfig.SetWithoutSource("kubernetes_kubelet_nodename", "eksnode") - defer mockConfig.SetWithoutSource("kubernetes_kubelet_nodename", "") - - env.SetFeatures(t, env.EKSFargate) - - t.Run("just tags", func(t *testing.T) { - mockConfig.SetWithoutSource("tags", []string{"some:tag", "another:tag", "nocolon"}) - defer mockConfig.SetWithoutSource("tags", []string{}) - staticTags := GetStaticTagsSlice(context.Background(), mockConfig) - assert.ElementsMatch(t, []string{ - "nocolon", - "some:tag", - "another:tag", - "eks_fargate_node:eksnode", - }, staticTags) - }) - - t.Run("tags and extra_tags", func(t *testing.T) { - mockConfig.SetWithoutSource("tags", []string{"some:tag", "nocolon"}) - mockConfig.SetWithoutSource("extra_tags", []string{"extra:tag", "missingcolon"}) - defer mockConfig.SetWithoutSource("tags", []string{}) - defer mockConfig.SetWithoutSource("extra_tags", []string{}) - staticTags := GetStaticTagsSlice(context.Background(), mockConfig) - assert.ElementsMatch(t, []string{ - "nocolon", - "missingcolon", - "some:tag", - "extra:tag", - "eks_fargate_node:eksnode", - }, staticTags) - }) -} - -func TestExtraGlobalEnvTags(t *testing.T) { - mockConfig := configmock.New(t) - mockConfig.SetWithoutSource("tags", []string{"some:tag", "nocolon"}) - mockConfig.SetWithoutSource("extra_tags", []string{"extra:tag", "missingcolon"}) - mockConfig.SetWithoutSource("cluster_checks.extra_tags", []string{"cluster:tag", "nocolon"}) - mockConfig.SetWithoutSource("orchestrator_explorer.extra_tags", []string{"orch:tag", "missingcolon"}) - - recordFlavor := flavor.GetFlavor() - defer func() { - flavor.SetFlavor(recordFlavor) - }() - - t.Run("Agent extraGlobalTags", func(t *testing.T) { - flavor.SetFlavor(flavor.DefaultAgent) - globalTags := GetGlobalEnvTags(mockConfig) - assert.Equal(t, map[string][]string(nil), globalTags) - }) - - t.Run("ClusterAgent extraGlobalTags", func(t *testing.T) { - flavor.SetFlavor(flavor.ClusterAgent) - globalTags := GetGlobalEnvTags(mockConfig) - assert.Equal(t, map[string][]string{ - "some": {"tag"}, - "extra": {"tag"}, - "cluster": {"tag"}, - "orch": {"tag"}, - }, globalTags) - }) -}