Skip to content

Instantly share code, notes, and snippets.

@knight42
Last active October 10, 2018 09:11
Show Gist options
  • Save knight42/985579443f43c90ed0a90543577a44a1 to your computer and use it in GitHub Desktop.
Save knight42/985579443f43c90ed0a90543577a44a1 to your computer and use it in GitHub Desktop.
remove unused fields in yaml of k8s object
#!/usr/bin/env python -O
# -*- coding: utf-8 -*-
import yaml
import sys
def delete_key(d, *keys):
for key in keys:
if d.get(key) is not None:
del d[key]
def main():
if sys.stdin.isatty():
with open(sys.argv[1]) as fin:
content = fin.read()
else:
content = sys.stdin.read()
data: dict = yaml.load(content)
delete_key(data, 'status')
if data.get('metadata'):
meta = data['metadata']
delete_key(meta, 'generation', 'resourceVersion', 'uid')
if meta.get('annotations'):
delete_key(meta['annotations'],
'kubectl.kubernetes.io/last-applied-configuration')
yaml.dump(data, sys.stdout, default_flow_style=False)
return 0
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment