Change:
<!-- Please use this template while reporting a bug and provide as much info as possible. Not doing so may result in your bug not being addressed in a timely manner. Thanks! If the matter is security related, please disclose it privately via --What happened: Azure cloud provider Instances interfaces don't work expected on Windows containers. This is because Windows containers don't support HostNetwork mode, while Azure cloud provider depends on "os.Hostname()" to check whether the VMName from IMDS is same with NodeName: To fix it, the dependency on Hostname should be removed. What you expected to happen: How to reproduce it (as minimally and precisely as possible): Anything else we need to know?: Environment: Kubernetes version (use ): Cloud provider or hardware configuration: OS (e.g: ): Kernel (e.g. ): Install tools: Network plugin and version (if this is a network-related bug): Others: /sig cloud-provider /priority critical-urgent /area provider/azure /milestone v1.18

Discussion:
import ( "context" "fmt" <del> "os" </del> <ins> "strconv" </ins> "strings" v1 "k8s.io/api/core/v1"

} func (az *Cloud) isCurrentInstance(name types.NodeName, metadataVMName string) (bool, error) { <del> var err error </del> nodeName := mapNodeNameToVMName(name) if az.VMType == vmTypeVMSS { <del> // VMSS vmName is not same with hostname, use hostname instead. metadataVMName, err = os.Hostname() if err != nil { return false, err </del> <ins> // VMSS vmName is not same with hostname, construct the node name "{computer-name-prefix}{base-36-instance-id}". // Refer https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-instance-ids#scale-set-vm-computer-name. if ssName, instanceID, err := extractVmssVMName(metadataVMName); err == nil { instance, err := strconv.ParseInt(instanceID, 10, 64) if err != nil { return false, fmt.Errorf("failed to parse VMSS instanceID %q: %v", instanceID, err) } metadataVMName = fmt.Sprintf("%s%06s", ssName, strconv.FormatInt(instance, 36)) </ins> } } metadataVMName = strings.ToLower(metadataVMName) <del> return (metadataVMName == nodeName), err </del> <ins> return (metadataVMName == nodeName), nil </ins> } // InstanceID returns the cloud provider ID of the specified instance.

} } } <ins> func TestIsCurrentInstance(t *testing.T) { cloud := &Cloud{ Config: Config{ VMType: vmTypeVMSS, }, } testcases := []struct { nodeName       string metadataVMName string expected       bool expectError    bool }{ { nodeName:       "node1", metadataVMName: "node1", expected:       true, }, { nodeName:       "node1", metadataVMName: "node2", expected:       false, }, { nodeName:       "vmss000001", metadataVMName: "vmss_1", expected:       true, }, { nodeName:       "vmss_2", metadataVMName: "vmss000000", expected:       false, }, { nodeName:       "vmss123456", metadataVMName: "vmss_$123", expected:       false, expectError:    true, }, } for i, test := range testcases { real, err := cloud.isCurrentInstance(types.NodeName(test.nodeName), test.metadataVMName) if test.expectError { if err == nil { t.Errorf("Test[%d] unexpected nil err", i) } } if real != test.expected { t.Errorf("Test[%d] unexpected isCurrentInstance result %v != %v", i, real, test.expected) } } } </ins>