Skip to content

Instantly share code, notes, and snippets.

@misberner
Created July 2, 2021 21:33
Show Gist options
  • Save misberner/e2a70338e26bec9382037c7b7c77cc15 to your computer and use it in GitHub Desktop.
Save misberner/e2a70338e26bec9382037c7b7c77cc15 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import yaml
def fix_descriptor_order(descriptors):
# Perform a stable sort (in Python, sorts are always stable) based on the path (excluding the last
# component). This ensures items at the same nesting level retain their relative order, and children
# will always come after their parents.
# We add a `.` in front for simplicity, to ensure every key contains at least one dot. This will have
# no impact on the output.
descriptors.sort(key=lambda d: f'.{d["path"]}'.rsplit('.', 1)[0])
def main():
csv_doc = yaml.safe_load(sys.stdin)
for crd in csv_doc['spec']['customresourcedefinitions']['owned']:
fix_descriptor_order(crd['specDescriptors'])
print(yaml.safe_dump(csv_doc))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment