From e3bdc463fa7f8d8b5dffe02f35a03012d4213fb5 Mon Sep 17 00:00:00 2001 From: Tariq Ibrahim Date: Wed, 12 Dec 2018 00:27:12 -0800 Subject: [PATCH] Revert "add logic to check if an azure VM is deallocated or not (#4908)" (#4980) This reverts commit 61cf4365 Signed-off-by: tariqibrahim --- discovery/azure/azure.go | 36 +++----- discovery/azure/azure_test.go | 125 ---------------------------- docs/configuration/configuration.md | 3 +- 3 files changed, 11 insertions(+), 153 deletions(-) diff --git a/discovery/azure/azure.go b/discovery/azure/azure.go index 58a7ca8e00..7c999382ef 100644 --- a/discovery/azure/azure.go +++ b/discovery/azure/azure.go @@ -47,7 +47,6 @@ const ( azureLabelMachinePrivateIP = azureLabel + "machine_private_ip" azureLabelMachineTag = azureLabel + "machine_tag_" azureLabelMachineScaleSet = azureLabel + "machine_scale_set" - azureLabelPowerState = azureLabel + "machine_power_state" ) var ( @@ -229,7 +228,6 @@ type virtualMachine struct { ScaleSet string Tags map[string]*string NetworkProfile compute.NetworkProfile - PowerStateCode string } // Create a new azureResource object from an ID string. @@ -304,21 +302,12 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) { return } - // We check if the virtual machine has been deallocated. - // If so, we skip them in service discovery. - if strings.EqualFold(vm.PowerStateCode, "PowerState/deallocated") { - level.Debug(d.logger).Log("msg", "Skipping virtual machine", "machine", vm.Name, "power_state", vm.PowerStateCode) - ch <- target{} - return - } - labels := model.LabelSet{ azureLabelMachineID: model.LabelValue(vm.ID), azureLabelMachineName: model.LabelValue(vm.Name), azureLabelMachineOSType: model.LabelValue(vm.OsType), azureLabelMachineLocation: model.LabelValue(vm.Location), azureLabelMachineResourceGroup: model.LabelValue(r.ResourceGroup), - azureLabelPowerState: model.LabelValue(vm.PowerStateCode), } if vm.ScaleSet != "" { @@ -346,6 +335,16 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) { continue } + // Unfortunately Azure does not return information on whether a VM is deallocated. + // This information is available via another API call however the Go SDK does not + // yet support this. On deallocated machines, this value happens to be nil so it + // is a cheap and easy way to determine if a machine is allocated or not. + if networkInterface.Properties.Primary == nil { + level.Debug(d.logger).Log("msg", "Skipping deallocated virtual machine", "machine", vm.Name) + ch <- target{} + return + } + if *networkInterface.Properties.Primary { for _, ip := range *networkInterface.Properties.IPConfigurations { if ip.Properties.PrivateIPAddress != nil { @@ -473,7 +472,6 @@ func mapFromVM(vm compute.VirtualMachine) virtualMachine { ScaleSet: "", Tags: tags, NetworkProfile: *(vm.Properties.NetworkProfile), - PowerStateCode: getPowerStateFromVMInstanceView(vm.Properties.InstanceView), } } @@ -494,7 +492,6 @@ func mapFromVMScaleSetVM(vm compute.VirtualMachineScaleSetVM, scaleSetName strin ScaleSet: scaleSetName, Tags: tags, NetworkProfile: *(vm.Properties.NetworkProfile), - PowerStateCode: getPowerStateFromVMInstanceView(vm.Properties.InstanceView), } } @@ -527,16 +524,3 @@ func (client *azureClient) getNetworkInterfaceByID(networkInterfaceID string) (n return result, nil } - -func getPowerStateFromVMInstanceView(instanceView *compute.VirtualMachineInstanceView) (powerState string) { - if instanceView.Statuses == nil { - return - } - for _, ivs := range *instanceView.Statuses { - code := *(ivs.Code) - if strings.HasPrefix(code, "PowerState") { - powerState = code - } - } - return -} diff --git a/discovery/azure/azure_test.go b/discovery/azure/azure_test.go index 7dab352430..daed79ea6f 100644 --- a/discovery/azure/azure_test.go +++ b/discovery/azure/azure_test.go @@ -26,10 +26,6 @@ func TestMapFromVMWithEmptyTags(t *testing.T) { vmType := "type" location := "westeurope" networkProfile := compute.NetworkProfile{} - provisioningStatusCode := "ProvisioningState/succeeded" - provisionDisplayStatus := "Provisioning succeeded" - powerStatusCode := "PowerState/running" - powerDisplayStatus := "VM running" properties := &compute.VirtualMachineProperties{ StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ @@ -37,20 +33,6 @@ func TestMapFromVMWithEmptyTags(t *testing.T) { }, }, NetworkProfile: &networkProfile, - InstanceView: &compute.VirtualMachineInstanceView{ - Statuses: &[]compute.InstanceViewStatus{ - { - Code: &provisioningStatusCode, - Level: "Info", - DisplayStatus: &provisionDisplayStatus, - }, - { - Code: &powerStatusCode, - Level: "Info", - DisplayStatus: &powerDisplayStatus, - }, - }, - }, } testVM := compute.VirtualMachine{ @@ -70,7 +52,6 @@ func TestMapFromVMWithEmptyTags(t *testing.T) { OsType: "Linux", Tags: map[string]*string{}, NetworkProfile: networkProfile, - PowerStateCode: "PowerState/running", } actualVM := mapFromVM(testVM) @@ -88,10 +69,6 @@ func TestMapFromVMWithTags(t *testing.T) { tags := map[string]*string{ "prometheus": new(string), } - provisioningStatusCode := "ProvisioningState/succeeded" - provisionDisplayStatus := "Provisioning succeeded" - powerStatusCode := "PowerState/running" - powerDisplayStatus := "VM running" networkProfile := compute.NetworkProfile{} properties := &compute.VirtualMachineProperties{ StorageProfile: &compute.StorageProfile{ @@ -100,20 +77,6 @@ func TestMapFromVMWithTags(t *testing.T) { }, }, NetworkProfile: &networkProfile, - InstanceView: &compute.VirtualMachineInstanceView{ - Statuses: &[]compute.InstanceViewStatus{ - { - Code: &provisioningStatusCode, - Level: "Info", - DisplayStatus: &provisionDisplayStatus, - }, - { - Code: &powerStatusCode, - Level: "Info", - DisplayStatus: &powerDisplayStatus, - }, - }, - }, } testVM := compute.VirtualMachine{ @@ -133,7 +96,6 @@ func TestMapFromVMWithTags(t *testing.T) { OsType: "Linux", Tags: tags, NetworkProfile: networkProfile, - PowerStateCode: "PowerState/running", } actualVM := mapFromVM(testVM) @@ -149,10 +111,6 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) { vmType := "type" location := "westeurope" networkProfile := compute.NetworkProfile{} - provisioningStatusCode := "ProvisioningState/succeeded" - provisionDisplayStatus := "Provisioning succeeded" - powerStatusCode := "PowerState/running" - powerDisplayStatus := "VM running" properties := &compute.VirtualMachineScaleSetVMProperties{ StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ @@ -160,20 +118,6 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) { }, }, NetworkProfile: &networkProfile, - InstanceView: &compute.VirtualMachineInstanceView{ - Statuses: &[]compute.InstanceViewStatus{ - { - Code: &provisioningStatusCode, - Level: "Info", - DisplayStatus: &provisionDisplayStatus, - }, - { - Code: &powerStatusCode, - Level: "Info", - DisplayStatus: &powerDisplayStatus, - }, - }, - }, } testVM := compute.VirtualMachineScaleSetVM{ @@ -195,7 +139,6 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) { Tags: map[string]*string{}, NetworkProfile: networkProfile, ScaleSet: scaleSet, - PowerStateCode: "PowerState/running", } actualVM := mapFromVMScaleSetVM(testVM, scaleSet) @@ -214,10 +157,6 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) { "prometheus": new(string), } networkProfile := compute.NetworkProfile{} - provisioningStatusCode := "ProvisioningState/succeeded" - provisionDisplayStatus := "Provisioning succeeded" - powerStatusCode := "PowerState/running" - powerDisplayStatus := "VM running" properties := &compute.VirtualMachineScaleSetVMProperties{ StorageProfile: &compute.StorageProfile{ OsDisk: &compute.OSDisk{ @@ -225,20 +164,6 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) { }, }, NetworkProfile: &networkProfile, - InstanceView: &compute.VirtualMachineInstanceView{ - Statuses: &[]compute.InstanceViewStatus{ - { - Code: &provisioningStatusCode, - Level: "Info", - DisplayStatus: &provisionDisplayStatus, - }, - { - Code: &powerStatusCode, - Level: "Info", - DisplayStatus: &powerDisplayStatus, - }, - }, - }, } testVM := compute.VirtualMachineScaleSetVM{ @@ -260,7 +185,6 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) { Tags: tags, NetworkProfile: networkProfile, ScaleSet: scaleSet, - PowerStateCode: "PowerState/running", } actualVM := mapFromVMScaleSetVM(testVM, scaleSet) @@ -269,52 +193,3 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) { t.Errorf("Expected %v got %v", expectedVM, actualVM) } } - -func TestGetPowerStatusFromVM(t *testing.T) { - provisioningStatusCode := "ProvisioningState/succeeded" - provisionDisplayStatus := "Provisioning succeeded" - powerStatusCode := "PowerState/running" - powerDisplayStatus := "VM running" - properties := &compute.VirtualMachineScaleSetVMProperties{ - StorageProfile: &compute.StorageProfile{ - OsDisk: &compute.OSDisk{ - OsType: "Linux", - }, - }, - InstanceView: &compute.VirtualMachineInstanceView{ - Statuses: &[]compute.InstanceViewStatus{ - { - Code: &provisioningStatusCode, - Level: "Info", - DisplayStatus: &provisionDisplayStatus, - }, - { - Code: &powerStatusCode, - Level: "Info", - DisplayStatus: &powerDisplayStatus, - }, - }, - }, - } - - testVM := compute.VirtualMachineScaleSetVM{ - Properties: properties, - } - - actual := getPowerStateFromVMInstanceView(testVM.Properties.InstanceView) - - expected := "PowerState/running" - - if actual != expected { - t.Errorf("expected powerStatus %s, but got %s instead", expected, actual) - } - - // Noq we test a virtualMachine with an empty InstanceView struct. - testVM.Properties.InstanceView = &compute.VirtualMachineInstanceView{} - - actual = getPowerStateFromVMInstanceView(testVM.Properties.InstanceView) - - if actual != "" { - t.Errorf("expected powerStatus %s, but got %s instead", expected, actual) - } -} diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 9461b7e3b8..971fbd78b4 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -263,11 +263,10 @@ The following meta labels are available on targets during relabeling: * `__meta_azure_machine_location`: the location the machine runs in * `__meta_azure_machine_name`: the machine name * `__meta_azure_machine_os_type`: the machine operating system -* `__meta_azure_machine_power_state`: the current power state of the machine * `__meta_azure_machine_private_ip`: the machine's private IP * `__meta_azure_machine_resource_group`: the machine's resource group -* `__meta_azure_machine_scale_set`: the name of the scale set which the vm is part of (this value is only set if you are using a [scale set](https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/)) * `__meta_azure_machine_tag_`: each tag value of the machine +* `__meta_azure_machine_scale_set`: the name of the scale set which the vm is part of (this value is only set if you are using a [scale set](https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/)) See below for the configuration options for Azure discovery: