Skip to content

Commit 9cedd76

Browse files
k3s introduction (#127)
* labelNodes k3s * application deployment update * wait for master update * Update docker-compose env var * documentation and deployment update
1 parent 17fdd42 commit 9cedd76

File tree

6 files changed

+128
-13
lines changed

6 files changed

+128
-13
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ docker pull activeeon/sal:dev
9898

9999
* Open [docker-compose.yaml](https://github.com/ow2-proactive/scheduling-abstraction-layer/blob/master/docker/docker-compose.yaml)
100100

101+
* Specify which version of the Kubernetes cluster you will deploy. Default scripts set for deployment are for k8s.
102+
103+
104+
```bash
105+
CLUSTER_TYPE: "k8s" # (defult) or "k3s"
106+
```
107+
101108
* Setup connection to the ProActive scheduler
102109

103110
```bash

docker/docker-compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +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"
4445
#Set up connection to ProActive server (PWS)
4546
PWS_URL: <CHANGE_ME>
4647
PWS_USERNAME: <CHANGE_ME>

docker/kubernetes/sal.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ spec:
6464
env:
6565
- name: MYSQL_DATABASE
6666
value: proactive
67+
- name: CLUSTER_TYPE
68+
value: "k8s" # Change to "k3s" when needed
6769
- name: PROPERTIES_FILENAME
6870
value: sal
6971
- name: PWS_URL

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.ow2.proactive.sal.service.nc.WhiteListedInstanceTypesUtils;
1818
import org.ow2.proactive.sal.service.service.application.PAFactory;
1919
import org.ow2.proactive.sal.service.util.ByonUtils;
20+
import org.ow2.proactive.sal.service.util.ClusterUtils;
2021
import org.ow2.proactive.sal.service.util.Utils;
2122
import org.ow2.proactive.scheduler.common.task.ScriptTask;
2223
import org.ow2.proactive.scheduler.common.task.TaskVariable;
@@ -78,6 +79,8 @@ public class TaskBuilder {
7879

7980
private static final String WAIT_FOR_MASTER_SCRIPT = "wait_for_master.groovy";
8081

82+
private static final String WAIT_FOR_MASTER_K3S_SCRIPT = "wait_for_master_k3s.groovy";
83+
8184
private static final String SET_TOKEN_SCRIPT = "set_token.groovy";
8285

8386
private static final String DELETE_NODE_SCRIPT = "delete_node.groovy";
@@ -899,9 +902,18 @@ private Map<String, TaskVariable> createVariablesMapForSynchronizationChannels(J
899902
}
900903

901904
private ScriptTask createWaitForMasterTask(String masterNodeToken) {
902-
ScriptTask waitForMasterTask = PAFactory.createGroovyScriptTaskFromFile("wait_for_master",
903-
WAIT_FOR_MASTER_SCRIPT);
905+
String clusterType = System.getenv(ClusterUtils.CLUSTER_TYPE_ENV);
906+
String waitForMasterScript;
907+
908+
if (ClusterUtils.CLUSTER_TYPE_K3S.equalsIgnoreCase(clusterType)) {
909+
waitForMasterScript = WAIT_FOR_MASTER_K3S_SCRIPT;
910+
} else {
911+
waitForMasterScript = WAIT_FOR_MASTER_SCRIPT;
912+
}
913+
914+
ScriptTask waitForMasterTask = PAFactory.createGroovyScriptTaskFromFile("wait_for_master", waitForMasterScript);
904915
waitForMasterTask.addGenericInformation("NODE_ACCESS_TOKEN", masterNodeToken);
916+
905917
return waitForMasterTask;
906918
}
907919

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

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
@Log4j2
2525
public 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("\nEOF\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;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import groovy.transform.Synchronized
2+
3+
// Define a function to retrieve the IP from a server
4+
def retrieveIPFromServer(server) {
5+
try {
6+
def url = "http://${server}"
7+
def response = new URL(url).text
8+
return response.trim()
9+
} catch (Exception e) {
10+
println "Failed to retrieve IP from ${server}: ${e.message}"
11+
return null
12+
}
13+
}
14+
15+
// Define a function to check the IP from multiple servers
16+
@Synchronized
17+
def checkIPFromServers() {
18+
def servers = [
19+
"checkip.amazonaws.com",
20+
"api.ipify.org",
21+
"ifconfig.me",
22+
"ipinfo.io/ip",
23+
"icanhazip.com",
24+
"ident.me",
25+
"myip.dnsomatic.com"
26+
];
27+
for (def server : servers) {
28+
def ip = retrieveIPFromServer(server)
29+
if (ip) {
30+
println "Address was retrieved from ${server}"
31+
println "Public IP: ${ip}"
32+
variables.put("masterIp", ip)
33+
return ip
34+
}
35+
}
36+
37+
println "Unable to retrieve the public IP from any server."
38+
}
39+
40+
// Call the function to check the IP
41+
result = checkIPFromServers()

0 commit comments

Comments
 (0)