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 <[email protected]>
  • Loading branch information
lzhecheng authored and k8s-infra-cherrypick-robot committed Jul 17, 2023
1 parent d811a1c commit 558d60e
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 @@ -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
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 @@ -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"
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 558d60e

Please sign in to comment.