Created
September 27, 2019 16:16
-
-
Save bjgill/dad40bbec2263f8a96b083b1b3fa6e18 to your computer and use it in GitHub Desktop.
Extract K8s CRDs for use by IntelliJ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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