Last active
May 25, 2020 05:15
-
-
Save jtigger/f106f442598e2bb145b0ce62213aa3a7 to your computer and use it in GitHub Desktop.
Overlay utility functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load("@ytt:overlay", "overlay") | |
# `k8s_resource` yields matcher function that returns subset matching the Kubernetes resource parameters. | |
def k8s_resource(apiVersion, kind, name): | |
return overlay.subset({"apiVersion": apiVersion, "kind": kind, "metadata": {"name": name}}) | |
end | |
# `shape` yields matcher function that returns True when the structure of `right` matches that of `left` | |
# (ignoring leaves). | |
# exactly :: when `True`, array items in `left` must match exactly in the order they appear in `right` | |
# otherwise, for each array item in `right` there must be an item in `left` that matches its | |
# structure. | |
def shape(exactly=True): | |
return lambda index, left, right: matches_by_shape(index, left, right, exactly) | |
end | |
def matches_by_shape(index, left, right, exactly): | |
if type(right) != "yamlfragment": # at leaf | |
return True | |
end | |
if str(left) != str(right): # str(yamlfragment) => sub-type | |
return False | |
end | |
if str(right) == "yamlfragment(*yamlmeta.Map)": | |
for r_key in right: | |
if r_key in left: | |
if not matches_by_shape(0, left[r_key], right[r_key], exactly): | |
return False | |
end | |
else: | |
return False | |
end | |
end | |
else: # is integer-indexed | |
for r_index in range(len(right)): | |
if exactly: | |
if not matches_by_shape(r_index, left[r_index], right[r_index], exactly): | |
return False | |
end | |
else: | |
for l_index in range(len(left)): | |
if matches_by_shape(l_index, left[l_index], right[r_index], exactly): | |
return True | |
end | |
end | |
return False | |
end | |
end | |
end | |
return True | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment