Skip to content

Commit 414d68c

Browse files
committed
internal/manifests/collector: cleanup
Signed-off-by: Benedikt Bongartz <[email protected]>
1 parent d6d01bd commit 414d68c

9 files changed

+77
-28
lines changed

internal/manifests/collector/annotations.go

+23-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
// Annotations return the annotations for OpenTelemetryCollector pod.
26-
func Annotations(instance v1alpha2.OpenTelemetryCollector) map[string]string {
26+
func Annotations(instance v1alpha2.OpenTelemetryCollector) (map[string]string, error) {
2727
// new map every time, so that we don't touch the instance's annotations
2828
annotations := map[string]string{}
2929

@@ -42,14 +42,19 @@ func Annotations(instance v1alpha2.OpenTelemetryCollector) map[string]string {
4242
}
4343
}
4444

45+
hash, err := getConfigMapSHA(instance.Spec.Config)
46+
if err != nil {
47+
return nil, err
48+
}
49+
4550
// make sure sha256 for configMap is always calculated
46-
annotations["opentelemetry-operator-config/sha256"] = getConfigMapSHA(instance.Spec.Config)
51+
annotations["opentelemetry-operator-config/sha256"] = hash
4752

48-
return annotations
53+
return annotations, nil
4954
}
5055

5156
// PodAnnotations return the spec annotations for OpenTelemetryCollector pod.
52-
func PodAnnotations(instance v1alpha2.OpenTelemetryCollector) map[string]string {
57+
func PodAnnotations(instance v1alpha2.OpenTelemetryCollector) (map[string]string, error) {
5358
// new map every time, so that we don't touch the instance's annotations
5459
podAnnotations := map[string]string{}
5560

@@ -58,24 +63,32 @@ func PodAnnotations(instance v1alpha2.OpenTelemetryCollector) map[string]string
5863
podAnnotations[k] = v
5964
}
6065

66+
annotations, err := Annotations(instance)
67+
if err != nil {
68+
return nil, err
69+
}
6170
// propagating annotations from metadata.annotations
62-
for kMeta, vMeta := range Annotations(instance) {
71+
for kMeta, vMeta := range annotations {
6372
if _, found := podAnnotations[kMeta]; !found {
6473
podAnnotations[kMeta] = vMeta
6574
}
6675
}
6776

77+
hash, err := getConfigMapSHA(instance.Spec.Config)
78+
if err != nil {
79+
return nil, err
80+
}
6881
// make sure sha256 for configMap is always calculated
69-
podAnnotations["opentelemetry-operator-config/sha256"] = getConfigMapSHA(instance.Spec.Config)
82+
podAnnotations["opentelemetry-operator-config/sha256"] = hash
7083

71-
return podAnnotations
84+
return podAnnotations, nil
7285
}
7386

74-
func getConfigMapSHA(config v1alpha2.Config) string {
87+
func getConfigMapSHA(config v1alpha2.Config) (string, error) {
7588
b, err := json.Marshal(&config)
7689
if err != nil {
77-
return "invalid"
90+
return "", err
7891
}
7992
h := sha256.Sum256(b)
80-
return fmt.Sprintf("%x", h)
93+
return fmt.Sprintf("%x", h), nil
8194
}

internal/manifests/collector/annotations_test.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223

2324
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
@@ -44,8 +45,10 @@ func TestDefaultAnnotations(t *testing.T) {
4445
}
4546

4647
// test
47-
annotations := Annotations(otelcol)
48-
podAnnotations := PodAnnotations(otelcol)
48+
annotations, err := Annotations(otelcol)
49+
require.NoError(t, err)
50+
podAnnotations, err := PodAnnotations(otelcol)
51+
require.NoError(t, err)
4952

5053
//verify
5154
assert.Equal(t, "true", annotations["prometheus.io/scrape"])
@@ -76,8 +79,10 @@ func TestNonDefaultPodAnnotation(t *testing.T) {
7679
}
7780

7881
// test
79-
annotations := Annotations(otelcol)
80-
podAnnotations := PodAnnotations(otelcol)
82+
annotations, err := Annotations(otelcol)
83+
require.NoError(t, err)
84+
podAnnotations, err := PodAnnotations(otelcol)
85+
require.NoError(t, err)
8186

8287
//verify
8388
assert.NotContains(t, annotations, "prometheus.io/scrape", "Prometheus scrape annotation should not exist")
@@ -116,8 +121,10 @@ func TestUserAnnotations(t *testing.T) {
116121
}
117122

118123
// test
119-
annotations := Annotations(otelcol)
120-
podAnnotations := PodAnnotations(otelcol)
124+
annotations, err := Annotations(otelcol)
125+
require.NoError(t, err)
126+
podAnnotations, err := PodAnnotations(otelcol)
127+
require.NoError(t, err)
121128

122129
//verify
123130
assert.Equal(t, "false", annotations["prometheus.io/scrape"])
@@ -141,8 +148,10 @@ func TestAnnotationsPropagateDown(t *testing.T) {
141148
}
142149

143150
// test
144-
annotations := Annotations(otelcol)
145-
podAnnotations := PodAnnotations(otelcol)
151+
annotations, err := Annotations(otelcol)
152+
require.NoError(t, err)
153+
podAnnotations, err := PodAnnotations(otelcol)
154+
require.NoError(t, err)
146155

147156
// verify
148157
assert.Len(t, annotations, 5)

internal/manifests/collector/config_replace.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func ReplaceConfig(instance v1alpha2.OpenTelemetryCollector) (string, error) {
4949
}
5050
// Check if TargetAllocator is enabled, if not, return the original config
5151
if !instance.Spec.TargetAllocator.Enabled {
52-
return string(cfgStr), nil
52+
return cfgStr, nil
5353
}
5454

5555
config, err := adapters.ConfigFromString(cfgStr)

internal/manifests/collector/daemonset.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,15 @@ func DaemonSet(params manifests.Params) (*appsv1.DaemonSet, error) {
3434
name := naming.Collector(otelCol.Name)
3535
labels := manifestutils.Labels(otelCol.ObjectMeta, name, otelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
3636

37-
annotations := Annotations(otelCol)
38-
podAnnotations := PodAnnotations(otelCol)
37+
annotations, err := Annotations(otelCol)
38+
if err != nil {
39+
return nil, err
40+
}
41+
podAnnotations, err := PodAnnotations(otelCol)
42+
if err != nil {
43+
return nil, err
44+
}
45+
3946
return &appsv1.DaemonSet{
4047
ObjectMeta: metav1.ObjectMeta{
4148
Name: naming.Collector(otelCol.Name),

internal/manifests/collector/deployment.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,15 @@ func Deployment(params manifests.Params) (*appsv1.Deployment, error) {
3434
name := naming.Collector(otelCol.Name)
3535
labels := manifestutils.Labels(otelCol.ObjectMeta, name, otelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
3636

37-
annotations := Annotations(otelCol)
38-
podAnnotations := PodAnnotations(otelCol)
37+
annotations, err := Annotations(otelCol)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
podAnnotations, err := PodAnnotations(otelCol)
43+
if err != nil {
44+
return nil, err
45+
}
3946

4047
return &appsv1.Deployment{
4148
ObjectMeta: metav1.ObjectMeta{

internal/manifests/collector/horizontalpodautoscaler.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ func HorizontalPodAutoscaler(params manifests.Params) (client.Object, error) {
3434
}
3535
name := naming.Collector(otelCol.Name)
3636
labels := manifestutils.Labels(otelCol.ObjectMeta, name, otelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
37-
annotations := Annotations(otelCol)
37+
annotations, err := Annotations(otelCol)
38+
if err != nil {
39+
return nil, err
40+
}
41+
3842
var result client.Object
3943

4044
objectMeta := metav1.ObjectMeta{

internal/manifests/collector/horizontalpodautoscaler_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ func TestHPA(t *testing.T) {
102102
// verify
103103
assert.Equal(t, "my-instance-collector", hpa.Name)
104104
assert.Equal(t, "my-instance-collector", hpa.Labels["app.kubernetes.io/name"])
105-
// require.NotNil(t, hpa.Spec.MinReplicas)
106105
assert.Equal(t, &minReplicas, hpa.Spec.MinReplicas)
107106
assert.Equal(t, maxReplicas, hpa.Spec.MaxReplicas)
108107
assert.Equal(t, 2, len(hpa.Spec.Metrics))

internal/manifests/collector/poddisruptionbudget.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ func PodDisruptionBudget(params manifests.Params) (*policyV1.PodDisruptionBudget
3737

3838
name := naming.Collector(otelCol.Name)
3939
labels := manifestutils.Labels(otelCol.ObjectMeta, name, otelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
40-
annotations := Annotations(otelCol)
40+
annotations, err := Annotations(otelCol)
41+
if err != nil {
42+
return nil, err
43+
}
4144

4245
objectMeta := metav1.ObjectMeta{
4346
Name: naming.PodDisruptionBudget(otelCol.Name),

internal/manifests/collector/statefulset.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,15 @@ func StatefulSet(params manifests.Params) (*appsv1.StatefulSet, error) {
3434
name := naming.Collector(otelCol.Name)
3535
labels := manifestutils.Labels(otelCol.ObjectMeta, name, otelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
3636

37-
annotations := Annotations(otelCol)
38-
podAnnotations := PodAnnotations(otelCol)
37+
annotations, err := Annotations(otelCol)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
podAnnotations, err := PodAnnotations(otelCol)
43+
if err != nil {
44+
return nil, err
45+
}
3946

4047
return &appsv1.StatefulSet{
4148
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)