Skip to content

Instantly share code, notes, and snippets.

@pivotaljohn
Last active March 2, 2023 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pivotaljohn/7e2bd99858884f9d1d0facab3f542f91 to your computer and use it in GitHub Desktop.
Save pivotaljohn/7e2bd99858884f9d1d0facab3f542f91 to your computer and use it in GitHub Desktop.
Example of how one could overlay a specific child node of some YAML with an ad-hoc set of values (e.g. a ConfigMap's payload).
#! Example of how one could overlay a specific child node of some YAML with an ad-hoc
#! set of values (e.g. a ConfigMap's payload).
#!
#! Features:
#! - custom/overlaying value can introduce new keys
#! - detects if the value of the named key is a string or YAML (and retains that format
#! after the edit.
#! - regardless of the format of the value of "key", parses as YAML and overlays on that.
#@ load("@ytt:overlay", "overlay")
#@ load("@ytt:data", "data")
#@ load("@ytt:yaml", "yaml")
#! build an overlay with a single top-level key, whose content either merges or appends
#@ def overlay_at(key, value):
#@overlay/match-child-defaults missing_ok=True
#@yaml/text-templated-strings
(@= key @): #@ value
#@ end
#! copy "yaml_map", except that the value at "key" is decoded (from a string to YAML)
#@ def decode_at(yaml_map, key):
#@ return overlay.apply(yaml_map, {key: None}, {key: yaml.decode(yaml_map[key])})
#@ end
#! copy "yaml_map", except that the value at "key" is encoded (from YAML to a string)
#@ def encode_at(yaml_map, key):
#@ return overlay.apply(yaml_map, {key: yaml.encode(yaml_map[key])})
#@ end
#! apply "right" as an overlay over "left" at the node named "key"
#@ def apply_overlay_at(left, key, right):
#@ value_is_string = type(left[key]) == "string"
#@ if value_is_string:
#@ left = decode_at(left, key)
#@ end
#@ overlayed = overlay.apply(left, right)
#@ if value_is_string:
#@ overlayed = encode_at(overlayed, key)
#@ end
#@ return overlayed
#@ end
#! produce an @overlay/replace function that overlays the target (i.e. "left") at the
#! child named "key" with the content of the annotated node in the overlay (i.e. "right")
#@ def merge_right_into_left_at(key):
#@ return lambda left, right: apply_overlay_at(left, key, overlay_at(key, right))
#@ end
#@ def tekton_pipeline_configmap():
apiVersion: v1
kind: ConfigMap
metadata:
name: feature-flags
namespace: tekton-pipelines
#@ end
#@overlay/match by=overlay.subset(tekton_pipeline_configmap())
#@overlay/replace via=merge_right_into_left_at("data")
--- #@ data.values["tekton-config"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment