try new feature...

This commit is contained in:
Eugenio Marzo
2022-10-20 23:39:53 +02:00
parent bc4794dc35
commit 791c5792d4
8 changed files with 180 additions and 98 deletions
-40
View File
@@ -1,40 +0,0 @@
jobs:
cpu_attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- "--cpu"
- "4"
- "--io"
- "2"
- "--vm"
- "1"
- "--vm-bytes"
- "1G"
- "--timeout"
- "10s"
- "--metrics-brief"
mem_attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- "--vm 2"
- "-vm-bytes"
- "1G"
experiments:
- name: cpu_attack_exp
job: cpu_attack
loop: 5
after:
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
- name: mem_attack_exp
job: mem_attack
loop: 5
after:
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
+53
View File
@@ -0,0 +1,53 @@
jobs:
# cpu-attack:
# image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
# command: "stress-ng"
# args:
# - --cpu
# - 4
# - --io
# - 2
# - --vm
# - 1
# - --vm-bytes
# - 1G
# - --timeout
# - 10s
# - --metrics-brief
# mem-attack:
# image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
# command: "stress-ng"
# args:
# - --vm
# - 2
# - --vm-bytes
# - 1G
cpu-attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --help
mem-attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --help
experiments:
- name: cpu-attack-exp
job: cpu-attack
loop: 5
after:
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
- name: mem-attack-exp
job: mem-attack
loop: 5
after:
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
+26 -25
View File
@@ -6,7 +6,8 @@ from kubernetes import client, config
from kubernetes.client.rest import ApiException
import requests
from string import Template
import string
import random
def create_container(image, name, command, args):
container = client.V1Container(
@@ -27,13 +28,13 @@ def create_container(image, name, command, args):
def create_pod_template(pod_name, container):
pod_template = client.V1PodTemplateSpec(
spec=client.V1PodSpec(restart_policy="Never", containers=[container]),
metadata=client.V1ObjectMeta(name=pod_name, labels={"pod_name": pod_name}),
metadata=client.V1ObjectMeta(name=pod_name, labels={"pod_name": pod_name, "approle": "chaosnode"}),
)
return pod_template
def create_job(job_name, pod_template):
metadata = client.V1ObjectMeta(name=job_name, labels={"job_name": job_name})
metadata = client.V1ObjectMeta(name=job_name, labels={"job_name": job_name, "approle": "chaosnode"})
job = client.V1Job(
api_version="batch/v1",
@@ -41,7 +42,7 @@ def create_job(job_name, pod_template):
metadata=metadata,
spec=client.V1JobSpec(backoff_limit=0, template=pod_template),
)
logger.info(job)
#logger.info(job)
return job
@@ -49,7 +50,7 @@ def create_job(job_name, pod_template):
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
logging.info('Starting script for KubeInvaders programming mode')
with open("./example.yaml", 'r') as stream:
with open("./experiments.yaml", 'r') as stream:
try:
parsed_yaml=yaml.safe_load(stream)
logging.info(f"Parsed yaml => {parsed_yaml}")
@@ -57,8 +58,9 @@ with open("./example.yaml", 'r') as stream:
print(exc)
configuration = client.Configuration()
configuration.api_key = {"authorization": "Bearer ...."}
configuration.host = 'https://localhost:6443'
token = os.environ["TOKEN"]
configuration.api_key = {"authorization": f"Bearer {token}"}
configuration.host = os.environ["ENDPOINT"]
configuration.insecure_skip_tls_verify = True
configuration.verify_ssl = False
@@ -75,25 +77,24 @@ for job in parsed_yaml["jobs"]:
for exp in parsed_yaml["experiments"]:
logging.info(f"Processing the experiment {exp}")
job_attrs = parsed_yaml["jobs"][exp["job"]]
logging.info(f"args= {job_attrs['args']}")
args = []
for arg in job_attrs['args']:
args.append(str(arg))
logging.info(f"args = {args}")
logging.info(f"command = {job_attrs['command']}")
container = create_container(
image = job_attrs["image"],
name = exp["name"],
command = job_attrs["command"],
args = job_attrs["args"]
image = job_attrs['image'],
name = exp['name'],
command = [job_attrs['command']],
args = args
)
letters = string.ascii_lowercase
rand_suffix = ''.join(random.choice(letters) for i in range(5))
job_name = f"{exp['name']}-{rand_suffix}"
pod_template = create_pod_template(exp["name"], container)
logging.info(f"Creating job {exp['name']}")
job_def = create_job(exp["name"], pod_template)
batch_api.create_namespaced_job('kubeinvaders', job_def)
# try:
# api_response = api_instance.list_namespaced_pod_template(namespace, pretty=True, label_selector='foo=bar')
# logging.info(api_response)
# except ApiException as e:
# logging.info(e)
# if len(api_response.items) == 0:
# logging.info("Black box exporter is not installed. Installing it...")
logging.info(f"Creating job {job_name}")
job_def = create_job(job_name, pod_template)
batch_api.create_namespaced_job('kubeinvaders', job_def)