Skip to content

Commit

Permalink
avoid computing metadata as tags each time in hostinfo tag extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
adel121 committed Aug 16, 2024
1 parent 52542e4 commit 133fbf1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 78 deletions.
2 changes: 1 addition & 1 deletion comp/metadata/host/hostimpl/hosttags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func getProvidersDefinitions(conf config.Reader) map[string]*providerDef {
}

if config.IsFeaturePresent(config.Kubernetes) {
providers["kubernetes"] = &providerDef{10, k8s.GetTags}
providers["kubernetes"] = &providerDef{10, k8s.NewKubeNodeTagsProvider(conf).GetTags}
}

if config.IsFeaturePresent(config.Docker) {
Expand Down
46 changes: 15 additions & 31 deletions pkg/util/kubernetes/hostinfo/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package hostinfo

import (
"context"
"strings"

k8smetadata "github.com/DataDog/datadog-agent/comp/core/tagger/k8s_metadata"
"github.com/DataDog/datadog-agent/comp/core/tagger/taglist"
Expand All @@ -19,14 +18,24 @@ import (
"github.com/DataDog/datadog-agent/pkg/util/log"
)

// KubeNodeTagsProvider allows computing node tags based on user configurations for node labels and annotations as tags
type KubeNodeTagsProvider struct {
metadataAsTags configutils.MetadataAsTags
}

// NewKubeNodeTagsProvider creates and returns a new kube node tags provider object
func NewKubeNodeTagsProvider(conf config.Reader) KubeNodeTagsProvider {
return KubeNodeTagsProvider{configutils.GetMetadataAsTags(conf)}
}

// GetTags gets the tags from the kubernetes apiserver and the kubelet
func GetTags(ctx context.Context) ([]string, error) {
tags, err := getNodeInfoTags(ctx)
func (k KubeNodeTagsProvider) GetTags(ctx context.Context) ([]string, error) {
tags, err := k.getNodeInfoTags(ctx)
if err != nil {
return nil, err
}

annotationsToTags := getAnnotationsToTags()
annotationsToTags := k.metadataAsTags.GetNodeAnnotationsAsTags()
if len(annotationsToTags) == 0 {
return tags, nil
}
Expand All @@ -41,7 +50,7 @@ func GetTags(ctx context.Context) ([]string, error) {
}

// getNodeInfoTags gets the tags from the kubelet and the cluster-agent
func getNodeInfoTags(ctx context.Context) ([]string, error) {
func (k KubeNodeTagsProvider) getNodeInfoTags(ctx context.Context) ([]string, error) {
nodeInfo, err := NewNodeInfo()
if err != nil {
log.Debugf("Unable to auto discover node info tags: %s", err)
Expand All @@ -56,7 +65,7 @@ func getNodeInfoTags(ctx context.Context) ([]string, error) {
return nil, err
}
tags := []string{"kube_node:" + nodeName}
labelsToTags := getLabelsToTags()
labelsToTags := k.metadataAsTags.GetNodeLabelsAsTags()
if len(labelsToTags) == 0 {
return tags, nil
}
Expand All @@ -79,31 +88,6 @@ func getDefaultLabelsToTags() map[string]string {
}
}

func getLabelsToTags() map[string]string {
labelsToTags := getDefaultLabelsToTags()

metadataAsTags := configutils.GetMetadataAsTags(config.Datadog())
for k, v := range metadataAsTags.GetNodeLabelsAsTags() {
// viper lower-cases map keys from yaml, but not from envvars
labelsToTags[strings.ToLower(k)] = v
}

return labelsToTags
}

func getAnnotationsToTags() map[string]string {
annotationsToTags := map[string]string{}

metadataAsTags := configutils.GetMetadataAsTags(config.Datadog())

for k, v := range metadataAsTags.GetNodeAnnotationsAsTags() {
// viper lower-cases map keys from yaml, but not from envvars
annotationsToTags[strings.ToLower(k)] = v
}

return annotationsToTags
}

func extractTags(nodeLabels, labelsToTags map[string]string) []string {
tagList := taglist.NewTagList()
labelsToTags, glob := k8smetadata.InitMetadataAsTags(labelsToTags)
Expand Down
46 changes: 0 additions & 46 deletions pkg/util/kubernetes/hostinfo/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"

configmock "github.com/DataDog/datadog-agent/pkg/config/mock"
)

func TestExtractTags(t *testing.T) {
Expand Down Expand Up @@ -114,47 +112,3 @@ func TestExtractTags(t *testing.T) {
})
}
}

func TestGetLabelsToTags(t *testing.T) {
tests := []struct {
name string
configLabelsAsTags map[string]string
expectLabelsAsTags map[string]string
}{
{
name: "no labels in config",
expectLabelsAsTags: map[string]string{
"kubernetes.io/role": "kube_node_role",
},
},
{
name: "override node role label",
configLabelsAsTags: map[string]string{
"kubernetes.io/role": "role",
},
expectLabelsAsTags: map[string]string{
"kubernetes.io/role": "role",
},
},
{
name: "lower case all labels",
configLabelsAsTags: map[string]string{
"A": "a",
},
expectLabelsAsTags: map[string]string{
"kubernetes.io/role": "kube_node_role",
"a": "a",
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
config := configmock.New(t)
config.SetWithoutSource("kubernetes_node_labels_as_tags", test.configLabelsAsTags)

actuaLabelsAsTags := getLabelsToTags()
assert.Equal(t, test.expectLabelsAsTags, actuaLabelsAsTags)
})
}
}

0 comments on commit 133fbf1

Please sign in to comment.