Skip to content

Commit

Permalink
Fix Target Allocator config reload (#1924)
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm authored Jul 14, 2023
1 parent ec1e0a3 commit 56f5de9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
12 changes: 9 additions & 3 deletions pkg/targetallocator/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,21 @@ func Annotations(instance v1alpha1.OpenTelemetryCollector) map[string]string {

configMap, err := ConfigMap(instance)
if err == nil {
annotations[configMapHashAnnotationKey] = getConfigMapSHA(configMap)
cmHash := getConfigMapSHA(configMap)
if cmHash != "" {
annotations[configMapHashAnnotationKey] = getConfigMapSHA(configMap)
}
}

return annotations
}

// getConfigMapSHA returns the hash of the content of the TA ConfigMap.
func getConfigMapSHA(configMap v1.ConfigMap) string {
configBytes := configMap.BinaryData[targetAllocatorFilename]
h := sha256.Sum256(configBytes)
configString, ok := configMap.Data[targetAllocatorFilename]
if !ok {
return ""
}
h := sha256.Sum256([]byte(configString))
return fmt.Sprintf("%x", h)
}
9 changes: 8 additions & 1 deletion pkg/targetallocator/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package targetallocator

import (
"crypto/sha256"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -32,10 +34,15 @@ func TestPodAnnotations(t *testing.T) {

func TestConfigMapHash(t *testing.T) {
instance := collectorInstance()
expectedConfigMap, err := ConfigMap(instance)
require.NoError(t, err)
expectedConfig := expectedConfigMap.Data[targetAllocatorFilename]
require.NotEmpty(t, expectedConfig)
expectedHash := sha256.Sum256([]byte(expectedConfig))
annotations := Annotations(instance)
require.Contains(t, annotations, configMapHashAnnotationKey)
cmHash := annotations[configMapHashAnnotationKey]
assert.Len(t, cmHash, 64)
assert.Equal(t, fmt.Sprintf("%x", expectedHash), cmHash)
}

func TestInvalidConfigNoHash(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/smoke-targetallocator/00-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ metadata:
status:
replicas: 1
readyReplicas: 1
observedGeneration: 1
---
apiVersion: v1
kind: ConfigMap
Expand Down
6 changes: 4 additions & 2 deletions tests/e2e/smoke-targetallocator/00-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ spec:
enabled: true
serviceAccount: ta
image: "local/opentelemetry-operator-targetallocator:e2e"
prometheusCR:
enabled: true
config: |
receivers:
jaeger:
Expand All @@ -42,9 +44,9 @@ spec:
scrape_interval: 10s
static_configs:
- targets: [ '0.0.0.0:8888' ]
processors:
exporters:
logging:
service:
Expand Down
6 changes: 4 additions & 2 deletions tests/e2e/smoke-targetallocator/01-change-ta-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ spec:
image: "local/opentelemetry-operator-targetallocator:e2e"
prometheusCR:
enabled: true
serviceMonitorSelector:
key: value # this is just an arbitrary change to the target allocator config
config: |
receivers:
jaeger:
Expand All @@ -24,9 +26,9 @@ spec:
scrape_interval: 10s
static_configs:
- targets: [ '0.0.0.0:8888' ]
processors:
exporters:
logging:
service:
Expand Down

0 comments on commit 56f5de9

Please sign in to comment.