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

Use standard label selectors in target allocator config #2564

Merged
merged 2 commits into from
Jan 25, 2024
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
18 changes: 18 additions & 0 deletions .chloggen/feat_ta_monitor-selectors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, 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: Use standard K8s label selectors for Prometheus CRs in target allocator config

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

# (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: |
This is a breaking change only for users of standalone target allocator. Operator users are unaffected.
The operator is still compatible with previous target allocator versions, and will be for the next 3 releases.
30 changes: 15 additions & 15 deletions cmd/otel-allocator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ const (
)

type Config struct {
ListenAddr string `yaml:"listen_addr,omitempty"`
KubeConfigFilePath string `yaml:"kube_config_file_path,omitempty"`
ClusterConfig *rest.Config `yaml:"-"`
RootLogger logr.Logger `yaml:"-"`
CollectorSelector *metav1.LabelSelector `yaml:"collector_selector,omitempty"`
PromConfig *promconfig.Config `yaml:"config"`
AllocationStrategy string `yaml:"allocation_strategy,omitempty"`
FilterStrategy string `yaml:"filter_strategy,omitempty"`
PrometheusCR PrometheusCRConfig `yaml:"prometheus_cr,omitempty"`
PodMonitorSelector map[string]string `yaml:"pod_monitor_selector,omitempty"`
ServiceMonitorSelector map[string]string `yaml:"service_monitor_selector,omitempty"`
ServiceMonitorNamespaceSelector *metav1.LabelSelector `yaml:"service_monitor_namespace_selector,omitempty"`
PodMonitorNamespaceSelector *metav1.LabelSelector `yaml:"pod_monitor_namespace_selector,omitempty"`
ListenAddr string `yaml:"listen_addr,omitempty"`
KubeConfigFilePath string `yaml:"kube_config_file_path,omitempty"`
ClusterConfig *rest.Config `yaml:"-"`
RootLogger logr.Logger `yaml:"-"`
CollectorSelector *metav1.LabelSelector `yaml:"collector_selector,omitempty"`
PromConfig *promconfig.Config `yaml:"config"`
AllocationStrategy string `yaml:"allocation_strategy,omitempty"`
FilterStrategy string `yaml:"filter_strategy,omitempty"`
PrometheusCR PrometheusCRConfig `yaml:"prometheus_cr,omitempty"`
}

type PrometheusCRConfig struct {
Enabled bool `yaml:"enabled,omitempty"`
ScrapeInterval model.Duration `yaml:"scrape_interval,omitempty"`
Enabled bool `yaml:"enabled,omitempty"`
PodMonitorSelector *metav1.LabelSelector `yaml:"pod_monitor_selector,omitempty"`
ServiceMonitorSelector *metav1.LabelSelector `yaml:"service_monitor_selector,omitempty"`
ServiceMonitorNamespaceSelector *metav1.LabelSelector `yaml:"service_monitor_namespace_selector,omitempty"`
PodMonitorNamespaceSelector *metav1.LabelSelector `yaml:"pod_monitor_namespace_selector,omitempty"`
ScrapeInterval model.Duration `yaml:"scrape_interval,omitempty"`
}

func LoadFromFile(file string, target *Config) error {
Expand Down
16 changes: 10 additions & 6 deletions cmd/otel-allocator/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ func TestLoad(t *testing.T) {
},
FilterStrategy: DefaultFilterStrategy,
PrometheusCR: PrometheusCRConfig{
PodMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"release": "test",
},
},
ServiceMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"release": "test",
},
},
ScrapeInterval: DefaultCRScrapeInterval,
},
PromConfig: &promconfig.Config{
Expand Down Expand Up @@ -160,12 +170,6 @@ func TestLoad(t *testing.T) {
},
},
},
PodMonitorSelector: map[string]string{
"release": "test",
},
ServiceMonitorSelector: map[string]string{
"release": "test",
},
},
wantErr: assert.NoError,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ collector_selector:
matchlabels:
app.kubernetes.io/instance: default.test
app.kubernetes.io/managed-by: opentelemetry-operator
pod_monitor_selector:
release: test
service_monitor_selector:
release: test
prometheus_cr:
pod_monitor_selector:
matchlabels:
release: test
service_monitor_selector:
matchlabels:
release: test
config:
scrape_configs:
- job_name: prometheus
Expand Down
18 changes: 7 additions & 11 deletions cmd/otel-allocator/watcher/promOperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,11 @@ func NewPrometheusCRWatcher(ctx context.Context, logger logr.Logger, cfg allocat
prom := &monitoringv1.Prometheus{
Spec: monitoringv1.PrometheusSpec{
CommonPrometheusFields: monitoringv1.CommonPrometheusFields{
ScrapeInterval: monitoringv1.Duration(cfg.PrometheusCR.ScrapeInterval.String()),
ServiceMonitorSelector: &metav1.LabelSelector{
MatchLabels: cfg.ServiceMonitorSelector,
},
PodMonitorSelector: &metav1.LabelSelector{
MatchLabels: cfg.PodMonitorSelector,
},
ServiceMonitorNamespaceSelector: cfg.ServiceMonitorNamespaceSelector,
PodMonitorNamespaceSelector: cfg.PodMonitorNamespaceSelector,
ScrapeInterval: monitoringv1.Duration(cfg.PrometheusCR.ScrapeInterval.String()),
ServiceMonitorSelector: cfg.PrometheusCR.ServiceMonitorSelector,
PodMonitorSelector: cfg.PrometheusCR.PodMonitorSelector,
ServiceMonitorNamespaceSelector: cfg.PrometheusCR.ServiceMonitorNamespaceSelector,
PodMonitorNamespaceSelector: cfg.PrometheusCR.PodMonitorNamespaceSelector,
},
},
}
Expand Down Expand Up @@ -116,8 +112,8 @@ func NewPrometheusCRWatcher(ctx context.Context, logger logr.Logger, cfg allocat
eventInterval: minEventInterval,
configGenerator: generator,
kubeConfigPath: cfg.KubeConfigFilePath,
podMonitorNamespaceSelector: cfg.PodMonitorNamespaceSelector,
serviceMonitorNamespaceSelector: cfg.ServiceMonitorNamespaceSelector,
podMonitorNamespaceSelector: cfg.PrometheusCR.PodMonitorNamespaceSelector,
serviceMonitorNamespaceSelector: cfg.PrometheusCR.ServiceMonitorNamespaceSelector,
resourceSelector: resourceSelector,
store: store,
}, nil
Expand Down
99 changes: 70 additions & 29 deletions cmd/otel-allocator/watcher/promOperator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ func TestLoadConfig(t *testing.T) {
},
},
},
cfg: allocatorconfig.Config{},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
},
want: &promconfig.Config{
ScrapeConfigs: []*promconfig.ScrapeConfig{
{
Expand Down Expand Up @@ -171,7 +176,12 @@ func TestLoadConfig(t *testing.T) {
},
},
},
cfg: allocatorconfig.Config{},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
},
want: &promconfig.Config{
GlobalConfig: promconfig.GlobalConfig{},
ScrapeConfigs: []*promconfig.ScrapeConfig{
Expand Down Expand Up @@ -232,7 +242,12 @@ func TestLoadConfig(t *testing.T) {
},
},
},
cfg: allocatorconfig.Config{},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
},
want: &promconfig.Config{
GlobalConfig: promconfig.GlobalConfig{},
ScrapeConfigs: []*promconfig.ScrapeConfig{
Expand Down Expand Up @@ -322,7 +337,12 @@ func TestLoadConfig(t *testing.T) {
},
},
},
cfg: allocatorconfig.Config{},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
},
want: &promconfig.Config{
ScrapeConfigs: []*promconfig.ScrapeConfig{
{
Expand Down Expand Up @@ -424,7 +444,12 @@ func TestLoadConfig(t *testing.T) {
},
},
},
cfg: allocatorconfig.Config{},
cfg: allocatorconfig.Config{
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
},
},
want: &promconfig.Config{
ScrapeConfigs: []*promconfig.ScrapeConfig{
{
Expand Down Expand Up @@ -506,8 +531,12 @@ func TestLoadConfig(t *testing.T) {
},
},
cfg: allocatorconfig.Config{
ServiceMonitorSelector: map[string]string{
"testsvc": "testsvc",
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"testsvc": "testsvc",
},
},
},
},
want: &promconfig.Config{
Expand Down Expand Up @@ -571,8 +600,12 @@ func TestLoadConfig(t *testing.T) {
},
},
cfg: allocatorconfig.Config{
PodMonitorSelector: map[string]string{
"testpod": "testpod",
PrometheusCR: allocatorconfig.PrometheusCRConfig{
PodMonitorSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"testpod": "testpod",
},
},
},
},
want: &promconfig.Config{
Expand Down Expand Up @@ -633,9 +666,13 @@ func TestLoadConfig(t *testing.T) {
},
},
cfg: allocatorconfig.Config{
ServiceMonitorNamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"label1": "label1",
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
ServiceMonitorNamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"label1": "label1",
},
},
},
},
Expand Down Expand Up @@ -697,9 +734,13 @@ func TestLoadConfig(t *testing.T) {
},
},
cfg: allocatorconfig.Config{
PodMonitorNamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"label1": "label1",
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
PodMonitorNamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"label1": "label1",
},
},
},
},
Expand Down Expand Up @@ -794,9 +835,13 @@ func TestNamespaceLabelUpdate(t *testing.T) {
}

cfg := allocatorconfig.Config{
PodMonitorNamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"label1": "label1",
PrometheusCR: allocatorconfig.PrometheusCRConfig{
ServiceMonitorSelector: &metav1.LabelSelector{},
PodMonitorSelector: &metav1.LabelSelector{},
PodMonitorNamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"label1": "label1",
},
},
},
}
Expand Down Expand Up @@ -1011,15 +1056,11 @@ func getTestPrometheusCRWatcher(t *testing.T, svcMonitors []*monitoringv1.Servic
prom := &monitoringv1.Prometheus{
Spec: monitoringv1.PrometheusSpec{
CommonPrometheusFields: monitoringv1.CommonPrometheusFields{
ScrapeInterval: monitoringv1.Duration("30s"),
ServiceMonitorSelector: &metav1.LabelSelector{
MatchLabels: cfg.ServiceMonitorSelector,
},
PodMonitorSelector: &metav1.LabelSelector{
MatchLabels: cfg.PodMonitorSelector,
},
ServiceMonitorNamespaceSelector: cfg.ServiceMonitorNamespaceSelector,
PodMonitorNamespaceSelector: cfg.PodMonitorNamespaceSelector,
ScrapeInterval: monitoringv1.Duration("30s"),
ServiceMonitorSelector: cfg.PrometheusCR.ServiceMonitorSelector,
PodMonitorSelector: cfg.PrometheusCR.PodMonitorSelector,
ServiceMonitorNamespaceSelector: cfg.PrometheusCR.ServiceMonitorNamespaceSelector,
PodMonitorNamespaceSelector: cfg.PrometheusCR.PodMonitorNamespaceSelector,
},
},
}
Expand Down Expand Up @@ -1056,8 +1097,8 @@ func getTestPrometheusCRWatcher(t *testing.T, svcMonitors []*monitoringv1.Servic
nsInformer: nsMonInf,
stopChannel: make(chan struct{}),
configGenerator: generator,
podMonitorNamespaceSelector: cfg.PodMonitorNamespaceSelector,
serviceMonitorNamespaceSelector: cfg.ServiceMonitorNamespaceSelector,
podMonitorNamespaceSelector: cfg.PrometheusCR.PodMonitorNamespaceSelector,
serviceMonitorNamespaceSelector: cfg.PrometheusCR.ServiceMonitorNamespaceSelector,
resourceSelector: resourceSelector,
store: store,
}, source
Expand Down
18 changes: 16 additions & 2 deletions controllers/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,13 @@ label_selector:
app.kubernetes.io/instance: test.test
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
prometheus_cr:
pod_monitor_selector:
matchlabels: {}
matchexpressions: []
service_monitor_selector:
matchlabels: {}
matchexpressions: []
`,
},
},
Expand Down Expand Up @@ -1384,7 +1391,7 @@ label_selector:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-targetallocator-config/hash": "bf084cbbdcb09d03a40ad2352e0869ccf75d01f5dec977938b94d5a3239ea491",
"opentelemetry-targetallocator-config/hash": "51477b182d2c9e7c0db27a2cbc9c7d35b24895b1cf0774d51a41b8d1753696ed",
},
},
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -1744,6 +1751,13 @@ label_selector:
app.kubernetes.io/instance: test.test
app.kubernetes.io/managed-by: opentelemetry-operator
app.kubernetes.io/part-of: opentelemetry
prometheus_cr:
pod_monitor_selector:
matchlabels: {}
matchexpressions: []
service_monitor_selector:
matchlabels: {}
matchexpressions: []
`,
},
},
Expand Down Expand Up @@ -1776,7 +1790,7 @@ label_selector:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-targetallocator-config/hash": "bf084cbbdcb09d03a40ad2352e0869ccf75d01f5dec977938b94d5a3239ea491",
"opentelemetry-targetallocator-config/hash": "51477b182d2c9e7c0db27a2cbc9c7d35b24895b1cf0774d51a41b8d1753696ed",
},
},
Spec: corev1.PodSpec{
Expand Down
6 changes: 4 additions & 2 deletions controllers/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,10 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
taConfig["config"] = promConfig["config"]
taConfig["allocation_strategy"] = "consistent-hashing"
taConfig["filter_strategy"] = "relabel-config"
taConfig["prometheus_cr"] = map[string]string{
"scrape_interval": "30s",
taConfig["prometheus_cr"] = map[string]any{
"scrape_interval": "30s",
"pod_monitor_selector": &metav1.LabelSelector{},
"service_monitor_selector": &metav1.LabelSelector{},
}
taConfigYAML, _ := yaml.Marshal(taConfig)
assert.Equal(t, string(taConfigYAML), actual.Data["targetallocator.yaml"])
Expand Down
Loading
Loading