Skip to content

Commit

Permalink
fix: 修复aks扩容实例ip获取问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Maclon9573 committed Sep 9, 2024
1 parent 9e6cbb0 commit 4c4790d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
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

0 comments on commit 4c4790d

Please sign in to comment.