Skip to content

Commit

Permalink
fix: deleted agent not purged in Multipass (#8)
Browse files Browse the repository at this point in the history
* fix: terminate Multipass VM after agent got deleted

* chore: check null directly for better readability
  • Loading branch information
hainenber authored Dec 15, 2024
1 parent 5070f39 commit f0dedea
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected void _terminate(TaskListener listener) {

try {
LOGGER.info("[multipass-cloud]: Terminating instance named '{}'", instanceName);
MultipassClient multipassClient = cloud.getMultipassClient();
MultipassClient multipassClient = new MultipassClient();
multipassClient.terminateInstance(instanceName);
LOGGER.info("[multipass-cloud]: Terminated instance named '{}'", instanceName);
Jenkins.get().removeNode(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.AncestorInPath;
Expand Down Expand Up @@ -58,7 +57,7 @@ public MultipassAgentTemplate(
String label,
String name) {
Jenkins jenkins = Jenkins.getInstanceOrNull();
if (Objects.nonNull(jenkins)) {
if (jenkins != null) {
jenkins.checkPermission(Jenkins.ADMINISTER);
}

Expand Down Expand Up @@ -165,7 +164,7 @@ public void setSshCredentialsId(String sshCredentialsId) {
* @return a @{link String} object.
*/
public String getDistroAlias() {
return Objects.isNull(distroAlias) ? DEFAULT_AGENT_DISTRIBUTION_ALIAS : distroAlias;
return distroAlias == null ? DEFAULT_AGENT_DISTRIBUTION_ALIAS : distroAlias;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/io/hainenber/jenkins/multipass/MultipassCloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ public MultipassCloud(String name, List<MultipassAgentTemplate> templates) {
? name
: String.format(
"multipass_cloud_%s", jenkinsController().clouds.size()));
this.templates = Objects.isNull(templates) ? Collections.emptyList() : templates;
this.templates = templates == null ? Collections.emptyList() : templates;
LOGGER.info("[multipass-cloud] Initializing Cloud {}", this);
}

public List<MultipassAgentTemplate> getTemplatesByLabel(Label label) {
return Objects.nonNull(this.templates)
return this.templates != null
? this.templates.stream()
.filter(t -> Objects.nonNull(t) && label.matches(t.getLabelSet()))
.filter(t -> t != null && label.matches(t.getLabelSet()))
.toList()
: Collections.emptyList();
}

public List<MultipassAgentTemplate> getTemplatesByName(String templateName) {
return Objects.nonNull(this.templates)
return this.templates != null
? this.templates.stream()
.filter(t -> Objects.nonNull(t) && t.getName().equals(templateName))
.filter(t -> t != null && t.getName().equals(templateName))
.toList()
: Collections.emptyList();
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public synchronized Collection<NodeProvisioner.PlannedNode> provision(CloudState
}

for (MultipassAgentTemplate t : matchingTemplates) {
String labelName = Objects.isNull(label) ? t.getLabels() : label.getDisplayName();
String labelName = label == null ? t.getLabels() : label.getDisplayName();
long currentlyProvisioningInstanceCount = getCurrentlyProvisionedAgentCount();
long numInstancesToLaunch = Math.max(excessWorkload - currentlyProvisioningInstanceCount, 0);
LOGGER.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public void launch(@Nonnull SlaveComputer slaveComputer, @Nonnull TaskListener l
"[multipass-cloud] Terminating Multipass agent {} due to problem launching or connecting to it.",
slaveComputer.getName());
var multipassComputer = ((MultipassComputer) slaveComputer).getNode();
if (Objects.nonNull(multipassComputer)) {
if (multipassComputer != null) {
multipassComputer.terminate();
}
}
}

protected void launchScript(MultipassComputer computer, TaskListener listener) throws IOException {
Node node = computer.getNode();
if (Objects.isNull(node)) {
if (node == null) {
LOGGER.info("[multipass-cloud] Not launching {} since it is missing a node.", computer);
return;
}
Expand Down Expand Up @@ -115,7 +115,7 @@ protected void launchScript(MultipassComputer computer, TaskListener listener) t
throw new RuntimeException("Cannot find the instance named " + instanceName);
}
var instanceHostIp = instance.get().getIpv4();
if (Objects.isNull(instanceHostIp)) {
if (instanceHostIp == null) {
throw new RuntimeException("Cannot find the instance named " + instanceName);
}
var sshConnection = new Connection(instanceHostIp.get(0), computer.getSshPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public String getOutput(CommandLine cmd) throws IOException {
}

public List<String> getDistributionAlias() throws IOException {
if (Objects.nonNull(availableDistroAliases)) {
if (availableDistroAliases != null) {
return availableDistroAliases;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void setSnapshots(int snapshots) {

@Nullable
public List<String> getIpv4() {
return Objects.isNull(ipv4) ? new ArrayList<>() : ipv4;
return ipv4 == null ? new ArrayList<>() : ipv4;
}

public void setIpv4(@Nullable List<String> ipv4) {
Expand Down

0 comments on commit f0dedea

Please sign in to comment.