diff --git a/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineInstance.java b/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineInstance.java index 9a394d026..daac33577 100644 --- a/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineInstance.java +++ b/src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineInstance.java @@ -16,11 +16,9 @@ package com.google.jenkins.plugins.computeengine; -import static com.google.jenkins.plugins.computeengine.ComputeEngineCloud.CLOUD_ID_LABEL_KEY; - +import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.cloud.graphite.platforms.plugin.client.ComputeClient.OperationException; import com.google.common.base.Strings; -import com.google.common.collect.ImmutableMap; import com.google.jenkins.plugins.computeengine.ssh.GoogleKeyCredential; import edu.umd.cs.findbugs.annotations.Nullable; import hudson.Extension; @@ -33,7 +31,6 @@ import hudson.slaves.RetentionStrategy; import java.io.IOException; import java.util.Collections; -import java.util.Map; import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; @@ -137,15 +134,15 @@ protected void _terminate(TaskListener listener) throws IOException, Interrupted .createSnapshotSync(cloud.getProjectId(), this.zone, this.getNodeName(), createSnapshotTimeout); } - Map filterLabel = ImmutableMap.of(CLOUD_ID_LABEL_KEY, cloud.getInstanceId()); - var instanceExistsInCloud = - cloud.getClient().listInstancesWithLabel(cloud.getProjectId(), filterLabel).stream() - .anyMatch(instance -> instance.getName().equals(name)); + boolean instanceExistsInCloud = instanceExistsInCloud(cloud); // If the instance exists in the cloud, attempt to terminate it. This is an async call and we // return immediately, hoping for the best. if (instanceExistsInCloud) { cloud.getClient().terminateInstanceAsync(cloud.getProjectId(), zone, name); + LOGGER.finer(() -> "Termination request was made for " + this.getNodeName()); + } else { + LOGGER.fine(() -> "Instance " + name + " not found in GCP, nothing to terminate"); } } catch (CloudNotFoundException cnfe) { listener.error(cnfe.getMessage()); @@ -172,6 +169,21 @@ public Optional getSSHKeyCredential() { return Optional.ofNullable(sshKeyCredential); } + private boolean instanceExistsInCloud(ComputeEngineCloud cloud) { + try { + return cloud.getClient().getInstance(cloud.getProjectId(), zone, name) != null; + } catch (GoogleJsonResponseException gjre) { + if (gjre.getStatusCode() == 404) { + return false; + } + LOGGER.log(Level.WARNING, "Error checking instance " + name, gjre); + return false; + } catch (IOException ioe) { + LOGGER.log(Level.WARNING, "Error checking instance " + name, ioe); + return false; + } + } + public ComputeEngineCloud getCloud() throws CloudNotFoundException { ComputeEngineCloud cloud = (ComputeEngineCloud) Jenkins.get().getCloud(cloudName); if (cloud == null)