2424@ Log4j2
2525public class ClusterUtils {
2626
27+ public static final String CLUSTER_TYPE_ENV = "CLUSTER_TYPE" ;
28+
29+ public static final String CLUSTER_TYPE_K3S = "k3s" ;
30+
31+ public static final String CLUSTER_TYPE_K8S = "k8s" ;
32+
2733 private static final String SCRIPTS_PATH = "/usr/local/tomcat/scripts/" ;
2834
2935 // TO be changed, the hardcoding of the ubuntu user is a bad practice.
@@ -33,6 +39,13 @@ public class ClusterUtils {
3339
3440 private static final String FILE_PATH = "/home/ubuntu/.profile" ;
3541
42+ // K3s-related commands
43+ private static final String CLI_K3s_USER_SELECTION = "$dau bash -c" ;
44+
45+ private static final String K3S_COMMANDS = "dau=\" sudo -H -E -u ubuntu\" \n " +
46+ "export KUBECONFIG=/etc/rancher/k3s/k3s.yaml\n " +
47+ "echo \" KUBECONFIG=${KUBECONFIG}\" | sudo tee -a /etc/environment\n " ;
48+
3649 public static Job createMasterNodeJob (String clusterName , ClusterNodeDefinition masterNode , PACloud cloud ,
3750 String envVars ) throws IOException {
3851 Job masterNodeJob = new Job ();
@@ -174,6 +187,33 @@ private static String getBashFilesContent(String fileName) throws IOException {
174187 }
175188
176189 public static String createLabelNodesScript (List <Map <String , String >> nodeLabels , String clusterName ) {
190+ String clusterType = System .getenv (CLUSTER_TYPE_ENV );
191+
192+ if (CLUSTER_TYPE_K3S .equalsIgnoreCase (clusterType )) {
193+ return createK3sLabelNodesScript (nodeLabels , clusterName );
194+ } else {
195+ return createK8sLabelNodesScript (nodeLabels , clusterName );
196+ }
197+ }
198+
199+ public static String createK3sLabelNodesScript (List <Map <String , String >> nodeLabels , String clusterName ) {
200+ StringBuilder script = new StringBuilder ();
201+ script .append (K3S_COMMANDS ).append ("\n " );
202+ for (Map <String , String > nodeLabelPair : nodeLabels ) {
203+ for (String nodeName : nodeLabelPair .keySet ()) {
204+ String label = nodeLabelPair .get (nodeName );
205+ script .append (String .format ("%s '%s %s-%s %s' \n " ,
206+ CLI_K3s_USER_SELECTION ,
207+ KUBE_LABEL_COMMAND ,
208+ nodeName .toLowerCase (),
209+ clusterName ,
210+ label ));
211+ }
212+ }
213+ return script .toString ();
214+ }
215+
216+ public static String createK8sLabelNodesScript (List <Map <String , String >> nodeLabels , String clusterName ) {
177217 StringBuilder script = new StringBuilder ();
178218 for (Map <String , String > nodeLabelPair : nodeLabels ) {
179219 for (String nodeName : nodeLabelPair .keySet ()) {
@@ -190,34 +230,46 @@ public static String createLabelNodesScript(List<Map<String, String>> nodeLabels
190230 }
191231
192232 public static String createDeployApplicationScript (ClusterApplication application ) throws IOException {
233+ String clusterType = System .getenv (CLUSTER_TYPE_ENV ); // Get cluster type from env variable
234+ return createDeployApplicationScript (application , clusterType );
235+ }
236+
237+ public static String createDeployApplicationScript (ClusterApplication application , String clusterType )
238+ throws IOException {
193239 String fileName = "/home/ubuntu/" + application .getAppName () + ".yaml" ;
194240 application .setYamlManager (ClusterApplication .PackageManagerEnum .getPackageManagerEnumByName (application .getPackageManager ()));
195- String appCommand = createAppCommand (application .getYamlManager (), fileName );
241+ String appCommand = createAppCommand (application .getYamlManager (), fileName , clusterType );
196242
197243 if (appCommand == null ) {
198244 LOGGER .error ("\" {}\" is not supported!" , application .getPackageManager ());
199245 throw new IOException ("yaml executor is not supported!" );
200246 }
201- BufferedReader bufReader = new BufferedReader ( new StringReader ( application . getAppFile ()));
247+
202248 StringBuilder script = new StringBuilder ();
203- String line = null ;
204- script .append ("sudo rm -f " + fileName + " || echo 'file was not found.' \n " );
249+ script .append ("sudo rm -f " ).append (fileName ).append (" || echo 'file was not found.' \n " );
205250
206- // start heredoc
251+ // Start heredoc
207252 script .append ("cat <<'EOF' >" ).append (fileName ).append ("\n " );
208- // embed the application YAML directly
209- script .append (application .getAppFile ());
210- // end heredoc
253+ script .append (application .getAppFile ()); // Embed the YAML file content
211254 script .append ("\n EOF\n " );
212255
213- script .append ("sudo chown ubuntu:ubuntu " + fileName + "\n " );
256+ script .append ("sudo chown ubuntu:ubuntu " ).append (fileName ).append ("\n " );
257+
258+ // Insert K3s-specific commands if needed
259+ if (CLUSTER_TYPE_K3S .equalsIgnoreCase (clusterType )) {
260+ script .append (K3S_COMMANDS ).append ("\n " );
261+ }
262+
214263 script .append (appCommand );
215264 return script .toString ();
216265 }
217266
218- private static String createAppCommand (ClusterApplication .PackageManagerEnum yamlManager , String fileName ) {
267+ private static String createAppCommand (ClusterApplication .PackageManagerEnum yamlManager , String fileName ,
268+ String clusterType ) {
219269 if (yamlManager != null ) {
220- return String .format ("%s '%s %s'" , CLI_USER_SELECTION , yamlManager .getCommand (), fileName );
270+ String cliSelection = CLUSTER_TYPE_K3S .equalsIgnoreCase (clusterType ) ? CLI_K3s_USER_SELECTION
271+ : CLI_USER_SELECTION ;
272+ return String .format ("%s '%s %s'" , cliSelection , yamlManager .getCommand (), fileName );
221273 } else {
222274 LOGGER .error ("The selected yaml executor is not supported!" );
223275 return null ;
0 commit comments