diff --git a/deployment/helm/skaha/skaha-config/k8s-resources.json b/deployment/helm/skaha/skaha-config/k8s-resources.json new file mode 100644 index 00000000..cacd7803 --- /dev/null +++ b/deployment/helm/skaha/skaha-config/k8s-resources.json @@ -0,0 +1,94 @@ +{ + "cores": { + "default": 1, + "defaultRequest": 1, + "defaultLimit": 16, + "defaultHeadless": 1, + "options": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16 + ] + }, + "memoryGB": { + "default": 2, + "defaultRequest": 4, + "defaultLimit": 192, + "defaultHeadless": 4, + "options": [ + 1, + 2, + 4, + 6, + 8, + 10, + 12, + 14, + 16, + 20, + 24, + 26, + 28, + 30, + 32, + 36, + 40, + 44, + 48, + 56, + 64, + 80, + 92, + 112, + 128, + 140, + 170, + 192 + ] + }, + "gpus": { + "options": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28 + ] + } +} diff --git a/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java b/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java index e67982a5..2315b61f 100644 --- a/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java +++ b/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java @@ -67,11 +67,12 @@ package org.opencadc.skaha.context; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - import org.opencadc.skaha.SkahaAction; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + /** * Output the resource context information. * @@ -86,11 +87,22 @@ public GetAction() { @Override public void doAction() throws Exception { super.initRequest(); - - ResourceContexts rc = new ResourceContexts(); - Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create(); - String json = gson.toJson(rc); + + File propertiesFile = ResourceContexts.getResourcesFile("k8s-resources.json"); + byte[] bytes = getBytes(propertiesFile); syncOutput.setHeader("Content-Type", "application/json"); - syncOutput.getOutputStream().write(json.getBytes()); + syncOutput.getOutputStream().write(bytes); + } + + private static byte[] getBytes(File propertiesFile) throws IOException { + byte[] bytes = new byte[(int) propertiesFile.length()]; + + try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) { + int bytesRead = fileInputStream.read(bytes); + if (bytesRead != bytes.length) { + throw new IOException("Could not read the entire file: " + propertiesFile.getAbsolutePath()); + } + } + return bytes; } } diff --git a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java index 5fc6f244..4bab0057 100644 --- a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java +++ b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java @@ -66,12 +66,14 @@ */ package org.opencadc.skaha.context; -import ca.nrc.cadc.util.MultiValuedProperties; import ca.nrc.cadc.util.PropertiesReader; +import java.io.File; +import java.io.FileReader; import java.util.ArrayList; import java.util.List; +import com.google.gson.*; import org.apache.log4j.Logger; import org.opencadc.skaha.SkahaAction; @@ -99,34 +101,43 @@ public class ResourceContexts { private final List availableGPUs = new ArrayList<>(); public ResourceContexts() { - try { - PropertiesReader reader = new PropertiesReader("k8s-resources.properties"); - MultiValuedProperties mvp = reader.getAllProperties(); - defaultRequestCores = Integer.valueOf(mvp.getFirstPropertyValue("cores-default-request")); - defaultLimitCores = Integer.valueOf(mvp.getFirstPropertyValue("cores-default-limit")); - defaultCores = Integer.valueOf(mvp.getFirstPropertyValue("cores-default")); - defaultCoresHeadless = Integer.valueOf(mvp.getFirstPropertyValue("cores-default-headless")); - defaultRequestRAM = Integer.valueOf(mvp.getFirstPropertyValue("mem-gb-default-request")); - defaultLimitRAM = Integer.valueOf(mvp.getFirstPropertyValue("mem-gb-default-limit")); - defaultRAM = Integer.valueOf(mvp.getFirstPropertyValue("mem-gb-default")); - defaultRAMHeadless = Integer.valueOf(mvp.getFirstPropertyValue("mem-gb-default-headless")); - String cOptions = mvp.getFirstPropertyValue("cores-options"); - String rOptions = mvp.getFirstPropertyValue("mem-gb-options"); - String gOptions = mvp.getFirstPropertyValue("gpus-options"); - - for (String c : cOptions.split(" ")) { - availableCores.add(Integer.valueOf(c)); - } - for (String r : rOptions.split(" ")) { - availableRAM.add(Integer.valueOf(r)); - } - for (String g : gOptions.split(" ")) { - availableGPUs.add(Integer.valueOf(g)); - } + try (final FileReader reader = new FileReader(getResourcesFile("k8s-resources.json"))) { + JsonElement jsonElement = JsonParser.parseReader(reader); + JsonObject jsonObject = jsonElement.getAsJsonObject(); + + // Extract fields into variables + JsonObject cores = jsonObject.getAsJsonObject("cores"); + defaultRequestCores = cores.get("defaultRequest").getAsInt(); + defaultLimitCores = cores.get("defaultLimit").getAsInt(); + defaultCores = cores.get("default").getAsInt(); + defaultCoresHeadless = cores.get("defaultHeadless").getAsInt(); + JsonArray coresOptions = cores.getAsJsonArray("options"); + coresOptions.asList().forEach(coreOption -> availableCores.add(coreOption.getAsInt())); + + JsonObject memory = jsonObject.getAsJsonObject("memoryGB"); + defaultRequestRAM = memory.get("defaultRequest").getAsInt(); + defaultLimitRAM = memory.get("defaultLimit").getAsInt(); + defaultRAM = memory.get("default").getAsInt(); + defaultRAMHeadless = memory.get("defaultHeadless").getAsInt(); + JsonArray ramOptions = memory.getAsJsonArray("options"); + ramOptions.asList().forEach(ramOption -> availableRAM.add(ramOption.getAsInt())); + + JsonObject gpus = jsonObject.getAsJsonObject("gpus"); + JsonArray gpuOptions = gpus.getAsJsonArray("options"); + gpuOptions.asList().forEach(gpuOption -> availableGPUs.add(gpuOption.getAsInt())); } catch (Exception e) { log.error(e); - throw new IllegalStateException("failed reading k8s-resources.properties", e); + throw new IllegalStateException("failed reading k8s-resources.json", e); + } + } + + public static File getResourcesFile(String fileName) { + String configDir = System.getProperty("user.home") + "/config"; + String configDirSystemProperty = PropertiesReader.class.getName() + ".dir"; + if (System.getProperty(configDirSystemProperty) != null) { + configDir = System.getProperty(configDirSystemProperty); } + return new File(new File(configDir), fileName); } public Integer getDefaultRequestCores() {