Skip to content

Commit 7c5a4e1

Browse files
Add validation for pod/service monitors for TargetAllocator and skip invalid ones (open-telemetry#2328)
* fix for adding validation * removing unused references * adding tests * adding some changes * cleaning up * adding change log * running goimports and adding return value check code for namespace informer * fixing lint error * fixing tests and comment * adding permissions for e2e tests * adding cluster roles instead of roles * updaintg readme * fixing comments * adding contant to same block * fixing lint errors * running go import * adding namespaces since that is required for informer * adding extected warnings * addressing comments * adding test for namespace label update * fixing goimports * making namespaceselectores as labelselectors
1 parent bf260f3 commit 7c5a4e1

File tree

12 files changed

+941
-275
lines changed

12 files changed

+941
-275
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: breaking
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
5+
component: target allocator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Use recommended interfaces(resource selector) by the prometheus-operator for watching CRs.
9+
10+
# One or more tracking issues related to the change
11+
issues: [2309]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext: The target allocator now requires get/list/watch permissions for namespaces. Update your RBAC permissions for the attached role, if necessary.

apis/v1alpha1/collector_webhook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var (
4747
Verbs: []string{"*"},
4848
}, {
4949
APIGroups: []string{""},
50-
Resources: []string{"nodes", "nodes/metrics", "services", "endpoints", "pods"},
50+
Resources: []string{"nodes", "nodes/metrics", "services", "endpoints", "pods", "namespaces"},
5151
Verbs: []string{"get", "list", "watch"},
5252
}, {
5353
APIGroups: []string{""},

apis/v1alpha1/collector_webhook_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ func TestOTELColValidatingWebhook(t *testing.T) {
563563
"missing the following rules for nodes/metrics: [get,list,watch]",
564564
"missing the following rules for services: [get,list,watch]",
565565
"missing the following rules for endpoints: [get,list,watch]",
566+
"missing the following rules for namespaces: [get,list,watch]",
566567
"missing the following rules for networking.k8s.io/ingresses: [get,list,watch]",
567568
"missing the following rules for nodes: [get,list,watch]",
568569
"missing the following rules for pods: [get,list,watch]",

cmd/otel-allocator/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ to collector instance pods by default.
124124

125125

126126
### RBAC
127-
The ServiceAccount that the TargetAllocator runs as, has to have access to the CRs. A role like this will provide that
127+
The ServiceAccount that the TargetAllocator runs as, has to have access to the CRs and the namespaces to watch for the pod and service monitors. A role like this will provide that
128128
access.
129129
```yaml
130130
apiVersion: rbac.authorization.k8s.io/v1
@@ -139,6 +139,10 @@ rules:
139139
- podmonitors
140140
verbs:
141141
- '*'
142+
- apiGroups: [""]
143+
resources:
144+
- namespaces
145+
verbs: ["get", "list", "watch"]
142146
```
143147
In addition, the TargetAllocator needs the same permissions as a Prometheus instance would to find the matching targets
144148
from the CR instances.

cmd/otel-allocator/config/config.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ const (
4444
)
4545

4646
type Config struct {
47-
ListenAddr string `yaml:"listen_addr,omitempty"`
48-
KubeConfigFilePath string `yaml:"kube_config_file_path,omitempty"`
49-
ClusterConfig *rest.Config `yaml:"-"`
50-
RootLogger logr.Logger `yaml:"-"`
51-
CollectorSelector *metav1.LabelSelector `yaml:"collector_selector,omitempty"`
52-
PromConfig *promconfig.Config `yaml:"config"`
53-
AllocationStrategy string `yaml:"allocation_strategy,omitempty"`
54-
FilterStrategy string `yaml:"filter_strategy,omitempty"`
55-
PrometheusCR PrometheusCRConfig `yaml:"prometheus_cr,omitempty"`
56-
PodMonitorSelector map[string]string `yaml:"pod_monitor_selector,omitempty"`
57-
ServiceMonitorSelector map[string]string `yaml:"service_monitor_selector,omitempty"`
47+
ListenAddr string `yaml:"listen_addr,omitempty"`
48+
KubeConfigFilePath string `yaml:"kube_config_file_path,omitempty"`
49+
ClusterConfig *rest.Config `yaml:"-"`
50+
RootLogger logr.Logger `yaml:"-"`
51+
CollectorSelector *metav1.LabelSelector `yaml:"collector_selector,omitempty"`
52+
PromConfig *promconfig.Config `yaml:"config"`
53+
AllocationStrategy string `yaml:"allocation_strategy,omitempty"`
54+
FilterStrategy string `yaml:"filter_strategy,omitempty"`
55+
PrometheusCR PrometheusCRConfig `yaml:"prometheus_cr,omitempty"`
56+
PodMonitorSelector map[string]string `yaml:"pod_monitor_selector,omitempty"`
57+
ServiceMonitorSelector map[string]string `yaml:"service_monitor_selector,omitempty"`
58+
ServiceMonitorNamespaceSelector *metav1.LabelSelector `yaml:"service_monitor_namespace_selector,omitempty"`
59+
PodMonitorNamespaceSelector *metav1.LabelSelector `yaml:"pod_monitor_namespace_selector,omitempty"`
5860
}
5961

6062
type PrometheusCRConfig struct {

cmd/otel-allocator/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func main() {
102102
defer close(interrupts)
103103

104104
if cfg.PrometheusCR.Enabled {
105-
promWatcher, err = allocatorWatcher.NewPrometheusCRWatcher(setupLog.WithName("prometheus-cr-watcher"), *cfg)
105+
promWatcher, err = allocatorWatcher.NewPrometheusCRWatcher(ctx, setupLog.WithName("prometheus-cr-watcher"), *cfg)
106106
if err != nil {
107107
setupLog.Error(err, "Can't start the prometheus watcher")
108108
os.Exit(1)

0 commit comments

Comments
 (0)