Simplest and Optimised way to read and edit YAMLs

12/22/2020

I've some YAML files which I wanted to apply to create Custom Resources. But before applying it I want to change the spec and ENVs of the YAML snippet. So what could be the best way to do this?

What I'm doing now is:

Let suppose this is the YAML

apiVersion: litmuschaos.io/v1alpha1
kind: ChaosEngine
metadata:
  name: nginx-chaos
  namespace: default
spec:
  appinfo:
    appns: 'default'
    applabel: 'app=nginx'
    appkind: 'deployment'
  # It can be true/false
  annotationCheck: 'false'
  # It can be active/stop
  engineState: 'active'
  chaosServiceAccount: pod-delete-sa
  monitoring: false
  # It can be delete/retain
  jobCleanUpPolicy: 'delete'
  experiments:
    - name: pod-delete
      spec:
        components:
          env:
            # set chaos duration (in sec) as desired
            - name: TOTAL_CHAOS_DURATION
              value: '30'

            # set chaos interval (in sec) as desired
            - name: CHAOS_INTERVAL
              value: '10'
              
            # pod failures without '--force' & default terminationGracePeriodSeconds
            - name: FORCE
              value: 'false'
  1. I Download this file from the raw link. (1 function for this)
  2. Replace a field like jobCleanUpPolicy: 'delete' to jobCleanUpPolicy: 'retain' (1 fn)
  3. Replace the next line when a match is found like value: 'false' to value: 'true' for FORCE ENV. (1 fn)
  4. Applying the final manifest created! (1 function)

Can this be optimized?

-- UDIT GAURAV
docker
environment-variables
go
kubernetes
yaml

2 Answers

12/22/2020

If you create a struct representing the resource (or, even better, can import the package which defines the CRD) you could take the yaml string, marshal it into the struct and then edit the fields directly as fields of a struct instead

-- Dom
Source: StackOverflow

4/16/2021

Hi You may use LitmusPortal to use this.Refer

-- Oum Kale
Source: StackOverflow