mirror of
https://github.com/lucky-sideburn/kubeinvaders.git
synced 2026-08-23 21:46:21 +00:00
fixed programming mode
This commit is contained in:
@@ -22,45 +22,49 @@ def create_container(image, name, command, args):
|
||||
)
|
||||
|
||||
logging.info(
|
||||
f"Created container with name: {container.name}, "
|
||||
f"image: {container.image} and args: {container.args}"
|
||||
f"[PROGRAMMING_MODE] Created container with name: {container.name}, "
|
||||
f"[PROGRAMMING_MODE] Image: {container.image} and Args: {container.args}"
|
||||
)
|
||||
|
||||
return container
|
||||
|
||||
def create_pod_template(pod_name, additional_labels, container, exp_name):
|
||||
pod_labels = {"chaos-controller": "kubeinvaders", "experiment-name": exp_name}
|
||||
pod_labels = {"chaos-controller": "kubeinvaders", "experiment-name": exp_name, "chaos-codename": codename}
|
||||
pod_labels.update(additional_labels)
|
||||
pod_template = client.V1PodTemplateSpec(
|
||||
spec=client.V1PodSpec(restart_policy="Never", containers=[container]),
|
||||
metadata=client.V1ObjectMeta(name=pod_name, labels=pod_labels),
|
||||
)
|
||||
#logging.info(f"pod_template {pod_template}")
|
||||
return pod_template
|
||||
|
||||
def create_job(job_name, pod_template):
|
||||
metadata = client.V1ObjectMeta(name=job_name, labels={"chaos-controller": "kubeinvaders"})
|
||||
|
||||
metadata = client.V1ObjectMeta(name=job_name, labels={"chaos-controller": "kubeinvaders", "chaos-codename": codename})
|
||||
job = client.V1Job(
|
||||
api_version="batch/v1",
|
||||
kind="Job",
|
||||
metadata=metadata,
|
||||
spec=client.V1JobSpec(backoff_limit=0, template=pod_template),
|
||||
)
|
||||
#logger.info(job)
|
||||
return job
|
||||
|
||||
|
||||
# create logger
|
||||
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
|
||||
logging.info('Starting script for KubeInvaders programming mode')
|
||||
logging.info('[PROGRAMMING_MODE] Starting script...')
|
||||
|
||||
if os.path.exists(sys.argv[1]) == False:
|
||||
logging.info("[PROGRAMMING_MODE] Chaos program not found, please check the path...")
|
||||
exit(0)
|
||||
|
||||
with open(sys.argv[1], 'r') as stream:
|
||||
try:
|
||||
logging.info('[PROGRAMMING_MODE] Trying to parse chaos program...')
|
||||
parsed_yaml=yaml.safe_load(stream)
|
||||
logging.info(f"Parsed yaml => {parsed_yaml}")
|
||||
#logging.info(f"Parsed yaml => {parsed_yaml}")
|
||||
except yaml.YAMLError as exc:
|
||||
print(exc + " ")
|
||||
ret = f"[PROGRAMMING_MODE] Invalid YAML syntax, please fix choas program code..."
|
||||
logging.info(ret)
|
||||
print(ret)
|
||||
quit()
|
||||
|
||||
r = redis.Redis(unix_socket_path='/tmp/redis.sock')
|
||||
|
||||
@@ -79,11 +83,12 @@ batch_api = client.BatchV1Api()
|
||||
namespace = "kubeinvaders"
|
||||
k8s_regex = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
|
||||
prom_regex = "[a-zA-Z_:][a-zA-Z0-9_:]*"
|
||||
codename = parsed_yaml["chaos-codename"]
|
||||
|
||||
for job in parsed_yaml["k8s_jobs"]:
|
||||
logging.info(f"Found job {job}")
|
||||
if not re.fullmatch(k8s_regex, job):
|
||||
ret = f"Invalid name for k8s_jobs: {job}, please match Kubernetes name format '[a-z0-9]([-a-z0-9]*[a-z0-9])?'"
|
||||
ret = f"[PROGRAMMING_MODE] Invalid name for k8s_jobs: {job}, please match Kubernetes name format '[a-z0-9]([-a-z0-9]*[a-z0-9])?'"
|
||||
logging.info(ret)
|
||||
print(ret)
|
||||
quit()
|
||||
@@ -91,19 +96,16 @@ for job in parsed_yaml["k8s_jobs"]:
|
||||
for exp in parsed_yaml["experiments"]:
|
||||
|
||||
for _ in range(exp["loop"]):
|
||||
logging.info(f"Processing the experiment {exp}")
|
||||
logging.info(f"[PROGRAMMING_MODE] Processing the experiment {exp}")
|
||||
job_attrs = parsed_yaml["k8s_jobs"][exp["k8s_job"]]
|
||||
args = []
|
||||
for arg in job_attrs['args']:
|
||||
args.append(str(arg))
|
||||
|
||||
logging.info(f"args = {args}")
|
||||
logging.info(f"command = {job_attrs['command']}")
|
||||
logging.info(f"image = {job_attrs['image']}")
|
||||
logging.info(f"k8s_job = {exp['k8s_job']}")
|
||||
logging.info(f"[PROGRAMMING_MODE] args = {args}, command = {job_attrs['command']}, image = {job_attrs['image']}, k8s_job = {exp['k8s_job']}")
|
||||
|
||||
if not re.fullmatch(prom_regex, exp["name"]):
|
||||
ret = f"Invalid name for experiment: {exp['name']}, please match Prometheus metric name format '[a-zA-Z_:][a-zA-Z0-9_:]*'"
|
||||
ret = f"[PROGRAMMING_MODE] Invalid name for experiment: {exp['name']}, please match Prometheus metric name format '[a-zA-Z_:][a-zA-Z0-9_:]*'"
|
||||
logging.info(ret)
|
||||
print(ret)
|
||||
quit()
|
||||
@@ -130,14 +132,17 @@ for exp in parsed_yaml["experiments"]:
|
||||
|
||||
try:
|
||||
batch_api.create_namespaced_job('kubeinvaders', job_def)
|
||||
logging.info(f"[PROGRAMMING_MODE] Job {job_name} created successfully")
|
||||
|
||||
except ApiException as e:
|
||||
logging.info(e)
|
||||
logging.info(f"[PROGRAMMING_MODE] Error creating job: {e}")
|
||||
quit()
|
||||
|
||||
if 'additional-labels' in job_attrs and 'chaos-codename' in job_attrs['additional-labels']:
|
||||
codename = job_attrs['additional-labels']['chaos-codename']
|
||||
metric_job_name = job_name.replace("-","_");
|
||||
r.set(f"chaos_jobs_status:{codename}:{exp['name']}:{metric_job_name}", 0.0)
|
||||
|
||||
# if 'additional-labels' in job_attrs and 'chaos-codename' in job_attrs['additional-labels']:
|
||||
# logging.info(f"[PROGRAMMING_MODE] Setting Redis keys for chaos-codename: {job_attrs['additional-labels']['chaos-codename']}")
|
||||
# codename = job_attrs['additional-labels']['chaos-codename']
|
||||
metric_job_name = job_name.replace("-","_");
|
||||
r.set(f"chaos_jobs_status:{codename}:{exp['name']}:{metric_job_name}", 0.0)
|
||||
|
||||
if r.exists('chaos_node_jobs_total') == 1:
|
||||
r.incr('chaos_node_jobs_total')
|
||||
|
||||
Reference in New Issue
Block a user