Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Commit

Permalink
controller/*: Use native Kubernetes functions
Browse files Browse the repository at this point in the history
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](kubernetes/kubernetes#57508)

Signed-off-by: Lorenzo Manacorda <lorenzo@kinvolk.io>
  • Loading branch information
Lorenzo Manacorda committed Apr 24, 2018
1 parent db9008d commit c1a6e60
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 58 deletions.
12 changes: 6 additions & 6 deletions pkg/controller/v1beta1/controller.go
Expand Up @@ -167,11 +167,11 @@ func (hc *HabitatController) cacheHabitats() {
}

func (hc *HabitatController) cacheDeployments() {
source := newListWatchFromClientWithLabels(
source := cache.NewFilteredListWatchFromClient(
hc.config.KubernetesClientset.AppsV1beta2().RESTClient(),
"deployments",
apiv1.NamespaceAll,
labelListOptions())
listOptions())

hc.deployInformer = cache.NewSharedIndexInformer(
source,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
31 changes: 9 additions & 22 deletions pkg/controller/v1beta1/utils.go
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/v1beta2/controller.go
Expand Up @@ -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,
Expand Down
33 changes: 5 additions & 28 deletions pkg/controller/v1beta2/utils.go
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()
}
}

Expand Down

0 comments on commit c1a6e60

Please sign in to comment.