Skip to content

Commit

Permalink
finish create cluster and delete cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
hzeng21 committed Oct 9, 2023
1 parent c6ff460 commit fd336cd
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 2 deletions.
132 changes: 130 additions & 2 deletions acto/kubernetes_engine/minikube.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,133 @@
import logging
import os
import subprocess
import time
from typing import List

import kubernetes
import yaml

from acto.common import kubernetes_client, print_event
from acto.constant import CONST

from . import base


class MiniKube(base.KubernetesEngine):
pass
class Minikube(base.KubernetesEngine):

def __init__(
self, acto_namespace: int, posthooks: List[base.KubernetesEnginePostHookType] = None,):
self.config_path = os.path.join(CONST.CLUSTER_CONFIG_FOLDER, f'MINIKUBE-{acto_namespace}.yaml')
if posthooks is not None:
self.posthooks = posthooks

def configure_cluster(self, num_nodes: int, version: str):
'''Create config file for kind'''
pass

def get_context_name(self, cluster_name: str) -> str:
'''Returns the kubecontext based onthe cluster name
KIND always adds `kind` before the cluster name
'''
pass

def create_cluster(self, name: str, kubeconfig: str):
'''Use subprocess to create kind cluster
Args:
name: name of the kind cluster
config: path of the config file for cluster
version: k8s version
'''
print_event('Creating a Minikube cluster...')
cmd = ['minikube', 'start']

if name:
cmd.extend(['--profile', name])
else:
cmd.extend(['--profile', CONST.CLUSTER_NAME])

if kubeconfig:
logging.info(f'Kubeconfig: {kubeconfig}')
os.environ["KUBECONFIG"] = "/users/hzeng21/.kube/config2"
else:
raise Exception('Missing kubeconfig for kind create')

num_nodes = 3
cmd.extend(['--nodes', str(num_nodes)])

cmd.extend(['--kubernetes-version', "v1.27.3"])

p = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
while p.returncode != 0:
logging.error('Failed to create minikube cluster, retrying')
self.delete_cluster(name, kubeconfig)
time.sleep(5)
p = subprocess.run(cmd)

try:
kubernetes.config.load_kube_config(config_file=kubeconfig,
context=name)
apiclient = kubernetes_client(kubeconfig, name)
except Exception as e:
logging.debug("Incorrect kube config file:")
with open(kubeconfig) as f:
logging.debug(f.read())
raise e

def load_images(self, images_archive_path: str, name: str):
logging.info('Loading preload images')
cmd = ['kind', 'load', 'image-archive']
if images_archive_path == None:
logging.warning('No image to preload, we at least should have operator image')

if name != None:
cmd.extend(['--name', name])
else:
logging.error('Missing cluster name for kind load')

p = subprocess.run(cmd + [images_archive_path])
if p.returncode != 0:
logging.error('Failed to preload images archive')

def delete_cluster(self, name: str, kubeconfig: str):
cmd = ['minikube', 'delete']

if name:
cmd.extend(['--profile', name])
else:
logging.error('Missing cluster name for kind delete')

if kubeconfig:
logging.info(f'Kubeconfig: {kubeconfig}')
os.environ["KUBECONFIG"] = "/users/hzeng21/.kube/config2"
else:
raise Exception('Missing kubeconfig for kind create')

while subprocess.run(cmd).returncode != 0:
continue

def get_node_list(self, name: str):
'''Get agent containers list of a K3S cluster
Args:
1. Name of the cluster
'''
worker_name_template = '%s-worker'
control_plane_name_template = '%s-control-plane'

if name == None:
name = CONST.CLUSTER_NAME

res = super().get_node_list(worker_name_template % name) + \
super().get_node_list(control_plane_name_template % name)

if len(res) == 0:
# no worker node can be found
logging.critical(f"No node for cluster {name} can be found")
raise RuntimeError

return res





13 changes: 13 additions & 0 deletions acto/kubernetes_engine/test_minikube.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest
import kubernetes

from acto.kubernetes_engine import kind
from . import base
from . import minikube as minikube

def test_minikube_create():
cluster = minikube.Minikube(acto_namespace = 0)
# cluster.create_cluster("test1", "/users/hzeng21/.kube/config2")
cluster.delete_cluster("test1","/users/hzeng21/.kube/config2")


0 comments on commit fd336cd

Please sign in to comment.