Pretty print size.

This commit is contained in:
Michael Lipp 2023-08-19 17:56:10 +02:00
parent cc15803ad6
commit 15d7589144
4 changed files with 63 additions and 8 deletions

View file

@ -85,7 +85,7 @@ data:
# cause the corresponding property to be omitted from the "-smp" option.
# If currentCpus is greater than maximumCpus, the latter is adjusted.
<#if cr.spec.vm.maximumCpus?? >
maximumCpus: ${ cr.spec.vm.maximumCpus.asInt?c }
maximumCpus: ${ parseQuantity(cr.spec.vm.maximumCpus.asString)?c }
</#if>
<#if cr.spec.vm.cpuTopology?? >
cpuSockets: ${ cr.spec.vm.cpuTopology.cpuSockets.asInt?c }
@ -94,14 +94,14 @@ data:
threadsPerCore: ${ cr.spec.vm.cpuTopology.threadsPerCore.asInt?c }
</#if>
<#if cr.spec.vm.currentCpus?? >
currentCpus: ${ cr.spec.vm.currentCpus.asInt?c }
currentCpus: ${ parseQuantity(cr.spec.vm.currentCpus.asString)?c }
</#if>
# RAM settings
# Maximum defaults to 1G
maximumRam: "${ cr.spec.vm.maximumRam.asString }"
maximumRam: "${ formatMemory(parseQuantity(cr.spec.vm.maximumRam.asString)) }"
<#if cr.spec.vm.currentRam?? >
currentRam: "${ cr.spec.vm.currentRam.asString }"
currentRam: "${ formatMemory(parseQuantity(cr.spec.vm.currentRam.asString)) }"
</#if>
# RTC settings.

View file

@ -24,6 +24,7 @@ import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapperBuilder;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.SimpleNumber;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateHashModel;
@ -34,6 +35,8 @@ import io.kubernetes.client.custom.Quantity;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.util.generic.dynamic.DynamicKubernetesApi;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
@ -42,6 +45,7 @@ import java.util.Map;
import java.util.Optional;
import static org.jdrupes.vmoperator.manager.Constants.VM_OP_GROUP;
import org.jdrupes.vmoperator.manager.VmDefChanged.Type;
import org.jdrupes.vmoperator.util.Convertions;
import org.jdrupes.vmoperator.util.ExtendedObjectWrapper;
import org.jgrapes.core.Channel;
import org.jgrapes.core.Component;
@ -203,6 +207,7 @@ public class Reconciler extends Component {
lbReconciler.reconcile(event, model, channel);
}
@SuppressWarnings("PMD.CognitiveComplexity")
private Map<String, Object> prepareModel(JsonObject vmDef)
throws TemplateModelException {
@SuppressWarnings("PMD.UseConcurrentHashMap")
@ -236,6 +241,34 @@ public class Reconciler extends Component {
}
}
});
model.put("formatMemory", new TemplateMethodModelEx() {
@Override
@SuppressWarnings("PMD.PreserveStackTrace")
public Object exec(@SuppressWarnings("rawtypes") List arguments)
throws TemplateModelException {
var arg = arguments.get(0);
if (arg instanceof SimpleNumber number) {
arg = number.getAsNumber();
}
BigInteger bigInt;
if (arg instanceof BigInteger value) {
bigInt = value;
} else if (arg instanceof BigDecimal dec) {
try {
bigInt = dec.toBigIntegerExact();
} catch (ArithmeticException e) {
return arg;
}
} else if (arg instanceof Integer value) {
bigInt = BigInteger.valueOf(value);
} else if (arg instanceof Long value) {
bigInt = BigInteger.valueOf(value);
} else {
return arg;
}
return Convertions.formatMemory(bigInt);
}
});
return model;
}