-
Notifications
You must be signed in to change notification settings - Fork 104
/
labels.go
79 lines (65 loc) · 2.43 KB
/
labels.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
// 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 2016-present Datadog, Inc.
package object
import (
"fmt"
"strings"
apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
"github.com/go-logr/logr"
"github.com/gobwas/glob"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetDefaultLabels return default labels attached to a DatadogAgent resource.
func GetDefaultLabels(dda metav1.Object, instanceName, version string) map[string]string {
labels := make(map[string]string)
labels[kubernetes.AppKubernetesNameLabelKey] = "datadog-agent-deployment"
labels[kubernetes.AppKubernetesInstanceLabelKey] = instanceName
labels[kubernetes.AppKubernetesPartOfLabelKey] = NewPartOfLabelValue(dda).String()
labels[kubernetes.AppKubernetesVersionLabelKey] = version
labels[kubernetes.AppKubernetesManageByLabelKey] = "datadog-operator"
// Copy Datadog labels from DDA Labels
for k, v := range dda.GetLabels() {
if strings.HasPrefix(k, DatadogTagPrefix) {
labels[k] = v
}
}
return labels
}
// GetDefaultAnnotations return default annotations attached to a DatadogAgent resource.
func GetDefaultAnnotations(dda metav1.Object) map[string]string {
// Currently we don't have any annotation to set by default
return map[string]string{}
}
// MergeAnnotationsLabels used to merge Annotations and Labels
func MergeAnnotationsLabels(logger logr.Logger, previousVal map[string]string, newVal map[string]string, filter string) map[string]string {
var globFilter glob.Glob
var err error
if filter != "" {
globFilter, err = glob.Compile(filter)
if err != nil {
logger.Error(err, "Unable to parse glob filter for metadata/annotations - discarding everything", "filter", filter)
}
}
mergedMap := make(map[string]string, len(newVal))
for k, v := range newVal {
mergedMap[k] = v
}
// Copy from previous if not in new match and matches globfilter
for k, v := range previousVal {
if _, found := newVal[k]; !found {
if (globFilter != nil && globFilter.Match(k)) || strings.Contains(k, "datadoghq.com") {
mergedMap[k] = v
}
}
}
return mergedMap
}
func GetChecksumAnnotationKey(keyName string) string {
if keyName == "" {
return ""
}
return fmt.Sprintf(apicommon.MD5ChecksumAnnotationKey, keyName)
}