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 target allocator readiness check #2883

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
16 changes: 16 additions & 0 deletions .chloggen/fix_load-initial-servicemonitors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: target allocator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix target allocator readiness check

# One or more tracking issues related to the change
issues: [2903]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
13 changes: 12 additions & 1 deletion cmd/otel-allocator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ func main() {
setupLog.Error(err, "Can't start the prometheus watcher")
os.Exit(1)
}
// apply the initial configuration
promConfig, loadErr := promWatcher.LoadConfig(ctx)
if loadErr != nil {
setupLog.Error(err, "Can't load initial Prometheus configuration from Prometheus CRs")
os.Exit(1)
}
loadErr = targetDiscoverer.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, promConfig.ScrapeConfigs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is done below in the rungroup which should be run once when it's first loaded. Do we need to also do it out here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. it's fine to do the load config here, but couldn't we just set it as cfg.PromConfig and let the rungroup handle the call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't in general, because cfg.PromConfig might not be empty. I could move it into the rungroup which established the Prometheus CR Watcher, if you think that'd be cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calls look similar, but they actually refer to different event sources.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i see that now, yes. I wonder if we should just send a message to the events channel to force an initial refresh rather than making new calls here?

if loadErr != nil {
setupLog.Error(err, "Can't load initial scrape targets from Prometheus CRs")
os.Exit(1)
}
runGroup.Add(
func() error {
promWatcherErr := promWatcher.Watch(eventChan, errChan)
Expand Down Expand Up @@ -138,7 +149,7 @@ func main() {
runGroup.Add(
func() error {
// Initial loading of the config file's scrape config
if cfg.PromConfig != nil {
if cfg.PromConfig != nil && len(cfg.PromConfig.ScrapeConfigs) > 0 {
err = targetDiscoverer.ApplyConfig(allocatorWatcher.EventSourceConfigMap, cfg.PromConfig.ScrapeConfigs)
if err != nil {
setupLog.Error(err, "Unable to apply initial configuration")
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/targetallocator/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) {
taConfig["collector_selector"] = taSpec.CollectorSelector

// Add scrape configs if present
if instance.Spec.ScrapeConfigs != nil {
if instance.Spec.ScrapeConfigs != nil && len(instance.Spec.ScrapeConfigs) > 0 {
taConfig["config"] = map[string]interface{}{
"scrape_configs": instance.Spec.ScrapeConfigs,
}
Expand Down
38 changes: 38 additions & 0 deletions internal/manifests/targetallocator/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
)
Expand Down Expand Up @@ -78,6 +79,43 @@ prometheus_cr:
assert.Equal(t, expectedLabels, actual.Labels)
assert.Equal(t, expectedData[targetAllocatorFilename], actual.Data[targetAllocatorFilename])

})
t.Run("should return target allocator config map without scrape configs", func(t *testing.T) {
expectedLabels["app.kubernetes.io/component"] = "opentelemetry-targetallocator"
expectedLabels["app.kubernetes.io/name"] = "my-instance-targetallocator"

expectedData := map[string]string{
targetAllocatorFilename: `allocation_strategy: consistent-hashing
collector_selector:
matchlabels:
app.kubernetes.io/component: opentelemetry-collector
app.kubernetes.io/instance: default.my-instance
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
matchexpressions: []
filter_strategy: relabel-config
prometheus_cr:
pod_monitor_selector: null
service_monitor_selector: null
`,
}
collector := collectorInstance()
targetAllocator := targetAllocatorInstance()
targetAllocator.Spec.ScrapeConfigs = []v1beta1.AnyConfig{}
cfg := config.New()
params := manifests.Params{
OtelCol: collector,
TargetAllocator: targetAllocator,
Config: cfg,
Log: logr.Discard(),
}
actual, err := ConfigMap(params)
require.NoError(t, err)

assert.Equal(t, "my-instance-targetallocator", actual.Name)
assert.Equal(t, expectedLabels, actual.Labels)
assert.Equal(t, expectedData[targetAllocatorFilename], actual.Data[targetAllocatorFilename])

})
t.Run("should return expected target allocator config map with label selectors", func(t *testing.T) {
expectedLabels["app.kubernetes.io/component"] = "opentelemetry-targetallocator"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,5 @@ spec:
enabled: true
prometheusCR:
enabled: true
scrapeInterval: 1s
scrapeInterval: 1s
jaronoff97 marked this conversation as resolved.
Show resolved Hide resolved
serviceAccount: ta
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: prometheus-cr
spec:
endpoints:
- port: monitoring
selector:
matchLabels:
app.kubernetes.io/managed-by: opentelemetry-operator
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: prometheus-cr
spec:
endpoints:
- port: monitoring
selector:
matchLabels:
app.kubernetes.io/managed-by: opentelemetry-operator
---
apiVersion: batch/v1
kind: Job
metadata:
Expand Down
Loading