79233488

Date: 2024-11-28 10:41:34
Score: 2.5
Natty:
Report link

A better option is probably to push your content through helm template first and then parse it. (https://stackoverflow.com/a/63502299/5430476)

Yes, templating makes this task much easier.

However, in my case, I need to adjust YAML nodes across 1 to 200 Helm YAML files in different charts.

For instance, I want to add the following label to all YAML files:

metadata:
  # ...
  labels:
    helm.sh/chart: {{ $.Chart.Name }}-{{ $.Chart.Version | replace "+" "_" }}

Inspired by @ilias-antoniadis's answer, I made some improvements on his option 1:

  1. Handling multiple {{ }} placeholders in a single value, as in the example above.
  2. Processing Helm's flow control syntax (e.g., if or range), which doesn’t include a colon (:) on the same line.
#!/usr/bin/env python
import sys
import re
import yaml


class SimpleGoTemplateHandler:
    def __init__(self):
        self.left = "{{"
        self.right = "}}"
        self.left_placeholder = "__GO_TEMPLATE_LEFT__"
        self.right_placeholder = "__GO_TEMPLATE_RIGHT__"
        self.pipe = "|"
        self.pipe_placeholder = "__THE_PIPE__"
        self.control_counter = 0  # Counter for Helm flow control statements.
        self.control_pattern = re.compile("^(\s*)(\{\{)")  # e.g. "  {{- if .Values.foo -}}"

    def __gen_control_key(self):
        self.control_counter += 1   # prevent duplicated keys
        return f"__CONTROL_{self.control_counter}__"

    def escape(self, content: str) -> str:
        # handle helm control syntax.
        # e.g.
        #   from:
        #     {{- if .Values.foo -}}
        #     {{- end -}}
        #   to:
        #     __CONTROL_1__: "{{- if .Values.foo -}}"
        #     __CONTROL_2__: "{{- end -}}"
        replaced_lines = []
        for line in content.split("\n"):
            pattern_match = self.control_pattern.match(line)
            if pattern_match:
                line = f"{pattern_match.group(1)}{self.__gen_control_key()}: \"{line}\""
            replaced_lines.append(line)

        content = "\n".join(replaced_lines)

        # handle go templates in values
        content = content.replace(f"{self.left}", f"{self.left_placeholder}").replace(f"{self.right}", f"{self.right_placeholder}")

        # handle yaml multiline syntax
        content = content.replace(f"{self.pipe}", f"{self.pipe_placeholder}")
        return content

    def unescape(self, content: str) -> str:
        # undo handle helm control syntax.
        content = re.sub(r"__CONTROL_\d+__: ", "", content)

        # undo handle yaml multiline syntax
        content = content.replace(f"{self.pipe_placeholder}", self.pipe)

        # undo handle go template in values
        return content.replace(f"{self.left_placeholder}", self.left).replace(f"{self.right_placeholder}", self.right)


handler = SimpleGoTemplateHandler()

with open(sys.argv[1]) as f:
    content = f.read()
    yaml_data = yaml.safe_load_all(handler.escape(content))

    # do something with your data

print(handler.unescape(yaml.dump_all(yaml_data, sort_keys=False, width=1000)))

# OR
# with open(sys.argv[1], "w") as f:
#     f.write(handler.unescape(yaml.dump_all(yaml_data, sort_keys=False, width=1000)))

To handle these cases:

  1. First, process Helm's flow control syntax by appending a dummy key to each line for parsing.
  2. Replace all {{ and }} with placeholders to avoid issues with YAML parsers.
  3. Replace all | (pipe characters), since after using pyyaml.dump, these can be split into multiple lines.

Unfortunately, I’m not familiar with Go, so I’m not sure how to use text/template effectively in this case.

It would be great if there were a handy tool to handle scenarios like this.

Reasons:
  • Blacklisted phrase (0.5): I need
  • Blacklisted phrase (1): stackoverflow
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @ilias-antoniadis's
  • Low reputation (0.5):
Posted by: ktc