forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotations.go
98 lines (84 loc) · 2.98 KB
/
annotations.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
89
90
91
92
93
94
95
96
97
98
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package manifestutils
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
)
// Annotations return the annotations for OpenTelemetryCollector pod.
func Annotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []string) (map[string]string, error) {
// new map every time, so that we don't touch the instance's annotations
annotations := map[string]string{}
// Enable Prometheus annotations by default if DisablePrometheusAnnotations is nil or true
if !instance.Spec.Observability.Metrics.DisablePrometheusAnnotations {
// Set default Prometheus annotations
annotations["prometheus.io/scrape"] = "true"
annotations["prometheus.io/port"] = "8888"
annotations["prometheus.io/path"] = "/metrics"
}
// allow override of prometheus annotations
if nil != instance.ObjectMeta.Annotations {
for k, v := range instance.ObjectMeta.Annotations {
if !isFilteredSet(k, filterAnnotations) {
annotations[k] = v
}
}
}
hash, err := getConfigMapSHA(instance.Spec.Config)
if err != nil {
return nil, err
}
// make sure sha256 for configMap is always calculated
annotations["opentelemetry-operator-config/sha256"] = hash
return annotations, nil
}
// PodAnnotations return the spec annotations for OpenTelemetryCollector pod.
func PodAnnotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []string) (map[string]string, error) {
// new map every time, so that we don't touch the instance's annotations
podAnnotations := map[string]string{}
if nil != instance.Spec.PodAnnotations {
for k, v := range instance.Spec.PodAnnotations {
if !isFilteredSet(k, filterAnnotations) {
podAnnotations[k] = v
}
}
}
annotations, err := Annotations(instance, filterAnnotations)
if err != nil {
return nil, err
}
// propagating annotations from metadata.annotations
for kMeta, vMeta := range annotations {
if _, found := podAnnotations[kMeta]; !found {
podAnnotations[kMeta] = vMeta
}
}
hash, err := getConfigMapSHA(instance.Spec.Config)
if err != nil {
return nil, err
}
// make sure sha256 for configMap is always calculated
podAnnotations["opentelemetry-operator-config/sha256"] = hash
return podAnnotations, nil
}
func getConfigMapSHA(config v1beta1.Config) (string, error) {
b, err := json.Marshal(&config)
if err != nil {
return "", err
}
h := sha256.Sum256(b)
return fmt.Sprintf("%x", h), nil
}