Add assignment based on last usage.

This commit is contained in:
Michael Lipp 2025-01-25 13:35:51 +01:00
parent 877d4c69cd
commit 5d722abd2e
9 changed files with 186 additions and 32 deletions

View file

@ -18,7 +18,11 @@
package org.jdrupes.vmoperator.common;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.kubernetes.client.openapi.models.V1Condition;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
@ -36,9 +40,12 @@ import org.jdrupes.vmoperator.util.DataPath;
/**
* Represents a VM definition.
*/
@SuppressWarnings({ "PMD.DataClass" })
@SuppressWarnings({ "PMD.DataClass", "PMD.TooManyMethods" })
public class VmDefinition {
private static ObjectMapper objectMapper
= new ObjectMapper().registerModule(new JavaTimeModule());
private String kind;
private String apiVersion;
private V1ObjectMeta metadata;
@ -306,6 +313,31 @@ public class VmDefinition {
return fromStatus("assignment", "user");
}
/**
* Last usage of assigned VM.
*
* @return the optional
*/
public Optional<Instant> assignmentLastUsed() {
return this.<String> fromStatus("assignment", "lastUsed")
.map(Instant::parse);
}
/**
* Return a condition from the status.
*
* @param name the condition's name
* @return the status, if the condition is defined
*/
public Optional<V1Condition> condition(String name) {
return this.<List<Map<String, Object>>> fromStatus("conditions")
.orElse(Collections.emptyList()).stream()
.filter(cond -> DataPath.get(cond, "type")
.map(name::equals).orElse(false))
.findFirst()
.map(cond -> objectMapper.convertValue(cond, V1Condition.class));
}
/**
* Return a condition's status.
*

View file

@ -18,6 +18,8 @@
package org.jdrupes.vmoperator.common;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
@ -36,6 +38,7 @@ import org.jdrupes.vmoperator.util.DataPath;
public class VmPool {
private String name;
private String retention;
private boolean defined;
private List<Grant> permissions = Collections.emptyList();
private final Set<String> vms
@ -86,6 +89,24 @@ public class VmPool {
this.defined = defined;
}
/**
* Gets the retention.
*
* @return the retention
*/
public String retention() {
return retention;
}
/**
* Sets the retention.
*
* @param retention the retention to set
*/
public void setRetention(String retention) {
this.retention = retention;
}
/**
* Permissions granted for a VM from the pool.
*
@ -113,6 +134,11 @@ public class VmPool {
return vms;
}
/**
* To string.
*
* @return the string
*/
@Override
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
public String toString() {
@ -147,4 +173,16 @@ public class VmPool {
.flatMap(Function.identity()).collect(Collectors.toSet());
}
/**
* Return the instant until which an assignment should be retained.
*
* @param lastUsed the last used
* @return the instant
*/
public Instant retainUntil(Instant lastUsed) {
if (retention.startsWith("P")) {
return lastUsed.plus(Duration.parse(retention));
}
return Instant.parse(retention);
}
}