Reputation: 23
I am currently using the following code to load in a single-document template YAML file, changing it slightly, and generating (i.e., dumping) different new deployment files. The code looks like this:
import yaml
import copy
import ruamel.yaml
from ruamel.yaml.scalarstring import (DoubleQuotedScalarString as dq,
SingleQuotedScalarString as sq)
#As requested, below a summary of the functionality of the function define_exp_parameters
def define_exp_parameters(experiment_template, deployments):
deployment_list = []
for duration in range(30, 71, 1):
for cpu_cores in range(1, 4):
exp_doc[0]['spec']['annotationCheck'] = 'true'
deployment_list.append(copy.deepcopy(exp_doc))
return deployment_list
testyaml = ruamel.yaml.YAML()
with open(template_dir, 'r') as read_file:
deployments = testyaml.load_all(read_file)
deployments = list(deployments)
# define_exp_parameters is used to generate different YAML deployments, each of which are added to a list, which is returned. The list basically consists of x YAML deployment definitions which are later dumped in seperate files.
deployments = define_exp_parameters(experiment_template, deployments)
i = 1
for deployment in deployments:
with open(save_dir) as created_file:
testyaml.default_flow_style = False
testyaml.dump(deployment, created_file)
i += 1
The template looks like this:
apiVersion: litmuschaos.io/v1alpha1
kind: ChaosEngine
metadata:
name: teastore-chaos
namespace: default
spec:
annotationCheck: 'false'
engineState: active
appinfo:
appns: default
applabel: app=teastore
appkind: deployment
chaosServiceAccount: litmus-admin
components:
runner:
runnerAnnotations:
sidecar.istio.io/inject: 'false'
jobCleanUpPolicy: delete
experiments:
- name: pod-cpu-hog
This results in files with the following structure:
- apiVersion: litmuschaos.io/v1alpha1
kind: ChaosEngine
metadata:
name: teastore-chaos
namespace: default
spec:
annotationCheck: 'false'
engineState: active
appinfo:
appns: default
applabel: app=teastore
appkind: deployment
chaosServiceAccount: litmus-admin
components:
runner:
runnerAnnotations:
sidecar.istio.io/inject: 'false'
jobCleanUpPolicy: delete
experiments:
- name: pod-cpu-hog
However, my desired structure would be the following (i.e., same as template):
apiVersion: litmuschaos.io/v1alpha1
kind: ChaosEngine
metadata:
name: teastore-chaos
namespace: default
spec:
annotationCheck: 'false'
engineState: active
appinfo:
appns: default
applabel: app=teastore
appkind: deployment
chaosServiceAccount: litmus-admin
components:
runner:
runnerAnnotations:
sidecar.istio.io/inject: 'false'
jobCleanUpPolicy: delete
experiments:
- name: pod-cpu-hog
Basically, there shouldn't be a dash in the first line (which, for some reason, is automatically generated/added as it isn't in the original template file) and there shouldn't be an extra first indent in the whole structure (which I think comes along with the dash). I am using ruamel.yaml for dumping as it preserves the explicit single quotations in strings (using the imported package). I have tried looking for similar cases and the ruamel.yaml documentation but I wasn't able to find the reason/solution for this.
Upvotes: 2
Views: 990
Reputation: 76578
Without having the source of define_exp_parameters()
it is impossible to exactly describe what goes wrong. But before calling that deployments
is a list
containing a single element that is a dict
(with keys apiVersion
, kind
, etc.). And after that call deployments
is a list of single elements list (which elements is aformentioned dict). You iterate over the "outer" list and dump a single element list, which, in block style, gives you the -
which is the block sequence element indicator.
If you can't fix define_exp_parameters()
to return a list for which each element is a dict again, you can just dump the first element of deployment
:
testyaml.dump(deployment[0], created_file)
Upvotes: 1