From ca95a8e75148bace0a6b5543f435a329d03935bc Mon Sep 17 00:00:00 2001 From: gujing <925973396@qq.com> Date: Fri, 10 Jun 2022 10:11:38 +0800 Subject: [PATCH] fix no ip bug; add event if not find ecs according to ip --- pkg/controller/node/manager.go | 2 +- pkg/controller/node/node_controller.go | 53 +++++++++++++------------- pkg/provider/alibaba/ecs/ecs.go | 9 ++--- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/pkg/controller/node/manager.go b/pkg/controller/node/manager.go index 288e2f39e..e802d37d2 100644 --- a/pkg/controller/node/manager.go +++ b/pkg/controller/node/manager.go @@ -179,7 +179,7 @@ func findCloudECSByIp(ins prvd.IInstance, node *v1.Node) (*prvd.NodeAttribute, e klog.Infof("try to find ecs for node %s by internal ip %v", node.Name, internalIP) cloudIns, err := ins.GetInstancesByIP(context.TODO(), internalIP) if err != nil { - return nil, fmt.Errorf("list instances by ip %s fail: %s", node.Name, err.Error()) + return nil, fmt.Errorf("list instances by ip fail: %s", err.Error()) } if cloudIns == nil { return nil, ErrNotFound diff --git a/pkg/controller/node/node_controller.go b/pkg/controller/node/node_controller.go index ad54eed94..54fb055c9 100644 --- a/pkg/controller/node/node_controller.go +++ b/pkg/controller/node/node_controller.go @@ -126,12 +126,36 @@ func (m *ReconcileNode) syncCloudNode(node *corev1.Node) error { klog.V(5).Infof("node %s is registered without cloud taint. return ok", node.Name) return nil } - return m.doAddCloudNode(node) + + start := time.Now() + defer func() { + metric.NodeLatency.WithLabelValues("remove_taint").Observe(metric.MsSince(start)) + }() + + nodeRef := &corev1.ObjectReference{ + Kind: "Node", + Name: node.Name, + UID: types.UID(node.Name), + Namespace: "", + } + + err := m.doAddCloudNode(node) + if err != nil { + m.record.Event( + nodeRef, + corev1.EventTypeWarning, + helper.FailedAddNode, + fmt.Sprintf("Error adding node: %s", helper.GetLogMessage(err)), + ) + return fmt.Errorf("doAddCloudNode %s error: %s", node.Name, err.Error()) + } + m.record.Event(nodeRef, corev1.EventTypeNormal, helper.InitializedNode, "Initialize node successfully") + log.Info("Successfully initialized node", "node", node.Name) + return nil } // This processes nodes that were added into the cluster, and cloud initialize them if appropriate func (m *ReconcileNode) doAddCloudNode(node *corev1.Node) error { - start := time.Now() instance, err := findCloudECS(m.cloud, node) if err != nil { if err == ErrNotFound { @@ -180,30 +204,7 @@ func (m *ReconcileNode) doAddCloudNode(node *corev1.Node) error { _ = m.syncNode([]corev1.Node{*node}) return true, nil } - - nodeRef := &corev1.ObjectReference{ - Kind: "Node", - Name: node.Name, - UID: types.UID(node.Name), - Namespace: "", - } - - err = wait.PollImmediate(2*time.Second, 20*time.Second, initializer) - if err != nil { - m.record.Event( - nodeRef, - corev1.EventTypeWarning, - helper.FailedAddNode, - fmt.Sprintf("Error adding node: %s", helper.GetLogMessage(err)), - ) - return fmt.Errorf("doAddCloudNode %s error: %s", node.Name, err.Error()) - } - - m.record.Event(nodeRef, corev1.EventTypeNormal, helper.InitializedNode, "Initialize node successfully") - metric.NodeLatency.WithLabelValues("remove_taint").Observe(metric.MsSince(start)) - log.Info("Successfully initialized node", "node", node.Name) - - return nil + return wait.PollImmediate(5*time.Second, 20*time.Second, initializer) } // syncNode sync the nodeAddress & cloud node existence diff --git a/pkg/provider/alibaba/ecs/ecs.go b/pkg/provider/alibaba/ecs/ecs.go index 0783075cc..e03b7e020 100644 --- a/pkg/provider/alibaba/ecs/ecs.go +++ b/pkg/provider/alibaba/ecs/ecs.go @@ -95,12 +95,9 @@ func (e *ECSProvider) GetInstancesByIP(ctx context.Context, ips []string) (*prvd return nil, fmt.Errorf("describe instances by ip %s error: %s", ips, err.Error()) } - if len(resp.Instances.Instance) == 0 { - return nil, nil - } - - if len(resp.Instances.Instance) > 1 { - return nil, fmt.Errorf("find multiple instances by ip %s", ips) + if len(resp.Instances.Instance) != 1 { + klog.V(5).Infof("RequestId: %s, API: %s, ips: %s", resp.RequestId, "DescribeInstances", req.PrivateIpAddresses) + return nil, fmt.Errorf("find none or multiple instances by ip %s", ips) } ins := resp.Instances.Instance[0]