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

Fix nodes power state on Azure #68921

Merged
merged 2 commits into from
Sep 21, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cloudprovider/providers/azure/azure_fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,6 @@ func (f *fakeVMSet) GetDataDisks(nodeName types.NodeName) ([]compute.DataDisk, e
return nil, fmt.Errorf("unimplemented")
}

func (f *fakeVMSet) GetProvisioningStateByNodeName(name string) (string, error) {
func (f *fakeVMSet) GetPowerStatusByNodeName(name string) (string, error) {
return "", fmt.Errorf("unimplemented")
}
11 changes: 9 additions & 2 deletions pkg/cloudprovider/providers/azure/azure_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ import (
"k8s.io/apimachinery/pkg/types"
)

const (
vmPowerStatePrefix = "PowerState/"
vmPowerStateStopped = "stopped"
vmPowerStateDeallocated = "deallocated"
)

// NodeAddresses returns the addresses of the specified instance.
func (az *Cloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.NodeAddress, error) {
// Returns nil for unmanaged nodes because azure cloud provider couldn't fetch information for them.
Expand Down Expand Up @@ -148,12 +154,13 @@ func (az *Cloud) InstanceShutdownByProviderID(ctx context.Context, providerID st
return false, err
}

provisioningState, err := az.vmSet.GetProvisioningStateByNodeName(string(nodeName))
powerStatus, err := az.vmSet.GetPowerStatusByNodeName(string(nodeName))
if err != nil {
return false, err
}
glog.V(5).Infof("InstanceShutdownByProviderID gets power status %q for node %q", powerStatus, nodeName)

return strings.ToLower(provisioningState) == "stopped" || strings.ToLower(provisioningState) == "deallocated", nil
return strings.ToLower(powerStatus) == vmPowerStateStopped || strings.ToLower(powerStatus) == vmPowerStateDeallocated, nil
}

// getComputeMetadata gets compute information from instance metadata.
Expand Down
117 changes: 109 additions & 8 deletions pkg/cloudprovider/providers/azure/azure_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,41 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
"github.com/Azure/go-autorest/autorest/to"
"k8s.io/apimachinery/pkg/types"
)

func setTestVirtualMachines(c *Cloud, vmList []string) {
// setTestVirtualMachines sets test virtual machine with powerstate.
func setTestVirtualMachines(c *Cloud, vmList map[string]string) {
virtualMachineClient := c.VirtualMachinesClient.(*fakeAzureVirtualMachinesClient)
store := map[string]map[string]compute.VirtualMachine{
"rg": make(map[string]compute.VirtualMachine),
}

for i := range vmList {
nodeName := vmList[i]
instanceID := fmt.Sprintf("/subscriptions/script/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/%s", nodeName)
store["rg"][nodeName] = compute.VirtualMachine{
for nodeName, powerState := range vmList {
instanceID := fmt.Sprintf("/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/%s", nodeName)
vm := compute.VirtualMachine{
Name: &nodeName,
ID: &instanceID,
Location: &c.Location,
}
if powerState != "" {
status := []compute.InstanceViewStatus{
{
Code: to.StringPtr(powerState),
},
{
Code: to.StringPtr("ProvisioningState/succeeded"),
},
}
vm.VirtualMachineProperties = &compute.VirtualMachineProperties{
InstanceView: &compute.VirtualMachineInstanceView{
Statuses: &status,
},
}
}

store["rg"][nodeName] = vm
}

virtualMachineClient.setFakeStore(store)
Expand All @@ -63,14 +81,14 @@ func TestInstanceID(t *testing.T) {
vmList: []string{"vm1"},
nodeName: "vm1",
metadataName: "vm1",
expected: "/subscriptions/script/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm1",
expected: "/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm1",
},
{
name: "InstanceID should get instanceID from Azure API if node is not local instance",
vmList: []string{"vm2"},
nodeName: "vm2",
metadataName: "vm1",
expected: "/subscriptions/script/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm2",
expected: "/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm2",
},
{
name: "InstanceID should report error if VM doesn't exist",
Expand All @@ -96,7 +114,11 @@ func TestInstanceID(t *testing.T) {
defer listener.Close()

cloud.metadata.baseURL = "http://" + listener.Addr().String() + "/"
setTestVirtualMachines(cloud, test.vmList)
vmListWithPowerState := make(map[string]string)
for _, vm := range test.vmList {
vmListWithPowerState[vm] = ""
}
setTestVirtualMachines(cloud, vmListWithPowerState)
instanceID, err := cloud.InstanceID(context.Background(), types.NodeName(test.nodeName))
if test.expectError {
if err == nil {
Expand All @@ -113,3 +135,82 @@ func TestInstanceID(t *testing.T) {
}
}
}

func TestInstanceShutdownByProviderID(t *testing.T) {
testcases := []struct {
name string
vmList map[string]string
nodeName string
expected bool
expectError bool
}{
{
name: "InstanceShutdownByProviderID should return false if the vm is in PowerState/Running status",
vmList: map[string]string{"vm1": "PowerState/Running"},
nodeName: "vm1",
expected: false,
},
{
name: "InstanceShutdownByProviderID should return true if the vm is in PowerState/Deallocated status",
vmList: map[string]string{"vm2": "PowerState/Deallocated"},
nodeName: "vm2",
expected: true,
},
{
name: "InstanceShutdownByProviderID should return false if the vm is in PowerState/Deallocating status",
vmList: map[string]string{"vm3": "PowerState/Deallocating"},
nodeName: "vm3",
expected: false,
},
{
name: "InstanceShutdownByProviderID should return false if the vm is in PowerState/Starting status",
vmList: map[string]string{"vm4": "PowerState/Starting"},
nodeName: "vm4",
expected: false,
},
{
name: "InstanceShutdownByProviderID should return true if the vm is in PowerState/Stopped status",
vmList: map[string]string{"vm5": "PowerState/Stopped"},
nodeName: "vm5",
expected: true,
},
{
name: "InstanceShutdownByProviderID should return false if the vm is in PowerState/Stopping status",
vmList: map[string]string{"vm6": "PowerState/Stopping"},
nodeName: "vm6",
expected: false,
},
{
name: "InstanceShutdownByProviderID should return false if the vm is in PowerState/Unknown status",
vmList: map[string]string{"vm7": "PowerState/Unknown"},
nodeName: "vm7",
expected: false,
},
{
name: "InstanceShutdownByProviderID should report error if VM doesn't exist",
vmList: map[string]string{"vm1": "PowerState/running"},
nodeName: "vm8",
expectError: true,
},
}

for _, test := range testcases {
cloud := getTestCloud()
setTestVirtualMachines(cloud, test.vmList)
providerID := "azure://" + cloud.getStandardMachineID("rg", test.nodeName)
hasShutdown, err := cloud.InstanceShutdownByProviderID(context.Background(), providerID)
if test.expectError {
if err == nil {
t.Errorf("Test [%s] unexpected nil err", test.name)
}
} else {
if err != nil {
t.Errorf("Test [%s] unexpected error: %v", test.name, err)
}
}

if hasShutdown != test.expected {
t.Errorf("Test [%s] unexpected hasShutdown: %v, expected %v", test.name, hasShutdown, test.expected)
}
}
}
17 changes: 14 additions & 3 deletions pkg/cloudprovider/providers/azure/azure_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,24 @@ func (as *availabilitySet) GetInstanceIDByNodeName(name string) (string, error)
return *machine.ID, nil
}

func (as *availabilitySet) GetProvisioningStateByNodeName(name string) (provisioningState string, err error) {
// GetPowerStatusByNodeName returns the power state of the specified node.
func (as *availabilitySet) GetPowerStatusByNodeName(name string) (powerState string, err error) {
vm, err := as.getVirtualMachine(types.NodeName(name))
if err != nil {
return provisioningState, err
return powerState, err
}

return *vm.ProvisioningState, nil
if vm.InstanceView != nil && vm.InstanceView.Statuses != nil {
statuses := *vm.InstanceView.Statuses
for _, status := range statuses {
state := to.String(status.Code)
if strings.HasPrefix(state, vmPowerStatePrefix) {
return strings.TrimPrefix(state, vmPowerStatePrefix), nil
}
}
}

return "", fmt.Errorf("failed to get power status for node %q", name)
}

// GetNodeNameByProviderID gets the node name by provider ID.
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudprovider/providers/azure/azure_vmsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ type VMSet interface {
// GetDataDisks gets a list of data disks attached to the node.
GetDataDisks(nodeName types.NodeName) ([]compute.DataDisk, error)

// GetProvisioningStateByNodeName gets the provisioning state by node name.
GetProvisioningStateByNodeName(name string) (string, error)
// GetPowerStatusByNodeName returns the power state of the specified node.
GetPowerStatusByNodeName(name string) (string, error)
}
17 changes: 14 additions & 3 deletions pkg/cloudprovider/providers/azure/azure_vmss.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,24 @@ func (ss *scaleSet) getVmssVM(nodeName string) (ssName, instanceID string, vm co
return ssName, instanceID, *(cachedVM.(*compute.VirtualMachineScaleSetVM)), nil
}

func (ss *scaleSet) GetProvisioningStateByNodeName(name string) (provisioningState string, err error) {
// GetPowerStatusByNodeName returns the power state of the specified node.
func (ss *scaleSet) GetPowerStatusByNodeName(name string) (powerState string, err error) {
_, _, vm, err := ss.getVmssVM(name)
if err != nil {
return provisioningState, err
return powerState, err
}

return *vm.ProvisioningState, nil
if vm.InstanceView != nil && vm.InstanceView.Statuses != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use one function for both azure_standard.go and azure_vmss?

	if vm.InstanceView != nil && vm.InstanceView.Statuses != nil {
 		statuses := *vm.InstanceView.Statuses
 		for _, status := range statuses {
 			state := to.String(status.Code)
 			if strings.HasPrefix(state, vmPowerStatePrefix) {
 				return strings.TrimPrefix(state, vmPowerStatePrefix), nil
 			}
 		}
 	}


Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much more complicated because they are different GO struct.

statuses := *vm.InstanceView.Statuses
for _, status := range statuses {
state := to.String(status.Code)
if strings.HasPrefix(state, vmPowerStatePrefix) {
return strings.TrimPrefix(state, vmPowerStatePrefix), nil
}
}
}

return "", fmt.Errorf("failed to get power status for node %q", name)
}

// getCachedVirtualMachineByInstanceID gets scaleSetVMInfo from cache.
Expand Down
18 changes: 18 additions & 0 deletions pkg/cloudprovider/providers/azure/azure_vmss_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ func (ss *scaleSet) newVmssVMCache() (*timedCache, error) {
return nil, nil
}

// Get instanceView for vmssVM.
if result.InstanceView == nil {
viewCtx, viewCancel := getContextWithCancel()
defer viewCancel()
view, err := ss.VirtualMachineScaleSetVMsClient.GetInstanceView(viewCtx, resourceGroup, ssName, instanceID)
// It is possible that the vmssVM gets removed just before this call. So check whether the VM exist again.
exists, message, realErr = checkResourceExistsFromError(err)
if realErr != nil {
return nil, realErr
}
if !exists {
glog.V(2).Infof("Virtual machine scale set VM %q not found with message: %q", key, message)
return nil, nil
}

result.InstanceView = &view
}

return &result, nil
}

Expand Down