88import java .io .IOException ;
99import java .io .Serializable ;
1010import java .util .*;
11+ import java .util .Map ;
1112
1213import org .apache .commons .lang3 .Validate ;
1314import org .ow2 .proactive .sal .model .*;
1819import org .springframework .beans .factory .annotation .Autowired ;
1920import org .springframework .stereotype .Service ;
2021
22+ import com .fasterxml .jackson .core .type .TypeReference ;
23+ import com .fasterxml .jackson .databind .ObjectMapper ;
24+
2125import lombok .extern .log4j .Log4j2 ;
2226
2327
2428@ Log4j2
2529@ Service ("ClusterService" )
2630public 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 );
0 commit comments