Skip to content

Instantly share code, notes, and snippets.

@bjgill
Created September 27, 2019 16:16
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 bjgill/dad40bbec2263f8a96b083b1b3fa6e18 to your computer and use it in GitHub Desktop.
Save bjgill/dad40bbec2263f8a96b083b1b3fa6e18 to your computer and use it in GitHub Desktop.
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