From eff5994ca63adf7da17c980afe534a80bfd74696 Mon Sep 17 00:00:00 2001 From: Lorenzo Manacorda Date: Tue, 24 Apr 2018 14:08:50 +0200 Subject: [PATCH] controller/*: Use native Kubernetes functions The `NewFilteredListWatchFromClient` function introduced in client-go v7.0.0 allows us to filter based on custom fields/labels, therefore we don't need our custom implementation of this functionality. The function was introduced [here](https://github.com/kubernetes/kubernetes/pull/57508) Signed-off-by: Lorenzo Manacorda --- pkg/controller/v1beta1/controller.go | 12 +++++----- pkg/controller/v1beta1/utils.go | 31 ++++++++------------------ pkg/controller/v1beta2/controller.go | 4 ++-- pkg/controller/v1beta2/utils.go | 33 +++++----------------------- 4 files changed, 22 insertions(+), 58 deletions(-) diff --git a/pkg/controller/v1beta1/controller.go b/pkg/controller/v1beta1/controller.go index d1ac3df8..dea020fe 100644 --- a/pkg/controller/v1beta1/controller.go +++ b/pkg/controller/v1beta1/controller.go @@ -167,11 +167,11 @@ func (hc *HabitatController) cacheHabitats() { } func (hc *HabitatController) cacheDeployments() { - source := newListWatchFromClientWithLabels( + source := cache.NewFilteredListWatchFromClient( hc.config.KubernetesClientset.AppsV1beta1().RESTClient(), "deployments", apiv1.NamespaceAll, - labelListOptions()) + listOptions()) hc.deployInformer = cache.NewSharedIndexInformer( source, @@ -190,11 +190,11 @@ func (hc *HabitatController) cacheDeployments() { } func (hc *HabitatController) cacheConfigMaps() { - source := newListWatchFromClientWithLabels( + source := cache.NewFilteredListWatchFromClient( hc.config.KubernetesClientset.CoreV1().RESTClient(), "configmaps", apiv1.NamespaceAll, - labelListOptions()) + listOptions()) hc.cmInformer = cache.NewSharedIndexInformer( source, @@ -213,11 +213,11 @@ func (hc *HabitatController) cacheConfigMaps() { } func (hc *HabitatController) watchPods(ctx context.Context) { - source := newListWatchFromClientWithLabels( + source := cache.NewFilteredListWatchFromClient( hc.config.KubernetesClientset.CoreV1().RESTClient(), "pods", apiv1.NamespaceAll, - labelListOptions()) + listOptions()) c := cache.NewSharedIndexInformer( source, diff --git a/pkg/controller/v1beta1/utils.go b/pkg/controller/v1beta1/utils.go index b73566dc..6e428617 100644 --- a/pkg/controller/v1beta1/utils.go +++ b/pkg/controller/v1beta1/utils.go @@ -26,11 +26,8 @@ import ( apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/apimachinery/pkg/watch" - "k8s.io/client-go/tools/cache" ) const ( @@ -74,26 +71,16 @@ func validateCustomObject(h habv1beta1.Habitat) error { return nil } -// newListWatchFromClientWithLabels is a modified newListWatchFromClient function from listWatch. -// Instead of using fields to filter, we modify the function to use labels. -func newListWatchFromClientWithLabels(c cache.Getter, resource string, namespace string, op metav1.ListOptions) *cache.ListWatch { - listFunc := func(_ metav1.ListOptions) (runtime.Object, error) { - return c.Get(). - Namespace(namespace). - Resource(resource). - VersionedParams(&op, metav1.ParameterCodec). - Do(). - Get() - } - watchFunc := func(_ metav1.ListOptions) (watch.Interface, error) { - op.Watch = true - return c.Get(). - Namespace(namespace). - Resource(resource). - VersionedParams(&op, metav1.ParameterCodec). - Watch() +// listOptions adds filtering for Habitat objects by adding a requirement +// for the Habitat label. +func listOptions() func(*metav1.ListOptions) { + ls := labels.SelectorFromSet(labels.Set(map[string]string{ + habv1beta1.HabitatLabel: "true", + })) + + return func(options *metav1.ListOptions) { + options.LabelSelector = ls.String() } - return &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc} } func labelListOptions() metav1.ListOptions { diff --git a/pkg/controller/v1beta2/controller.go b/pkg/controller/v1beta2/controller.go index f7c4e4ff..bae9edff 100644 --- a/pkg/controller/v1beta2/controller.go +++ b/pkg/controller/v1beta2/controller.go @@ -177,11 +177,11 @@ func (hc *HabitatController) cacheConfigMaps() { } func (hc *HabitatController) watchPods(ctx context.Context) { - source := newListWatchFromClientWithLabels( + source := cache.NewFilteredListWatchFromClient( hc.config.KubernetesClientset.CoreV1().RESTClient(), "pods", apiv1.NamespaceAll, - labelListOptions()) + listOptions()) c := cache.NewSharedIndexInformer( source, diff --git a/pkg/controller/v1beta2/utils.go b/pkg/controller/v1beta2/utils.go index 35552710..ef6784ec 100644 --- a/pkg/controller/v1beta2/utils.go +++ b/pkg/controller/v1beta2/utils.go @@ -26,11 +26,8 @@ import ( apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/errors" "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/apimachinery/pkg/watch" - "k8s.io/client-go/tools/cache" ) const ( @@ -78,35 +75,15 @@ func validateCustomObject(h habv1beta1.Habitat) error { return nil } -// newListWatchFromClientWithLabels is a modified newListWatchFromClient function from listWatch. -// Instead of using fields to filter, we modify the function to use labels. -func newListWatchFromClientWithLabels(c cache.Getter, resource string, namespace string, op metav1.ListOptions) *cache.ListWatch { - listFunc := func(_ metav1.ListOptions) (runtime.Object, error) { - return c.Get(). - Namespace(namespace). - Resource(resource). - VersionedParams(&op, metav1.ParameterCodec). - Do(). - Get() - } - watchFunc := func(_ metav1.ListOptions) (watch.Interface, error) { - op.Watch = true - return c.Get(). - Namespace(namespace). - Resource(resource). - VersionedParams(&op, metav1.ParameterCodec). - Watch() - } - return &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc} -} - -func labelListOptions() metav1.ListOptions { +// listOptions adds filtering for Habitat objects by adding a requirement +// for the Habitat label. +func listOptions() func(*metav1.ListOptions) { ls := labels.SelectorFromSet(labels.Set(map[string]string{ habv1beta1.HabitatLabel: "true", })) - return metav1.ListOptions{ - LabelSelector: ls.String(), + return func(options *metav1.ListOptions) { + options.LabelSelector = ls.String() } }