kubectl reformats "script" block after edit is applied

9/17/2021
- name: getversion
      workingDir: $(resources.inputs.app.path)
      image: gittools/gitversion:5.6.10-alpine.3.12-x64-3.1
      script: |
        #!/usr/bin/env ash
        git tag
        # so we are making a hack, that if we try to merge hotfix branch into main we should 
        [ -e GitVersion-hotfix.yml ] && /tools/dotnet-gitversion | grep hotfix 1>/dev/null && (echo "hotfix"; rm -rf GitVersion.yml; cp GitVersion-hotfix.yml GitVersion.yml)

        # since this is a busybox we can't use bash regexp here
        if echo $(params.branch) | grep -E '^refs/heads/(main|release/[0-9.]+)$' > /dev/null;
        then
          local_tags=$(git tag --points-at HEAD)
          echo "remove local head tags"
          echo $local_tags
          git tag -d $local_tags
        fi
        
        /tools/dotnet-gitversion /updateprojectfiles
        git status
        chown -R "$(params.user_id):$(params.group_id)" "$(workspaces.source.path)"
        chown -R "$(params.user_id):$(params.group_id)" "$(resources.inputs.app.path)"
        echo "DONE"

so when I use kubectl edit commend, and then try to do one more time to edit it - I get this

 script: "#!/usr/bin/env ash\n# so we are making a hack, that if we try to merge
      hotfix branch into main we should \n[ -e GitVersion-hotfix.yml ] && /tools/dotnet-gitversion
      | grep hotfix 1>/dev/null && (echo \"hotfix\"; rm -rf GitVersion.yml; cp GitVersion-hotfix.yml
      GitVersion.yml)\n\n# since this is a busybox we can't use bash regexp here\nif
      echo $(params.branch) | grep -E '^refs/heads/(main|release/[0-9.]+)$' > /dev/null;\nthen\n
      \ local_tags=$(git tag --points-at HEAD)\n  echo \"remove local head tags\"\n
      \ echo $local_tags\n  git tag -d $local_tags\nfi\n\nsleep 3000\n\n/tools/dotnet-gitversion
      /updateprojectfiles\ngit status\nchown -R \"$(params.user_id):$(params.group_id)\"
      \"$(workspaces.source.path)\"\nchown -R \"$(params.user_id):$(params.group_id)\"
      \"$(resources.inputs.app.path)\"\necho \"DONE\"\n"
    workingDir: $(resources.inputs.app.path)

How to fix?

-- DmitrySemenov
kubectl
kubernetes

1 Answer

9/21/2021

Both ways of formatting the script: element are valid YAML, and they represent exactly the same multi-line string. In other words, there's no concurrent modification being done: K8s ingests a multi-line string (started by script: |) and outputs a multi-line string encoded as a double-quoted single-line string with newline characters (script: "...\n...")

I could find no way of customizing kubectl's YAML output, so you must resort to an external formatting program to prettify the YAML file to be edited.This is one such online formatter: (Refer to screenshot)enter image description here command-line formatters can be easily built e.g. with Python.

Building upon the solution presented in here, perhaps the simplest solution is to create a wrapper shell script for the editor that reformats the file to be edited then proceeds to edit it. For example:

#! /bin/sh

/usr/bin/python3 -c '
import yaml, sys; 
r = open(sys.argv[1])
data=yaml.safe_load(r.read()); 
r.close();
w = open(sys.argv[1], "w");
yaml.dump(data, stream=w, default_flow_style=False, default_style="|")
w.close()
' "$1"

exec /path/to/my/editor "$@"
-- Fariya Rahmat
Source: StackOverflow