Skip to content

Instantly share code, notes, and snippets.

@bjgill

bjgill/get_crds.py

Created Sep 27, 2019
Embed
What would you like to do?
Extract K8s CRDs for use by IntelliJ
"""
Extract Kubernetes Custom Resource Definitions (CRDs) from the raw output of `kubectl get crds -o yaml` for use by
IntelliJ Ultimate.
"""
# Crown Copyright (Government Digital Service)
# Released under the MIT license
import argparse
import os
import sys
import yaml
def name(crd):
return crd['spec']['names']['singular']
def deduplicate(crds):
"""
Use only the latest generation of each CRD
There can be multiple generations of a CRD in use. However, we assume that you're only likely to be developing for the latest.
"""
crds.sort(key=lambda c: c['metadata']['generation'])
return { name(crd): crd for crd in crds }.values()
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('destination', help='Directory to put the CRDs in')
args = parser.parse_args()
crds = yaml.safe_load(sys.stdin.read())['items']
# Kubernetes gives us a list of CRDs, but IntelliJ expects a set of files, each containing a single CRD.
for crd in deduplicate(crds):
with open(os.path.join(args.destination, "{}.yaml".format(name(crd))), 'w') as f:
f.write(yaml.dump(crd))
print("Finished creating CRDs. Now go to Preferences -> Languages & Frameworks -> Kubernetes and add all the CRDs in {}.".format(args.destination))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.