Skip to content

Commit

Permalink
Merge pull request #720 from aratikakadiya/main
Browse files Browse the repository at this point in the history
Replace k8s-resources.properties with k8s-resources.json file
  • Loading branch information
at88mph authored Nov 7, 2024
2 parents 07b7e2d + f4f3bc5 commit 65783d8
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 34 deletions.
94 changes: 94 additions & 0 deletions deployment/helm/skaha/skaha-config/k8s-resources.json
Original file line number Diff line number Diff line change
@@ -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
]
}
}
28 changes: 20 additions & 8 deletions skaha/src/main/java/org/opencadc/skaha/context/GetAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -99,34 +101,43 @@ public class ResourceContexts {
private final List<Integer> 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() {
Expand Down

0 comments on commit 65783d8

Please sign in to comment.