Skip to content

Commit

Permalink
kubevirt: detect when running within kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
karmab committed Jan 7, 2025
1 parent 8ff56aa commit 1bd66bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
25 changes: 14 additions & 11 deletions kvirt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def __init__(self, client=None, debug=False, quiet=False, region=None, zone=None
if self.type == 'kubevirt':
kubeconfig_file = options.get('kubeconfig')
if kubeconfig_file is None:
error("Missing kubeconfig in the configuration. Leaving")
sys.exit(1)
if not os.path.exists('/var/run/secrets/kubernetes.io/serviceaccount'):
error("Missing kubeconfig in the configuration. Leaving")
sys.exit(1)
elif not os.path.exists(os.path.expanduser(kubeconfig_file)):
error("Kubeconfig file path doesn't exist. Leaving")
sys.exit(1)
Expand Down Expand Up @@ -2743,18 +2744,19 @@ def delete_kube(self, cluster, overrides={}):
elif vmclients:
deleteclients.update({cli: Kconfig(client=cli).k for cli in vmclients if cli != self.client})
if hypershift:
oc = which('kubectl') or which('oc')
kubeconfigmgmt = f"{clusterdir}/kubeconfig.mgmt"
if os.path.exists(f'{clusterdir}/bmcs.yml'):
call(f'KUBECONFIG={kubeconfigmgmt} oc delete -f {clusterdir}/bmcs.yml', shell=True)
call(f'KUBECONFIG={kubeconfigmgmt} oc delete -f {clusterdir}/autoapprovercron.yml', shell=True)
call(f'KUBECONFIG={kubeconfigmgmt} oc delete -f {clusterdir}/nodepools.yaml', shell=True)
call(f'KUBECONFIG={kubeconfigmgmt} oc delete -f {clusterdir}/hostedcluster.yaml', shell=True)
call(f'KUBECONFIG={kubeconfigmgmt} {oc} delete -f {clusterdir}/bmcs.yml', shell=True)
call(f'KUBECONFIG={kubeconfigmgmt} {oc} delete -f {clusterdir}/autoapprovercron.yml', shell=True)
call(f'KUBECONFIG={kubeconfigmgmt} {oc} delete -f {clusterdir}/nodepools.yaml', shell=True)
call(f'KUBECONFIG={kubeconfigmgmt} {oc} delete -f {clusterdir}/hostedcluster.yaml', shell=True)
if not assisted and ('baremetal_iso' in clusterdata or 'baremetal_hosts' in clusterdata):
call(f'KUBECONFIG={kubeconfigmgmt} oc -n default delete all -l app=httpd-kcli', shell=True)
call(f'KUBECONFIG={kubeconfigmgmt} oc -n default delete pvc httpd-kcli-pvc', shell=True)
ingress_ip = clusterdata.get('ingress_ip')
if self.type == 'kubevirt' and clusterdata.get('platform') is None and ingress_ip is None:
call(f'KUBECONFIG={kubeconfigmgmt} oc -n {k.namespace} delete route {cluster}-ingress', shell=True)
call(f'KUBECONFIG={kubeconfigmgmt} {oc} -n {k.namespace} delete route {cluster}-ingress', shell=True)
for hypervisor in deleteclients:
c = deleteclients[hypervisor]
for vm in sorted(c.list(), key=lambda x: x['name']):
Expand All @@ -2781,7 +2783,8 @@ def delete_kube(self, cluster, overrides={}):
if f"{cluster}-ingress" in k.list_services(k.namespace):
k.delete_service(f"{cluster}-ingress", k.namespace)
try:
call(f'oc delete -n {k.namespace} route {cluster}-ingress', shell=True)
oc = which('kubectl') or which('oc')
call(f'{oc} delete -n {k.namespace} route {cluster}-ingress', shell=True)
except:
pass
if self.type in ['aws', 'azure', 'gcp', 'ibm', 'hcloud'] and not gke and not eks and not aks:
Expand Down Expand Up @@ -2986,8 +2989,8 @@ def update_kube(self, cluster, _type, overrides={}, plan=None):
existing_workers = len([v for v in planvms if v.startswith(f'{cluster}-worker-')])
if data['ctlplanes'] != existing_ctlplanes or data['workers'] != existing_workers:
os.environ['KUBECONFIG'] = f"{clusterdir}/auth/kubeconfig"
binary = 'oc' if which('oc') is not None else 'kubectl'
nodescmd = f'{binary} get node -o name'
kubectl = which('kubectl') or which('oc')
nodescmd = f'{kubectl} get node -o name'
nodes = [n.strip().replace('node/', '') for n in os.popen(nodescmd).readlines()]
for vm in self.k.list():
vmname = vm['name']
Expand All @@ -2997,7 +3000,7 @@ def update_kube(self, cluster, _type, overrides={}, plan=None):
for node in nodes:
if node.split('.')[0] == vmname:
pprint(f"Deleting node {node} from your cluster")
call(f'{binary} delete node {node}', shell=True)
call(f'{kubectl} delete node {node}', shell=True)
break
self.k.delete(vmname)

Expand Down
5 changes: 3 additions & 2 deletions kvirt/providers/kubevirt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def __init__(self, kubeconfig_file, context=None, debug=False, namespace=None,
return
elif context is not None:
self.kubectl += f' --context {context}'
kubeconfig_file = os.path.expanduser(kubeconfig_file)
self.kubectl += f' --kubeconfig {kubeconfig_file}'
if kubeconfig_file is not None:
kubeconfig_file = os.path.expanduser(kubeconfig_file)
self.kubectl += f' --kubeconfig {kubeconfig_file}'
self.access_mode = access_mode
self.conn = 'OK'
self.debug = debug
Expand Down

0 comments on commit 1bd66bb

Please sign in to comment.