From e53c189587f8fe0f2aebb243e213ea03b5e38e76 Mon Sep 17 00:00:00 2001 From: Vitor Savian Date: Thu, 16 Nov 2023 20:58:42 -0300 Subject: [PATCH] Handle nil pointer when runtime core is not ready in etcd Signed-off-by: Vitor --- pkg/etcd/etcd.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/pkg/etcd/etcd.go b/pkg/etcd/etcd.go index dc4a476d73..c92e9e3ff1 100644 --- a/pkg/etcd/etcd.go +++ b/pkg/etcd/etcd.go @@ -1056,39 +1056,36 @@ func (e *ETCD) manageLearners(ctx context.Context) { continue } - nodes, err := e.getETCDNodes() - if err != nil { - logrus.Errorf("Error while listing nodes with etcd status: %v", err) - continue - } - client, err := util.GetClientSet(e.config.Runtime.KubeConfigSupervisor) if err != nil { logrus.Errorf("Failed to get k8s client for patch node status condition: %v", err) continue } + nodes, err := e.getETCDNodes() + if err != nil { + logrus.Warnf("Failed to list nodes with etcd role: %v", err) + } + for _, member := range members.Members { if member.IsLearner { if err := e.trackLearnerProgress(ctx, progress, member); err != nil { logrus.Errorf("Failed to track learner progress towards promotion: %v", err) } - - for _, node := range nodes.Items { + for _, node := range nodes { if strings.HasPrefix(member.Name, node.Name+"-") { - _, _, err := e.setEtcdStatusCondition(&node, client.CoreV1(), member.Name, false) + _, _, err := e.setEtcdStatusCondition(node, client.CoreV1(), member.Name, false) if err != nil { logrus.Errorf("Unable to set etcd status condition %s: %v", member.Name, err) } } } - break } - for _, node := range nodes.Items { + for _, node := range nodes { if strings.HasPrefix(member.Name, node.Name+"-") { - _, _, err := e.setEtcdStatusCondition(&node, client.CoreV1(), member.Name, true) + _, _, err := e.setEtcdStatusCondition(node, client.CoreV1(), member.Name, true) if err != nil { logrus.Errorf("Unable to set etcd status condition %s: %v", member.Name, err) } @@ -1098,11 +1095,15 @@ func (e *ETCD) manageLearners(ctx context.Context) { } } -func (e *ETCD) getETCDNodes() (*v1.NodeList, error) { +func (e *ETCD) getETCDNodes() ([]*v1.Node, error) { + if e.config.Runtime.Core == nil { + return nil, errors.New("runtime core not ready") + } + nodes := e.config.Runtime.Core.Core().V1().Node() etcdSelector := labels.Set{util.ETCDRoleLabelKey: "true"} - return nodes.List(metav1.ListOptions{LabelSelector: etcdSelector.String()}) + return nodes.Cache().List(etcdSelector.AsSelector()) } // trackLearnerProcess attempts to promote a learner. If it cannot be promoted, progress through the raft index is tracked.