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

Generate target allocator manifests from TargetAllocator CR #2725

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 deletions controllers/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
"github.com/open-telemetry/opentelemetry-operator/pkg/featuregate"
)

Expand Down Expand Up @@ -1350,6 +1351,7 @@ collector_selector:
app.kubernetes.io/instance: test.test
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
matchexpressions: []
config:
scrape_configs:
- job_name: example
Expand Down Expand Up @@ -1408,7 +1410,7 @@ prometheus_cr:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-targetallocator-config/hash": "59307aaa5652c8723f7803aa2d2b631389d1a0267444a4a8dc559878b5c4aa2c",
"opentelemetry-targetallocator-config/hash": "ba38217bad7e399f1210b90a464252159a8c4e17060c246799b8e4cb29a6f18f",
},
},
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -1747,6 +1749,7 @@ collector_selector:
app.kubernetes.io/instance: test.test
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
matchexpressions: []
config:
scrape_configs:
- job_name: example
Expand Down Expand Up @@ -1805,7 +1808,7 @@ prometheus_cr:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-targetallocator-config/hash": "59307aaa5652c8723f7803aa2d2b631389d1a0267444a4a8dc559878b5c4aa2c",
"opentelemetry-targetallocator-config/hash": "ba38217bad7e399f1210b90a464252159a8c4e17060c246799b8e4cb29a6f18f",
},
},
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -1972,10 +1975,13 @@ prometheus_cr:
Config: cfg,
OtelCol: tt.args.instance,
}
targetAllocator, err := collector.TargetAllocator(params)
require.NoError(t, err)
params.TargetAllocator = *targetAllocator
if len(tt.featuregates) > 0 {
fg := strings.Join(tt.featuregates, ",")
flagset := featuregate.Flags(colfeaturegate.GlobalRegistry())
if err := flagset.Set(featuregate.FeatureGatesFlag, fg); err != nil {
if err = flagset.Set(featuregate.FeatureGatesFlag, fg); err != nil {
t.Errorf("featuregate setting error = %v", err)
return
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
assert.NoError(t, err)

taConfig := make(map[interface{}]interface{})
taConfig["collector_selector"] = map[string]any{
"matchlabels": map[string]string{
taConfig["collector_selector"] = metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/instance": "default.test",
"app.kubernetes.io/managed-by": "opentelemetry-operator",
"app.kubernetes.io/component": "opentelemetry-collector",
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/targetallocator/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
const configMapHashAnnotationKey = "opentelemetry-targetallocator-config/hash"

// Annotations returns the annotations for the TargetAllocator Pod.
func Annotations(instance v1beta1.OpenTelemetryCollector, configMap *v1.ConfigMap) map[string]string {
func Annotations(instance v1beta1.TargetAllocator, configMap *v1.ConfigMap) map[string]string {
// Make a copy of PodAnnotations to be safe
annotations := make(map[string]string, len(instance.Spec.PodAnnotations))
for key, value := range instance.Spec.PodAnnotations {
Expand Down
18 changes: 9 additions & 9 deletions internal/manifests/targetallocator/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
)

func TestPodAnnotations(t *testing.T) {
instance := collectorInstance()
instance := targetAllocatorInstance()
instance.Spec.PodAnnotations = map[string]string{
"key": "value",
}
Expand All @@ -39,26 +38,27 @@ func TestPodAnnotations(t *testing.T) {

func TestConfigMapHash(t *testing.T) {
cfg := config.New()
instance := collectorInstance()
collector := collectorInstance()
targetAllocator := targetAllocatorInstance()
params := manifests.Params{
OtelCol: instance,
Config: cfg,
Log: logr.Discard(),
OtelCol: collector,
TargetAllocator: targetAllocator,
Config: cfg,
Log: logr.Discard(),
}
expectedConfigMap, err := ConfigMap(params)
require.NoError(t, err)
expectedConfig := expectedConfigMap.Data[targetAllocatorFilename]
require.NotEmpty(t, expectedConfig)
expectedHash := sha256.Sum256([]byte(expectedConfig))
annotations := Annotations(instance, expectedConfigMap)
annotations := Annotations(targetAllocator, expectedConfigMap)
require.Contains(t, annotations, configMapHashAnnotationKey)
cmHash := annotations[configMapHashAnnotationKey]
assert.Equal(t, fmt.Sprintf("%x", expectedHash), cmHash)
}

func TestInvalidConfigNoHash(t *testing.T) {
instance := collectorInstance()
instance.Spec.Config = v1beta1.Config{}
instance := targetAllocatorInstance()
annotations := Annotations(instance, nil)
require.NotContains(t, annotations, configMapHashAnnotationKey)
}
64 changes: 25 additions & 39 deletions internal/manifests/targetallocator/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import (

"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/targetallocator/adapters"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)

Expand All @@ -32,61 +29,50 @@ const (
)

func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) {
name := naming.TAConfigMap(params.OtelCol.Name)
labels := Labels(params.OtelCol, name)
// TODO: https://github.com/open-telemetry/opentelemetry-operator/issues/2603
cfgStr, err := params.OtelCol.Spec.Config.Yaml()
if err != nil {
return &corev1.ConfigMap{}, err
}

// Collector supports environment variable substitution, but the TA does not.
// TA ConfigMap should have a single "$", as it does not support env var substitution
prometheusReceiverConfig, err := adapters.UnescapeDollarSignsInPromConfig(cfgStr)
if err != nil {
return &corev1.ConfigMap{}, err
}
instance := params.TargetAllocator
name := naming.TAConfigMap(instance.Name)
labels := Labels(instance, name)
taSpec := instance.Spec

taConfig := make(map[interface{}]interface{})
prometheusCRConfig := make(map[interface{}]interface{})
collectorSelectorLabels := manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, collector.ComponentOpenTelemetryCollector)
taConfig["collector_selector"] = map[string]any{
"matchlabels": collectorSelectorLabels,
}
taConfig["collector_selector"] = taSpec.CollectorSelector
// The below instruction is here for compatibility with the previous target allocator version
// TODO: Drop it after 3 more versions
taConfig["label_selector"] = collectorSelectorLabels
// We only take the "config" from the returned object, if it's present
if prometheusConfig, ok := prometheusReceiverConfig["config"]; ok {
taConfig["config"] = prometheusConfig
taConfig["label_selector"] = taSpec.CollectorSelector.MatchLabels

// Add scrape configs if present
if instance.Spec.ScrapeConfigs != nil {
taConfig["config"] = map[string]interface{}{
"scrape_configs": instance.Spec.ScrapeConfigs,
}
}

if len(params.OtelCol.Spec.TargetAllocator.AllocationStrategy) > 0 {
taConfig["allocation_strategy"] = params.OtelCol.Spec.TargetAllocator.AllocationStrategy
if len(taSpec.AllocationStrategy) > 0 {
taConfig["allocation_strategy"] = taSpec.AllocationStrategy
} else {
taConfig["allocation_strategy"] = v1beta1.TargetAllocatorAllocationStrategyConsistentHashing
}
taConfig["filter_strategy"] = taSpec.FilterStrategy

taConfig["filter_strategy"] = params.OtelCol.Spec.TargetAllocator.FilterStrategy

if params.OtelCol.Spec.TargetAllocator.PrometheusCR.ScrapeInterval.Size() > 0 {
prometheusCRConfig["scrape_interval"] = params.OtelCol.Spec.TargetAllocator.PrometheusCR.ScrapeInterval.Duration
if taSpec.PrometheusCR.ScrapeInterval.Size() > 0 {
prometheusCRConfig["scrape_interval"] = taSpec.PrometheusCR.ScrapeInterval.Duration
}

prometheusCRConfig["service_monitor_selector"] = params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector
prometheusCRConfig["service_monitor_selector"] = taSpec.PrometheusCR.ServiceMonitorSelector

// The below instruction is here for compatibility with the previous target allocator version
// TODO: Drop it after 3 more versions
if params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector != nil {
taConfig["service_monitor_selector"] = &params.OtelCol.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector.MatchLabels
if taSpec.PrometheusCR.ServiceMonitorSelector != nil {
taConfig["service_monitor_selector"] = &taSpec.PrometheusCR.ServiceMonitorSelector.MatchLabels
}

prometheusCRConfig["pod_monitor_selector"] = params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector
prometheusCRConfig["pod_monitor_selector"] = taSpec.PrometheusCR.PodMonitorSelector

// The below instruction is here for compatibility with the previous target allocator version
// TODO: Drop it after 3 more versions
if params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector != nil {
taConfig["pod_monitor_selector"] = &params.OtelCol.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector.MatchLabels
if taSpec.PrometheusCR.PodMonitorSelector != nil {
taConfig["pod_monitor_selector"] = &taSpec.PrometheusCR.PodMonitorSelector.MatchLabels
}

if len(prometheusCRConfig) > 0 {
Expand All @@ -101,9 +87,9 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: params.OtelCol.Namespace,
Namespace: instance.Namespace,
Labels: labels,
Annotations: params.OtelCol.Annotations,
Annotations: instance.Annotations,
},
Data: map[string]string{
targetAllocatorFilename: string(taConfigYAML),
Expand Down
Loading
Loading