Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions pkg/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ import (
kapi "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/tools/cache"

k8sClient "github.com/Mellanox/ib-kubernetes/pkg/k8s-client"
resEventHandler "github.com/Mellanox/ib-kubernetes/pkg/watcher/handler"
)

var kubevirtSelectorString = SelectorMustValidateFromSet(labels.Set{"kubevirt.io": "virt-launcher"}).String()
// capiClusterNameSelector matches any pod owned by a Cluster API machine —
// covers both KubeVirt virt-launcher pods (via CAPK) and Virtink VM pods (via CAPCH).
// CAPI propagates this label onto every machine-owned pod regardless of provider.
var capiClusterNameSelector = func() string {
req, err := labels.NewRequirement("cluster.x-k8s.io/cluster-name", selection.Exists, nil)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super tiny nit: I think the reason I did the mustValidate and panic was to fail early if the label was malformed. If this provides the same protection or you think it's unnecessary, feel free to ignore

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it validates the key format too. And panic if it's malformed

if err != nil {
panic(err)
}
return labels.NewSelector().Add(*req).String()
}()

type StopFunc func()

Expand All @@ -28,11 +38,11 @@ type watcher struct {

func NewWatcher(eventHandler resEventHandler.ResourceEventHandler, client k8sClient.Client) Watcher {
resource := eventHandler.GetResourceObject().GetObjectKind().GroupVersionKind().Kind
filterByKubevirtLabel := func(options *metav1.ListOptions) {
options.LabelSelector = kubevirtSelectorString
filterByCAPIClusterName := func(options *metav1.ListOptions) {
options.LabelSelector = capiClusterNameSelector
}
watchList := cache.NewFilteredListWatchFromClient(
client.GetRestClient(), resource, kapi.NamespaceAll, filterByKubevirtLabel)
client.GetRestClient(), resource, kapi.NamespaceAll, filterByCAPIClusterName)
return &watcher{eventHandler: eventHandler, watchList: watchList}
}

Expand All @@ -55,13 +65,3 @@ func (w *watcher) RunBackground() StopFunc {
func (w *watcher) GetHandler() resEventHandler.ResourceEventHandler {
return w.eventHandler
}

// SelectorMustValidateFromSet acts like regex.MustCompile for labels.Selector objects.
// It will attempt to validate the selector from the label set and panic if it fails.
func SelectorMustValidateFromSet(set labels.Set) labels.Selector {
selector, err := labels.ValidatedSelectorFromSet(set)
if err != nil {
panic(err)
}
return selector
}
Loading