Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复aks扩容实例ip获取问题 #3501

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ func handlerIPConfig(config *armnetwork.InterfaceIPConfiguration) (string, bool)
// (https://learn.microsoft.com/zh-cn/azure/virtual-network
// /virtual-networks-faq#what-address-ranges-can-i-use-in-my-vnets)
ip := net.ParseIP(*config.Properties.PrivateIPAddress)
if !(ip != nil && ip.IsPrivate()) { // 取反
if ip == nil {
return "", false
}
return ip.String(), true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ func differentInstance(rootCtx context.Context, client api.AksService, info *clo
for i, vm := range vmList {
nodeID := fmt.Sprintf("%s/%s/%s", *vm.Name, *vm.InstanceID, asg.AutoScalingName)
if _, ok := nodeMap[nodeID]; !ok {
blog.Infof("differentInstance[%s] got new instance %s", taskID, nodeID)
// 如果当前vm不存在于nodeMap中,则为扩容出来的机器
res = append(res, vmList[i])
}
Expand Down Expand Up @@ -420,24 +421,14 @@ func getNodeMap(ctx context.Context, taskID string, info *cloudprovider.CloudDep
func transInstancesToNode(rootCtx context.Context, info *cloudprovider.CloudDependBasicInfo, client api.AksService,
vmList []*armcompute.VirtualMachineScaleSetVM) ([]string, error) {
var (
err error
nodeIPs = make([]string, 0)
nodes []*proto.Node
asg = info.NodeGroup.AutoScaling
interfaceList = make([]*armnetwork.Interface, 0)
taskID = cloudprovider.GetTaskIDFromContext(rootCtx)
ctx, cancel = context.WithTimeout(rootCtx, 30*time.Second)
err error
nodeIPs = make([]string, 0)
nodes []*proto.Node
taskID = cloudprovider.GetTaskIDFromContext(rootCtx)
)
defer cancel()

// 获取 interface list
err = retry.Do(func() error {
interfaceList, err = client.ListSetInterfaceAndReturn(ctx, asg.AutoScalingName, asg.AutoScalingID)
if err != nil {
return errors.Wrapf(err, "transInstancesToNode[%s] ListSetInterfaceAndReturn failed", taskID)
}
return nil
}, retry.Context(ctx), retry.Attempts(3))
// ensure instance ip is attached
interfaceList, err := checkInstance(client, info, vmList)
if err != nil {
return nil, errors.Wrapf(err, "transInstancesToNode[%s] get vm network interface failed", taskID)
}
Expand All @@ -462,6 +453,42 @@ func transInstancesToNode(rootCtx context.Context, info *cloudprovider.CloudDepe
return nodeIPs, nil
}

func checkInstance(client api.AksService, info *cloudprovider.CloudDependBasicInfo,
vmList []*armcompute.VirtualMachineScaleSetVM) ([]*armnetwork.Interface, error) {
var (
asg = info.NodeGroup.AutoScaling
interfaceList = make([]*armnetwork.Interface, 0)
err error
)

timeCtx, cancel := context.WithTimeout(context.TODO(), 5*time.Minute)
defer cancel()
errLoop := loop.LoopDoFunc(timeCtx, func() error {
var success int
interfaceList, err = client.ListSetInterfaceAndReturn(context.TODO(), asg.AutoScalingName, asg.AutoScalingID)
if err != nil {
return err
}
ipMap := api.VmMatchInterface(vmList, interfaceList)

for _, vm := range vmList { // 字段对齐
if ip, ok := ipMap[*vm.Name]; ok && len(ip) != 0 {
success++
}
}
blog.Infof("checkInstance ip count, current %d, desired %d", success, len(vmList))
if success == len(vmList) {
return loop.EndLoop
}
return nil
})
if errLoop != nil {
return nil, errLoop
}

return interfaceList, nil
}

// vmToNode vm to node
func vmToNode(client api.AksService, info *cloudprovider.CloudDependBasicInfo,
vmList []*armcompute.VirtualMachineScaleSetVM, interfaceList []*armnetwork.Interface) ([]*proto.Node, error) { // nolint
Expand Down
Loading