diff --git a/pkg/provider/azure_instances.go b/pkg/provider/azure_instances.go index 6a95eee054..7d47fbe5d5 100644 --- a/pkg/provider/azure_instances.go +++ b/pkg/provider/azure_instances.go @@ -227,6 +227,15 @@ func (az *Cloud) InstanceExists(ctx context.Context, node *v1.Node) (bool, error if node == nil { return false, nil } + unmanaged, err := az.IsNodeUnmanaged(node.Name) + if err != nil { + return false, err + } + if unmanaged { + klog.V(4).Infof("InstanceExists: omitting unmanaged node %q", node.Name) + return false, nil + } + providerID := node.Spec.ProviderID if providerID == "" { var err error @@ -297,6 +306,14 @@ func (az *Cloud) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, err if node == nil { return false, nil } + unmanaged, err := az.IsNodeUnmanaged(node.Name) + if err != nil { + return false, err + } + if unmanaged { + klog.V(4).Infof("InstanceShutdown: omitting unmanaged node %q", node.Name) + return false, nil + } providerID := node.Spec.ProviderID if providerID == "" { var err error @@ -492,11 +509,18 @@ func (az *Cloud) CurrentNodeName(ctx context.Context, hostname string) (types.No // translated into specific fields in the Node object on registration. // Use the node.name or node.spec.providerID field to find the node in the cloud provider. func (az *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) { + meta := cloudprovider.InstanceMetadata{} if node == nil { - return &cloudprovider.InstanceMetadata{}, nil + return &meta, nil + } + unmanaged, err := az.IsNodeUnmanaged(node.Name) + if err != nil { + return &meta, err + } + if unmanaged { + klog.V(4).Infof("InstanceMetadata: omitting unmanaged node %q", node.Name) + return &meta, nil } - - meta := cloudprovider.InstanceMetadata{} if node.Spec.ProviderID != "" { meta.ProviderID = node.Spec.ProviderID diff --git a/pkg/provider/azure_instances_test.go b/pkg/provider/azure_instances_test.go index 89955bbf91..7719f7698a 100644 --- a/pkg/provider/azure_instances_test.go +++ b/pkg/provider/azure_instances_test.go @@ -24,6 +24,7 @@ import ( "strings" "testing" + "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/wait" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2022-03-01/compute" @@ -880,7 +881,20 @@ func TestInstanceMetadata(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - t.Run("", func(t *testing.T) { + t.Run("instance not exists", func(t *testing.T) { + cloud := GetTestCloud(ctrl) + cloud.unmanagedNodes = sets.New("node0") + + meta, err := cloud.InstanceMetadata(context.Background(), &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node0", + }, + }) + assert.NoError(t, err) + assert.Equal(t, cloudprovider.InstanceMetadata{}, *meta) + }) + + t.Run("instance exists", func(t *testing.T) { cloud := GetTestCloud(ctrl) expectedVM := buildDefaultTestVirtualMachine("as", []string{"/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Network/networkInterfaces/k8s-agentpool1-00000000-nic-1"}) expectedVM.HardwareProfile = &compute.HardwareProfile{