Skip to content

Commit 7b373be

Browse files
Containerization Flavor (#129)
* k3s flavor * quality improvements * sa * docker-compose rollback
1 parent 9cedd76 commit 7b373be

File tree

6 files changed

+95
-17
lines changed

6 files changed

+95
-17
lines changed

docker/docker-compose.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ services:
4141
- ./wait_for_db.sh:/usr/local/tomcat/bin/wait_for_db.sh
4242
environment:
4343
PROPERTIES_FILENAME: sal
44-
CLUSTER_TYPE: "k8s" # (defult) or "k3s"
44+
CLUSTER_TYPE: "k8s" # (defult) or "k3s"
4545
#Set up connection to ProActive server (PWS)
4646
PWS_URL: <CHANGE_ME>
4747
PWS_USERNAME: <CHANGE_ME>
@@ -68,4 +68,4 @@ networks:
6868
db-tier: {}
6969
#Comment this part if you do not want to include volume for peristant data storage
7070
volumes:
71-
mariadb_data: {}
71+
mariadb_data: {}

sal-service/src/main/java/org/ow2/proactive/sal/service/rest/JobRest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public ResponseEntity<Long> submitJob(@ApiParam(value = "Proactive authenticatio
9292
final String sessionId, @ApiParam(value = "A job identifier", required = true)
9393
@PathVariable
9494
final String jobId) throws NotConnectedException {
95-
return ResponseEntity.ok(jobService.submitJob(sessionId, jobId));
95+
return ResponseEntity.ok(jobService.submitJob(sessionId, jobId, null));
9696
}
9797

9898
@RequestMapping(value = "/{jobId}/status", method = RequestMethod.GET)

sal-service/src/main/java/org/ow2/proactive/sal/service/service/ClusterService.java

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.IOException;
99
import java.io.Serializable;
1010
import java.util.*;
11+
import java.util.Map;
1112

1213
import org.apache.commons.lang3.Validate;
1314
import org.ow2.proactive.sal.model.*;
@@ -18,12 +19,17 @@
1819
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.stereotype.Service;
2021

22+
import com.fasterxml.jackson.core.type.TypeReference;
23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
2125
import lombok.extern.log4j.Log4j2;
2226

2327

2428
@Log4j2
2529
@Service("ClusterService")
2630
public class ClusterService {
31+
public static final String CONTAINERIZATION_FLAVOR_ENV = "export CONTAINERIZATION_FLAVOR=";
32+
2733
@Autowired
2834
private PAGatewayService paGatewayService;
2935

@@ -39,6 +45,8 @@ public class ClusterService {
3945
@Autowired
4046
private EdgeService edgeService;
4147

48+
private static final ObjectMapper objectMapper = new ObjectMapper();
49+
4250
private boolean isValidClusterName(String name) {
4351
return name != null && !name.isEmpty() && name.matches("^[a-z0-9-]+$");
4452
}
@@ -56,6 +64,62 @@ private void validateNode(ClusterNodeDefinition node) {
5664
}
5765
}
5866

67+
public static String stripQuotes(String value) {
68+
if (value == null || value.isEmpty())
69+
return value;
70+
71+
int start = 0;
72+
int end = value.length();
73+
74+
if (value.charAt(0) == '"' || value.charAt(0) == '\'') {
75+
start++;
76+
}
77+
if (value.length() > 1 &&
78+
(value.charAt(value.length() - 1) == '"' || value.charAt(value.length() - 1) == '\'')) {
79+
end--;
80+
}
81+
82+
return value.substring(start, end);
83+
}
84+
85+
public static String getContainerizationFlavor(String envVarsScript) {
86+
if (envVarsScript == null || envVarsScript.isEmpty()) {
87+
return null;
88+
}
89+
90+
try {
91+
String[] lines = envVarsScript.split("\\r?\\n");
92+
93+
for (String line : lines) {
94+
line = line.trim();
95+
96+
if (line.contains(CONTAINERIZATION_FLAVOR_ENV)) {
97+
// Extract only the export part before '>>'
98+
int exportIndex = line.indexOf(CONTAINERIZATION_FLAVOR_ENV);
99+
String exportPart = line.substring(exportIndex).split(">>")[0].trim();
100+
101+
// Split and get the value part
102+
String[] keyValue = exportPart.replace("export ", "").split("=", 2);
103+
if (keyValue.length == 2) {
104+
String rawValue = keyValue[1].trim();
105+
106+
// Remove surrounding quotes and convert to lower case
107+
String cleanValue = stripQuotes(rawValue.trim()).toLowerCase();
108+
109+
return ClusterUtils.CLUSTER_TYPE_K3S.equals(cleanValue) ? ClusterUtils.CLUSTER_TYPE_K3S
110+
: ClusterUtils.CLUSTER_TYPE_K8S;
111+
}
112+
}
113+
}
114+
115+
return null;
116+
} catch (Exception e) {
117+
LOGGER.error("Failed to parse containerization flavor from envVarsScript: {}", e.getMessage(), e);
118+
return null;
119+
}
120+
121+
}
122+
59123
public boolean defineCluster(String sessionId, ClusterDefinition clusterDefinition)
60124
throws NotConnectedException, IOException {
61125
if (!paGatewayService.isConnectionActive(sessionId)) {
@@ -95,7 +159,9 @@ public boolean defineCluster(String sessionId, ClusterDefinition clusterDefiniti
95159
}
96160

97161
cluster.setStatus(ClusterStatus.DEFINED);
162+
98163
cluster.setEnvVars(ClusterUtils.createEnvVarsScript(clusterDefinition.getEnvVars()));
164+
99165
nodes.forEach(repositoryService::saveClusterNodeDefinition);
100166
cluster.setNodes(nodes);
101167
repositoryService.saveCluster(cluster);
@@ -210,7 +276,8 @@ private void submitClusterNode(String sessionId, Cluster cluster, String nodeNam
210276
repositoryService.saveDeployment(currentDeployment);
211277
repositoryService.flush();
212278
// submit job
213-
jobService.submitJob(sessionId, jobId);
279+
String containerizationFlavor = getContainerizationFlavor(cluster.getEnvVars());
280+
jobService.submitJob(sessionId, jobId, containerizationFlavor);
214281
LOGGER.info("Node {} is submitted for deployment", nodeName);
215282
} else {
216283
LOGGER.error("The node {} was not found in the cluster {} definition", nodeName, cluster.getName());
@@ -343,13 +410,15 @@ public Long labelNodes(String sessionId, String clusterName, List<Map<String, St
343410
LOGGER.info("labelNodes endpoint is called to label nodes in the cluster: " + clusterName);
344411
String masterNodeToken = "";
345412
Cluster cluster = ClusterUtils.getClusterByName(clusterName, repositoryService.listCluster());
413+
346414
if (cluster != null) {
347415
masterNodeToken = cluster.getMasterNode() + "_" + clusterName;
348416
} else {
349417
LOGGER.error("The cluster with the name {} was not found!", clusterName);
350418
return -1L;
351419
}
352-
String script = ClusterUtils.createLabelNodesScript(nodeLabels, clusterName);
420+
String containerizationFlavor = getContainerizationFlavor(cluster.getEnvVars());
421+
String script = ClusterUtils.createLabelNodesScript(nodeLabels, clusterName, containerizationFlavor);
353422

354423
try {
355424
String paJobName = "label_nodes_" + clusterName;
@@ -377,7 +446,8 @@ public Long deployApplication(String sessionId, String clusterName, ClusterAppli
377446
}
378447
String script = "";
379448
try {
380-
script = ClusterUtils.createDeployApplicationScript(application);
449+
String containerizationFlavor = getContainerizationFlavor(cluster.getEnvVars());
450+
script = ClusterUtils.createDeployApplication(application, containerizationFlavor);
381451

382452
} catch (IOException e) {
383453
throw new RuntimeException(e);

sal-service/src/main/java/org/ow2/proactive/sal/service/service/JobService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public String getGraphInDotFormat(String sessionId, String jobId) throws NotConn
304304
* @return The submitted job id
305305
*/
306306
@Transactional
307-
public Long submitJob(String sessionId, String jobId) throws NotConnectedException {
307+
public Long submitJob(String sessionId, String jobId, String containerizationFlavor) throws NotConnectedException {
308308
if (!paGatewayService.isConnectionActive(sessionId)) {
309309
throw new NotConnectedException();
310310
}
@@ -319,7 +319,9 @@ public Long submitJob(String sessionId, String jobId) throws NotConnectedExcepti
319319
.stream()
320320
.filter(task -> task.getDeployments() != null && !task.getDeployments().isEmpty())
321321
.forEach(task -> {
322-
List<ScriptTask> scriptTasks = taskBuilder.buildPATask(task, jobToSubmit);
322+
List<ScriptTask> scriptTasks = taskBuilder.buildPATask(task,
323+
jobToSubmit,
324+
containerizationFlavor);
323325

324326
addAllScriptTasksToPAJob(paJob, task, scriptTasks);
325327
repositoryService.saveTask(task);

sal-service/src/main/java/org/ow2/proactive/sal/service/service/TaskBuilder.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ public List<ScriptTask> buildReconfigurationPATask(Task task, Job job,
747747
} else if (addedTaskNames.contains(task.getName())) {
748748
// When the scaled task is a parent of the task to be built
749749
LOGGER.info("Building task [{}] as a new added task ", task.getTaskId());
750-
scriptTasks.addAll(buildPATask(task, job));
750+
scriptTasks.addAll(buildPATask(task, job, null));
751751
} else {
752752
LOGGER.warn("Task [{}] is neither unchanged nor added. This should not figure ine job!", task.getTaskId());
753753
}
@@ -800,7 +800,7 @@ private List<ScriptTask> setFirstAndLastSubmittedTaskNamesFromScriptTasks(Task t
800800
* @param job The related job skeleton
801801
* @return A list of ProActive tasks
802802
*/
803-
public List<ScriptTask> buildPATask(Task task, Job job) {
803+
public List<ScriptTask> buildPATask(Task task, Job job, String containerizationFlavor) {
804804
List<ScriptTask> scriptTasks = new LinkedList<>();
805805
LOGGER.debug("Building PA task for: {}", task.getTaskId());
806806
if (task.getDeployments() == null || task.getDeployments().isEmpty()) {
@@ -816,7 +816,8 @@ public List<ScriptTask> buildPATask(Task task, Job job) {
816816
String suffix = "_" + deployment.getNumber();
817817
scriptTasks.add(createInfraTask(task, deployment, suffix, token));
818818
if (deployment.getWorker() != null && deployment.getWorker()) {
819-
ScriptTask waitForMasterTask = createWaitForMasterTask(deployment.getMasterToken());
819+
ScriptTask waitForMasterTask = createWaitForMasterTask(deployment.getMasterToken(),
820+
containerizationFlavor);
820821
waitForMasterTask.addDependence(scriptTasks.get(scriptTasks.size() - 1));
821822
scriptTasks.add(waitForMasterTask);
822823
}
@@ -901,8 +902,9 @@ private Map<String, TaskVariable> createVariablesMapForSynchronizationChannels(J
901902
return (variablesMap);
902903
}
903904

904-
private ScriptTask createWaitForMasterTask(String masterNodeToken) {
905-
String clusterType = System.getenv(ClusterUtils.CLUSTER_TYPE_ENV);
905+
private ScriptTask createWaitForMasterTask(String masterNodeToken, String containerizationFlavor) {
906+
String clusterType = (containerizationFlavor != null) ? containerizationFlavor
907+
: System.getenv(ClusterUtils.CLUSTER_TYPE_ENV);
906908
String waitForMasterScript;
907909

908910
if (ClusterUtils.CLUSTER_TYPE_K3S.equalsIgnoreCase(clusterType)) {

sal-service/src/main/java/org/ow2/proactive/sal/service/util/ClusterUtils.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ private static String getBashFilesContent(String fileName) throws IOException {
186186

187187
}
188188

189-
public static String createLabelNodesScript(List<Map<String, String>> nodeLabels, String clusterName) {
190-
String clusterType = System.getenv(CLUSTER_TYPE_ENV);
189+
public static String createLabelNodesScript(List<Map<String, String>> nodeLabels, String clusterName,
190+
String containerizationFlavor) {
191+
String clusterType = (containerizationFlavor != null) ? containerizationFlavor
192+
: System.getenv(CLUSTER_TYPE_ENV);
191193

192194
if (CLUSTER_TYPE_K3S.equalsIgnoreCase(clusterType)) {
193195
return createK3sLabelNodesScript(nodeLabels, clusterName);
@@ -229,8 +231,10 @@ public static String createK8sLabelNodesScript(List<Map<String, String>> nodeLab
229231
return script.toString();
230232
}
231233

232-
public static String createDeployApplicationScript(ClusterApplication application) throws IOException {
233-
String clusterType = System.getenv(CLUSTER_TYPE_ENV); // Get cluster type from env variable
234+
public static String createDeployApplication(ClusterApplication application, String containerizationFlavor)
235+
throws IOException {
236+
String clusterType = (containerizationFlavor != null) ? containerizationFlavor
237+
: System.getenv(CLUSTER_TYPE_ENV);
234238
return createDeployApplicationScript(application, clusterType);
235239
}
236240

0 commit comments

Comments
 (0)