-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
static_tags.go
88 lines (76 loc) · 2.62 KB
/
static_tags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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 util
import (
"context"
"strings"
"github.com/DataDog/datadog-agent/pkg/config"
configUtils "github.com/DataDog/datadog-agent/pkg/config/utils"
"github.com/DataDog/datadog-agent/pkg/util/fargate"
"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) []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(config.Datadog(), false)...)
// EKS Fargate specific tags
if config.IsFeaturePresent(config.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
// <key>:<value> pairs for tags. Tags not matching this pattern are omitted.
func GetStaticTags(ctx context.Context) map[string]string {
tags := GetStaticTagsSlice(ctx)
if tags == nil {
return nil
}
rv := make(map[string]string, len(tags))
for _, t := range tags {
tagParts := strings.SplitN(t, ":", 2)
if len(tagParts) == 2 {
rv[tagParts[0]] = tagParts[1]
}
}
return rv
}