3

I have the below test.yaml file where I am trying to update the revision number using python but it mess up the values, any idea how to fix it?

spec:
  source:
     helm:
      valueFiles:
      - ../secrets/secrets.yaml
      values: |
        custom:
          address:
            domain: services-uat.com
            port: "80"
        revision: 1.0.1

Python code:

    version = "1.0.2"
    resource_file = yaml.safe_load(open('test.yaml'))
    source_exist = resource_file["spec"]["source"].get("helm")
    VERSION_MATCHSTR = r"\s*revision\s*:\s*(\d+\.\d+\.\d+)"

    revision_to_replace = re.search(VERSION_MATCHSTR, source_exist["values"]).group(
        1
    )
    logger.info(f"Revision {revision_to_replace} will be replaced with {version}")

    values_with_updated_version = source_exist["values"].replace(
        revision_to_replace, f"{version}"
    )
    resource_file["spec"]["source"]["helm"].update(
        {"values": values_with_updated_version}
    )
    with open('test.yaml', "w") as file:
        yaml.safe_dump(resource_file, file)

Output:

spec:
  source:
    helm:
      valueFiles:
      - ../secrets/secrets.yaml
      values: "custom:\n  address:\n    domain: services-uat.com\n\
        \    port: \"80\"\nrevision: 1.0.2\n"
user3222101
  • 1,270
  • 2
  • 24
  • 43

1 Answers1

0

In all seriousness, if you don't parse the values content as YAML, parsing the outer content only gives you a false sense of generality of your code. If you're using RegEx anyway, why not simply do it for the whole file:

import re

input = """
spec:
  source:
     helm:
      valueFiles:
      - ../secrets/secrets.yaml
      values: |
        custom:
          address:
            domain: services-uat.com
            port: "80"
        revision: 1.0.1
"""

version = "1.0.2"
print(re.sub(r'(revision\s*:\s*)\d+\.\d+\.\d+', r'\g<1>' + version, input))

And if this is not feasible because it could match something else it shouldn't, I would suggest putting those values into an own valueFile so that you can easily update it there.

Keeping YAML style is generally impossible (i.e. there are cases where you just can't), though you can exercise some control over it, see this question.

flyx
  • 35,506
  • 7
  • 89
  • 126