use of com.azure.resourcemanager.resources.models.GenericResource in project azure-vm-agents-plugin by jenkinsci.
the class AzureVMAgentCleanUpTask method cleanLeakedResources.
public void cleanLeakedResources(AzureVMCloud cloud, String resourceGroup, DeploymentRegistrar deploymentRegistrar) {
try {
final List<String> validVMs = getValidVMs();
final AzureResourceManager azureClient = cloud.getAzureClient();
final AzureVMManagementServiceDelegate serviceDelegate = cloud.getServiceDelegate();
// can't use listByTag because for some reason that method strips all the tags from the outputted resources
// (https://github.com/Azure/azure-sdk-for-java/issues/1436)
final PagedIterable<GenericResource> resources = azureClient.genericResources().listByResourceGroup(resourceGroup);
if (resources == null || !resources.iterator().hasNext()) {
LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: No resources found in rg: " + resourceGroup);
return;
}
final PriorityQueue<GenericResource> resourcesMarkedForDeletion = new PriorityQueue<>(10, new Comparator<GenericResource>() {
@Override
public int compare(GenericResource o1, GenericResource o2) {
int o1Priority = getPriority(o1);
int o2Priority = getPriority(o2);
if (o1Priority == o2Priority) {
return 0;
}
return (o1Priority < o2Priority) ? -1 : 1;
}
private int getPriority(GenericResource resource) {
// suppress magic number check
// CHECKSTYLE:OFF
final String type = resource.type();
if (StringUtils.containsIgnoreCase(type, "virtualMachine")) {
return 1;
}
if (StringUtils.containsIgnoreCase(type, "networkInterface")) {
return 2;
}
if (StringUtils.containsIgnoreCase(type, "IPAddress")) {
return 3;
}
return 4;
// CHECKSTYLE:ON
}
});
LOGGER.log(getNormalLoggingLevel(), String.format("cleanLeakedResources: beginning to look at leaked " + "resources in rg: %s", resourceGroup));
for (GenericResource resource : resources) {
final Map<String, String> tags = resource.tags();
if (!tags.containsKey(Constants.AZURE_RESOURCES_TAG_NAME) || !deploymentRegistrar.getDeploymentTag().matches(new AzureUtil.DeploymentTag(tags.get(Constants.AZURE_RESOURCES_TAG_NAME)))) {
continue;
}
boolean shouldSkipDeletion = false;
for (String validVM : validVMs) {
if (resource.name().contains(validVM)) {
shouldSkipDeletion = true;
break;
}
}
// we're not removing storage accounts of networks - someone else might be using them
if (shouldSkipDeletion || StringUtils.containsIgnoreCase(resource.type(), "StorageAccounts") || StringUtils.containsIgnoreCase(resource.type(), "virtualNetworks")) {
continue;
}
resourcesMarkedForDeletion.add(resource);
}
LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: %d resources marked for deletion" + resourcesMarkedForDeletion.size());
while (!resourcesMarkedForDeletion.isEmpty()) {
try {
final GenericResource resource = resourcesMarkedForDeletion.poll();
if (resource == null) {
LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: resource was null continuing");
continue;
}
LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: looking at {0} from resource group {1}", new Object[] { resource.name(), resourceGroup });
URI osDiskURI = null;
String managedOsDiskId = null;
if (StringUtils.containsIgnoreCase(resource.type(), "virtualMachine")) {
LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: retrieving VM {0} from resource group {1}", new Object[] { resource.name(), resourceGroup });
VirtualMachine virtualMachine = azureClient.virtualMachines().getById(resource.id());
if (!virtualMachine.isManagedDiskEnabled()) {
osDiskURI = new URI(virtualMachine.osUnmanagedDiskVhdUri());
} else {
managedOsDiskId = virtualMachine.osDiskId();
}
LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: completed retrieving VM {0} from resource group {1}", new Object[] { resource.name(), resourceGroup });
}
LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: deleting {0} from resource group {1}", new Object[] { resource.name(), resourceGroup });
azureClient.genericResources().deleteById(resource.id());
if (osDiskURI != null) {
serviceDelegate.removeStorageBlob(osDiskURI, resourceGroup);
}
if (managedOsDiskId != null) {
azureClient.disks().deleteById(managedOsDiskId);
serviceDelegate.removeImage(azureClient, resource.name(), resourceGroup);
}
LOGGER.log(getNormalLoggingLevel(), "cleanLeakedResources: deleted {0} from resource group {1}", new Object[] { resource.name(), resourceGroup });
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to clean resource ", e);
}
}
} catch (Exception e) {
// No need to throw exception back, just log and move on.
LOGGER.log(Level.WARNING, "Failed to clean leaked resources ", e);
}
}
use of com.azure.resourcemanager.resources.models.GenericResource in project azure-vm-agents-plugin by jenkinsci.
the class ITAzureVMAgentCleanUpTask method cleanLeakedResourcesRemovesDeployedResources.
@Test
public void cleanLeakedResourcesRemovesDeployedResources() throws Exception {
final AzureUtil.DeploymentTag tagValue = new AzureUtil.DeploymentTag("some_value/123");
final AzureUtil.DeploymentTag matchingTagValue = new AzureUtil.DeploymentTag("some_value/9999123");
final String cloudName = "some_cloud_name";
AzureVMCloud cloud = mock(AzureVMCloud.class);
when(cloud.getCloudName()).thenReturn(cloudName);
when(cloud.getAzureClient()).thenReturn(azureClient);
when(cloud.getServiceDelegate()).thenReturn(delegate);
final DeploymentRegistrar deploymentRegistrarMock = mock(DeploymentRegistrar.class);
when(deploymentRegistrarMock.getDeploymentTag()).thenReturn(tagValue);
final DeploymentRegistrar deploymentRegistrarMock_matching = mock(DeploymentRegistrar.class);
when(deploymentRegistrarMock_matching.getDeploymentTag()).thenReturn(matchingTagValue);
final AzureVMDeploymentInfo deployment = createDefaultDeployment(2, deploymentRegistrarMock);
final List<String> validVMs = Collections.singletonList(deployment.getVmBaseName() + "0");
AzureVMAgentCleanUpTask cleanUpTask = spy(AzureVMAgentCleanUpTask.class);
when(cleanUpTask.getValidVMs()).thenReturn(validVMs);
// should remove second deployment
cleanUpTask.cleanLeakedResources(cloud, testEnv.azureResourceGroup, deploymentRegistrarMock_matching);
// give time for azure to realize that some resources are missing
Thread.sleep(20 * 1000);
PagedIterable<GenericResource> resources = azureClient.genericResources().listByResourceGroup(testEnv.azureResourceGroup);
for (GenericResource resource : resources) {
if (StringUtils.containsIgnoreCase(resource.type(), "storageAccounts") || StringUtils.containsIgnoreCase(resource.type(), "virtualNetworks")) {
continue;
}
if (resource.tags().get(Constants.AZURE_RESOURCES_TAG_NAME) != null && matchingTagValue.matches(new AzureUtil.DeploymentTag(resource.tags().get(Constants.AZURE_RESOURCES_TAG_NAME)))) {
String resourceName = resource.name();
String depl = deployment.getVmBaseName() + "0";
Assert.assertTrue("Resource shouldn't exist: " + resourceName + " (vmbase: " + depl + " )", resourceName.contains(depl));
}
}
}
Aggregations