1- // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2- // or more contributor license agreements. Licensed under the Elastic License 2.0;
3- // you may not use this file except in compliance with the Elastic License 2.0.
4-
5- package operator
6-
7- import (
8- "context"
9-
10- corev1 "k8s.io/api/core/v1"
11- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12- "k8s.io/apimachinery/pkg/labels"
13- "sigs.k8s.io/controller-runtime/pkg/client"
14-
15- ulog "github.com/elastic/cloud-on-k8s/v3/pkg/utils/log"
16- )
17-
18- // ShouldManageNamespace determines if the operator should manage resources in the given namespace
19- // based on the configured namespace label selector.
20- func (p Parameters ) ShouldManageNamespace (ctx context.Context , c client.Client , namespace string ) (bool , error ) {
21- // If no namespace label selector is configured, manage all namespaces (backwards compatibility)
22- if p .NamespaceLabelSelector == nil {
23- return true , nil
24- }
25-
26- log := ulog .FromContext (ctx )
27-
28- // Get the namespace object
29- var ns corev1.Namespace
30- if err := c .Get (ctx , client.ObjectKey {Name : namespace }, & ns ); err != nil {
31- log .Error (err , "Failed to get namespace" , "namespace" , namespace )
32- return false , err
33- }
34-
35- // Convert LabelSelector to labels.Selector
36- selector , err := metav1 .LabelSelectorAsSelector (p .NamespaceLabelSelector )
37- if err != nil {
38- log .Error (err , "Failed to convert namespace label selector" , "selector" , p .NamespaceLabelSelector )
39- return false , err
40- }
41-
42- // Check if namespace labels match the selector
43- matches := selector .Matches (labels .Set (ns .Labels ))
44-
45- log .V (1 ).Info ("Namespace filtering check" ,
46- "namespace" , namespace ,
47- "labels" , ns .Labels ,
48- "selector" , p .NamespaceLabelSelector ,
49- "matches" , matches )
50-
51- return matches , nil
52- }
1+ // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+ // or more contributor license agreements. Licensed under the Elastic License 2.0;
3+ // you may not use this file except in compliance with the Elastic License 2.0.
4+
5+ package operator
6+
7+ import (
8+ "context"
9+
10+ corev1 "k8s.io/api/core/v1"
11+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+ "k8s.io/apimachinery/pkg/labels"
13+ "sigs.k8s.io/controller-runtime/pkg/client"
14+
15+ ulog "github.com/elastic/cloud-on-k8s/v3/pkg/utils/log"
16+ )
17+
18+ // ShouldManageNamespace determines if the operator should manage resources in the given namespace
19+ // based on the configured namespace label selector.
20+ func (p Parameters ) ShouldManageNamespace (ctx context.Context , c client.Client , namespace string ) (bool , error ) {
21+ // If no namespace label selector is configured, manage all namespaces (backwards compatibility)
22+ if p .NamespaceLabelSelector == nil {
23+ return true , nil
24+ }
25+
26+ log := ulog .FromContext (ctx )
27+
28+ // Get the namespace object
29+ var ns corev1.Namespace
30+ if err := c .Get (ctx , client.ObjectKey {Name : namespace }, & ns ); err != nil {
31+ log .Error (err , "Failed to get namespace" , "namespace" , namespace )
32+ return false , err
33+ }
34+
35+ // Convert LabelSelector to labels.Selector
36+ selector , err := metav1 .LabelSelectorAsSelector (p .NamespaceLabelSelector )
37+ if err != nil {
38+ log .Error (err , "Failed to convert namespace label selector" , "selector" , p .NamespaceLabelSelector )
39+ return false , err
40+ }
41+
42+ // Check if namespace labels match the selector
43+ matches := selector .Matches (labels .Set (ns .Labels ))
44+
45+ log .V (1 ).Info ("Namespace filtering check" ,
46+ "namespace" , namespace ,
47+ "labels" , ns .Labels ,
48+ "selector" , p .NamespaceLabelSelector ,
49+ "matches" , matches )
50+
51+ return matches , nil
52+ }
0 commit comments