Skip to content

Commit d5220d9

Browse files
committed
refactor(controller): use kubernetes informers provided by client-go
1 parent bbf9432 commit d5220d9

4 files changed

Lines changed: 6 additions & 66 deletions

File tree

pkg/controller/controller.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
rbacv1 "k8s.io/api/rbac/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/types"
29+
informers_core_v1 "k8s.io/client-go/informers/core/v1"
2930
"k8s.io/client-go/kubernetes/scheme"
3031
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
3132
"k8s.io/client-go/tools/cache"
@@ -401,16 +402,8 @@ func (c *Controller) initSharedInformers() {
401402
}
402403

403404
// Pods
404-
podLw := &cache.ListWatch{
405-
ListFunc: c.podListFunc,
406-
WatchFunc: c.podWatchFunc,
407-
}
408-
409-
c.podInformer = cache.NewSharedIndexInformer(
410-
podLw,
411-
&v1.Pod{},
412-
constants.QueueResyncPeriodPod,
413-
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
405+
c.podInformer = informers_core_v1.NewPodInformer(c.KubeClient.Clientset,
406+
c.opConfig.WatchedNamespace, constants.QueueResyncPeriodPod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
414407

415408
c.podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
416409
AddFunc: c.podAdd,
@@ -419,15 +412,7 @@ func (c *Controller) initSharedInformers() {
419412
})
420413

421414
// Kubernetes Nodes
422-
nodeLw := &cache.ListWatch{
423-
ListFunc: c.nodeListFunc,
424-
WatchFunc: c.nodeWatchFunc,
425-
}
426-
427-
c.nodesInformer = cache.NewSharedIndexInformer(
428-
nodeLw,
429-
&v1.Node{},
430-
constants.QueueResyncPeriodNode,
415+
informers_core_v1.NewNodeInformer(c.KubeClient.Clientset, constants.QueueResyncPeriodNode,
431416
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
432417

433418
c.nodesInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{

pkg/controller/node.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,11 @@ import (
99
v1 "k8s.io/api/core/v1"
1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1111
"k8s.io/apimachinery/pkg/labels"
12-
"k8s.io/apimachinery/pkg/runtime"
13-
"k8s.io/apimachinery/pkg/watch"
1412

1513
"github.com/zalando/postgres-operator/pkg/cluster"
1614
"github.com/zalando/postgres-operator/pkg/util"
1715
)
1816

19-
func (c *Controller) nodeListFunc(options metav1.ListOptions) (runtime.Object, error) {
20-
opts := metav1.ListOptions{
21-
Watch: options.Watch,
22-
ResourceVersion: options.ResourceVersion,
23-
TimeoutSeconds: options.TimeoutSeconds,
24-
}
25-
26-
return c.KubeClient.Nodes().List(context.TODO(), opts)
27-
}
28-
29-
func (c *Controller) nodeWatchFunc(options metav1.ListOptions) (watch.Interface, error) {
30-
opts := metav1.ListOptions{
31-
Watch: options.Watch,
32-
ResourceVersion: options.ResourceVersion,
33-
TimeoutSeconds: options.TimeoutSeconds,
34-
}
35-
36-
return c.KubeClient.Nodes().Watch(context.TODO(), opts)
37-
}
38-
3917
func (c *Controller) nodeAdd(obj interface{}) {
4018
node, ok := obj.(*v1.Node)
4119
if !ok {

pkg/controller/pod.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,14 @@
11
package controller
22

33
import (
4-
"context"
5-
64
v1 "k8s.io/api/core/v1"
7-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8-
"k8s.io/apimachinery/pkg/runtime"
9-
"k8s.io/apimachinery/pkg/watch"
105

116
"github.com/zalando/postgres-operator/pkg/cluster"
127
"github.com/zalando/postgres-operator/pkg/spec"
138
"github.com/zalando/postgres-operator/pkg/util"
149
"k8s.io/apimachinery/pkg/types"
1510
)
1611

17-
func (c *Controller) podListFunc(options metav1.ListOptions) (runtime.Object, error) {
18-
opts := metav1.ListOptions{
19-
Watch: options.Watch,
20-
ResourceVersion: options.ResourceVersion,
21-
TimeoutSeconds: options.TimeoutSeconds,
22-
}
23-
24-
return c.KubeClient.Pods(c.opConfig.WatchedNamespace).List(context.TODO(), opts)
25-
}
26-
27-
func (c *Controller) podWatchFunc(options metav1.ListOptions) (watch.Interface, error) {
28-
opts := metav1.ListOptions{
29-
Watch: options.Watch,
30-
ResourceVersion: options.ResourceVersion,
31-
TimeoutSeconds: options.TimeoutSeconds,
32-
}
33-
34-
return c.KubeClient.Pods(c.opConfig.WatchedNamespace).Watch(context.TODO(), opts)
35-
}
36-
3712
func (c *Controller) dispatchPodEvent(clusterName spec.NamespacedName, event cluster.PodEvent) {
3813
c.clustersMu.RLock()
3914
cluster, ok := c.clusters[clusterName]

pkg/util/k8sutil/k8sutil.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type KubernetesClient struct {
6767
zalandov1.FabricEventStreamsGetter
6868

6969
RESTClient rest.Interface
70+
Clientset *kubernetes.Clientset
7071
AcidV1ClientSet *zalandoclient.Clientset
7172
Zalandov1ClientSet *zalandoclient.Clientset
7273
}
@@ -148,6 +149,7 @@ func NewFromConfig(cfg *rest.Config) (KubernetesClient, error) {
148149
return kubeClient, fmt.Errorf("could not get clientset: %v", err)
149150
}
150151

152+
kubeClient.Clientset = client
151153
kubeClient.PodsGetter = client.CoreV1()
152154
kubeClient.ServicesGetter = client.CoreV1()
153155
kubeClient.EndpointsGetter = client.CoreV1()

0 commit comments

Comments
 (0)