From f0c7588bc44674e410aa3d60c32e3c038d70bdd1 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Wed, 16 Nov 2022 12:29:51 -0500 Subject: [PATCH 1/8] Add new selector for pod and service monitor --- cmd/otel-allocator/config/config.go | 11 ++++-- cmd/otel-allocator/main.go | 2 +- cmd/otel-allocator/watcher/main.go | 8 ++-- cmd/otel-allocator/watcher/promOperator.go | 44 ++++++++++++++++++---- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/cmd/otel-allocator/config/config.go b/cmd/otel-allocator/config/config.go index af26544f0c..bf411f43c3 100644 --- a/cmd/otel-allocator/config/config.go +++ b/cmd/otel-allocator/config/config.go @@ -18,6 +18,7 @@ import ( "flag" "fmt" "io/fs" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "os" "path/filepath" "time" @@ -39,10 +40,12 @@ const DefaultResyncTime = 5 * time.Minute const DefaultConfigFilePath string = "/conf/targetallocator.yaml" type Config struct { - LabelSelector map[string]string `yaml:"label_selector,omitempty"` - Config *promconfig.Config `yaml:"config"` - AllocationStrategy *string `yaml:"allocation_strategy,omitempty"` - FilterStrategy *string `yaml:"filter_strategy,omitempty"` + LabelSelector map[string]string `yaml:"label_selector,omitempty"` + Config *promconfig.Config `yaml:"config"` + AllocationStrategy *string `yaml:"allocation_strategy,omitempty"` + FilterStrategy *string `yaml:"filter_strategy,omitempty"` + PodMonitorSelector *metav1.LabelSelector `yaml:"pod_monitor_selector,omitempty"` + ServiceMonitorSelector *metav1.LabelSelector `yaml:"service_monitor_selector,omitempty"` } func (c Config) GetAllocationStrategy() string { diff --git a/cmd/otel-allocator/main.go b/cmd/otel-allocator/main.go index 9dd52a1054..07d1373f17 100644 --- a/cmd/otel-allocator/main.go +++ b/cmd/otel-allocator/main.go @@ -83,7 +83,7 @@ func main() { os.Exit(1) } - watcher, err := allocatorWatcher.NewWatcher(setupLog, cliConf, allocator) + watcher, err := allocatorWatcher.NewWatcher(setupLog, cfg, cliConf, allocator) if err != nil { setupLog.Error(err, "Can't start the watchers") os.Exit(1) diff --git a/cmd/otel-allocator/watcher/main.go b/cmd/otel-allocator/watcher/main.go index 8dbd71036f..d983c40f74 100644 --- a/cmd/otel-allocator/watcher/main.go +++ b/cmd/otel-allocator/watcher/main.go @@ -59,21 +59,21 @@ func (e EventSource) String() string { return eventSourceToString[e] } -func NewWatcher(logger logr.Logger, config config.CLIConfig, allocator allocation.Allocator) (*Manager, error) { +func NewWatcher(logger logr.Logger, cfg config.Config, cliConfig config.CLIConfig, allocator allocation.Allocator) (*Manager, error) { watcher := Manager{ allocator: allocator, Events: make(chan Event), Errors: make(chan error), } - fileWatcher, err := newConfigMapWatcher(logger, config) + fileWatcher, err := newConfigMapWatcher(logger, cliConfig) if err != nil { return nil, err } watcher.watchers = append(watcher.watchers, &fileWatcher) - if *config.PromCRWatcherConf.Enabled { - promWatcher, err := newCRDMonitorWatcher(config) + if *cliConfig.PromCRWatcherConf.Enabled { + promWatcher, err := newCRDMonitorWatcher(cfg, cliConfig) if err != nil { return nil, err } diff --git a/cmd/otel-allocator/watcher/promOperator.go b/cmd/otel-allocator/watcher/promOperator.go index 09eee6f50f..6f2aec145c 100644 --- a/cmd/otel-allocator/watcher/promOperator.go +++ b/cmd/otel-allocator/watcher/promOperator.go @@ -16,6 +16,7 @@ package watcher import ( "fmt" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" allocatorconfig "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/config" @@ -33,8 +34,8 @@ import ( "k8s.io/client-go/tools/cache" ) -func newCRDMonitorWatcher(config allocatorconfig.CLIConfig) (*PrometheusCRWatcher, error) { - mClient, err := monitoringclient.NewForConfig(config.ClusterConfig) +func newCRDMonitorWatcher(cfg allocatorconfig.Config, cliConfig allocatorconfig.CLIConfig) (*PrometheusCRWatcher, error) { + mClient, err := monitoringclient.NewForConfig(cliConfig.ClusterConfig) if err != nil { return nil, err } @@ -61,11 +62,23 @@ func newCRDMonitorWatcher(config allocatorconfig.CLIConfig) (*PrometheusCRWatche return nil, err } + servMonSelector, err := getSelector(cfg.ServiceMonitorSelector) + if err != nil { + return nil, err + } + + podMonSelector, err := getSelector(cfg.PodMonitorSelector) + if err != nil { + return nil, err + } + return &PrometheusCRWatcher{ - kubeMonitoringClient: mClient, - informers: monitoringInformers, - stopChannel: make(chan struct{}), - configGenerator: generator, + kubeMonitoringClient: mClient, + informers: monitoringInformers, + stopChannel: make(chan struct{}), + configGenerator: generator, + serviceMonitorSelector: servMonSelector, + podMonitorSelector: podMonSelector, }, nil } @@ -74,6 +87,20 @@ type PrometheusCRWatcher struct { informers map[string]*informers.ForResource stopChannel chan struct{} configGenerator *prometheus.ConfigGenerator + + serviceMonitorSelector labels.Selector + podMonitorSelector labels.Selector +} + +func getSelector(s *metav1.LabelSelector) (labels.Selector, error) { + if s == nil { + return labels.NewSelector(), nil + } + sel, err := metav1.LabelSelectorAsSelector(s) + if err != nil { + return nil, err + } + return sel, nil } // Start wrapped informers and wait for an initial sync. @@ -118,7 +145,8 @@ func (w *PrometheusCRWatcher) Close() error { func (w *PrometheusCRWatcher) CreatePromConfig(kubeConfigPath string) (*promconfig.Config, error) { serviceMonitorInstances := make(map[string]*monitoringv1.ServiceMonitor) - smRetrieveErr := w.informers[monitoringv1.ServiceMonitorName].ListAll(labels.NewSelector(), func(sm interface{}) { + + smRetrieveErr := w.informers[monitoringv1.ServiceMonitorName].ListAll(w.serviceMonitorSelector, func(sm interface{}) { monitor := sm.(*monitoringv1.ServiceMonitor) key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(monitor) serviceMonitorInstances[key] = monitor @@ -128,7 +156,7 @@ func (w *PrometheusCRWatcher) CreatePromConfig(kubeConfigPath string) (*promconf } podMonitorInstances := make(map[string]*monitoringv1.PodMonitor) - pmRetrieveErr := w.informers[monitoringv1.PodMonitorName].ListAll(labels.NewSelector(), func(pm interface{}) { + pmRetrieveErr := w.informers[monitoringv1.PodMonitorName].ListAll(w.podMonitorSelector, func(pm interface{}) { monitor := pm.(*monitoringv1.PodMonitor) key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(monitor) podMonitorInstances[key] = monitor From 28aad977802a05dd2a965c225d5069ffd46611d7 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Wed, 16 Nov 2022 15:42:34 -0500 Subject: [PATCH 2/8] Use map, add tests --- apis/v1alpha1/opentelemetrycollector_types.go | 4 + apis/v1alpha1/zz_generated.deepcopy.go | 26 ++- ...emetry-operator.clusterserviceversion.yaml | 36 ++-- ...ntelemetry.io_opentelemetrycollectors.yaml | 90 ++++++++++ cmd/otel-allocator/config/config.go | 13 +- cmd/otel-allocator/config/config_test.go | 154 ++++++++++++---- .../testdata/pod_service_selector_test.yaml | 14 ++ cmd/otel-allocator/watcher/promOperator.go | 22 +-- ...ntelemetry.io_opentelemetrycollectors.yaml | 90 ++++++++++ docs/api.md | 164 ++++++++++++++++++ pkg/collector/reconcile/configmap.go | 8 + pkg/collector/reconcile/configmap_test.go | 39 +++++ 12 files changed, 588 insertions(+), 72 deletions(-) create mode 100644 cmd/otel-allocator/config/testdata/pod_service_selector_test.yaml diff --git a/apis/v1alpha1/opentelemetrycollector_types.go b/apis/v1alpha1/opentelemetrycollector_types.go index d16abab40f..e09bffc01d 100644 --- a/apis/v1alpha1/opentelemetrycollector_types.go +++ b/apis/v1alpha1/opentelemetrycollector_types.go @@ -177,6 +177,10 @@ type OpenTelemetryTargetAllocator struct { // All CR instances which the ServiceAccount has access to will be retrieved. This includes other namespaces. // +optional PrometheusCR OpenTelemetryTargetAllocatorPrometheusCR `json:"prometheusCR,omitempty"` + // PodMonitors to be selected for target discovery. + PodMonitorSelector *map[string]string `json:"podMonitorSelector,omitempty"` + // ServiceMonitors to be selected for target discovery. + ServiceMonitorSelector *map[string]string `json:"serviceMonitorSelector,omitempty"` } type OpenTelemetryTargetAllocatorPrometheusCR struct { diff --git a/apis/v1alpha1/zz_generated.deepcopy.go b/apis/v1alpha1/zz_generated.deepcopy.go index 3b2613097c..a8c754e503 100644 --- a/apis/v1alpha1/zz_generated.deepcopy.go +++ b/apis/v1alpha1/zz_generated.deepcopy.go @@ -20,8 +20,8 @@ package v1alpha1 import ( - "k8s.io/api/autoscaling/v2" - "k8s.io/api/core/v1" + v2 "k8s.io/api/autoscaling/v2" + v1 "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" "k8s.io/apimachinery/pkg/runtime" ) @@ -481,6 +481,28 @@ func (in *OpenTelemetryTargetAllocator) DeepCopyInto(out *OpenTelemetryTargetAll **out = **in } out.PrometheusCR = in.PrometheusCR + if in.PodMonitorSelector != nil { + in, out := &in.PodMonitorSelector, &out.PodMonitorSelector + *out = new(map[string]string) + if **in != nil { + in, out := *in, *out + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + } + if in.ServiceMonitorSelector != nil { + in, out := &in.ServiceMonitorSelector, &out.ServiceMonitorSelector + *out = new(map[string]string) + if **in != nil { + in, out := *in, *out + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OpenTelemetryTargetAllocator. diff --git a/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml b/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml index 007b9c3b18..903d14195d 100644 --- a/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml +++ b/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml @@ -289,6 +289,24 @@ spec: control-plane: controller-manager spec: containers: + - args: + - --secure-listen-address=0.0.0.0:8443 + - --upstream=http://127.0.0.1:8080/ + - --logtostderr=true + - --v=0 + image: gcr.io/kubebuilder/kube-rbac-proxy:v0.13.0 + name: kube-rbac-proxy + ports: + - containerPort: 8443 + name: https + protocol: TCP + resources: + limits: + cpu: 500m + memory: 128Mi + requests: + cpu: 5m + memory: 64Mi - args: - --metrics-addr=127.0.0.1:8080 - --enable-leader-election @@ -320,24 +338,6 @@ spec: - mountPath: /tmp/k8s-webhook-server/serving-certs name: cert readOnly: true - - args: - - --secure-listen-address=0.0.0.0:8443 - - --upstream=http://127.0.0.1:8080/ - - --logtostderr=true - - --v=0 - image: gcr.io/kubebuilder/kube-rbac-proxy:v0.13.0 - name: kube-rbac-proxy - ports: - - containerPort: 8443 - name: https - protocol: TCP - resources: - limits: - cpu: 500m - memory: 128Mi - requests: - cpu: 5m - memory: 64Mi serviceAccountName: opentelemetry-operator-controller-manager terminationGracePeriodSeconds: 10 volumes: diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml index fcdfed60de..733bec7235 100644 --- a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml +++ b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml @@ -1711,6 +1711,51 @@ spec: description: Image indicates the container image to use for the OpenTelemetry TargetAllocator. type: string + podMonitorSelector: + description: PodMonitors to be selected for target discovery. + properties: + matchExpressions: + description: matchExpressions is a list of label selector + requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector + that contains values, a key, and an operator that relates + the key and values. + properties: + key: + description: key is the label key that the selector + applies to. + type: string + operator: + description: operator represents a key's relationship + to a set of values. Valid operators are In, NotIn, + Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If + the operator is In or NotIn, the values array must + be non-empty. If the operator is Exists or DoesNotExist, + the values array must be empty. This array is replaced + during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A + single {key,value} in the matchLabels map is equivalent + to an element of matchExpressions, whose key field is "key", + the operator is "In", and the values array contains only + "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic prometheusCR: description: PrometheusCR defines the configuration for the retrieval of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1 @@ -1735,6 +1780,51 @@ spec: description: ServiceAccount indicates the name of an existing service account to use with this instance. type: string + serviceMonitorSelector: + description: ServiceMonitors to be selected for target discovery. + properties: + matchExpressions: + description: matchExpressions is a list of label selector + requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector + that contains values, a key, and an operator that relates + the key and values. + properties: + key: + description: key is the label key that the selector + applies to. + type: string + operator: + description: operator represents a key's relationship + to a set of values. Valid operators are In, NotIn, + Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If + the operator is In or NotIn, the values array must + be non-empty. If the operator is Exists or DoesNotExist, + the values array must be empty. This array is replaced + during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A + single {key,value} in the matchLabels map is equivalent + to an element of matchExpressions, whose key field is "key", + the operator is "In", and the values array contains only + "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic type: object tolerations: description: Toleration to schedule OpenTelemetry Collector pods. diff --git a/cmd/otel-allocator/config/config.go b/cmd/otel-allocator/config/config.go index bf411f43c3..998a750ae9 100644 --- a/cmd/otel-allocator/config/config.go +++ b/cmd/otel-allocator/config/config.go @@ -18,7 +18,6 @@ import ( "flag" "fmt" "io/fs" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "os" "path/filepath" "time" @@ -40,12 +39,12 @@ const DefaultResyncTime = 5 * time.Minute const DefaultConfigFilePath string = "/conf/targetallocator.yaml" type Config struct { - LabelSelector map[string]string `yaml:"label_selector,omitempty"` - Config *promconfig.Config `yaml:"config"` - AllocationStrategy *string `yaml:"allocation_strategy,omitempty"` - FilterStrategy *string `yaml:"filter_strategy,omitempty"` - PodMonitorSelector *metav1.LabelSelector `yaml:"pod_monitor_selector,omitempty"` - ServiceMonitorSelector *metav1.LabelSelector `yaml:"service_monitor_selector,omitempty"` + LabelSelector map[string]string `yaml:"label_selector,omitempty"` + Config *promconfig.Config `yaml:"config"` + AllocationStrategy *string `yaml:"allocation_strategy,omitempty"` + FilterStrategy *string `yaml:"filter_strategy,omitempty"` + PodMonitorSelector *map[string]string `yaml:"pod_monitor_selector,omitempty"` + ServiceMonitorSelector *map[string]string `yaml:"service_monitor_selector,omitempty"` } func (c Config) GetAllocationStrategy() string { diff --git a/cmd/otel-allocator/config/config_test.go b/cmd/otel-allocator/config/config_test.go index 3c45ffefe4..4df0a93800 100644 --- a/cmd/otel-allocator/config/config_test.go +++ b/cmd/otel-allocator/config/config_test.go @@ -15,7 +15,12 @@ package config import ( + "fmt" "testing" + "time" + + commonconfig "github.com/prometheus/common/config" + promconfig "github.com/prometheus/prometheus/config" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/discovery" @@ -23,39 +28,130 @@ import ( "github.com/stretchr/testify/assert" ) -const testFile = "./testdata/config_test.yaml" - -func TestConfigLoad(t *testing.T) { - expectedFileSDConfig := &file.SDConfig{ - Files: []string{"./file_sd_test.json"}, - RefreshInterval: model.Duration(300000000000), +func TestLoad(t *testing.T) { + type args struct { + file string } - expectedStaticSDConfig := discovery.StaticConfig{ + tests := []struct { + name string + args args + want Config + wantErr assert.ErrorAssertionFunc + }{ + { + name: "file sd load", + args: args{ + file: "./testdata/config_test.yaml", + }, + want: Config{ + LabelSelector: map[string]string{ + "app.kubernetes.io/instance": "default.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + }, + Config: &promconfig.Config{ + GlobalConfig: promconfig.GlobalConfig{ + ScrapeInterval: model.Duration(60 * time.Second), + ScrapeTimeout: model.Duration(10 * time.Second), + EvaluationInterval: model.Duration(60 * time.Second), + }, + ScrapeConfigs: []*promconfig.ScrapeConfig{ + { + JobName: "prometheus", + HonorTimestamps: true, + ScrapeInterval: model.Duration(60 * time.Second), + ScrapeTimeout: model.Duration(10 * time.Second), + MetricsPath: "/metrics", + Scheme: "http", + HTTPClientConfig: commonconfig.HTTPClientConfig{ + FollowRedirects: true, + }, + ServiceDiscoveryConfigs: []discovery.Config{ + &file.SDConfig{ + Files: []string{"./file_sd_test.json"}, + RefreshInterval: model.Duration(5 * time.Minute), + }, + discovery.StaticConfig{ + { + Targets: []model.LabelSet{ + {model.AddressLabel: "prom.domain:9001"}, + {model.AddressLabel: "prom.domain:9002"}, + {model.AddressLabel: "prom.domain:9003"}, + }, + Labels: model.LabelSet{ + "my": "label", + }, + Source: "0", + }, + }, + }, + }, + }, + }, + }, + wantErr: assert.NoError, + }, { - Targets: []model.LabelSet{ - {model.AddressLabel: "prom.domain:9001"}, - {model.AddressLabel: "prom.domain:9002"}, - {model.AddressLabel: "prom.domain:9003"}, + name: "service monitor pod monitor selector", + args: args{ + file: "./testdata/pod_service_selector_test.yaml", }, - Labels: model.LabelSet{ - "my": "label", + want: Config{ + LabelSelector: map[string]string{ + "app.kubernetes.io/instance": "default.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + }, + Config: &promconfig.Config{ + GlobalConfig: promconfig.GlobalConfig{ + ScrapeInterval: model.Duration(60 * time.Second), + ScrapeTimeout: model.Duration(10 * time.Second), + EvaluationInterval: model.Duration(60 * time.Second), + }, + ScrapeConfigs: []*promconfig.ScrapeConfig{ + { + JobName: "prometheus", + HonorTimestamps: true, + ScrapeInterval: model.Duration(60 * time.Second), + ScrapeTimeout: model.Duration(10 * time.Second), + MetricsPath: "/metrics", + Scheme: "http", + HTTPClientConfig: commonconfig.HTTPClientConfig{ + FollowRedirects: true, + }, + ServiceDiscoveryConfigs: []discovery.Config{ + discovery.StaticConfig{ + { + Targets: []model.LabelSet{ + {model.AddressLabel: "prom.domain:9001"}, + {model.AddressLabel: "prom.domain:9002"}, + {model.AddressLabel: "prom.domain:9003"}, + }, + Labels: model.LabelSet{ + "my": "label", + }, + Source: "0", + }, + }, + }, + }, + }, + }, + PodMonitorSelector: &map[string]string{ + "release": "test", + }, + ServiceMonitorSelector: &map[string]string{ + "release": "test", + }, }, - Source: "0", + wantErr: assert.NoError, }, } - - cfg := Config{} - err := unmarshal(&cfg, testFile) - assert.NoError(t, err) - - scrapeConfig := *cfg.Config.ScrapeConfigs[0] - actualFileSDConfig := scrapeConfig.ServiceDiscoveryConfigs[0] - actulaStaticSDConfig := scrapeConfig.ServiceDiscoveryConfigs[1] - t.Log(actulaStaticSDConfig) - - assert.Equal(t, cfg.LabelSelector["app.kubernetes.io/instance"], "default.test") - assert.Equal(t, cfg.LabelSelector["app.kubernetes.io/managed-by"], "opentelemetry-operator") - assert.Equal(t, scrapeConfig.JobName, "prometheus") - assert.Equal(t, expectedFileSDConfig, actualFileSDConfig) - assert.Equal(t, expectedStaticSDConfig, actulaStaticSDConfig) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := Load(tt.args.file) + if !tt.wantErr(t, err, fmt.Sprintf("Load(%v)", tt.args.file)) { + return + } + assert.Equalf(t, tt.want, got, "Load(%v)", tt.args.file) + }) + } } diff --git a/cmd/otel-allocator/config/testdata/pod_service_selector_test.yaml b/cmd/otel-allocator/config/testdata/pod_service_selector_test.yaml new file mode 100644 index 0000000000..c0ff54ad36 --- /dev/null +++ b/cmd/otel-allocator/config/testdata/pod_service_selector_test.yaml @@ -0,0 +1,14 @@ +label_selector: + app.kubernetes.io/instance: default.test + app.kubernetes.io/managed-by: opentelemetry-operator +pod_monitor_selector: + release: test +service_monitor_selector: + release: test +config: + scrape_configs: + - job_name: prometheus + static_configs: + - targets: ["prom.domain:9001", "prom.domain:9002", "prom.domain:9003"] + labels: + my: label \ No newline at end of file diff --git a/cmd/otel-allocator/watcher/promOperator.go b/cmd/otel-allocator/watcher/promOperator.go index 6f2aec145c..abb77fa4b3 100644 --- a/cmd/otel-allocator/watcher/promOperator.go +++ b/cmd/otel-allocator/watcher/promOperator.go @@ -16,7 +16,6 @@ package watcher import ( "fmt" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" allocatorconfig "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/config" @@ -62,15 +61,9 @@ func newCRDMonitorWatcher(cfg allocatorconfig.Config, cliConfig allocatorconfig. return nil, err } - servMonSelector, err := getSelector(cfg.ServiceMonitorSelector) - if err != nil { - return nil, err - } + servMonSelector := getSelector(cfg.ServiceMonitorSelector) - podMonSelector, err := getSelector(cfg.PodMonitorSelector) - if err != nil { - return nil, err - } + podMonSelector := getSelector(cfg.PodMonitorSelector) return &PrometheusCRWatcher{ kubeMonitoringClient: mClient, @@ -92,15 +85,12 @@ type PrometheusCRWatcher struct { podMonitorSelector labels.Selector } -func getSelector(s *metav1.LabelSelector) (labels.Selector, error) { +func getSelector(s *map[string]string) labels.Selector { + sel := labels.NewSelector() if s == nil { - return labels.NewSelector(), nil - } - sel, err := metav1.LabelSelectorAsSelector(s) - if err != nil { - return nil, err + return sel } - return sel, nil + return labels.SelectorFromSet(*s) } // Start wrapped informers and wait for an initial sync. diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml index 1196232386..e79f4a286e 100644 --- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml +++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml @@ -1709,6 +1709,51 @@ spec: description: Image indicates the container image to use for the OpenTelemetry TargetAllocator. type: string + podMonitorSelector: + description: PodMonitors to be selected for target discovery. + properties: + matchExpressions: + description: matchExpressions is a list of label selector + requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector + that contains values, a key, and an operator that relates + the key and values. + properties: + key: + description: key is the label key that the selector + applies to. + type: string + operator: + description: operator represents a key's relationship + to a set of values. Valid operators are In, NotIn, + Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If + the operator is In or NotIn, the values array must + be non-empty. If the operator is Exists or DoesNotExist, + the values array must be empty. This array is replaced + during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A + single {key,value} in the matchLabels map is equivalent + to an element of matchExpressions, whose key field is "key", + the operator is "In", and the values array contains only + "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic prometheusCR: description: PrometheusCR defines the configuration for the retrieval of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1 @@ -1733,6 +1778,51 @@ spec: description: ServiceAccount indicates the name of an existing service account to use with this instance. type: string + serviceMonitorSelector: + description: ServiceMonitors to be selected for target discovery. + properties: + matchExpressions: + description: matchExpressions is a list of label selector + requirements. The requirements are ANDed. + items: + description: A label selector requirement is a selector + that contains values, a key, and an operator that relates + the key and values. + properties: + key: + description: key is the label key that the selector + applies to. + type: string + operator: + description: operator represents a key's relationship + to a set of values. Valid operators are In, NotIn, + Exists and DoesNotExist. + type: string + values: + description: values is an array of string values. If + the operator is In or NotIn, the values array must + be non-empty. If the operator is Exists or DoesNotExist, + the values array must be empty. This array is replaced + during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} pairs. A + single {key,value} in the matchLabels map is equivalent + to an element of matchExpressions, whose key field is "key", + the operator is "In", and the values array contains only + "value". The requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic type: object tolerations: description: Toleration to schedule OpenTelemetry Collector pods. diff --git a/docs/api.md b/docs/api.md index 015c87490a..fc42e04474 100644 --- a/docs/api.md +++ b/docs/api.md @@ -4556,6 +4556,13 @@ TargetAllocator indicates a value which determines whether to spawn a target all Image indicates the container image to use for the OpenTelemetry TargetAllocator.
false + + podMonitorSelector + object + + PodMonitors to be selected for target discovery.
+ + false prometheusCR object @@ -4579,6 +4586,88 @@ TargetAllocator indicates a value which determines whether to spawn a target all ServiceAccount indicates the name of an existing service account to use with this instance.
false + + serviceMonitorSelector + object + + ServiceMonitors to be selected for target discovery.
+ + false + + + + +### OpenTelemetryCollector.spec.targetAllocator.podMonitorSelector +[↩ Parent](#opentelemetrycollectorspectargetallocator) + + + +PodMonitors to be selected for target discovery. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
matchExpressions[]object + matchExpressions is a list of label selector requirements. The requirements are ANDed.
+
false
matchLabelsmap[string]string + matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
+
false
+ + +### OpenTelemetryCollector.spec.targetAllocator.podMonitorSelector.matchExpressions[index] +[↩ Parent](#opentelemetrycollectorspectargetallocatorpodmonitorselector) + + + +A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
keystring + key is the label key that the selector applies to.
+
true
operatorstring + operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
+
true
values[]string + values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
+
false
@@ -4610,6 +4699,81 @@ PrometheusCR defines the configuration for the retrieval of PrometheusOperator C +### OpenTelemetryCollector.spec.targetAllocator.serviceMonitorSelector +[↩ Parent](#opentelemetrycollectorspectargetallocator) + + + +ServiceMonitors to be selected for target discovery. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
matchExpressions[]object + matchExpressions is a list of label selector requirements. The requirements are ANDed.
+
false
matchLabelsmap[string]string + matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
+
false
+ + +### OpenTelemetryCollector.spec.targetAllocator.serviceMonitorSelector.matchExpressions[index] +[↩ Parent](#opentelemetrycollectorspectargetallocatorservicemonitorselector) + + + +A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
keystring + key is the label key that the selector applies to.
+
true
operatorstring + operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
+
true
values[]string + values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
+
false
+ + ### OpenTelemetryCollector.spec.tolerations[index] [↩ Parent](#opentelemetrycollectorspec) diff --git a/pkg/collector/reconcile/configmap.go b/pkg/collector/reconcile/configmap.go index dce74a4f2c..3ae1a7b89d 100644 --- a/pkg/collector/reconcile/configmap.go +++ b/pkg/collector/reconcile/configmap.go @@ -124,6 +124,14 @@ func desiredTAConfigMap(params Params) (corev1.ConfigMap, error) { taConfig["filter_strategy"] = params.Instance.Spec.TargetAllocator.FilterStrategy } + if params.Instance.Spec.TargetAllocator.ServiceMonitorSelector != nil { + taConfig["service_monitor_selector"] = ¶ms.Instance.Spec.TargetAllocator.ServiceMonitorSelector + } + + if params.Instance.Spec.TargetAllocator.PodMonitorSelector != nil { + taConfig["pod_monitor_selector"] = ¶ms.Instance.Spec.TargetAllocator.PodMonitorSelector + } + taConfigYAML, err := yaml.Marshal(taConfig) if err != nil { return corev1.ConfigMap{}, err diff --git a/pkg/collector/reconcile/configmap_test.go b/pkg/collector/reconcile/configmap_test.go index bdc13d79ca..77c6a1f803 100644 --- a/pkg/collector/reconcile/configmap_test.go +++ b/pkg/collector/reconcile/configmap_test.go @@ -209,6 +209,45 @@ label_selector: assert.Equal(t, expectedData, actual.Data) }) + t.Run("should return expected target allocator config map with label selectors", func(t *testing.T) { + expectedLables["app.kubernetes.io/component"] = "opentelemetry-targetallocator" + expectedLables["app.kubernetes.io/name"] = "test-targetallocator" + + expectedData := map[string]string{ + "targetallocator.yaml": `allocation_strategy: least-weighted +config: + scrape_configs: + - job_name: otel-collector + scrape_interval: 10s + static_configs: + - targets: + - 0.0.0.0:8888 + - 0.0.0.0:9999 +label_selector: + app.kubernetes.io/component: opentelemetry-collector + app.kubernetes.io/instance: default.test + app.kubernetes.io/managed-by: opentelemetry-operator +pod_monitor_selector: + release: test +service_monitor_selector: + release: test +`, + } + p := params() + p.Instance.Spec.TargetAllocator.PodMonitorSelector = &map[string]string{ + "release": "test", + } + p.Instance.Spec.TargetAllocator.ServiceMonitorSelector = &map[string]string{ + "release": "test", + } + actual, err := desiredTAConfigMap(p) + assert.NoError(t, err) + + assert.Equal(t, "test-targetallocator", actual.Name) + assert.Equal(t, expectedLables, actual.Labels) + assert.Equal(t, expectedData, actual.Data) + + }) } From 17e28195f7765039d2de5d074559e8e846861708 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Wed, 16 Nov 2022 15:43:32 -0500 Subject: [PATCH 3/8] Fix manifests --- ...ntelemetry.io_opentelemetrycollectors.yaml | 88 +------------------ 1 file changed, 4 insertions(+), 84 deletions(-) diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml index e79f4a286e..aa567ca9ad 100644 --- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml +++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml @@ -1710,50 +1710,10 @@ spec: OpenTelemetry TargetAllocator. type: string podMonitorSelector: + additionalProperties: + type: string description: PodMonitors to be selected for target discovery. - properties: - matchExpressions: - description: matchExpressions is a list of label selector - requirements. The requirements are ANDed. - items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. - properties: - key: - description: key is the label key that the selector - applies to. - type: string - operator: - description: operator represents a key's relationship - to a set of values. Valid operators are In, NotIn, - Exists and DoesNotExist. - type: string - values: - description: values is an array of string values. If - the operator is In or NotIn, the values array must - be non-empty. If the operator is Exists or DoesNotExist, - the values array must be empty. This array is replaced - during a strategic merge patch. - items: - type: string - type: array - required: - - key - - operator - type: object - type: array - matchLabels: - additionalProperties: - type: string - description: matchLabels is a map of {key,value} pairs. A - single {key,value} in the matchLabels map is equivalent - to an element of matchExpressions, whose key field is "key", - the operator is "In", and the values array contains only - "value". The requirements are ANDed. - type: object type: object - x-kubernetes-map-type: atomic prometheusCR: description: PrometheusCR defines the configuration for the retrieval of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1 @@ -1779,50 +1739,10 @@ spec: service account to use with this instance. type: string serviceMonitorSelector: + additionalProperties: + type: string description: ServiceMonitors to be selected for target discovery. - properties: - matchExpressions: - description: matchExpressions is a list of label selector - requirements. The requirements are ANDed. - items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. - properties: - key: - description: key is the label key that the selector - applies to. - type: string - operator: - description: operator represents a key's relationship - to a set of values. Valid operators are In, NotIn, - Exists and DoesNotExist. - type: string - values: - description: values is an array of string values. If - the operator is In or NotIn, the values array must - be non-empty. If the operator is Exists or DoesNotExist, - the values array must be empty. This array is replaced - during a strategic merge patch. - items: - type: string - type: array - required: - - key - - operator - type: object - type: array - matchLabels: - additionalProperties: - type: string - description: matchLabels is a map of {key,value} pairs. A - single {key,value} in the matchLabels map is equivalent - to an element of matchExpressions, whose key field is "key", - the operator is "In", and the values array contains only - "value". The requirements are ANDed. - type: object type: object - x-kubernetes-map-type: atomic type: object tolerations: description: Toleration to schedule OpenTelemetry Collector pods. From a400184ae2a23ca083fad8c0573407f5eee3db55 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Wed, 16 Nov 2022 15:46:22 -0500 Subject: [PATCH 4/8] Fixed make --- apis/v1alpha1/zz_generated.deepcopy.go | 4 +- ...emetry-operator.clusterserviceversion.yaml | 36 ++-- ...ntelemetry.io_opentelemetrycollectors.yaml | 88 +--------- config/manager/kustomization.yaml | 6 + docs/api.md | 158 +----------------- 5 files changed, 34 insertions(+), 258 deletions(-) diff --git a/apis/v1alpha1/zz_generated.deepcopy.go b/apis/v1alpha1/zz_generated.deepcopy.go index a8c754e503..f6e7e17f4e 100644 --- a/apis/v1alpha1/zz_generated.deepcopy.go +++ b/apis/v1alpha1/zz_generated.deepcopy.go @@ -20,8 +20,8 @@ package v1alpha1 import ( - v2 "k8s.io/api/autoscaling/v2" - v1 "k8s.io/api/core/v1" + "k8s.io/api/autoscaling/v2" + "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" "k8s.io/apimachinery/pkg/runtime" ) diff --git a/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml b/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml index 903d14195d..007b9c3b18 100644 --- a/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml +++ b/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml @@ -289,24 +289,6 @@ spec: control-plane: controller-manager spec: containers: - - args: - - --secure-listen-address=0.0.0.0:8443 - - --upstream=http://127.0.0.1:8080/ - - --logtostderr=true - - --v=0 - image: gcr.io/kubebuilder/kube-rbac-proxy:v0.13.0 - name: kube-rbac-proxy - ports: - - containerPort: 8443 - name: https - protocol: TCP - resources: - limits: - cpu: 500m - memory: 128Mi - requests: - cpu: 5m - memory: 64Mi - args: - --metrics-addr=127.0.0.1:8080 - --enable-leader-election @@ -338,6 +320,24 @@ spec: - mountPath: /tmp/k8s-webhook-server/serving-certs name: cert readOnly: true + - args: + - --secure-listen-address=0.0.0.0:8443 + - --upstream=http://127.0.0.1:8080/ + - --logtostderr=true + - --v=0 + image: gcr.io/kubebuilder/kube-rbac-proxy:v0.13.0 + name: kube-rbac-proxy + ports: + - containerPort: 8443 + name: https + protocol: TCP + resources: + limits: + cpu: 500m + memory: 128Mi + requests: + cpu: 5m + memory: 64Mi serviceAccountName: opentelemetry-operator-controller-manager terminationGracePeriodSeconds: 10 volumes: diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml index 733bec7235..18c4fbd1e2 100644 --- a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml +++ b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml @@ -1712,50 +1712,10 @@ spec: OpenTelemetry TargetAllocator. type: string podMonitorSelector: + additionalProperties: + type: string description: PodMonitors to be selected for target discovery. - properties: - matchExpressions: - description: matchExpressions is a list of label selector - requirements. The requirements are ANDed. - items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. - properties: - key: - description: key is the label key that the selector - applies to. - type: string - operator: - description: operator represents a key's relationship - to a set of values. Valid operators are In, NotIn, - Exists and DoesNotExist. - type: string - values: - description: values is an array of string values. If - the operator is In or NotIn, the values array must - be non-empty. If the operator is Exists or DoesNotExist, - the values array must be empty. This array is replaced - during a strategic merge patch. - items: - type: string - type: array - required: - - key - - operator - type: object - type: array - matchLabels: - additionalProperties: - type: string - description: matchLabels is a map of {key,value} pairs. A - single {key,value} in the matchLabels map is equivalent - to an element of matchExpressions, whose key field is "key", - the operator is "In", and the values array contains only - "value". The requirements are ANDed. - type: object type: object - x-kubernetes-map-type: atomic prometheusCR: description: PrometheusCR defines the configuration for the retrieval of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1 @@ -1781,50 +1741,10 @@ spec: service account to use with this instance. type: string serviceMonitorSelector: + additionalProperties: + type: string description: ServiceMonitors to be selected for target discovery. - properties: - matchExpressions: - description: matchExpressions is a list of label selector - requirements. The requirements are ANDed. - items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. - properties: - key: - description: key is the label key that the selector - applies to. - type: string - operator: - description: operator represents a key's relationship - to a set of values. Valid operators are In, NotIn, - Exists and DoesNotExist. - type: string - values: - description: values is an array of string values. If - the operator is In or NotIn, the values array must - be non-empty. If the operator is Exists or DoesNotExist, - the values array must be empty. This array is replaced - during a strategic merge patch. - items: - type: string - type: array - required: - - key - - operator - type: object - type: array - matchLabels: - additionalProperties: - type: string - description: matchLabels is a map of {key,value} pairs. A - single {key,value} in the matchLabels map is equivalent - to an element of matchExpressions, whose key field is "key", - the operator is "In", and the values array contains only - "value". The requirements are ANDed. - type: object type: object - x-kubernetes-map-type: atomic type: object tolerations: description: Toleration to schedule OpenTelemetry Collector pods. diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index 5c5f0b84cb..41bdee0f95 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -1,2 +1,8 @@ resources: - manager.yaml +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +images: +- name: controller + newName: ghcr.io/jacob.aronoff/opentelemetry-operator/opentelemetry-operator + newTag: 0.49.0-174-g17e2819 diff --git a/docs/api.md b/docs/api.md index fc42e04474..bef36ebd10 100644 --- a/docs/api.md +++ b/docs/api.md @@ -4557,8 +4557,8 @@ TargetAllocator indicates a value which determines whether to spawn a target all false - podMonitorSelector - object + podMonitorSelector + map[string]string PodMonitors to be selected for target discovery.
@@ -4587,85 +4587,10 @@ TargetAllocator indicates a value which determines whether to spawn a target all false - serviceMonitorSelector - object - - ServiceMonitors to be selected for target discovery.
- - false - - - - -### OpenTelemetryCollector.spec.targetAllocator.podMonitorSelector -[↩ Parent](#opentelemetrycollectorspectargetallocator) - - - -PodMonitors to be selected for target discovery. - - - - - - - - - - - - - - - - - + - - -
NameTypeDescriptionRequired
matchExpressions[]object - matchExpressions is a list of label selector requirements. The requirements are ANDed.
-
false
matchLabelsserviceMonitorSelector map[string]string - matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
-
false
- - -### OpenTelemetryCollector.spec.targetAllocator.podMonitorSelector.matchExpressions[index] -[↩ Parent](#opentelemetrycollectorspectargetallocatorpodmonitorselector) - - - -A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. - - - - - - - - - - - - - - - - - - - - - - - - @@ -4699,81 +4624,6 @@ PrometheusCR defines the configuration for the retrieval of PrometheusOperator C
NameTypeDescriptionRequired
keystring - key is the label key that the selector applies to.
-
true
operatorstring - operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
-
true
values[]string - values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
+ ServiceMonitors to be selected for target discovery.
false
-### OpenTelemetryCollector.spec.targetAllocator.serviceMonitorSelector -[↩ Parent](#opentelemetrycollectorspectargetallocator) - - - -ServiceMonitors to be selected for target discovery. - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescriptionRequired
matchExpressions[]object - matchExpressions is a list of label selector requirements. The requirements are ANDed.
-
false
matchLabelsmap[string]string - matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
-
false
- - -### OpenTelemetryCollector.spec.targetAllocator.serviceMonitorSelector.matchExpressions[index] -[↩ Parent](#opentelemetrycollectorspectargetallocatorservicemonitorselector) - - - -A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values. - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescriptionRequired
keystring - key is the label key that the selector applies to.
-
true
operatorstring - operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
-
true
values[]string - values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
-
false
- - ### OpenTelemetryCollector.spec.tolerations[index] [↩ Parent](#opentelemetrycollectorspec) From c5ed601742b44b3242603ead7ab030cd80cd28cc Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Wed, 16 Nov 2022 15:47:03 -0500 Subject: [PATCH 5/8] remove kustomization --- config/manager/kustomization.yaml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index 41bdee0f95..5c5f0b84cb 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -1,8 +1,2 @@ resources: - manager.yaml -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization -images: -- name: controller - newName: ghcr.io/jacob.aronoff/opentelemetry-operator/opentelemetry-operator - newTag: 0.49.0-174-g17e2819 From 35b245f79cef1e7784bf60e3f915c8596e225b99 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Thu, 17 Nov 2022 15:07:18 -0500 Subject: [PATCH 6/8] Changed based on feedback --- apis/v1alpha1/opentelemetrycollector_types.go | 14 +++++-- apis/v1alpha1/zz_generated.deepcopy.go | 38 ++++++++----------- ...ntelemetry.io_opentelemetrycollectors.yaml | 26 ++++++++----- cmd/otel-allocator/config/config.go | 4 +- cmd/otel-allocator/watcher/promOperator.go | 4 +- ...ntelemetry.io_opentelemetrycollectors.yaml | 26 ++++++++----- pkg/collector/reconcile/configmap.go | 8 ++-- pkg/collector/reconcile/configmap_test.go | 4 +- 8 files changed, 67 insertions(+), 57 deletions(-) diff --git a/apis/v1alpha1/opentelemetrycollector_types.go b/apis/v1alpha1/opentelemetrycollector_types.go index e09bffc01d..dc2f86fce6 100644 --- a/apis/v1alpha1/opentelemetrycollector_types.go +++ b/apis/v1alpha1/opentelemetrycollector_types.go @@ -177,16 +177,22 @@ type OpenTelemetryTargetAllocator struct { // All CR instances which the ServiceAccount has access to will be retrieved. This includes other namespaces. // +optional PrometheusCR OpenTelemetryTargetAllocatorPrometheusCR `json:"prometheusCR,omitempty"` - // PodMonitors to be selected for target discovery. - PodMonitorSelector *map[string]string `json:"podMonitorSelector,omitempty"` - // ServiceMonitors to be selected for target discovery. - ServiceMonitorSelector *map[string]string `json:"serviceMonitorSelector,omitempty"` } type OpenTelemetryTargetAllocatorPrometheusCR struct { // Enabled indicates whether to use a PrometheusOperator custom resources as targets or not. // +optional Enabled bool `json:"enabled,omitempty"` + // PodMonitors to be selected for target discovery. + // This is a map of {key,value} pairs. Each {key,value} in the map is going to exactly match a label in a + // PodMonitor's meta labels. The requirements are ANDed. + // +optional + PodMonitorSelector map[string]string `json:"podMonitorSelector,omitempty"` + // ServiceMonitors to be selected for target discovery. + // This is a map of {key,value} pairs. Each {key,value} in the map is going to exactly match a label in a + // ServiceMonitor's meta labels. The requirements are ANDed. + // +optional + ServiceMonitorSelector map[string]string `json:"serviceMonitorSelector,omitempty"` } // ScaleSubresourceStatus defines the observed state of the OpenTelemetryCollector's diff --git a/apis/v1alpha1/zz_generated.deepcopy.go b/apis/v1alpha1/zz_generated.deepcopy.go index f6e7e17f4e..1038386010 100644 --- a/apis/v1alpha1/zz_generated.deepcopy.go +++ b/apis/v1alpha1/zz_generated.deepcopy.go @@ -480,29 +480,7 @@ func (in *OpenTelemetryTargetAllocator) DeepCopyInto(out *OpenTelemetryTargetAll *out = new(int32) **out = **in } - out.PrometheusCR = in.PrometheusCR - if in.PodMonitorSelector != nil { - in, out := &in.PodMonitorSelector, &out.PodMonitorSelector - *out = new(map[string]string) - if **in != nil { - in, out := *in, *out - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } - } - } - if in.ServiceMonitorSelector != nil { - in, out := &in.ServiceMonitorSelector, &out.ServiceMonitorSelector - *out = new(map[string]string) - if **in != nil { - in, out := *in, *out - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } - } - } + in.PrometheusCR.DeepCopyInto(&out.PrometheusCR) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OpenTelemetryTargetAllocator. @@ -518,6 +496,20 @@ func (in *OpenTelemetryTargetAllocator) DeepCopy() *OpenTelemetryTargetAllocator // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *OpenTelemetryTargetAllocatorPrometheusCR) DeepCopyInto(out *OpenTelemetryTargetAllocatorPrometheusCR) { *out = *in + if in.PodMonitorSelector != nil { + in, out := &in.PodMonitorSelector, &out.PodMonitorSelector + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.ServiceMonitorSelector != nil { + in, out := &in.ServiceMonitorSelector, &out.ServiceMonitorSelector + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OpenTelemetryTargetAllocatorPrometheusCR. diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml index 18c4fbd1e2..1e95f54bd7 100644 --- a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml +++ b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml @@ -1711,11 +1711,6 @@ spec: description: Image indicates the container image to use for the OpenTelemetry TargetAllocator. type: string - podMonitorSelector: - additionalProperties: - type: string - description: PodMonitors to be selected for target discovery. - type: object prometheusCR: description: PrometheusCR defines the configuration for the retrieval of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1 @@ -1727,6 +1722,22 @@ spec: description: Enabled indicates whether to use a PrometheusOperator custom resources as targets or not. type: boolean + podMonitorSelector: + additionalProperties: + type: string + description: PodMonitors to be selected for target discovery. + This is a map of {key,value} pairs. Each {key,value} in + the map is going to exactly match a label in a PodMonitor's + meta labels. The requirements are ANDed. + type: object + serviceMonitorSelector: + additionalProperties: + type: string + description: ServiceMonitors to be selected for target discovery. + This is a map of {key,value} pairs. Each {key,value} in + the map is going to exactly match a label in a ServiceMonitor's + meta labels. The requirements are ANDed. + type: object type: object replicas: description: Replicas is the number of pod instances for the underlying @@ -1740,11 +1751,6 @@ spec: description: ServiceAccount indicates the name of an existing service account to use with this instance. type: string - serviceMonitorSelector: - additionalProperties: - type: string - description: ServiceMonitors to be selected for target discovery. - type: object type: object tolerations: description: Toleration to schedule OpenTelemetry Collector pods. diff --git a/cmd/otel-allocator/config/config.go b/cmd/otel-allocator/config/config.go index 998a750ae9..ed16eddce6 100644 --- a/cmd/otel-allocator/config/config.go +++ b/cmd/otel-allocator/config/config.go @@ -43,8 +43,8 @@ type Config struct { Config *promconfig.Config `yaml:"config"` AllocationStrategy *string `yaml:"allocation_strategy,omitempty"` FilterStrategy *string `yaml:"filter_strategy,omitempty"` - PodMonitorSelector *map[string]string `yaml:"pod_monitor_selector,omitempty"` - ServiceMonitorSelector *map[string]string `yaml:"service_monitor_selector,omitempty"` + PodMonitorSelector map[string]string `yaml:"pod_monitor_selector,omitempty"` + ServiceMonitorSelector map[string]string `yaml:"service_monitor_selector,omitempty"` } func (c Config) GetAllocationStrategy() string { diff --git a/cmd/otel-allocator/watcher/promOperator.go b/cmd/otel-allocator/watcher/promOperator.go index abb77fa4b3..2b33b633bc 100644 --- a/cmd/otel-allocator/watcher/promOperator.go +++ b/cmd/otel-allocator/watcher/promOperator.go @@ -85,12 +85,12 @@ type PrometheusCRWatcher struct { podMonitorSelector labels.Selector } -func getSelector(s *map[string]string) labels.Selector { +func getSelector(s map[string]string) labels.Selector { sel := labels.NewSelector() if s == nil { return sel } - return labels.SelectorFromSet(*s) + return labels.SelectorFromSet(s) } // Start wrapped informers and wait for an initial sync. diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml index aa567ca9ad..f04031ee7f 100644 --- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml +++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml @@ -1709,11 +1709,6 @@ spec: description: Image indicates the container image to use for the OpenTelemetry TargetAllocator. type: string - podMonitorSelector: - additionalProperties: - type: string - description: PodMonitors to be selected for target discovery. - type: object prometheusCR: description: PrometheusCR defines the configuration for the retrieval of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1 @@ -1725,6 +1720,22 @@ spec: description: Enabled indicates whether to use a PrometheusOperator custom resources as targets or not. type: boolean + podMonitorSelector: + additionalProperties: + type: string + description: PodMonitors to be selected for target discovery. + This is a map of {key,value} pairs. Each {key,value} in + the map is going to exactly match a label in a PodMonitor's + meta labels. The requirements are ANDed. + type: object + serviceMonitorSelector: + additionalProperties: + type: string + description: ServiceMonitors to be selected for target discovery. + This is a map of {key,value} pairs. Each {key,value} in + the map is going to exactly match a label in a ServiceMonitor's + meta labels. The requirements are ANDed. + type: object type: object replicas: description: Replicas is the number of pod instances for the underlying @@ -1738,11 +1749,6 @@ spec: description: ServiceAccount indicates the name of an existing service account to use with this instance. type: string - serviceMonitorSelector: - additionalProperties: - type: string - description: ServiceMonitors to be selected for target discovery. - type: object type: object tolerations: description: Toleration to schedule OpenTelemetry Collector pods. diff --git a/pkg/collector/reconcile/configmap.go b/pkg/collector/reconcile/configmap.go index 3ae1a7b89d..801fa7ba63 100644 --- a/pkg/collector/reconcile/configmap.go +++ b/pkg/collector/reconcile/configmap.go @@ -124,12 +124,12 @@ func desiredTAConfigMap(params Params) (corev1.ConfigMap, error) { taConfig["filter_strategy"] = params.Instance.Spec.TargetAllocator.FilterStrategy } - if params.Instance.Spec.TargetAllocator.ServiceMonitorSelector != nil { - taConfig["service_monitor_selector"] = ¶ms.Instance.Spec.TargetAllocator.ServiceMonitorSelector + if params.Instance.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector != nil { + taConfig["service_monitor_selector"] = ¶ms.Instance.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector } - if params.Instance.Spec.TargetAllocator.PodMonitorSelector != nil { - taConfig["pod_monitor_selector"] = ¶ms.Instance.Spec.TargetAllocator.PodMonitorSelector + if params.Instance.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector != nil { + taConfig["pod_monitor_selector"] = ¶ms.Instance.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector } taConfigYAML, err := yaml.Marshal(taConfig) diff --git a/pkg/collector/reconcile/configmap_test.go b/pkg/collector/reconcile/configmap_test.go index 77c6a1f803..22bef856a4 100644 --- a/pkg/collector/reconcile/configmap_test.go +++ b/pkg/collector/reconcile/configmap_test.go @@ -234,10 +234,10 @@ service_monitor_selector: `, } p := params() - p.Instance.Spec.TargetAllocator.PodMonitorSelector = &map[string]string{ + p.Instance.Spec.TargetAllocator.PrometheusCR.PodMonitorSelector = map[string]string{ "release": "test", } - p.Instance.Spec.TargetAllocator.ServiceMonitorSelector = &map[string]string{ + p.Instance.Spec.TargetAllocator.PrometheusCR.ServiceMonitorSelector = map[string]string{ "release": "test", } actual, err := desiredTAConfigMap(p) From 0e5adc6d327254c32917c6d8a53dd25c101ba84e Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Thu, 17 Nov 2022 15:24:40 -0500 Subject: [PATCH 7/8] Docs --- docs/api.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/api.md b/docs/api.md index bef36ebd10..832176e986 100644 --- a/docs/api.md +++ b/docs/api.md @@ -4556,13 +4556,6 @@ TargetAllocator indicates a value which determines whether to spawn a target all Image indicates the container image to use for the OpenTelemetry TargetAllocator.
false - - podMonitorSelector - map[string]string - - PodMonitors to be selected for target discovery.
- - false prometheusCR object @@ -4586,13 +4579,6 @@ TargetAllocator indicates a value which determines whether to spawn a target all ServiceAccount indicates the name of an existing service account to use with this instance.
false - - serviceMonitorSelector - map[string]string - - ServiceMonitors to be selected for target discovery.
- - false @@ -4620,6 +4606,20 @@ PrometheusCR defines the configuration for the retrieval of PrometheusOperator C Enabled indicates whether to use a PrometheusOperator custom resources as targets or not.
false + + podMonitorSelector + map[string]string + + PodMonitors to be selected for target discovery. This is a map of {key,value} pairs. Each {key,value} in the map is going to exactly match a label in a PodMonitor's meta labels. The requirements are ANDed.
+ + false + + serviceMonitorSelector + map[string]string + + ServiceMonitors to be selected for target discovery. This is a map of {key,value} pairs. Each {key,value} in the map is going to exactly match a label in a ServiceMonitor's meta labels. The requirements are ANDed.
+ + false From 690e51cce1cb7341309a14a8f87c45ed74e28ca0 Mon Sep 17 00:00:00 2001 From: Jacob Aronoff Date: Thu, 17 Nov 2022 15:25:59 -0500 Subject: [PATCH 8/8] config whoops --- cmd/otel-allocator/config/config_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/otel-allocator/config/config_test.go b/cmd/otel-allocator/config/config_test.go index 4df0a93800..48ba9f39ae 100644 --- a/cmd/otel-allocator/config/config_test.go +++ b/cmd/otel-allocator/config/config_test.go @@ -135,10 +135,10 @@ func TestLoad(t *testing.T) { }, }, }, - PodMonitorSelector: &map[string]string{ + PodMonitorSelector: map[string]string{ "release": "test", }, - ServiceMonitorSelector: &map[string]string{ + ServiceMonitorSelector: map[string]string{ "release": "test", }, },