Skip to content

Commit

Permalink
latest fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
simonccc committed Aug 25, 2024
1 parent e743f9f commit d46cffe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 52 deletions.
56 changes: 30 additions & 26 deletions lib/kopsrox_k3s.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def k3s_check(vmid: int):
except:
exit(0)

#print('k3s-check:', get_node)

# if not found or Ready
if re.search('NotReady', get_node) or re.search('NotFound', get_node):
exit(0)
Expand All @@ -35,10 +33,10 @@ def k3s_check(vmid: int):
def k3s_check_mon(vmid):

# how long to wait for
wait = int(30)
wait: int = 30

# check count
count = int(0)
count: int = 0

# run k3s_check
while not k3s_check(vmid):
Expand All @@ -57,34 +55,31 @@ def k3s_check_mon(vmid):
# create a master/slave/worker
def k3s_init_node(vmid: int = masterid,nodetype = 'master'):


# nodetype error check
if nodetype not in ['master', 'slave', 'worker']:
kmsg('k3s_init-node', f'{nodetype} invalid nodetype', 'err')
exit(0)

# check node has internet
internet_check(vmid)

# defines
k3s_install_base = f'cat /k3s.sh | INSTALL_K3S_VERSION="{k3s_version}"'
k3s_install_flags = f' --disable servicelb --tls-san {network_ip}'
k3s_install_master = f'{k3s_install_base} sh -s - server --cluster-init {k3s_install_flags}'
k3s_install_worker = f'{k3s_install_base} K3S_URL="https://{network_ip}:6443" '

# nodetype error check
if nodetype not in ['master', 'slave', 'worker']:
kmsg('k3s_init-node', f'{nodetype} invalid nodetye', 'err')
exit(0)

# check status of node
try:
if k3s_check(vmid):
print(vmid, 'DEBUG ok')
k3s_check(vmid)
except:
kmsg(f'k3s_{nodetype}-init', vmnames[vmid])
kmsg(f'k3s_{nodetype}-init', f'installing {k3s_version} on {vmnames[vmid]}')

# get existing token if it exists
token_fname = f'{cluster_name}.k3stoken'
if os.path.isfile(token_fname):
token = open(token_fname, "r").read()
master_cmd = f' --token {token}'
else:
token = ''
master_cmd = ''

# master
if nodetype == 'master':
Expand All @@ -101,15 +96,20 @@ def k3s_init_node(vmid: int = masterid,nodetype = 'master'):
if nodetype == 'worker':
init_cmd = f'{k3s_install_worker}{k3s_token_cmd} sh -s'

# stderr
init_cmd = init_cmd + ' 2>&1'

# run command
init_cmd_out = qaexec(vmid,(init_cmd + ' 2>1'))
init_cmd_out = qaexec(vmid,init_cmd)

# wait until ready
try:
k3s_check_mon(vmid)
except:
kmsg(f'k3s_{nodetype}-init', f'{vmnames[vmid]} {init_cmd_out}', 'err')
exit()
foo = k3s_check_mon(vmid)
print(foo)
kmsg(f'k3s_{nodetype}-init', f'k3s_check_mon failure {vmid}:{vmnames[vmid]}', 'err')
exit(0)

# final steps for first master - kubevip, export kubeconfig and token
if nodetype == 'master':
Expand All @@ -118,7 +118,7 @@ def k3s_init_node(vmid: int = masterid,nodetype = 'master'):
export_k3s_token()

# remove a node
def k3s_remove_node(vmid):
def k3s_remove_node(vmid: int):
vmname = vmnames[vmid]
kmsg('k3s_remove-node', vmname)

Expand Down Expand Up @@ -168,7 +168,7 @@ def k3s_update_cluster():
while ( master_count <= 2 ):

# so eg 601 + 1 = 602 = m2
slave_masterid = int(masterid) + master_count
slave_masterid = masterid + master_count
slave_hostname = vmnames[slave_masterid]
kmsg('k3s_slave-check', slave_hostname)

Expand Down Expand Up @@ -200,7 +200,7 @@ def k3s_update_cluster():
if workers > 0:

# first id in the loop
worker_count = int(1)
worker_count: int = 1

# cycle through possible workers
while ( worker_count <= workers ):
Expand Down Expand Up @@ -291,17 +291,21 @@ def install_kube_vip():
kv_manifest = open('./lib/kubevip/kubevip.yaml', "r").read().replace('KOPSROX_IP', network_ip).strip()

# create the manifest
kv_install_manifest = qaexec(masterid, f'''cat <<EOF> /tmp/kubevip.yaml
qaexec(masterid, f'''cat <<EOF> /tmp/kubevip.yaml
{kv_manifest}
EOF
''')

# apply / replace the manifest
kubevip_install = kubectl('replace --force -f /tmp/kubevip.yaml')

if not re.search('daemonset.apps/kubevip', kubevip_install):
# check output
if not re.search('daemonset.apps/kubevip replaced', kubevip_install):
kmsg('k3s_kubevip', f'failed to install kube-vip\n{kubevip_install}', 'err')
exit(0)

kmsg('k6s_kubevip', f'created {network_ip} vip')
# install completed
kmsg('k3s_kubevip', f'{network_ip} vip active')

# return current vip master
def get_kube_vip_master():
Expand All @@ -313,7 +317,7 @@ def get_kube_vip_master():
kubevip_m = ''
return(kubevip_m)

# reload kubevip
def kubevip_reload():
reload = kubectl('rollout restart daemonset kubevip -n kube-system')
print(reload)
time.sleep(2)
7 changes: 3 additions & 4 deletions lib/verb_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# functions
from kopsrox_config import masterid,cluster_name,cluster_info,list_kopsrox_vm,cluster_id
from kopsrox_proxmox import clone,internet_check,qaexec
from kopsrox_proxmox import clone,qaexec
from kopsrox_k3s import k3s_update_cluster,k3s_rm_cluster,k3s_init_node,export_k3s_token
from kopsrox_kmsg import kmsg

Expand All @@ -21,15 +21,14 @@

# update current cluster
if cmd == 'update':
internet_check(masterid)
k3s_update_cluster()

# create new cluster / master server
if cmd == 'create':

# if masterid not found running
if not masterid in list_kopsrox_vm():
kmsg(kname,f'creating {cluster_name} cluster id {cluster_id}', 'sys')
kmsg(kname,f'id:{cluster_id} name:{cluster_name}', 'sys')
clone(masterid)

# install k3s on master
Expand All @@ -40,5 +39,5 @@

# destroy the cluster
if cmd == 'destroy':
kmsg(kname, f'!! destroying {cluster_name} !!', 'sys')
kmsg(kname, f'{cluster_name}', 'err')
k3s_rm_cluster()
37 changes: 15 additions & 22 deletions lib/verb_image.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3

# functions
from kopsrox_config import prox, local_os_process, image_info, cloud_image_desc, kopsrox_img
from kopsrox_config import prox, local_os_process, image_info, cloud_image_desc, kopsrox_img, k3s_version

# variables
from kopsrox_config import node, storage, cluster_id, cloud_image_url, cluster_name, cloudinitsshkey, cloudinituser, cloudinitpass
Expand All @@ -17,14 +17,15 @@

# define command
cmd = sys.argv[2]
kname = 'image_'+cmd
kname = 'image_'

# create image
if cmd == 'create':
kmsg(f'{kname}create', f'{cluster_name}-i1 base image', 'sys')

# get image name from url
cloud_image = cloud_image_url.split('/')[-1]
kmsg(kname, f'[{cluster_name}] creating new image based on {cloud_image}', 'sys')
kmsg(f'{kname}download', f'{cloud_image_url}')

# check img can be downloaded
try:
Expand All @@ -34,34 +35,25 @@
kmsg(kname, f'unable to download {cloud_image_url}', 'err')
exit(0)

# patch image
kmsg(f'{kname}', 'installing qemu-guest-agent')
# script to install disable selinux on Rocky
virtc_script = f'''\
curl -v https://get.k3s.io > /k3s.sh
cat /k3s.sh | INSTALL_K3S_SKIP_ENABLE=true INSTALL_K3S_SKIP_SELINUX_RPM=true INSTALL_K3S_VERSION={k3s_version} sh -s - server --cluster-init > /k3s-install.log 2>&1
# script to install qemu-guest-agent on multiple os's disable selinux on Rocky
install_qga = '''
curl -sfL https://get.k3s.io > /k3s.sh
export INSTALL_K3S_SKIP_ENABLE=true
export INSTALL_K3S_SKIP_SELINUX_RPM=true
bash /k3s.sh
if [ ! -f /usr/bin/qemu-ga ]
then
if [ -f /bin/yum ]
then
yum install -y qemu-guest-agent
else
apt update && apt install qemu-guest-agent -y
fi
fi
if [ -f /etc/selinux/config ]
then
sed -i s/enforcing/disabled/g /etc/selinux/config
fi
if [ -f /etc/sysconfig/qemu-ga ]
then
cp /dev/null /etc/sysconfig/qemu-ga
fi'''
patch_cmd = f'sudo virt-customize -a {cloud_image} --run-command "{install_qga}"'
patch_cmd = f'sudo virt-customize --smp 2 -m 2048 -a {cloud_image} --install qemu-guest-agent --run-command "{virtc_script}"'
kmsg('image_virt-customize', 'configuring image')
patch_out = local_os_process(patch_cmd)
print(patch_out)

# destroy template if it exists
try:
Expand All @@ -85,13 +77,14 @@
agent = ('enabled=true'),
hotplug = 0,
ciupgrade = 0,
description = cloud_image,
description = f'{cluster_name} template\n{cloud_image}\n{k3s_version}',
ciuser = cloudinituser,
cipassword = cloudinitpass,
sshkeys = cloudinitsshkey,
))

# shell to import disk
# import-from requires the full path os.getcwd required here
import_cmd = f'\
sudo qm set {cluster_id} --scsi0 {storage}:0,import-from={os.getcwd()}/{cloud_image},iothread=true,aio=native,discard=on,ssd=1 ; \
mv {cloud_image} {cloud_image}.patched'
Expand Down

0 comments on commit d46cffe

Please sign in to comment.