Skip to content

Updated chart templates to remove requirement of replacing underscore with hyphen in generated charts #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ To know more about LitmusChaos experiments [refer](https://litmuschaos.github.io
- Now fork and clone [chaos-charts](https://github.com/litmuschaos/chaos-charts), Enter into [workflow](https://github.com/litmuschaos/chaos-charts/tree/master/workflows) directory.
- Enter into `charts` directory to add charts which has been generated using sdk, for [reference](https://github.com/litmuschaos/chaos-charts/tree/master/charts/cassandra)
- Enter into `workflow` directory and add workflow manifests for [reference](https://github.com/litmuschaos/chaos-charts/tree/master/workflows/podtato-head)
- **Note**: Update `sample_category` and `sample_exec_chaos` to `sample-category` and `sample-exec-chaos` inside manifest in every chart name while updating chaos-charts. Example: `sample_category.package.yaml` to `sample-category.package.yaml`
- Connect your Git repository with chaos-center [ChaosHub](https://docs.litmuschaos.io/docs/concepts/chaoshub/)
- Workflow can be added as a predefined workflow in Github and users can test by following the given steps:
- Fork and clone [chaos-charts](https://github.com/litmuschaos/chaos-charts), now Enter into [workflow](https://github.com/litmuschaos/chaos-charts/tree/master/workflows) directory.
Expand Down
7 changes: 3 additions & 4 deletions contribute/developer-guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ scaffolded files consist of placeholders which can then be filled as desired.
$ ls -ltr sample_category/charts

total 24
-rw-rw-r-- 1 oumkale oumkale 144 Jul 7 18:48 sample_category.package.yaml
-rw-rw-r-- 1 oumkale oumkale 848 Jul 7 18:48 sample_category.category_chartserviceversion.yaml
-rw-rw-r-- 1 oumkale oumkale 989 Jul 7 18:48 sample_exec_chaos.experiment_chartserviceversion.yaml
-rw-rw-r-- 1 oumkale oumkale 144 Jul 7 18:48 sample-category.package.yaml
-rw-rw-r-- 1 oumkale oumkale 848 Jul 7 18:48 sample-category.chartserviceversion.yaml
-rw-rw-r-- 1 oumkale oumkale 989 Jul 7 18:48 sample-exec-chaos.chartserviceversion.yaml
-rw-rw-r-- 1 oumkale oumkale 1540 Jul 7 18:48 experiment.yaml
-rw-rw-r-- 1 oumkale oumkale 1224 Jul 7 18:48 rbac.yaml
-rw-rw-r-- 1 oumkale oumkale 731 Jul 7 18:48 engine.yaml
Expand Down Expand Up @@ -220,7 +220,6 @@ Follow the steps provided below to setup okteto & test the experiment execution.
This should take you to the bash prompt on the dev container into which the content of the litmus-python repo is loaded.

- Note :
- Replace `_` in chart manifest with `-` ex: sample_category to sample-category. Don't replace in directory name.
- Add packages routes for all the files which are generated from sdk in `setup.py` before creating image.
example :
```
Expand Down
81 changes: 57 additions & 24 deletions contribute/developer-guide/generate_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,66 @@ def generate_init(init_path):
init_path = init_path + '/' + '__init__.py'
open(init_path, mode='a').close()


# generate_csv creates the experiment chartserviceversion manifest
def generate_csv(csv_parent_path, csv_name, csv_config, litmus_env):

csv_filename = csv_parent_path + '/' + csv_name + '.' + 'experiment_chartserviceversion.yaml'
csv_filename = csv_parent_path + '/' + csv_name + '.chartserviceversion.yaml'
# Load Jinja2 template
template = litmus_env.get_template('./templates/experiment_chartserviceversion.tmpl')
template = litmus_env.get_template('./templates/experiment-chartserviceversion.tmpl')
output_from_parsed_template = template.render(csv_config)

with open(csv_filename, "w") as f:
f.write(output_from_parsed_template)


# generate_csv creates the category chartserviceversion manifest
def generate_csv_cat(csv_parent_path, csv_name, csv_config, litmus_env):

csv_filename = csv_parent_path + '/' + csv_name + '.' + 'category_chartserviceversion.yaml'
csv_filename = csv_parent_path + '/' + csv_name + '.chartserviceversion.yaml'

# Load Jinja2 template
template = litmus_env.get_template('./templates/category_chartserviceversion.tmpl')
template = litmus_env.get_template('./templates/category-chartserviceversion.tmpl')
output_from_parsed_template = template.render(csv_config)

with open(csv_filename, "w") as f:
f.write(output_from_parsed_template)


# generate_chart creates the experiment custom resource manifest
def generate_chart(chart_parent_path, chart_config, litmus_env):
chart_filename = chart_parent_path + '/' + 'experiment.yaml'

# Load Jinja2 template
template = litmus_env.get_template('./templates/experiment_custom_resource.tmpl')
template = litmus_env.get_template('./templates/experiment-custom-resource.tmpl')
output_from_parsed_template = template.render(chart_config)

with open(chart_filename, "w") as f:
f.write(output_from_parsed_template)


# generate_rbac creates the rbac for the experiment
def generate_rbac(chart_parent_path, chart_config, litmus_env):
rbac_filename = chart_parent_path + '/' + 'rbac.yaml'

# Load Jinja2 template
template = litmus_env.get_template('./templates/experiment_rbac.tmpl')
template = litmus_env.get_template('./templates/experiment-rbac.tmpl')
output_from_parsed_template = template.render(chart_config)

with open(rbac_filename, "w") as f:
f.write(output_from_parsed_template)


# generate_engine creates the chaos engine for the experiment
def generate_engine(chart_parent_path, chart_config, litmus_env):
engine_filename = chart_parent_path + '/' + 'engine.yaml'

# Load Jinja2 template
template = litmus_env.get_template('./templates/experiment_engine.tmpl')
template = litmus_env.get_template('./templates/experiment-engine.tmpl')
output_from_parsed_template = template.render(chart_config)

with open(engine_filename, "w") as f:
f.write(output_from_parsed_template)


# generate_chaoslib creates the chaosLib for the experiment
def generate_chaoslib(chaoslib_parent_path, chaoslib_name, chaoslib_config, litmus_env):
chaoslib_filename = chaoslib_parent_path + '/' + chaoslib_name + '.py'
Expand All @@ -71,12 +80,14 @@ def generate_chaoslib(chaoslib_parent_path, chaoslib_name, chaoslib_config, litm
# Load Jinja2 template
template = litmus_env.get_template('./templates/chaoslib.tmpl')
output_from_parsed_template = template.render(chaoslib_config)

with open(chaoslib_filename, "w") as f:
f.write(output_from_parsed_template)

# generate __init__.py file
generate_init(chaoslib_parent_path)


# generate_environment creates the environment for the experiment
def generate_environment(environment_parent_path, environment_config, litmus_env):
environment_filename = environment_parent_path + '/environment.py'
Expand All @@ -86,12 +97,14 @@ def generate_environment(environment_parent_path, environment_config, litmus_env
# Load Jinja2 template
template = litmus_env.get_template('./templates/environment.tmpl')
output_from_parsed_template = template.render(environment_config)

with open(environment_filename, "w") as f:
f.write(output_from_parsed_template)

# generate __init__.py file
generate_init(environment_parent_path)


# generate_types creates the types.py for the experiment
def generate_types(types_parent_path, types_config, litmus_env):
types_filename = types_parent_path + '/types.py'
Expand All @@ -101,51 +114,60 @@ def generate_types(types_parent_path, types_config, litmus_env):
# Load Jinja2 template
template = litmus_env.get_template('./templates/types.tmpl')
output_from_parsed_template = template.render(types_config)

with open(types_filename, "w") as f:
f.write(output_from_parsed_template)

# generate __init__.py file
generate_init(types_parent_path)


# generate_k8s_deployment creates the experiment kubernetes deployment manifest
def generate_k8s_deployment(k8s_parent_path, k8s_config, litmus_env):
k8s_filename = k8s_parent_path + '/' + 'test.yml'

# Load Jinja2 template
template = litmus_env.get_template('./templates/experiment_k8s_deployment.tmpl')
template = litmus_env.get_template('./templates/experiment-k8s-deployment.tmpl')
output_from_parsed_template = template.render(k8s_config)

with open(k8s_filename, "w") as f:
f.write(output_from_parsed_template)


# generate_experiment creates the expriment.py file
def generate_experiment(experiment_parent_path, experiment_name, experiment_config, litmus_env):
experiment_filename = experiment_parent_path + '/' + experiment_name + '.py'

# Load Jinja2 template
template = litmus_env.get_template('./templates/experiment.tmpl')
output_from_parsed_template = template.render(experiment_config)

with open(experiment_filename, "w+") as f:
f.write(output_from_parsed_template)

# generate __init__.py file
generate_init(experiment_parent_path)


# generate_package creates the package manifest
def generate_package(package_parent_path, config, package_name, litmus_env):
package_filename = package_parent_path + '/' + package_name + '.package.yaml'

# Load Jinja2 template
template = litmus_env.get_template('./templates/package.tmpl')
output_package = template.render(config)

with open(package_filename, "w") as f:
f.write(output_package)


# create_dir create new directory
def create_dir(path):
if os.path.isdir(path) != True:
os.makedirs(path)

def generate_icon(chart_parent_path, litmus_root, image_name, litmus_env):

def generate_icon(chart_parent_path, litmus_root, image_name):
src_dir = litmus_root + "/contribute/developer-guide/icons/"
dst_dir = chart_parent_path + '/' + "icons/"
create_dir(dst_dir)
Expand All @@ -154,6 +176,7 @@ def generate_icon(chart_parent_path, litmus_root, image_name, litmus_env):
shutil.copy(jpgfile, dst_dir)
os.rename(dst_dir + '/' + 'k8s.png', dst_dir + '/' + image_name +'.png')


def main():

parser = argparse.ArgumentParser()
Expand All @@ -178,17 +201,27 @@ def main():
# get name and category
entity_name = config['name']
category_name = config['category']

# Replace underscore (_) with hyphen (-)
# in entity_name_yaml and category_name_yaml
# for chart filenames and k8s objects
# like service-accounts, chart file names etc.
entity_name_k8s = entity_name.replace("_", "-")
category_name_k8s = category_name.replace("_", "-")

env = Environment(loader = FileSystemLoader('./'), trim_blocks=True, lstrip_blocks=True, autoescape=select_autoescape(['yaml']))

# store the litmus root from bootstrap folder
litmus_root = os.path.abspath(os.path.join("..", os.pardir))

# initilise directories
exp_root_dir = litmus_root + '/experiments/' + '/' + config['category']
exp_root_dir = litmus_root + '/experiments/' + '/' + category_name
create_dir(exp_root_dir)
experiment_root_dir = exp_root_dir + '/' + config['name']

experiment_root_dir = exp_root_dir + '/' + entity_name
create_dir(experiment_root_dir)

# Generate init files
generate_init(exp_root_dir)

# if generate_type is chart, only generate the chart(top)-level CSV & package manifests
Expand All @@ -201,21 +234,21 @@ def main():
if chartType == "category" or chartType == "all":

# generate icon for category
generate_icon(chart_dir, litmus_root, category_name, env)
generate_icon(chart_dir, litmus_root, category_name_k8s)

# generate category chartserviceversion
generate_csv_cat(chart_dir, category_name, config, env)
generate_csv_cat(chart_dir, category_name_k8s, config, env)

# generate package
generate_package(chart_dir, config, category_name, env)
generate_package(chart_dir, config, category_name_k8s, env)

if chartType == "experiment" or chartType == "all":

# generate icon for category
generate_icon(chart_dir, litmus_root, entity_name, env)
generate_icon(chart_dir, litmus_root, entity_name_k8s)

# generate experiment charts
generate_csv(chart_dir, entity_name, config, env)
generate_csv(chart_dir, entity_name_k8s, config, env)

# generate experiment-custom-resource
generate_chart(chart_dir, config, env)
Expand Down Expand Up @@ -245,13 +278,13 @@ def main():
generate_init(experiment_root_dir)

# initialise chaosLib, environment and types directory
chaoslib_dir = litmus_root + '/chaosLib/litmus/' + config['name'] + '/lib'
environment_dir = litmus_root + '/pkg/' + config['category'] + '/environment'
types_dir = litmus_root + '/pkg/' + config['category'] + '/types'
chaoslib_dir = litmus_root + '/chaosLib/litmus/' + entity_name + '/lib'
environment_dir = litmus_root + '/pkg/' + category_name + '/environment'
types_dir = litmus_root + '/pkg/' + category_name + '/types'

# create and generate __init__.py file in chaosLib experiment dir
create_dir(litmus_root + '/chaosLib/litmus/' + config['name'])
generate_init(litmus_root + '/chaosLib/litmus/' + config['name'])
create_dir(litmus_root + '/chaosLib/litmus/' + entity_name)
generate_init(litmus_root + '/chaosLib/litmus/' + entity_name)

# generate experiment.py
generate_experiment(experiment_dir, entity_name, config, env)
Expand All @@ -268,7 +301,7 @@ def main():
# generate k8s deployment
generate_k8s_deployment(test_dir, config, env)

generate_init(litmus_root + '/pkg/' + config['category'])
generate_init(litmus_root + '/pkg/' + category_name)

print("experiment created successfully")
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
apiVersion: litmuchaos.io/v1alpha1
kind: ChartServiceVersion
metadata:
name: {{ category }}
name: {{ category | replace("_", "-") }}
version: {{ version }}
annotations:
categories: {{ category }}
categories: {{ category | replace("_", "-") }}
spec:
displayName: {{ ccategory }} chaos
displayName: {{ category | replace("_", "-") }} chaos
categoryDescription: >
{{ description }}
experiments:
- {{ name }}
- {{ name | replace("_", "-") }}
keywords:
{%- for key in keywords %}
- "{{ key }}"
{%- endfor %}
maintainers:
{%- for i in maintainers %}
- name: {{- i.name }}
email: {{- i.email }}
- name: {{ i.name }}
email: {{ i.email }}
{%- endfor %}
minKubeVersion: {{ minkubernetesversion }}
provider:
name: {{ provider.name }}
links:
{%- for ref in references %}
- name: {{- ref.name }}
url: {{- ref.url }}
- name: {{ ref.name }}
url: {{ ref.url }}
{%- endfor %}
icon:
- url:
mediatype: ""
chaosexpcrdlink: https://github.com/raw/litmuschaos/chaos-charts/master/charts/{{ category }}/experiments.yaml
chaosexpcrdlink: https://github.com/raw/litmuschaos/chaos-charts/master/charts/{{ category | replace("_", "-") }}/experiments.yaml
2 changes: 1 addition & 1 deletion contribute/developer-guide/templates/environment.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pkg.maths.maths as maths

#GetENV fetches all the env variables from the runner pod
def GetENV(experimentDetails):
experimentDetails.ExperimentName = os.getenv("EXPERIMENT_NAME", "pod-delete")
experimentDetails.ExperimentName = os.getenv("EXPERIMENT_NAME", "{{ name | replace("_", "-") }}")
experimentDetails.ChaosNamespace = os.getenv("CHAOS_NAMESPACE", "litmus")
experimentDetails.EngineName = os.getenv("CHAOSENGINE", "")
experimentDetails.ChaosDuration = maths.atoi(os.getenv("TOTAL_CHAOS_DURATION", "30"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apiVersion: litmuchaos.io/v1alpha1
kind: ChartServiceVersion
metadata:
name: {{ name }}
name: {{ name | replace("_", "-") }}
version: {{ version }}
annotations:
categories: {{ category }}
categories: {{ category | replace("_", "-") }}
spec:
displayName: {{ name }}
displayName: {{ name | replace("_", "-") }}
categoryDescription: >
{{ description }}
keywords:
Expand All @@ -20,8 +20,8 @@ spec:
maturity: {{ maturity }}
maintainers:
{%- for i in maintainers %}
- name: {{- i.name }}
email: {{- i.email }}
- name: {{ i.name }}
email: {{ i.email }}
{%- endfor %}
minKubeVersion: {{ minkubernetesversion }}
provider:
Expand All @@ -31,10 +31,10 @@ spec:
app.kubernetes.io/version: latest
links:
{%- for ref in references %}
- name: {{- ref.name }}
url: {{- ref.url }}
- name: {{ ref.name }}
url: {{ ref.url }}
{%- endfor %}
icon:
- url:
mediatype: ""
chaosexpcrdlink: https://github.com/raw/litmuschaos/chaos-charts/master/charts/{{ category }}/{{ name }}/experiment.yaml
chaosexpcrdlink: https://github.com/raw/litmuschaos/chaos-charts/master/charts/{{ category | replace("_", "-") }}/{{ name | replace("_", "-") }}/experiment.yaml
Loading