Skip to content

Instantly share code, notes, and snippets.

@chmouel
Last active July 10, 2018 09:53
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 chmouel/1e558420f8c2fd1d1eaaa16a97e46771 to your computer and use it in GitHub Desktop.
Save chmouel/1e558420f8c2fd1d1eaaa16a97e46771 to your computer and use it in GitHub Desktop.
Filter processed openshift templates by types
#!/usr/bin/env python3
# Use python3
# Usage example :
# oc process openshift//cakephp-mysql-persistent -o json \
# |python ~/os-template-filter-type.py BuildConfig dc
# will get the BuildConfig and the DeploymentConfig (alias dc)
# You can specify a specific name for example is=stream will get only the
# ImageStream named stream
import json
import sys
# There is many more aliases, feel free to add them
aliases = {
("bc", "BuildConfig"),
("dc", "DeploymentConfig"),
("is", "ImageStream"),
("svc", "Service"),
("sa", "ServiceAccount")
}
wanted = sys.argv[1:]
if not wanted:
print("Gives the kind of object to get, i.e: bc, ImageStream")
sys.exit(1)
def getAlias(t):
for i in aliases:
if t in i:
return i[1]
processedJson = json.load(sys.stdin)
if not 'items' in processedJson:
print("Could not find items in json")
sys.exit(1)
items = processedJson['items']
newitems = []
for obj in items:
for w in wanted:
name = None
if '=' in w:
w, name = w.split("=")
resolved = getAlias(w)
if resolved and obj.get('kind') == resolved:
if name and obj.get('metadata') and \
obj.get('metadata').get('name') \
and obj['metadata']['name'] != name:
continue
newitems.append(obj)
processedJson['items'] = newitems
print(json.dumps(processedJson, indent=4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment