Skip to content

Commit

Permalink
Fix collector config update (#670)
Browse files Browse the repository at this point in the history
* fix/460 : Added opentelemetry-operator-config/sha256 annotaton to pod annotations

* PR comments fixes

* Created e2e test
  • Loading branch information
mcariapas authored Jan 27, 2022
1 parent 7c69ad5 commit d965ac3
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 10 deletions.
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.

16 changes: 16 additions & 0 deletions pkg/collector/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ func Annotations(instance v1alpha1.OpenTelemetryCollector) map[string]string {
return annotations
}

// PodAnnotations return the spec annotations for OpenTelemetryCollector pod.
func PodAnnotations(instance v1alpha1.OpenTelemetryCollector) map[string]string {
// new map every time, so that we don't touch the instance's annotations
podAnnotations := map[string]string{}

// allow override of pod annotations
for k, v := range instance.Spec.PodAnnotations {
podAnnotations[k] = v
}

// make sure sha256 for configMap is always calculated
podAnnotations["opentelemetry-operator-config/sha256"] = getConfigMapSHA(instance.Spec.Config)

return podAnnotations
}

func getConfigMapSHA(config string) string {
h := sha256.Sum256([]byte(config))
return fmt.Sprintf("%x", h)
Expand Down
9 changes: 9 additions & 0 deletions pkg/collector/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ func TestDefaultAnnotations(t *testing.T) {

// test
annotations := Annotations(otelcol)
podAnnotations := PodAnnotations(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 @@ -64,12 +66,14 @@ func TestUserAnnotations(t *testing.T) {

// test
annotations := Annotations(otelcol)
podAnnotations := PodAnnotations(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 +82,17 @@ 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)
podAnnotations := PodAnnotations(otelcol)

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

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

return appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -46,7 +47,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
3 changes: 2 additions & 1 deletion pkg/collector/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Deployment(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTele
labels["app.kubernetes.io/name"] = naming.Collector(otelcol)

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

return appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -47,7 +48,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
3 changes: 2 additions & 1 deletion pkg/collector/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func StatefulSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTel
labels["app.kubernetes.io/name"] = naming.Collector(otelcol)

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

return appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -47,7 +48,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
26 changes: 26 additions & 0 deletions tests/e2e/smoke-restarting-deployment/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: restarting-collector
status:
readyReplicas: 1

---

apiVersion: v1
kind: Pod
spec:
containers:
- name: otc-container
status:
phase: Running

---

apiVersion: v1
kind: Service
metadata:
name: restarting-collector
spec:
ports:
- targetPort: 14250
7 changes: 7 additions & 0 deletions tests/e2e/smoke-restarting-deployment/00-errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Service
metadata:
name: restarting-collector
spec:
ports:
- targetPort: 4317
21 changes: 21 additions & 0 deletions tests/e2e/smoke-restarting-deployment/00-install.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: opentelemetry.io/v1alpha1
kind: OpenTelemetryCollector
metadata:
name: restarting
spec:
config: |
receivers:
jaeger:
protocols:
grpc:
processors:
exporters:
logging:
service:
pipelines:
traces:
receivers: [jaeger]
processors: []
exporters: [logging]
args:
metrics-level: detailed
27 changes: 27 additions & 0 deletions tests/e2e/smoke-restarting-deployment/01-assert-second-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: restarting-collector
status:
readyReplicas: 1

---

apiVersion: v1
kind: Pod
spec:
containers:
- name: otc-container
status:
phase: Running

---

apiVersion: v1
kind: Service
metadata:
name: restarting-collector
spec:
ports:
- targetPort: 14250
- targetPort: 4317
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: opentelemetry.io/v1alpha1
kind: OpenTelemetryCollector
metadata:
name: restarting
spec:
config: |
receivers:
jaeger:
protocols:
grpc:
otlp:
protocols:
grpc:
processors:
exporters:
logging:
service:
pipelines:
traces:
receivers: [jaeger, otlp]
processors: []
exporters: [logging]
args:
metrics-level: detailed

0 comments on commit d965ac3

Please sign in to comment.