Skip to content

Commit c82354c

Browse files
committed
Use TA config for setting namespaces to watch
Signed-off-by: Charlie Le <[email protected]>
1 parent ecd4b2b commit c82354c

File tree

13 files changed

+29
-18
lines changed

13 files changed

+29
-18
lines changed

.chloggen/namespace-ta.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ component: target allocator
66

77
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
88
note: |
9-
Add support for `WATCH_NAMESPACE` environment variable in the target allocator.
9+
Add support for watch specific namespace(s) in the target allocator.
1010
1111
# One or more tracking issues related to the change
1212
issues: [3086]
@@ -15,4 +15,4 @@ issues: [3086]
1515
# These lines will be padded with 2 spaces and then inserted directly into the document.
1616
# Use pipe (|) for multiline entries.
1717
subtext: |
18-
This variable can be set to an empty string to watch all namespaces, or to a comma-separated list of namespaces to watch.
18+
This flag can be set to an empty string to watch all namespaces (default) or to a comma-separated list of namespaces to watch.

apis/v1beta1/targetallocator_types.go

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ type TargetAllocatorPrometheusCR struct {
1212
// Enabled indicates whether to use a PrometheusOperator custom resources as targets or not.
1313
// +optional
1414
Enabled bool `json:"enabled,omitempty"`
15+
// WatchNamespace to look for Prometheus CRs. If not set, all namespaces are used which requires a ClusterRole for listing all namespaces.
16+
// +optional
17+
WatchNamespace string `json:"watchNamespace,omitempty"`
1518
// Default interval between consecutive scrapes. Intervals set in ServiceMonitors and PodMonitors override it.
1619
//Equivalent to the same setting on the Prometheus CR.
1720
//

bundle/community/manifests/opentelemetry.io_opentelemetrycollectors.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -7999,6 +7999,8 @@ spec:
79997999
type: object
80008000
type: object
80018001
x-kubernetes-map-type: atomic
8002+
watchNamespace:
8003+
type: string
80028004
type: object
80038005
replicas:
80048006
format: int32

bundle/community/manifests/opentelemetry.io_targetallocators.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,8 @@ spec:
23672367
type: object
23682368
type: object
23692369
x-kubernetes-map-type: atomic
2370+
watchNamespace:
2371+
type: string
23702372
type: object
23712373
replicas:
23722374
format: int32

bundle/openshift/manifests/opentelemetry.io_opentelemetrycollectors.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -7999,6 +7999,8 @@ spec:
79997999
type: object
80008000
type: object
80018001
x-kubernetes-map-type: atomic
8002+
watchNamespace:
8003+
type: string
80028004
type: object
80038005
replicas:
80048006
format: int32

bundle/openshift/manifests/opentelemetry.io_targetallocators.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,8 @@ spec:
23672367
type: object
23682368
type: object
23692369
x-kubernetes-map-type: atomic
2370+
watchNamespace:
2371+
type: string
23702372
type: object
23712373
replicas:
23722374
format: int32

cmd/otel-allocator/README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ rules:
258258

259259
#### Namespace-scoped RBAC
260260

261-
If you want to have the TargetAllocator watch a specific namespace, you can set the WATCH_NAMESPACE environment variable
262-
in the TargetAllocator's deployment. This is useful if you want to restrict the TargetAllocator to only watch Prometheus
261+
If you want to have the TargetAllocator watch a specific namespace, you can set the watchNamespace field
262+
in the TargetAllocator's prometheusCR configuration. This is useful if you want to restrict the TargetAllocator to only watch Prometheus
263263
CRs in a specific namespace, and not have cluster-wide access.
264264

265265
```yaml
@@ -268,13 +268,11 @@ CRs in a specific namespace, and not have cluster-wide access.
268268
serviceAccount: opentelemetry-targetallocator-sa
269269
prometheusCR:
270270
enabled: true
271-
env:
272-
- name: WATCH_NAMESPACE
273-
value: "foo"
271+
watchNamespace: foo
274272
```
275273

276274
In this case, you will need to create a Role and RoleBinding instead of a ClusterRole and ClusterRoleBinding. The Role
277-
and RoleBinding should be created in the namespace specified in the WATCH_NAMESPACE environment variable.
275+
and RoleBinding should be created in the namespace specified by the watchNamespace field.
278276

279277
```yaml
280278
apiVersion: rbac.authorization.k8s.io/v1

cmd/otel-allocator/internal/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type Config struct {
5252

5353
type PrometheusCRConfig struct {
5454
Enabled bool `yaml:"enabled,omitempty"`
55+
WatchNamespace string `yaml:"watch_namespace,omitempty"`
5556
PodMonitorSelector *metav1.LabelSelector `yaml:"pod_monitor_selector,omitempty"`
5657
PodMonitorNamespaceSelector *metav1.LabelSelector `yaml:"pod_monitor_namespace_selector,omitempty"`
5758
ServiceMonitorSelector *metav1.LabelSelector `yaml:"service_monitor_selector,omitempty"`

cmd/otel-allocator/internal/watcher/promOperator.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,15 @@ func NewPrometheusCRWatcher(ctx context.Context, logger logr.Logger, cfg allocat
5454
return nil, err
5555
}
5656

57-
// Check env var for WATCH_NAMESPACE and use it if its set, else use v1.NamespaceAll
58-
// This is to allow the operator to watch only a specific namespace
59-
watchNamespace, found := os.LookupEnv("WATCH_NAMESPACE")
6057
allowList := map[string]struct{}{}
61-
if found {
62-
logger.Info("watching namespace(s)", "namespaces", watchNamespace)
63-
for _, ns := range strings.Split(watchNamespace, ",") {
58+
if cfg.PrometheusCR.WatchNamespace != "" {
59+
logger.Info("watching namespace(s)", "namespaces", cfg.PrometheusCR.WatchNamespace)
60+
for _, ns := range strings.Split(cfg.PrometheusCR.WatchNamespace, ",") {
6461
allowList[ns] = struct{}{}
6562
}
6663
} else {
64+
logger.Info("cfg.PrometheusCR.WatchNamespace is unset, watching all namespaces")
6765
allowList = map[string]struct{}{v1.NamespaceAll: {}}
68-
logger.Info("the env var WATCH_NAMESPACE isn't set, watching all namespaces")
6966
}
7067

7168
factory := informers.NewMonitoringInformerFactories(allowList, map[string]struct{}{}, mClient, allocatorconfig.DefaultResyncTime, nil) //TODO decide what strategy to use regarding namespaces

config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -7985,6 +7985,8 @@ spec:
79857985
type: object
79867986
type: object
79877987
x-kubernetes-map-type: atomic
7988+
watchNamespace:
7989+
type: string
79887990
type: object
79897991
replicas:
79907992
format: int32

config/crd/bases/opentelemetry.io_targetallocators.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,8 @@ spec:
23642364
type: object
23652365
type: object
23662366
x-kubernetes-map-type: atomic
2367+
watchNamespace:
2368+
type: string
23672369
type: object
23682370
replicas:
23692371
format: int32

internal/manifests/targetallocator/configmap.go

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ func ConfigMap(params Params) (*corev1.ConfigMap, error) {
9494
prometheusCRConfig["scrape_interval"] = taSpec.PrometheusCR.ScrapeInterval.Duration
9595
}
9696

97+
prometheusCRConfig["watch_namespace"] = taSpec.PrometheusCR.WatchNamespace
98+
9799
prometheusCRConfig["service_monitor_selector"] = taSpec.PrometheusCR.ServiceMonitorSelector
98100

99101
prometheusCRConfig["pod_monitor_selector"] = taSpec.PrometheusCR.PodMonitorSelector

tests/e2e-targetallocator/targetallocator-namespace/resources/otelcol.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ spec:
77
"zap-log-level": "debug"
88
prometheusCR:
99
enabled: true
10+
watchNamespace: ($namespace)
1011
scrapeInterval: 1s
1112
scrapeConfigSelector: {}
1213
probeSelector: {}
@@ -16,9 +17,6 @@ spec:
1617
metrics:
1718
disablePrometheusAnnotations: true
1819
enableMetrics: true
19-
env:
20-
- name: WATCH_NAMESPACE
21-
value: "($namespace)"
2220
serviceAccount: ta
2321
---
2422
apiVersion: opentelemetry.io/v1beta1

0 commit comments

Comments
 (0)