Skip to content

Commit

Permalink
Merge branch 'fix/choose-bios' into 'main'
Browse files Browse the repository at this point in the history
Enhance logging

See merge request org/jdrupes/vm-operator!10
  • Loading branch information
mnlipp committed Nov 12, 2024
2 parents b8aa925 + 12d6745 commit 65ceed9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
5 changes: 5 additions & 0 deletions deploy/crds/vms-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,11 @@ spec:
- accessConsole
- "*"
default: []
loggingProperties:
type: string
description: >-
Override the default logging properties for
the runner for this VM.
vm:
type: object
description: Defines the VM.
Expand Down
12 changes: 12 additions & 0 deletions dev-example/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
metallb.universe.tf/loadBalancerIPs: 192.168.168.1
metallb.universe.tf/ip-allocated-from-pool: single-common
metallb.universe.tf/allow-shared-ip: single-common
loggingProperties: |
# Defaults for namespace (VM domain)
handlers=java.util.logging.ConsoleHandler
#org.jgrapes.level=FINE
#org.jgrapes.core.handlerTracking.level=FINER
org.jdrupes.vmoperator.runner.qemu.level=FINEST
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tb %1$td %1$tT %4$s %5$s%6$s%n
"/GuiSocketServer":
port: 8888
"/GuiHttpServer":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.jdrupes.vmoperator.manager;

import com.google.gson.JsonObject;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import io.kubernetes.client.custom.V1Patch;
Expand All @@ -36,6 +37,8 @@
import static org.jdrupes.vmoperator.manager.Constants.APP_NAME;
import static org.jdrupes.vmoperator.manager.Constants.VM_OP_NAME;
import org.jdrupes.vmoperator.manager.events.VmChannel;
import org.jdrupes.vmoperator.util.DataPath;
import org.jdrupes.vmoperator.util.GsonPtr;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
Expand Down Expand Up @@ -71,10 +74,6 @@ public ConfigMapReconciler(Configuration fmConfig) {
public Map<String, Object> reconcile(Map<String, Object> model,
VmChannel channel)
throws IOException, TemplateException, ApiException {
// Get API
DynamicKubernetesApi cmApi = new DynamicKubernetesApi("", "v1",
"configmaps", channel.client());

// Combine template and data and parse result
var fmTemplate = fmConfig.getTemplate("runnerConfig.ftl.yaml");
StringWriter out = new StringWriter();
Expand All @@ -84,8 +83,26 @@ public Map<String, Object> reconcile(Map<String, Object> model,
var mapDef = Dynamics.newFromYaml(
new Yaml(new SafeConstructor(new LoaderOptions())), out.toString());

// Maybe override logging.properties from reconciler configuration.
DataPath.<String> get(model, "reconciler", "loggingProperties")
.ifPresent(props -> {
GsonPtr.to(mapDef.getRaw()).get(JsonObject.class, "data")
.get().addProperty("logging.properties", props);
});

// Maybe override logging.properties from VM definition.
DataPath.<String> get(model, "cr", "spec", "loggingProperties")
.ifPresent(props -> {
GsonPtr.to(mapDef.getRaw()).get(JsonObject.class, "data")
.get().addProperty("logging.properties", props);
});

// Get API
DynamicKubernetesApi cmApi = new DynamicKubernetesApi("", "v1",
"configmaps", channel.client());

// Apply and maybe force pod update
var newState = K8s.apply(cmApi, mapDef, out.toString());
var newState = K8s.apply(cmApi, mapDef, mapDef.getRaw().toString());
maybeForceUpdate(channel.client(), newState);
@SuppressWarnings("unchecked")
var res = (Map<String, Object>) channel.client().getJSON().getGson()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@
* ```
* This makes all VM consoles available at IP address 192.168.168.1
* with the port numbers from the VM definitions.
*
* * `loggingProperties`: If defined, specifies the default logging
* properties to be used by the runners managed by the controller.
* This property is a string that holds the content of
* a logging.properties file.
*/
@SuppressWarnings({ "PMD.DataflowAnomalyAnalysis",
"PMD.AvoidDuplicateLiterals" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ private void collect(List<String> result, JsonNode node) {
public String name() {
return name;
}

@Override
public String toString() {
return "Command " + name + ": " + command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
Expand Down Expand Up @@ -318,6 +320,7 @@ public void onConfigurationUpdate(ConfigurationUpdate event) {
});
}

@SuppressWarnings("PMD.LambdaCanBeMethodReference")
private void processInitialConfiguration(Configuration newConfig) {
try {
config = newConfig;
Expand All @@ -333,12 +336,15 @@ private void processInitialConfiguration(Configuration newConfig) {
var tplData = dataFromTemplate();
swtpmDefinition = Optional.ofNullable(tplData.get(SWTPM))
.map(d -> new CommandDefinition(SWTPM, d)).orElse(null);
logger.finest(() -> swtpmDefinition.toString());
qemuDefinition = Optional.ofNullable(tplData.get(QEMU))
.map(d -> new CommandDefinition(QEMU, d)).orElse(null);
logger.finest(() -> qemuDefinition.toString());
cloudInitImgDefinition
= Optional.ofNullable(tplData.get(CLOUD_INIT_IMG))
.map(d -> new CommandDefinition(CLOUD_INIT_IMG, d))
.orElse(null);
logger.finest(() -> cloudInitImgDefinition.toString());

// Forward some values to child components
qemuMonitor.configure(config.monitorSocket,
Expand All @@ -364,6 +370,12 @@ private void setFirmwarePaths() throws IOException {
break;
}
}
if (codePaths.iterator().hasNext() && config.firmwareRom == null) {
throw new IllegalArgumentException("No ROM found, candidates were: "
+ StreamSupport.stream(codePaths.spliterator(), false)
.map(JsonNode::asText).collect(Collectors.joining(", ")));
}

// Get file for firmware vars, if necessary
config.firmwareVars = config.dataDir.resolve(FW_VARS);
if (!Files.exists(config.firmwareVars)) {
Expand Down Expand Up @@ -405,12 +417,14 @@ private JsonNode dataFromTemplate()
model.put("hasDisplayPassword", config.hasDisplayPassword);
model.put("cloudInit", config.cloudInit);
model.put("vm", config.vm);
logger.finest(() -> "Processing template with model: " + model);

// Combine template and data and parse result
// (tempting, but no need to use a pipe here)
var fmTemplate = fmConfig.getTemplate(templatePath.toString());
StringWriter out = new StringWriter();
fmTemplate.process(model, out);
logger.finest(() -> "Result of processing template: " + out);
return yamlMapper.readValue(out.toString(), JsonNode.class);
}

Expand Down Expand Up @@ -746,6 +760,10 @@ private void shutdown() {
props = Runner.class.getResourceAsStream("logging.properties");
}
LogManager.getLogManager().readConfiguration(props);
Logger.getLogger(Runner.class.getName()).log(Level.CONFIG,
() -> path.isPresent()
? "Using logging configuration from " + path.get()
: "Using default logging configuration");
} catch (IOException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 65ceed9

Please sign in to comment.