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

Automated cherry pick of #60959: Set node external IP for azure node when disabling UseInstanceMetadata #61023

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/cloudprovider/providers/azure/azure_backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,19 @@ func (az *Cloud) VirtualMachineClientListWithRetry() ([]compute.VirtualMachine,
}

// GetIPForMachineWithRetry invokes az.getIPForMachine with exponential backoff retry
func (az *Cloud) GetIPForMachineWithRetry(name types.NodeName) (string, error) {
var ip string
func (az *Cloud) GetIPForMachineWithRetry(name types.NodeName) (string, string, error) {
var ip, publicIP string
err := wait.ExponentialBackoff(az.requestBackoff(), func() (bool, error) {
var retryErr error
ip, retryErr = az.getIPForMachine(name)
ip, publicIP, retryErr = az.getIPForMachine(name)
if retryErr != nil {
glog.Errorf("backoff: failure, will retry,err=%v", retryErr)
return false, nil
}
glog.V(2).Infof("backoff: success")
return true, nil
})
return ip, err
return ip, publicIP, err
}

// CreateOrUpdateSGWithRetry invokes az.SecurityGroupsClient.CreateOrUpdate with exponential backoff retry
Expand Down
42 changes: 32 additions & 10 deletions pkg/cloudprovider/providers/azure/azure_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,39 @@ import (

// NodeAddresses returns the addresses of the specified instance.
func (az *Cloud) NodeAddresses(name types.NodeName) ([]v1.NodeAddress, error) {
addressGetter := func(nodeName types.NodeName) ([]v1.NodeAddress, error) {
ip, publicIP, err := az.GetIPForMachineWithRetry(nodeName)
if err != nil {
glog.V(2).Infof("NodeAddresses(%s) abort backoff", nodeName)
return nil, err
}

addresses := []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: ip},
{Type: v1.NodeHostName, Address: string(name)},
}
if len(publicIP) > 0 {
addresses = append(addresses, v1.NodeAddress{
Type: v1.NodeExternalIP,
Address: publicIP,
})
}
return addresses, nil
}

if az.UseInstanceMetadata {
isLocalInstance, err := az.isCurrentInstance(name)
if err != nil {
return nil, err
}

// Not local instance, get addresses from Azure ARM API.
if !isLocalInstance {
return addressGetter(name)
}

ipAddress := IPAddress{}
err := az.metadata.Object("instance/network/interface/0/ipv4/ipAddress/0", &ipAddress)
err = az.metadata.Object("instance/network/interface/0/ipv4/ipAddress/0", &ipAddress)
if err != nil {
return nil, err
}
Expand All @@ -48,16 +78,8 @@ func (az *Cloud) NodeAddresses(name types.NodeName) ([]v1.NodeAddress, error) {
}
return addresses, nil
}
ip, err := az.GetIPForMachineWithRetry(name)
if err != nil {
glog.V(2).Infof("NodeAddresses(%s) abort backoff", name)
return nil, err
}

return []v1.NodeAddress{
{Type: v1.NodeInternalIP, Address: ip},
{Type: v1.NodeHostName, Address: string(name)},
}, nil
return addressGetter(name)
}

// NodeAddressesByProviderID returns the node addresses of an instances with the specified unique providerID
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudprovider/providers/azure/azure_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (az *Cloud) CreateRoute(clusterName string, nameHint string, kubeRoute *clo
}
}

targetIP, err := az.getIPForMachine(kubeRoute.TargetNode)
targetIP, _, err := az.getIPForMachine(kubeRoute.TargetNode)
if err != nil {
return err
}
Expand Down
38 changes: 27 additions & 11 deletions pkg/cloudprovider/providers/azure/azure_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,37 +404,37 @@ outer:
return -1, fmt.Errorf("SecurityGroup priorities are exhausted")
}

func (az *Cloud) getIPForMachine(nodeName types.NodeName) (string, error) {
func (az *Cloud) getIPForMachine(nodeName types.NodeName) (string, string, error) {
if az.Config.VMType == vmTypeVMSS {
ip, err := az.getIPForVmssMachine(nodeName)
ip, publicIP, err := az.getIPForVmssMachine(nodeName)
if err == cloudprovider.InstanceNotFound || err == ErrorNotVmssInstance {
return az.getIPForStandardMachine(nodeName)
}

return ip, err
return ip, publicIP, err
}

return az.getIPForStandardMachine(nodeName)
}

func (az *Cloud) getIPForStandardMachine(nodeName types.NodeName) (string, error) {
func (az *Cloud) getIPForStandardMachine(nodeName types.NodeName) (string, string, error) {
az.operationPollRateLimiter.Accept()
machine, err := az.getVirtualMachine(nodeName)
if err != nil {
glog.Errorf("error: az.getIPForMachine(%s), az.getVirtualMachine(%s), err=%v", nodeName, nodeName, err)
return "", err
return "", "", err
}

nicID, err := getPrimaryInterfaceID(machine)
if err != nil {
glog.Errorf("error: az.getIPForMachine(%s), getPrimaryInterfaceID(%v), err=%v", nodeName, machine, err)
return "", err
return "", "", err
}

nicName, err := getLastSegment(nicID)
if err != nil {
glog.Errorf("error: az.getIPForMachine(%s), getLastSegment(%s), err=%v", nodeName, nicID, err)
return "", err
return "", "", err
}

az.operationPollRateLimiter.Accept()
Expand All @@ -443,17 +443,33 @@ func (az *Cloud) getIPForStandardMachine(nodeName types.NodeName) (string, error
glog.V(10).Infof("InterfacesClient.Get(%q): end", nicName)
if err != nil {
glog.Errorf("error: az.getIPForMachine(%s), az.InterfacesClient.Get(%s, %s, %s), err=%v", nodeName, az.ResourceGroup, nicName, "", err)
return "", err
return "", "", err
}

ipConfig, err := getPrimaryIPConfig(nic)
if err != nil {
glog.Errorf("error: az.getIPForMachine(%s), getPrimaryIPConfig(%v), err=%v", nodeName, nic, err)
return "", err
return "", "", err
}

targetIP := *ipConfig.PrivateIPAddress
return targetIP, nil
privateIP := *ipConfig.PrivateIPAddress
publicIP := ""
if ipConfig.PublicIPAddress != nil && ipConfig.PublicIPAddress.ID != nil {
pipID := *ipConfig.PublicIPAddress.ID
pipName, err := getLastSegment(pipID)
if err != nil {
return "", "", fmt.Errorf("failed to get publicIP name for node %q with pipID %q", nodeName, pipID)
}
pip, existsPip, err := az.getPublicIPAddress(pipName)
if err != nil {
return "", "", err
}
if existsPip {
publicIP = *pip.IPAddress
}
}

return privateIP, publicIP, nil
}

// splitProviderID converts a providerID to a NodeName.
Expand Down
19 changes: 11 additions & 8 deletions pkg/cloudprovider/providers/azure/azure_util_vmss.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,30 @@ import (
"k8s.io/kubernetes/pkg/cloudprovider"
)

func (az *Cloud) getIPForVmssMachine(nodeName types.NodeName) (string, error) {
// getIPForVmssMachine gets machine private IP and public IP by node name.
// TODO(feiskyer): Azure vmss doesn't support associating a public IP to single virtual machine yet,
// fix this after it is supported.
func (az *Cloud) getIPForVmssMachine(nodeName types.NodeName) (string, string, error) {
az.operationPollRateLimiter.Accept()
machine, exists, err := az.getVmssVirtualMachine(nodeName)
if !exists {
return "", cloudprovider.InstanceNotFound
return "", "", cloudprovider.InstanceNotFound
}
if err != nil {
glog.Errorf("error: az.getIPForVmssMachine(%s), az.getVmssVirtualMachine(%s), err=%v", nodeName, nodeName, err)
return "", err
return "", "", err
}

nicID, err := getPrimaryInterfaceIDForVmssMachine(machine)
if err != nil {
glog.Errorf("error: az.getIPForVmssMachine(%s), getPrimaryInterfaceID(%v), err=%v", nodeName, machine, err)
return "", err
return "", "", err
}

nicName, err := getLastSegment(nicID)
if err != nil {
glog.Errorf("error: az.getIPForVmssMachine(%s), getLastSegment(%s), err=%v", nodeName, nicID, err)
return "", err
return "", "", err
}

az.operationPollRateLimiter.Accept()
Expand All @@ -56,17 +59,17 @@ func (az *Cloud) getIPForVmssMachine(nodeName types.NodeName) (string, error) {
glog.V(10).Infof("InterfacesClient.Get(%q): end", nicName)
if err != nil {
glog.Errorf("error: az.getIPForVmssMachine(%s), az.GetVirtualMachineScaleSetNetworkInterface.Get(%s, %s, %s), err=%v", nodeName, az.ResourceGroup, nicName, "", err)
return "", err
return "", "", err
}

ipConfig, err := getPrimaryIPConfig(nic)
if err != nil {
glog.Errorf("error: az.getIPForVmssMachine(%s), getPrimaryIPConfig(%v), err=%v", nodeName, nic, err)
return "", err
return "", "", err
}

targetIP := *ipConfig.PrivateIPAddress
return targetIP, nil
return targetIP, "", nil
}

// This returns the full identifier of the primary NIC for the given VM.
Expand Down