Skip to content

Commit 437d50d

Browse files
committed
Refactor Target Allocator namespace handling: replace WatchNamespace with AllowNamespaces and DenyNamespaces
Signed-off-by: Charlie Le <[email protected]>
1 parent d58e2a4 commit 437d50d

32 files changed

+475
-211
lines changed

.chloggen/namespace-ta.yaml

+3-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 specific namespace(s) in the target allocator.
9+
Add support for setting the allowNamespaces and denyNamespaces in the target allocator.
1010
1111
# One or more tracking issues related to the change
1212
issues: [3086]
@@ -15,4 +15,5 @@ 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 flag can be set to an empty string to watch all namespaces (default) or to a comma-separated list of namespaces to watch.
18+
allowNamespaces can be set to an empty string to watch all namespaces (default) or to a comma-separated list of namespaces to watch.
19+
denyNamespaces can be set to an empty string to deny watching any namespaces (default) or to a comma-separated list of namespaces to deny watching.

apis/v1beta1/targetallocator_types.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ 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.
15+
// AllowNamespaces Namespaces to scope the interaction of the Target Allocator and the apiserver (allow list). This is mutually exclusive with DenyNamespaces.
1616
// +optional
17-
WatchNamespace string `json:"watchNamespace,omitempty"`
17+
AllowNamespaces string `json:"allowNamespaces,omitempty"`
18+
// DenyNamespaces Namespaces to scope the interaction of the Target Allocator and the apiserver (allow list). This is mutually exclusive with AllowNamespaces.
19+
// +optional
20+
DenyNamespaces string `json:"denyNamespaces,omitempty"`
1821
// Default interval between consecutive scrapes. Intervals set in ServiceMonitors and PodMonitors override it.
1922
//Equivalent to the same setting on the Prometheus CR.
2023
//

bundle/community/manifests/opentelemetry.io_opentelemetrycollectors.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -7889,6 +7889,10 @@ spec:
78897889
type: object
78907890
prometheusCR:
78917891
properties:
7892+
allowNamespaces:
7893+
type: string
7894+
denyNamespaces:
7895+
type: string
78927896
enabled:
78937897
type: boolean
78947898
podMonitorSelector:
@@ -7999,8 +8003,6 @@ spec:
79998003
type: object
80008004
type: object
80018005
x-kubernetes-map-type: atomic
8002-
watchNamespace:
8003-
type: string
80048006
type: object
80058007
replicas:
80068008
format: int32

bundle/community/manifests/opentelemetry.io_targetallocators.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,10 @@ spec:
22572257
type: string
22582258
prometheusCR:
22592259
properties:
2260+
allowNamespaces:
2261+
type: string
2262+
denyNamespaces:
2263+
type: string
22602264
enabled:
22612265
type: boolean
22622266
podMonitorSelector:
@@ -2367,8 +2371,6 @@ spec:
23672371
type: object
23682372
type: object
23692373
x-kubernetes-map-type: atomic
2370-
watchNamespace:
2371-
type: string
23722374
type: object
23732375
replicas:
23742376
format: int32

bundle/openshift/manifests/opentelemetry.io_opentelemetrycollectors.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -7889,6 +7889,10 @@ spec:
78897889
type: object
78907890
prometheusCR:
78917891
properties:
7892+
allowNamespaces:
7893+
type: string
7894+
denyNamespaces:
7895+
type: string
78927896
enabled:
78937897
type: boolean
78947898
podMonitorSelector:
@@ -7999,8 +8003,6 @@ spec:
79998003
type: object
80008004
type: object
80018005
x-kubernetes-map-type: atomic
8002-
watchNamespace:
8003-
type: string
80048006
type: object
80058007
replicas:
80068008
format: int32

bundle/openshift/manifests/opentelemetry.io_targetallocators.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,10 @@ spec:
22572257
type: string
22582258
prometheusCR:
22592259
properties:
2260+
allowNamespaces:
2261+
type: string
2262+
denyNamespaces:
2263+
type: string
22602264
enabled:
22612265
type: boolean
22622266
podMonitorSelector:
@@ -2367,8 +2371,6 @@ spec:
23672371
type: object
23682372
type: object
23692373
x-kubernetes-map-type: atomic
2370-
watchNamespace:
2371-
type: string
23722374
type: object
23732375
replicas:
23742376
format: int32

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

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

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

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

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

57+
if cfg.PrometheusCR.AllowNamespaces != "" && cfg.PrometheusCR.DenyNamespaces != "" {
58+
return nil, fmt.Errorf("both allow and deny namespaces are set, only one should be set")
59+
}
60+
5761
allowList := map[string]struct{}{}
58-
if cfg.PrometheusCR.WatchNamespace != "" {
59-
logger.Info("watching namespace(s)", "namespaces", cfg.PrometheusCR.WatchNamespace)
60-
for _, ns := range strings.Split(cfg.PrometheusCR.WatchNamespace, ",") {
62+
if cfg.PrometheusCR.AllowNamespaces != "" {
63+
logger.Info("watching namespace(s)", "namespaces", cfg.PrometheusCR.AllowNamespaces)
64+
for _, ns := range strings.Split(cfg.PrometheusCR.AllowNamespaces, ",") {
6165
allowList[ns] = struct{}{}
6266
}
6367
} else {
64-
logger.Info("cfg.PrometheusCR.WatchNamespace is unset, watching all namespaces")
68+
logger.Info("cfg.PrometheusCR.AllowNamespaces is unset, watching all namespaces")
6569
allowList = map[string]struct{}{v1.NamespaceAll: {}}
6670
}
6771

68-
factory := informers.NewMonitoringInformerFactories(allowList, map[string]struct{}{}, mClient, allocatorconfig.DefaultResyncTime, nil) //TODO decide what strategy to use regarding namespaces
72+
denyList := map[string]struct{}{}
73+
if cfg.PrometheusCR.DenyNamespaces != "" {
74+
logger.Info("excluding namespace(s)", "namespaces", cfg.PrometheusCR.DenyNamespaces)
75+
for _, ns := range strings.Split(cfg.PrometheusCR.DenyNamespaces, ",") {
76+
denyList[ns] = struct{}{}
77+
}
78+
}
79+
80+
factory := informers.NewMonitoringInformerFactories(allowList, denyList, mClient, allocatorconfig.DefaultResyncTime, nil)
6981

7082
monitoringInformers, err := getInformers(factory)
7183
if err != nil {
@@ -111,7 +123,7 @@ func NewPrometheusCRWatcher(ctx context.Context, logger logr.Logger, cfg allocat
111123
logger.Error(err, "Retrying namespace informer creation in promOperator CRD watcher")
112124
return true
113125
}, func() error {
114-
nsMonInf, err = getNamespaceInformer(ctx, allowList, promLogger, clientset, operatorMetrics)
126+
nsMonInf, err = getNamespaceInformer(ctx, allowList, denyList, promLogger, clientset, operatorMetrics)
115127
return err
116128
})
117129
if getNamespaceInformerErr != nil {
@@ -161,7 +173,7 @@ type PrometheusCRWatcher struct {
161173
store *assets.StoreBuilder
162174
}
163175

164-
func getNamespaceInformer(ctx context.Context, allowList map[string]struct{}, promOperatorLogger *slog.Logger, clientset kubernetes.Interface, operatorMetrics *operator.Metrics) (cache.SharedIndexInformer, error) {
176+
func getNamespaceInformer(ctx context.Context, allowList, denyList map[string]struct{}, promOperatorLogger *slog.Logger, clientset kubernetes.Interface, operatorMetrics *operator.Metrics) (cache.SharedIndexInformer, error) {
165177
kubernetesVersion, err := clientset.Discovery().ServerVersion()
166178
if err != nil {
167179
return nil, err
@@ -177,7 +189,7 @@ func getNamespaceInformer(ctx context.Context, allowList map[string]struct{}, pr
177189
clientset.CoreV1(),
178190
clientset.AuthorizationV1().SelfSubjectAccessReviews(),
179191
allowList,
180-
map[string]struct{}{},
192+
denyList,
181193
)
182194
if err != nil {
183195
return nil, err

config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -7875,6 +7875,10 @@ spec:
78757875
type: object
78767876
prometheusCR:
78777877
properties:
7878+
allowNamespaces:
7879+
type: string
7880+
denyNamespaces:
7881+
type: string
78787882
enabled:
78797883
type: boolean
78807884
podMonitorSelector:
@@ -7985,8 +7989,6 @@ spec:
79857989
type: object
79867990
type: object
79877991
x-kubernetes-map-type: atomic
7988-
watchNamespace:
7989-
type: string
79907992
type: object
79917993
replicas:
79927994
format: int32

config/crd/bases/opentelemetry.io_targetallocators.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,10 @@ spec:
22542254
type: string
22552255
prometheusCR:
22562256
properties:
2257+
allowNamespaces:
2258+
type: string
2259+
denyNamespaces:
2260+
type: string
22572261
enabled:
22582262
type: boolean
22592263
podMonitorSelector:
@@ -2364,8 +2368,6 @@ spec:
23642368
type: object
23652369
type: object
23662370
x-kubernetes-map-type: atomic
2367-
watchNamespace:
2368-
type: string
23692371
type: object
23702372
replicas:
23712373
format: int32

0 commit comments

Comments
 (0)