Last active
July 15, 2022 02:36
-
-
Save zoidyzoidzoid/6af8c80cc5b706e2adcf25df3dc2f7e1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env bash | |
set -e | |
for full_name in $(kubectl get all -o name | grep -vE replicaset\|pod); do | |
name="$(echo "$full_name" | sed -e "s/.*\///g")"; | |
type="$(echo "$full_name" | sed -e "s/\/.*//g")"; | |
case "${type}" in | |
deployment*) | |
type=Deployment | |
;; | |
"service") | |
type=Service | |
;; | |
"configmap") | |
type=ConfigMap | |
;; | |
"job.batch") | |
type=Job | |
;; | |
"cronjob.batch") | |
type=CronJob | |
;; | |
esac | |
filename="${name}.${type}.yaml" | |
echo "Exporting ${filename}" | |
kubectl get $full_name --export -o json --export | jq 'del(.metadata.selfLink)' | jy > "${filename}" | |
done |
This file contains hidden or 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
# coding: utf-8 | |
import json | |
from subprocess import check_output | |
import ruamel.yaml as yaml | |
def trim_labels(data): | |
for k, v in data.get('labels', {}).copy().items(): | |
if k not in ('app', 'k8s-app'): | |
data['labels'].pop(k, None) | |
if data['labels'] == {}: | |
data.pop('labels') | |
def main(): | |
def my_represent_none(self, data): | |
return self.represent_scalar(u'tag:yaml.org,2002:null', u'null') | |
yaml.RoundTripRepresenter.add_representer(type(None), my_represent_none) | |
output = check_output('kubectl get all -o json'.split(' ')) | |
resources = json.loads(output) | |
for resource in resources['items']: | |
if resource['metadata'].get('ownerReferences', None): | |
continue | |
fn = '{}.{}.yaml'.format(resource['metadata']['name'], resource['kind']) | |
resource['metadata'].pop('annotations', None) | |
trim_labels(resource['metadata']) | |
trim_labels(resource['spec'].get('template', {}).get('metadata', {})) | |
trim_labels(resource['spec'].get('jobTemplate', {}).get('metadata', {})) | |
trim_labels(resource['spec'].get('jobTemplate', {}).get('spec', {}).get('template', {}).get('metadata', {})) | |
resource['metadata']['creationTimestamp'] = None | |
resource['metadata'].pop('generation', None) | |
resource['metadata'].pop('namespace', None) | |
resource['metadata'].pop('resourceVersion', None) | |
resource['metadata'].pop('selfLink', None) | |
resource['metadata'].pop('uid', None) | |
resource['status'] = {} | |
with open(fn, 'w') as f: | |
print('Exporting {}'.format(fn)) | |
f.write(yaml.round_trip_dump(resource)) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or 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
#!/usr/bin/env python3 | |
# coding: utf-8 | |
import json | |
import sys | |
from subprocess import check_output | |
def trim_labels(data): | |
for k, v in data.get('labels', {}).copy().items(): | |
if k not in ('app', 'k8s-app'): | |
data['labels'].pop(k, None) | |
if data['labels'] == {}: | |
data.pop('labels') | |
def main(): | |
args = sys.argv[1:] | |
if not args: | |
print('No arguments') | |
sys.exit(1) | |
output = check_output('kubectl get {} -o json'.format(' '.join(sys.argv[1:])).split(' ')) | |
resources = json.loads(output) | |
for resource in resources['items']: | |
if resource['metadata'].get('ownerReferences', None): | |
continue | |
resource['metadata'].pop('annotations', None) | |
trim_labels(resource['metadata']) | |
trim_labels(resource['spec'].get('template', {}).get('metadata', {})) | |
trim_labels(resource['spec'].get('jobTemplate', {}).get('metadata', {})) | |
trim_labels(resource['spec'].get('jobTemplate', {}).get('spec', {}).get('template', {}).get('metadata', {})) | |
resource['metadata']['creationTimestamp'] = None | |
resource['metadata'].pop('generation', None) | |
resource['metadata'].pop('namespace', None) | |
resource['metadata'].pop('resourceVersion', None) | |
resource['metadata'].pop('selfLink', None) | |
resource['metadata'].pop('uid', None) | |
resource['status'] = {} | |
print(json.dumps(resources, indent=2, separators=(',', ': '))) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or 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
#!/usr/bin/env python3 | |
# coding: utf-8 | |
import sys | |
import ruamel.yaml as yaml | |
def split_yaml_file(multiple_document_file): | |
with open(multiple_document_file) as f: | |
docs = list(yaml.round_trip_load_all(f)) | |
for doc in docs: | |
fn = '{}-{}.yaml'.format(doc['metadata']['name'].split('.')[0], doc['kind']).lower().replace('customresourcedefinition', 'crd') | |
print('Writing {}'.format(fn)) | |
with open(fn, 'w') as f: | |
f.write(yaml.round_trip_dump(file)) | |
if __name__ == '__main__': | |
if len(sys.argv) <= 1: | |
print( | |
"Specify file name for file to split\n" | |
"e.g. ./split_yaml_resources.py calico.yaml") | |
sys.exit(1) | |
split_yaml_file(sys.argv[1]) |
What about removing nodePort
?
@MosheM123 That should be easy enough. I'm planning on moving this to a repo, so folks can PR it.
@MosheM123 That should be easy enough. I'm planning on moving this to a repo, so folks can PR it.
aight, let me know, I'd love to contribute.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the suggestion, @Moulick!
So I originally used an export.sh script, which I've added to this diff now. Sometimes I fiddle with stuff and then wanna regenerate the Kuebrnetes manifests since I've
kubectl edit
ed them to get them into a working state, or updated them withkubectl set
.export_resources.py
matches the use ofexport.sh
, where you call it with no arguments and then it exports all your resources of all resource types/kinds to YAML files from your current kubectl context.kubectl-export
is a bit different where it does a single resource. One of the reasons is so I didn't need PyYAML or any other non standard library dependencies for it.I generally use it in a similar way to
export_resources.py
though, except for a single deployment or service, e.g.:These rely on @sourcegraph's useful
jy
tool that converts JSON to YAML, but for an example that doesn't require something like that: