Skip to content

Commit

Permalink
Skip unmanaged Nodes for instancesV2
Browse files Browse the repository at this point in the history
Signed-off-by: Zhecheng Li <zhechengli@microsoft.com>
  • Loading branch information
lzhecheng committed Jul 19, 2023
1 parent 0bd7a84 commit a0f9e5b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
30 changes: 27 additions & 3 deletions pkg/provider/azure_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -498,11 +515,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
Expand Down
16 changes: 15 additions & 1 deletion pkg/provider/azure_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/sets"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -874,7 +875,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.NewString("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{
Expand Down

0 comments on commit a0f9e5b

Please sign in to comment.