Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix collector config update #670

Merged
merged 3 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

364 changes: 364 additions & 0 deletions go.sum

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions pkg/collector/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
)

// Annotations return the annotations for OpenTelemetryCollector pod.
func Annotations(instance v1alpha1.OpenTelemetryCollector) map[string]string {
func Annotations(instance v1alpha1.OpenTelemetryCollector) (map[string]string, map[string]string) {
mcariapas marked this conversation as resolved.
Show resolved Hide resolved
// new map every time, so that we don't touch the instance's annotations
annotations := map[string]string{}
annotations, podAnnotations := map[string]string{}, map[string]string{}

// set default prometheus annotations
annotations["prometheus.io/scrape"] = "true"
Expand All @@ -37,10 +37,18 @@ func Annotations(instance v1alpha1.OpenTelemetryCollector) map[string]string {
annotations[k] = v
}
}

if nil != instance.Spec.PodAnnotations {
mcariapas marked this conversation as resolved.
Show resolved Hide resolved
for k, v := range instance.Spec.PodAnnotations {
podAnnotations[k] = v
}
}

// make sure sha256 for configMap is always calculated
annotations["opentelemetry-operator-config/sha256"] = getConfigMapSHA(instance.Spec.Config)
podAnnotations["opentelemetry-operator-config/sha256"] = getConfigMapSHA(instance.Spec.Config)
mcariapas marked this conversation as resolved.
Show resolved Hide resolved

return annotations
return annotations, podAnnotations
}

func getConfigMapSHA(config string) string {
Expand Down
12 changes: 9 additions & 3 deletions pkg/collector/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ func TestDefaultAnnotations(t *testing.T) {
}

// test
annotations := Annotations(otelcol)
annotations, podAnnotations := Annotations(otelcol)

//verify
assert.Equal(t, "true", annotations["prometheus.io/scrape"])
assert.Equal(t, "8888", annotations["prometheus.io/port"])
assert.Equal(t, "/metrics", annotations["prometheus.io/path"])
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", annotations["opentelemetry-operator-config/sha256"])
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", podAnnotations["opentelemetry-operator-config/sha256"])
}

func TestUserAnnotations(t *testing.T) {
Expand All @@ -63,13 +64,14 @@ func TestUserAnnotations(t *testing.T) {
}

// test
annotations := Annotations(otelcol)
annotations, podAnnotations := Annotations(otelcol)

//verify
assert.Equal(t, "false", annotations["prometheus.io/scrape"])
assert.Equal(t, "1234", annotations["prometheus.io/port"])
assert.Equal(t, "/test", annotations["prometheus.io/path"])
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", annotations["opentelemetry-operator-config/sha256"])
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", podAnnotations["opentelemetry-operator-config/sha256"])
}

func TestAnnotationsPropagateDown(t *testing.T) {
Expand All @@ -78,12 +80,16 @@ func TestAnnotationsPropagateDown(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"myapp": "mycomponent"},
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
PodAnnotations: map[string]string{"pod_annotation": "pod_annotation_value"},
},
}

// test
annotations := Annotations(otelcol)
annotations, podAnnotations := Annotations(otelcol)

// verify
assert.Len(t, annotations, 5)
assert.Equal(t, "mycomponent", annotations["myapp"])
assert.Equal(t, "pod_annotation_value", podAnnotations["pod_annotation"])
}
4 changes: 2 additions & 2 deletions pkg/collector/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func DaemonSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem
labels := Labels(otelcol)
labels["app.kubernetes.io/name"] = naming.Collector(otelcol)

annotations := Annotations(otelcol)
annotations, podAnnotations := Annotations(otelcol)

return appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -46,7 +46,7 @@ func DaemonSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Annotations: otelcol.Spec.PodAnnotations,
Annotations: podAnnotations,
},
Spec: corev1.PodSpec{
ServiceAccountName: ServiceAccountName(otelcol),
Expand Down
10 changes: 8 additions & 2 deletions pkg/collector/daemonset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ func TestDaemonSetNewDefault(t *testing.T) {

assert.Len(t, d.Spec.Template.Spec.Containers, 1)

// none of the default annotations should propagate down to the pod
assert.Empty(t, d.Spec.Template.Annotations)
// verify sha256 podAnnotation
expectedAnnotations := map[string]string{
"opentelemetry-operator-config/sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}
assert.Equal(t, expectedAnnotations, d.Spec.Template.Annotations)

// the pod selector should match the pod spec's labels
assert.Equal(t, d.Spec.Selector.MatchLabels, d.Spec.Template.Labels)
Expand Down Expand Up @@ -90,6 +93,9 @@ func TestDaemonsetPodAnnotations(t *testing.T) {
// test
ds := DaemonSet(cfg, logger, otelcol)

//Add sha256 podAnnotation
testPodAnnotationValues["opentelemetry-operator-config/sha256"] = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

// verify
assert.Equal(t, "my-instance-collector", ds.Name)
assert.Equal(t, testPodAnnotationValues, ds.Spec.Template.Annotations)
Expand Down
4 changes: 2 additions & 2 deletions pkg/collector/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Deployment(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTele
labels := Labels(otelcol)
labels["app.kubernetes.io/name"] = naming.Collector(otelcol)

annotations := Annotations(otelcol)
annotations, podAnnotations := Annotations(otelcol)

return appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -47,7 +47,7 @@ func Deployment(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTele
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Annotations: otelcol.Spec.PodAnnotations,
Annotations: podAnnotations,
},
Spec: corev1.PodSpec{
ServiceAccountName: ServiceAccountName(otelcol),
Expand Down
10 changes: 8 additions & 2 deletions pkg/collector/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ func TestDeploymentNewDefault(t *testing.T) {

assert.Len(t, d.Spec.Template.Spec.Containers, 1)

// none of the default annotations should propagate down to the pod
assert.Empty(t, d.Spec.Template.Annotations)
// verify sha256 podAnnotation
expectedAnnotations := map[string]string{
"opentelemetry-operator-config/sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}
assert.Equal(t, expectedAnnotations, d.Spec.Template.Annotations)

// the pod selector should match the pod spec's labels
assert.Equal(t, d.Spec.Template.Labels, d.Spec.Selector.MatchLabels)
Expand All @@ -82,6 +85,9 @@ func TestDeploymentPodAnnotations(t *testing.T) {
// test
d := Deployment(cfg, logger, otelcol)

//Add sha256 podAnnotation
testPodAnnotationValues["opentelemetry-operator-config/sha256"] = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

// verify
assert.Equal(t, "my-instance-collector", d.Name)
assert.Equal(t, testPodAnnotationValues, d.Spec.Template.Annotations)
Expand Down
4 changes: 2 additions & 2 deletions pkg/collector/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func StatefulSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTel
labels := Labels(otelcol)
labels["app.kubernetes.io/name"] = naming.Collector(otelcol)

annotations := Annotations(otelcol)
annotations, podAnnotations := Annotations(otelcol)

return appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -47,7 +47,7 @@ func StatefulSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTel
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Annotations: otelcol.Spec.PodAnnotations,
Annotations: podAnnotations,
},
Spec: corev1.PodSpec{
ServiceAccountName: ServiceAccountName(otelcol),
Expand Down
10 changes: 8 additions & 2 deletions pkg/collector/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ func TestStatefulSetNewDefault(t *testing.T) {

assert.Len(t, ss.Spec.Template.Spec.Containers, 1)

// none of the default annotations should propagate down to the pod
assert.Empty(t, ss.Spec.Template.Annotations)
// verify sha256 podAnnotation
expectedAnnotations := map[string]string{
"opentelemetry-operator-config/sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}
assert.Equal(t, expectedAnnotations, ss.Spec.Template.Annotations)

// the pod selector should match the pod spec's labels
assert.Equal(t, ss.Spec.Selector.MatchLabels, ss.Spec.Template.Labels)
Expand Down Expand Up @@ -141,6 +144,9 @@ func TestStatefulSetPodAnnotations(t *testing.T) {
// test
ss := StatefulSet(cfg, logger, otelcol)

//Add sha256 podAnnotation
testPodAnnotationValues["opentelemetry-operator-config/sha256"] = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

// verify
assert.Equal(t, "my-instance-collector", ss.Name)
assert.Equal(t, testPodAnnotationValues, ss.Spec.Template.Annotations)
Expand Down