Improve readability.
This commit is contained in:
parent
224855efd3
commit
981cbe2744
3 changed files with 65 additions and 20 deletions
|
|
@ -353,6 +353,24 @@ public class VmDefinition {
|
||||||
.map("True"::equals).orElse(false));
|
.map("True"::equals).orElse(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the console is in use.
|
||||||
|
*
|
||||||
|
* @return true, if successful
|
||||||
|
*/
|
||||||
|
public boolean consoleConnected() {
|
||||||
|
return conditionStatus("ConsoleConnected").orElse(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the last known console user.
|
||||||
|
*
|
||||||
|
* @return the optional
|
||||||
|
*/
|
||||||
|
public Optional<String> consoleUser() {
|
||||||
|
return this.<String> fromStatus("consoleUser");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set extra data (locally used, unknown to kubernetes).
|
* Set extra data (locally used, unknown to kubernetes).
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ import io.kubernetes.client.apimachinery.GroupVersionKind;
|
||||||
import io.kubernetes.client.openapi.ApiException;
|
import io.kubernetes.client.openapi.ApiException;
|
||||||
import io.kubernetes.client.util.Watch;
|
import io.kubernetes.client.util.Watch;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -171,17 +170,18 @@ public class PoolMonitor extends
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync last usage to console state change if user matches
|
// Sync last usage to console state change if user matches
|
||||||
var assignedTo = vmDef.assignedTo().orElse(null);
|
if (vmDef.assignedTo()
|
||||||
if (assignedTo == null || !assignedTo
|
.map(at -> at.equals(vmDef.consoleUser().orElse(null)))
|
||||||
.equals(vmDef.<String> fromStatus("consoleUser").orElse(null))) {
|
.orElse(true)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var lastUsed
|
|
||||||
= vmDef.assignmentLastUsed().orElse(Instant.ofEpochSecond(0));
|
var ccChange = vmDef.condition("ConsoleConnected")
|
||||||
var conChange = vmDef.condition("ConsoleConnected")
|
.map(cc -> cc.getLastTransitionTime().toInstant());
|
||||||
.map(c -> c.getLastTransitionTime().toInstant())
|
if (ccChange
|
||||||
.orElse(Instant.ofEpochSecond(0));
|
.map(tt -> vmDef.assignmentLastUsed().map(alu -> alu.isAfter(tt))
|
||||||
if (!conChange.isAfter(lastUsed)) {
|
.orElse(true))
|
||||||
|
.orElse(true)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var vmStub = VmDefinitionStub.get(client(),
|
var vmStub = VmDefinitionStub.get(client(),
|
||||||
|
|
@ -190,7 +190,7 @@ public class PoolMonitor extends
|
||||||
vmStub.updateStatus(from -> {
|
vmStub.updateStatus(from -> {
|
||||||
JsonObject status = from.status();
|
JsonObject status = from.status();
|
||||||
var assignment = GsonPtr.to(status).to("assignment");
|
var assignment = GsonPtr.to(status).to("assignment");
|
||||||
assignment.set("lastUsed", conChange.toString());
|
assignment.set("lastUsed", ccChange.get().toString());
|
||||||
return status;
|
return status;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -283,15 +283,7 @@ public class VmMonitor extends
|
||||||
// Find available VM.
|
// Find available VM.
|
||||||
var pool = vmPool;
|
var pool = vmPool;
|
||||||
assignedVm = channelManager.channels().stream()
|
assignedVm = channelManager.channels().stream()
|
||||||
.filter(c -> c.vmDefinition().pools()
|
.filter(c -> isAssignable(pool, c.vmDefinition()))
|
||||||
.contains(event.fromPool()))
|
|
||||||
.filter(c -> !c.vmDefinition()
|
|
||||||
.conditionStatus("ConsoleConnected").orElse(false))
|
|
||||||
.filter(c -> c.vmDefinition().assignedTo().isEmpty()
|
|
||||||
|| pool.retainUntil(c.vmDefinition()
|
|
||||||
.<String> fromStatus("assignment", "lastUsed")
|
|
||||||
.map(Instant::parse).orElse(Instant.ofEpochSecond(0)))
|
|
||||||
.isBefore(Instant.now()))
|
|
||||||
.sorted(Comparator.comparing(c -> c.vmDefinition()
|
.sorted(Comparator.comparing(c -> c.vmDefinition()
|
||||||
.assignmentLastUsed().orElse(Instant.ofEpochSecond(0))))
|
.assignmentLastUsed().orElse(Instant.ofEpochSecond(0))))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
|
|
@ -320,4 +312,39 @@ public class VmMonitor extends
|
||||||
assignedVm.get()));
|
assignedVm.get()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("PMD.SimplifyBooleanReturns")
|
||||||
|
private boolean isAssignable(VmPool pool, VmDefinition vmDef) {
|
||||||
|
// Check if the VM is in the pool
|
||||||
|
if (!vmDef.pools().contains(pool.name())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the VM is not in use
|
||||||
|
if (vmDef.consoleConnected()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If not assigned, it's usable
|
||||||
|
if (vmDef.assignedTo().isEmpty()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it is to be retained
|
||||||
|
if (vmDef.assignmentLastUsed()
|
||||||
|
.map(lu -> pool.retainUntil(lu))
|
||||||
|
.map(ru -> Instant.now().isBefore(ru)).orElse(false)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Additional check in case lastUsed has not been updated
|
||||||
|
// by PoolMonitor#onVmDefChanged() yet ("race condition")
|
||||||
|
if (vmDef.condition("ConsoleConnected")
|
||||||
|
.map(cc -> cc.getLastTransitionTime().toInstant())
|
||||||
|
.map(t -> pool.retainUntil(t))
|
||||||
|
.map(ru -> Instant.now().isBefore(ru)).orElse(false)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue