Skip to content

Commit 8ac48fe

Browse files
committed
introduce --label-selector option to limit reconcilled objects
1 parent 11460c0 commit 8ac48fe

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

cmd/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ import (
2727
_ "k8s.io/client-go/plugin/pkg/client/auth"
2828

2929
"go.uber.org/zap/zapcore"
30+
corev1 "k8s.io/api/core/v1"
31+
"k8s.io/apimachinery/pkg/labels"
3032
"k8s.io/apimachinery/pkg/runtime"
33+
"k8s.io/apimachinery/pkg/selection"
3134
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3235
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3336
ctrl "sigs.k8s.io/controller-runtime"
37+
"sigs.k8s.io/controller-runtime/pkg/cache"
38+
"sigs.k8s.io/controller-runtime/pkg/client"
3439
"sigs.k8s.io/controller-runtime/pkg/healthz"
3540
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3641
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -39,6 +44,7 @@ import (
3944

4045
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
4146
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller"
47+
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/global"
4248
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/logger"
4349

4450
// +kubebuilder:scaffold:imports
@@ -80,6 +86,7 @@ func main() {
8086
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
8187
flag.BoolVar(&enableHTTP2, "enable-http2", false,
8288
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
89+
flag.StringVar(&global.LabelSelector, "label-selector", "", "Label selector to filter watched resources (namely nodes).")
8390

8491
flag.StringVar(&certificateNamespace, "certificate-namespace", "monsoon3", "The namespace for the certificates. ")
8592
flag.StringVar(&certificateIssuerName, "certificate-issuer-name", "nova-hypervisor-agents-ca-issuer",
@@ -138,6 +145,28 @@ func main() {
138145
}
139146
restConfig := ctrl.GetConfigOrDie()
140147

148+
var cacheOptions cache.Options
149+
if global.LabelSelector != "" {
150+
setupLog.Info("setting up cache with label selector", "selector", global.LabelSelector)
151+
selector := labels.NewSelector()
152+
req, err := labels.NewRequirement(global.LabelSelector, selection.Exists, nil)
153+
if err != nil {
154+
setupLog.Error(err, "unable to parse label selector")
155+
os.Exit(1)
156+
}
157+
158+
cacheOptions = cache.Options{
159+
ByObject: map[client.Object]cache.ByObject{
160+
&corev1.Node{}: {
161+
Label: selector.Add(*req),
162+
},
163+
&kvmv1.Hypervisor{}: {
164+
Label: selector.Add(*req),
165+
},
166+
},
167+
}
168+
}
169+
141170
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
142171
Scheme: scheme,
143172
Metrics: metricsServerOptions,
@@ -156,6 +185,9 @@ func main() {
156185
// if you are doing or is intended to do any operation such as perform cleanups
157186
// after the manager stops then its usage might be unsafe.
158187
// LeaderElectionReleaseOnCancel: true,
188+
189+
// Optionally configure the cache to listen/watch for specific labeled resources only
190+
Cache: cacheOptions,
159191
})
160192

161193
if err != nil {

internal/controller/eviction_controller.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@ import (
2929
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/hypervisors"
3030
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
3131
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/services"
32+
corev1 "k8s.io/api/core/v1"
3233
"k8s.io/apimachinery/pkg/api/meta"
3334
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3435
"k8s.io/apimachinery/pkg/runtime"
36+
"k8s.io/apimachinery/pkg/types"
3537
ctrl "sigs.k8s.io/controller-runtime"
3638
"sigs.k8s.io/controller-runtime/pkg/client"
3739
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3840
logger "sigs.k8s.io/controller-runtime/pkg/log"
3941

4042
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
43+
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/global"
4144
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack"
4245
)
4346

@@ -69,6 +72,14 @@ func (r *EvictionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
6972
return ctrl.Result{}, client.IgnoreNotFound(err)
7073
}
7174

75+
if global.LabelSelector != "" {
76+
// This test-fetch the Node assigned to the eviction, it won't be cached if it's not part of our partition so
77+
// we won't reconcile evictions for nodes outside our partition
78+
if err := r.Get(ctx, types.NamespacedName{Name: eviction.Spec.Hypervisor}, &corev1.Node{}); err != nil {
79+
return ctrl.Result{}, client.IgnoreNotFound(err)
80+
}
81+
}
82+
7283
log := logger.FromContext(ctx).
7384
WithName("Eviction").
7485
WithValues("hypervisor", eviction.Spec.Hypervisor)

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/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)