From 3d235de2e84b2ff09c79220605f0956110e604b8 Mon Sep 17 00:00:00 2001 From: Thomas Jungblut Date: Fri, 15 Mar 2024 17:50:44 +0100 Subject: [PATCH] NO-JIRA: move etcdcli to master node listing --- pkg/etcdcli/etcdcli.go | 37 +++++++++++++++++++++++-------------- pkg/etcdcli/etcdcli_test.go | 5 +++++ pkg/operator/starter.go | 6 +++++- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/pkg/etcdcli/etcdcli.go b/pkg/etcdcli/etcdcli.go index 762f54813..6a7bee403 100644 --- a/pkg/etcdcli/etcdcli.go +++ b/pkg/etcdcli/etcdcli.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io/ioutil" + v1 "k8s.io/client-go/informers/core/v1" "net" "net/url" "os" @@ -15,7 +16,6 @@ import ( configv1informers "github.com/openshift/client-go/config/informers/externalversions/config/v1" configv1listers "github.com/openshift/client-go/config/listers/config/v1" "github.com/openshift/library-go/pkg/operator/events" - "github.com/openshift/library-go/pkg/operator/v1helpers" "go.etcd.io/etcd/api/v3/etcdserverpb" "go.etcd.io/etcd/client/pkg/v3/logutil" "go.etcd.io/etcd/client/pkg/v3/transport" @@ -43,9 +43,10 @@ const ( ) type etcdClientGetter struct { - nodeLister corev1listers.NodeLister - configmapsLister corev1listers.ConfigMapLister - networkLister configv1listers.NetworkLister + masterNodeLister corev1listers.NodeLister + masterNodeLabelSelector labels.Selector + configmapsLister corev1listers.ConfigMapLister + networkLister configv1listers.NetworkLister nodeListerSynced cache.InformerSynced configmapsListerSynced cache.InformerSynced @@ -56,19 +57,26 @@ type etcdClientGetter struct { clientPool *EtcdClientPool } -func NewEtcdClient(kubeInformers v1helpers.KubeInformersForNamespaces, networkInformer configv1informers.NetworkInformer, eventRecorder events.Recorder) EtcdClient { +func NewEtcdClient( + masterNodeInformer cache.SharedIndexInformer, + masterNodeLister corev1listers.NodeLister, + masterNodeLabelSelector labels.Selector, + configMapInformer v1.ConfigMapInformer, + networkInformer configv1informers.NetworkInformer, + eventRecorder events.Recorder) EtcdClient { g := &etcdClientGetter{ - nodeLister: kubeInformers.InformersFor("").Core().V1().Nodes().Lister(), - configmapsLister: kubeInformers.InformersFor(operatorclient.TargetNamespace).Core().V1().ConfigMaps().Lister(), - networkLister: networkInformer.Lister(), - nodeListerSynced: kubeInformers.InformersFor("").Core().V1().Nodes().Informer().HasSynced, - configmapsListerSynced: kubeInformers.InformersFor(operatorclient.TargetNamespace).Core().V1().ConfigMaps().Informer().HasSynced, - networkListerSynced: networkInformer.Informer().HasSynced, - eventRecorder: eventRecorder.WithComponentSuffix("etcd-client"), + masterNodeLister: masterNodeLister, + masterNodeLabelSelector: masterNodeLabelSelector, + configmapsLister: configMapInformer.Lister(), + networkLister: networkInformer.Lister(), + nodeListerSynced: masterNodeInformer.HasSynced, + configmapsListerSynced: configMapInformer.Informer().HasSynced, + networkListerSynced: networkInformer.Informer().HasSynced, + eventRecorder: eventRecorder.WithComponentSuffix("etcd-client"), } endpointFunc := func() ([]string, error) { - return endpoints(g.nodeLister, g.configmapsLister, g.networkLister, + return endpoints(g.masterNodeLister, g.masterNodeLabelSelector, g.configmapsLister, g.networkLister, g.nodeListerSynced, g.configmapsListerSynced, g.networkListerSynced) } newFunc := func() (*clientv3.Client, error) { @@ -473,6 +481,7 @@ func (g *etcdClientGetter) Defragment(ctx context.Context, member *etcdserverpb. } func endpoints(nodeLister corev1listers.NodeLister, + masterNodeLabelSelector labels.Selector, configmapsLister corev1listers.ConfigMapLister, networkLister configv1listers.NetworkLister, nodeListerSynced cache.InformerSynced, @@ -501,7 +510,7 @@ func endpoints(nodeLister corev1listers.NodeLister, return nil, fmt.Errorf("failed to list cluster network: %w", err) } - nodes, err := nodeLister.List(labels.Set{"node-role.kubernetes.io/master": ""}.AsSelector()) + nodes, err := nodeLister.List(masterNodeLabelSelector) if err != nil { return nil, fmt.Errorf("failed to list control plane nodes: %w", err) } diff --git a/pkg/etcdcli/etcdcli_test.go b/pkg/etcdcli/etcdcli_test.go index 4b7a33ab4..fcd32d543 100644 --- a/pkg/etcdcli/etcdcli_test.go +++ b/pkg/etcdcli/etcdcli_test.go @@ -3,6 +3,7 @@ package etcdcli import ( "fmt" "go.etcd.io/etcd/api/v3/etcdserverpb" + "k8s.io/apimachinery/pkg/labels" "testing" configv1listers "github.com/openshift/client-go/config/listers/config/v1" @@ -95,8 +96,12 @@ func TestEndpointFunc(t *testing.T) { } } + nodeSelector, err := labels.Parse("node-role.kubernetes.io/master") + require.NoError(t, err) + endpoints, err := endpoints( corev1listers.NewNodeLister(indexer), + nodeSelector, corev1listers.NewConfigMapLister(indexer), configv1listers.NewNetworkLister(indexer), syncTrue, syncTrue, syncTrue, diff --git a/pkg/operator/starter.go b/pkg/operator/starter.go index 159d1179a..aad8b928d 100644 --- a/pkg/operator/starter.go +++ b/pkg/operator/starter.go @@ -152,6 +152,7 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle clusterVersions := configInformers.Config().V1().ClusterVersions() networkInformer := configInformers.Config().V1().Networks() jobsInformer := kubeInformersForNamespaces.InformersFor(operatorclient.TargetNamespace).Batch().V1().Jobs().Informer() + configMapInformer := kubeInformersForNamespaces.InformersFor(operatorclient.TargetNamespace).Core().V1().ConfigMaps() versionRecorder := status.NewVersionGetter() clusterOperator, err := configClient.ConfigV1().ClusterOperators().Get(ctx, "etcd", metav1.GetOptions{}) @@ -180,7 +181,10 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle } etcdClient := etcdcli.NewEtcdClient( - kubeInformersForNamespaces, + masterNodeInformer, + masterNodeLister, + masterNodeLabelSelector, + configMapInformer, networkInformer, controllerContext.EventRecorder)