Skip to content

Commit 0d01a64

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

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed

cmd/main.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ 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+
corev1 "k8s.io/api/core/v1"
26+
"k8s.io/apimachinery/pkg/labels"
2927
"k8s.io/apimachinery/pkg/runtime"
28+
"k8s.io/apimachinery/pkg/selection"
3029
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3130
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
31+
"sigs.k8s.io/controller-runtime/pkg/cache"
32+
"sigs.k8s.io/controller-runtime/pkg/client"
33+
34+
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
35+
// to ensure that exec-entrypoint and run can make use of them.
36+
_ "k8s.io/client-go/plugin/pkg/client/auth"
3237
ctrl "sigs.k8s.io/controller-runtime"
3338
"sigs.k8s.io/controller-runtime/pkg/healthz"
3439
"sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -38,6 +43,7 @@ import (
3843

3944
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
4045
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller"
46+
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/global"
4147

4248
// +kubebuilder:scaffold:imports
4349

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

8289
flag.StringVar(&certificateNamespace, "certificate-namespace", "monsoon3", "The namespace for the certificates. ")
8390
flag.StringVar(&certificateIssuerName, "certificate-issuer-name", "nova-hypervisor-agents-ca-issuer",
@@ -135,6 +142,28 @@ func main() {
135142
}
136143
restConfig := ctrl.GetConfigOrDie()
137144

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

158190
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/controller/node_eviction_label_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ func (r *NodeEvictionLabelReconciler) Reconcile(ctx context.Context, req ctrl.Re
7373
Name: name,
7474
},
7575
}
76-
7776
var err error
7877
if !found {
7978
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)