Skip to content

Commit

Permalink
Merge pull request #7 from J-hoplin1/k3s-version-select
Browse files Browse the repository at this point in the history
enable k3s version select
  • Loading branch information
J-Hoplin authored Jul 3, 2023
2 parents 4ad98f6 + 91ac1ff commit f181ff7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#Multipass Ubuntu Version
MULTIPASS_INSTANCE=22.04

# K3S Version
K3S_VERSION=v1.26.6+k3s1


MASTER_DEFAULT_CPU=2
MASTER_DEFAULT_MEMORY=2048M
MASTER_DEFAULT_STORAGE=20G
Expand Down
10 changes: 10 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ cd K3S-Virtual-Cluster
pip3 install -r requirements.txt
```

3. Make your K3S Version of dotenv file. Default is `v1.26.6+k3s1`. Find available version [here](https://github.com/k3s-io/k3s/releases)

```
...
K3S_VERSION=v1.26.6+k3s1
...
```

---

## Commands
Expand Down
10 changes: 5 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ class Resolver(Utility):
def __init__(self):
pass

def cluster_init(self):
def cluster_init(self, version):
os.system('clear')

def NodeInit(name, cpu, memory, disk, tp, *args):
if tp == NodeType.MASTER:
subprocess.run(["bash", f"{Assets.SCRIPT_PATH}/nodeInit.sh", name,
cpu, memory, disk, tp], stdout=subprocess.PIPE)
cpu, memory, disk, tp, version], stdout=subprocess.PIPE)
elif tp == NodeType.WORKER:
token, ip = args
subprocess.run(["bash", f"{Assets.SCRIPT_PATH}/nodeInit.sh", name, cpu,
memory, disk, tp, token, ip], stdout=subprocess.PIPE)
memory, disk, tp, token, ip, version], stdout=subprocess.PIPE)
else:
raise exceptions.IllegalControlException("Invalid node type")

Expand Down Expand Up @@ -207,7 +207,7 @@ def terminate_cluster(self):
print(self.getNormalMessage("Complete to terminate cluster!"))

@InstanceNameAlreadyTakenChecker()
def add_node(self, name, **kwargs):
def add_node(self, name, version, **kwargs):
masterConfig: dict = self.readConfig(Assets.MASTER_CONFIG)
# Get worker node config
workerConfig: dict = self.readConfig(Assets.WORKER_CONFIG)
Expand Down Expand Up @@ -236,7 +236,7 @@ def add_node(self, name, **kwargs):
print(self.getNormalMessage(
f"Initiating worker node : {workerNodeName} ..."))
subprocess.run(["bash", f"{Assets.SCRIPT_PATH}/nodeInit.sh", workerNodeName, workerNodeCPU,
workerNodeMemory, workerNodeStorage, NodeType.WORKER, token, ip], stdout=subprocess.PIPE)
workerNodeMemory, workerNodeStorage, NodeType.WORKER, token, ip, version], stdout=subprocess.PIPE)
print(self.getNormalMessage(
f"Complete to build worker node : {workerNodeName} ..."))
workerConfig[name]["ip"] = subprocess.run(
Expand Down
5 changes: 3 additions & 2 deletions cluster.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import os
import dotenv
from app import Resolver
from exception import exceptions
Expand All @@ -25,13 +26,13 @@

try:
if argList.cluster == "init":
resolver.cluster_init()
resolver.cluster_init(os.environ["K3S_VERSION"])
elif argList.cluster == "terminate":
resolver.terminate_cluster()
elif argList.cluster == "add":
if not argList.name:
raise exceptions.RequiredCommandLineOptionLost('-n')
resolver.add_node(argList.name)
resolver.add_node(argList.name, os.environ["K3S_VERSION"])
elif argList.cluster == "shell":
if not argList.name:
raise exceptions.RequiredCommandLineOptionLost('-n')
Expand Down
19 changes: 19 additions & 0 deletions scripts/common/master-node-util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

function master-node-install(){
# Master node global installation script

sudo snap install helm --classic
sudo snap install jq

# Prometheus metrics for lens metrics collector
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo add stable https://charts.helm.sh/stable
helm repo update
helm install prometheus prometheus-community/kube-prometheus-stack
}


# Ignore stdin, stdout, stderr outputs to prevent frontend warning logs
master-node-install > /dev/null 2>&1
9 changes: 6 additions & 3 deletions scripts/nodeInit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ node_disk=$4
node_type=$5
master_token=$6
master_ip=$7
k3s_version=$8

common_path=$(dirname $(realpath $0))/common


if [ $node_type = "master" ]; then
echo "Here"
multipass launch --name $node_name --cpus $node_cpu --memory $node_memory --disk $node_disk
multipass exec $node_name -- /bin/bash -c "curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" sh -"
multipass exec $node_name -- /bin/bash -c "curl -sfL https://get.k3s.io | INSTALL_K3S_CHANNEL='$k3s_version' K3S_KUBECONFIG_MODE='644' INSTALL_K3S_EXEC='--disable traefik' sh -"
multipass transfer $common_path/master-node-util.sh $node_name:master-node-util.sh
multipass exec $node_name -- /bin/bash -c "chmod u+rwx ~/master-node-util.sh"
multipass exec $node_name -- /bin/bash -c "~/master-node-util.sh"
else
multipass launch --name $node_name --cpus $node_cpu --memory $node_memory --disk $node_disk
multipass exec $node_name -- /bin/bash -c "curl -sfL https://get.k3s.io | K3S_TOKEN=\"$master_token\" K3S_URL=https://$master_ip:6443 sh -"
multipass exec $node_name -- /bin/bash -c "curl -sfL https://get.k3s.io | INSTALL_K3S_CHANNEL='$k3s_version' K3S_TOKEN='$master_token' K3S_URL=https://$master_ip:6443 sh -"
fi

multipass transfer $common_path/util.sh $node_name:util.sh
Expand Down

0 comments on commit f181ff7

Please sign in to comment.