【问题标题】:kubectl reformats "script" block after edit is applied应用编辑后 kubectl 重新格式化“脚本”块
【发布时间】:2021-11-11 23:05:39
【问题描述】:
- 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"

所以当我使用kubectl edit 推荐,然后尝试再编辑一次时 - 我明白了

 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)

如何解决?

【问题讨论】:

    标签: kubernetes kubectl


    【解决方案1】:

    script: 元素的两种格式化方式都是有效的 YAML,它们表示完全相同的多行字符串。换句话说,没有同时进行修改:K8s 摄取一个多行字符串(以script: | 开头)并输出一个多行字符串,编码为带有换行符的双引号单行字符串(script: "...\n..."

    我找不到自定义kubectl 的 YAML 输出的方法,因此您必须借助外部格式化程序来美化要编辑的 YAML 文件。online formatter 就是这样一个:(请参阅屏幕截图)@ 987654322@ 命令行格式化程序可以轻松构建,例如Python

    here 中提出的解决方案的基础上,也许最简单的解决方案是为编辑器创建一个包装外壳脚本,该脚本重新格式化要编辑的文件,然后继续编辑它。例如:

    #! /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 "$@"
    

    【讨论】:

      猜你喜欢
      • 2012-08-03
      • 2013-09-09
      • 1970-01-01
      • 2011-02-10
      • 2020-03-07
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多