Skip to content

Commit 7627d2a

Browse files
committed
introduce --label-selector option to limit reconcilled objects
1 parent 8fcf045 commit 7627d2a

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

cmd/main.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ import (
2222
"flag"
2323
"os"
2424

25-
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
26-
// to ensure that exec-entrypoint and run can make use of them.
27-
_ "k8s.io/client-go/plugin/pkg/client/auth"
28-
25+
"k8s.io/apimachinery/pkg/labels"
2926
"k8s.io/apimachinery/pkg/runtime"
27+
"k8s.io/apimachinery/pkg/selection"
3028
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3129
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
30+
31+
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
32+
// to ensure that exec-entrypoint and run can make use of them.
33+
_ "k8s.io/client-go/plugin/pkg/client/auth"
3234
ctrl "sigs.k8s.io/controller-runtime"
35+
"sigs.k8s.io/controller-runtime/pkg/cache"
3336
"sigs.k8s.io/controller-runtime/pkg/healthz"
3437
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3538
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -38,6 +41,7 @@ import (
3841

3942
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
4043
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller"
44+
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/global"
4145

4246
// +kubebuilder:scaffold:imports
4347

@@ -78,6 +82,7 @@ func main() {
7882
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
7983
flag.BoolVar(&enableHTTP2, "enable-http2", false,
8084
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
85+
flag.StringVar(&global.LabelSelector, "label-selector", "", "Label selector to filter watched resources (namely nodes).")
8186

8287
flag.StringVar(&certificateNamespace, "certificate-namespace", "monsoon3", "The namespace for the certificates. ")
8388
flag.StringVar(&certificateIssuerName, "certificate-issuer-name", "nova-hypervisor-agents-ca-issuer",
@@ -135,6 +140,21 @@ func main() {
135140
}
136141
restConfig := ctrl.GetConfigOrDie()
137142

143+
var cacheOptions cache.Options
144+
if global.LabelSelector != "" {
145+
setupLog.Info("setting up cache with label selector", "selector", global.LabelSelector)
146+
selector := labels.NewSelector()
147+
req, err := labels.NewRequirement(global.LabelSelector, selection.Exists, nil)
148+
if err != nil {
149+
setupLog.Error(err, "unable to parse label selector")
150+
os.Exit(1)
151+
}
152+
153+
cacheOptions = cache.Options{
154+
DefaultLabelSelector: selector.Add(*req),
155+
}
156+
}
157+
138158
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
139159
Scheme: scheme,
140160
Metrics: metricsServerOptions,
@@ -153,6 +173,9 @@ func main() {
153173
// if you are doing or is intended to do any operation such as perform cleanups
154174
// after the manager stops then its usage might be unsafe.
155175
// LeaderElectionReleaseOnCancel: true,
176+
177+
// Optionally configure the cache to listen/watch for specific labeled resources only
178+
Cache: cacheOptions,
156179
})
157180

158181
if err != nil {

internal/controller/hypervisor_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"sigs.k8s.io/controller-runtime/pkg/predicate"
3838

3939
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
40+
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/global"
4041
)
4142

4243
const (
@@ -174,6 +175,11 @@ func (hv *HypervisorController) SetupWithManager(mgr ctrl.Manager) error {
174175
return fmt.Errorf("failed to create label selector predicate: %w", err)
175176
}
176177

178+
// append the custom label selector from global config
179+
if global.LabelSelector != "" {
180+
transferLabels = append(transferLabels, global.LabelSelector)
181+
}
182+
177183
return ctrl.NewControllerManagedBy(mgr).
178184
Named(HypervisorControllerName).
179185
For(&corev1.Node{}).

internal/controller/node_eviction_label_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
logger "sigs.k8s.io/controller-runtime/pkg/log"
3535

3636
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
37+
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/global"
3738
)
3839

3940
const (
@@ -74,6 +75,13 @@ func (r *NodeEvictionLabelReconciler) Reconcile(ctx context.Context, req ctrl.Re
7475
},
7576
}
7677

78+
// Add global label selector if configured to ensure we reconcile them
79+
if global.LabelSelector != "" {
80+
eviction.Labels = map[string]string{
81+
global.LabelSelector: "true",
82+
}
83+
}
84+
7785
var err error
7886
if !found {
7987
newNode := node.DeepCopy()

internal/global/global.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
SPDX-FileCopyrightText: Copyright 2025 SAP SE or an SAP affiliate company and cobaltcore-dev contributors
3+
SPDX-License-Identifier: Apache-2.0
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package global
19+
20+
var (
21+
// LabelSelector is a custom label that is used to select resources managed by the operator.
22+
LabelSelector = ""
23+
)

0 commit comments

Comments
 (0)