├── .github └── workflows │ ├── lint.yml │ ├── release.yml │ └── test.yml ├── .gitignore ├── .goreleaser.yml ├── LICENSE ├── README.md ├── TODO.md ├── cmd └── kubectl-split-yaml │ └── main.go ├── go.mod ├── go.sum └── internal ├── cmd └── splityaml.go ├── saveresources └── saveresources.go └── walkresources ├── testdata ├── ketall-list-resource │ ├── input.yaml │ ├── output0.yaml │ ├── output1.yaml │ ├── output2.yaml │ └── output3.yaml ├── list-resource │ ├── input.yaml │ ├── output0.yaml │ └── output1.yaml ├── multi-doc-resources │ ├── input.yaml │ ├── output0.yaml │ └── output1.yaml ├── recursive-list-resource │ ├── input.yaml │ ├── output0.yaml │ ├── output1.yaml │ ├── output2.yaml │ └── output3.yaml └── single-resource │ ├── input.yaml │ └── output0.yaml ├── walkresources.go └── walkresources_test.go /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | name: golangci-lint 2 | on: 3 | push: 4 | tags: 5 | - v* 6 | branches: 7 | - master 8 | pull_request: 9 | jobs: 10 | golangci: 11 | name: lint 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: golangci-lint 16 | uses: golangci/golangci-lint-action@v1 17 | with: 18 | # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. 19 | version: v1.26 20 | 21 | # Optional: working directory, useful for monorepos 22 | # working-directory: somedir 23 | 24 | # Optional: golangci-lint command line arguments. 25 | # args: --issues-exit-code=0 26 | 27 | # Optional: show only new issues if it's a pull request. The default value is `false`. 28 | # only-new-issues: true 29 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: goreleaser 2 | on: 3 | push: 4 | tags: 5 | - v* 6 | pull_request: 7 | jobs: 8 | goreleaser: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - 12 | name: Checkout 13 | uses: actions/checkout@v2 14 | with: 15 | fetch-depth: 0 16 | - 17 | name: Set up Go 18 | uses: actions/setup-go@v2 19 | with: 20 | go-version: 1.14 21 | - 22 | name: Run GoReleaser 23 | uses: goreleaser/goreleaser-action@v2 24 | with: 25 | version: latest 26 | args: release --rm-dist 27 | env: 28 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 29 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | on: 3 | push: 4 | pull_request: 5 | jobs: 6 | build: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | matrix: 10 | os: ['ubuntu-latest', 'macos-latest', 'windows-latest'] 11 | go: ['1.13'] 12 | name: Go ${{ matrix.go }} ${{ matrix.os }} 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Setup go 16 | uses: actions/setup-go@v1 17 | with: 18 | go-version: ${{ matrix.go }} 19 | - run: go test ./... 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /dist 2 | -------------------------------------------------------------------------------- /.goreleaser.yml: -------------------------------------------------------------------------------- 1 | project_name: kubectl-split-yaml 2 | before: 3 | hooks: 4 | - go mod download 5 | builds: 6 | - env: 7 | - CGO_ENABLED=0 8 | main: ./cmd/kubectl-split-yaml 9 | goos: 10 | - linux 11 | - darwin 12 | - windows 13 | goarch: 14 | - 386 15 | - amd64 16 | - arm 17 | - arm64 18 | checksum: 19 | name_template: 'checksums.txt' 20 | snapshot: 21 | name_template: "{{ .Tag }}-next" 22 | changelog: 23 | sort: asc 24 | filters: 25 | exclude: 26 | - '^docs:' 27 | - '^test:' 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kubectl-split-yaml 2 | 3 | A `kubectl` plugin to split Kubernetes YAML output into one file per resource. 4 | 5 | 6 | ## Example 7 | 8 | ```shell 9 | $ kubectl get all -o yaml | kubectl split-yaml -p resources 10 | resources/v1--Pod/default--nginx-86c57db685-4vnjc.yaml 11 | resources/v1--Service/default--nginx.yaml 12 | resources/apps_v1--Deployment/default--nginx.yaml 13 | ``` 14 | 15 | 16 | ## Usage 17 | 18 | `kubectl split-yaml [flags]`, where `[flags]` can contain: 19 | * `-f`: input file. If not given, or set to `-`, standard input is used. 20 | * `-p`: output directory. Defaults to `./split-yaml`. 21 | * `-t`: filename template: Defaults to `{{.apiVersion}}--{{.kind}}/{{.namespace}}--{{.name}}.yaml` 22 | 23 | 24 | ## Installation 25 | 26 | **Recommended**: Install [Krew](https://krew.sigs.k8s.io/) to manage kubectl 27 | plugins then run `kubectl krew update; kubectl krew install split-yaml` 28 | 29 | Otherwise, binaries are available from the [releases page](https://github.com/nathforge/kubectl-split-yaml/releases). 30 | 31 | 32 | ## Recommended projects 33 | 34 | * [ketall](https://github.com/corneliusweig/ketall): like `kubectl get all`, but gets *everything*. 35 | * [kubectl-neat](https://github.com/itaysk/kubectl-neat): removes clutter from Kubernetes resources; metadata, status, etc. 36 | 37 | 38 | ## License 39 | 40 | Apache 2.0. See [LICENSE](LICENSE). 41 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | * saveresources tests 2 | * [Automate krew releases](https://krew.sigs.k8s.io/docs/developer-guide/release/automating-updates/) 3 | -------------------------------------------------------------------------------- /cmd/kubectl-split-yaml/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/spf13/pflag" 7 | 8 | "github.com/nathforge/kubectl-split-yaml/internal/cmd" 9 | ) 10 | 11 | func main() { 12 | flags := pflag.NewFlagSet("kubectl-split-yaml", pflag.ExitOnError) 13 | pflag.CommandLine = flags 14 | 15 | root := cmd.NewCmdSplitYAML(cmd.IOStreams{ 16 | In: os.Stdin, 17 | Out: os.Stdout, 18 | ErrOut: os.Stderr, 19 | }) 20 | if err := root.Execute(); err != nil { 21 | os.Exit(1) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/nathforge/kubectl-split-yaml 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/go-yaml/yaml v2.1.0+incompatible 7 | github.com/kr/pretty v0.2.0 // indirect 8 | github.com/spf13/cobra v1.0.0 9 | github.com/spf13/pflag v1.0.5 10 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect 11 | gopkg.in/yaml.v2 v2.2.8 12 | ) 13 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 3 | github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= 4 | github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= 5 | github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= 6 | github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= 7 | github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= 8 | github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= 9 | github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= 10 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 11 | github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= 12 | github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= 13 | github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= 14 | github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= 15 | github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= 16 | github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= 17 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 18 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 19 | github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= 20 | github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= 21 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 22 | github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= 23 | github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= 24 | github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= 25 | github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= 26 | github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= 27 | github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= 28 | github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o= 29 | github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= 30 | github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= 31 | github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= 32 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 33 | github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= 34 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 35 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 36 | github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 37 | github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo= 38 | github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= 39 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 40 | github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= 41 | github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= 42 | github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= 43 | github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= 44 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 45 | github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= 46 | github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= 47 | github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= 48 | github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= 49 | github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= 50 | github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= 51 | github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 52 | github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= 53 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 54 | github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= 55 | github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= 56 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 57 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 58 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 59 | github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= 60 | github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= 61 | github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 62 | github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= 63 | github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= 64 | github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= 65 | github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= 66 | github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 67 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 68 | github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= 69 | github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= 70 | github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= 71 | github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 72 | github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= 73 | github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= 74 | github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= 75 | github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= 76 | github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= 77 | github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= 78 | github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 79 | github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 80 | github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= 81 | github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= 82 | github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= 83 | github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= 84 | github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= 85 | github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= 86 | github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= 87 | github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= 88 | github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= 89 | github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= 90 | github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 91 | github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= 92 | github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 93 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 94 | github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= 95 | github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= 96 | github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= 97 | github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= 98 | go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= 99 | go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= 100 | go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= 101 | go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= 102 | golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 103 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 104 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 105 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 106 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 107 | golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 108 | golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 109 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 110 | golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= 111 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 112 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 113 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 114 | golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 115 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 116 | golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 117 | golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 118 | golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 119 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 120 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 121 | golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= 122 | golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 123 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 124 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 125 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 126 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 127 | google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 128 | google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= 129 | gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= 130 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 131 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 132 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= 133 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 134 | gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= 135 | gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= 136 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 137 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 138 | gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= 139 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 140 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 141 | -------------------------------------------------------------------------------- /internal/cmd/splityaml.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "html/template" 6 | "io" 7 | "os" 8 | 9 | "github.com/nathforge/kubectl-split-yaml/internal/saveresources" 10 | "github.com/nathforge/kubectl-split-yaml/internal/walkresources" 11 | "github.com/spf13/cobra" 12 | "gopkg.in/yaml.v2" 13 | ) 14 | 15 | var ( 16 | splitYAMLExample = ` # save deployment resources 17 | kubectl get deploy -o yaml | %[1]s split-yaml -p deployments 18 | 19 | # split single file into multiple files 20 | %[1]s split-yaml -f bigfile.yaml -p smallerfiles` 21 | ) 22 | 23 | type IOStreams struct { 24 | In io.Reader 25 | Out io.Writer 26 | ErrOut io.Writer 27 | } 28 | 29 | type SplitYAMLOptions struct { 30 | ioStreams IOStreams 31 | inputFilename string 32 | outputPath string 33 | template string 34 | quiet bool 35 | } 36 | 37 | func NewSplitYAMLOptions(streams IOStreams) *SplitYAMLOptions { 38 | return &SplitYAMLOptions{ 39 | ioStreams: streams, 40 | inputFilename: "-", 41 | outputPath: "split-yaml", 42 | template: "{{.apiVersion}}--{{.kind}}/{{.namespace}}--{{.name}}.yaml", 43 | quiet: false, 44 | } 45 | } 46 | 47 | func NewCmdSplitYAML(streams IOStreams) *cobra.Command { 48 | o := NewSplitYAMLOptions(streams) 49 | 50 | cmd := &cobra.Command{ 51 | Use: "kubectl-split-yaml [flags]", 52 | Short: "Split Kubernetes YAML output into one file per resource", 53 | Example: fmt.Sprintf(splitYAMLExample, "kubectl"), 54 | SilenceUsage: true, 55 | Args: cobra.NoArgs, 56 | RunE: func(c *cobra.Command, args []string) error { 57 | if err := o.Validate(); err != nil { 58 | return err 59 | } 60 | if err := o.Run(); err != nil { 61 | // Is this a YAML decoding error? 62 | // Add a hint - user may have forgotten to pass `-o yaml` to 63 | // `kubectl get` 64 | if _, ok := err.(*yaml.TypeError); ok { 65 | return fmt.Errorf( 66 | "%w\n\n"+ 67 | "Is your input in YAML format?\n"+ 68 | "`kubectl get` can output YAML with the `-o yaml` option", 69 | err, 70 | ) 71 | } 72 | return err 73 | } 74 | return nil 75 | }, 76 | } 77 | 78 | cmd.Flags().StringVarP(&o.inputFilename, "input", "f", o.inputFilename, "Input filename; use \"-\" for stdin") 79 | cmd.Flags().StringVarP(&o.outputPath, "output-path", "p", o.outputPath, "Output path") 80 | cmd.Flags().StringVarP(&o.template, "template", "t", o.template, "Filename template") 81 | cmd.Flags().BoolVar(&o.quiet, "quiet", o.quiet, "Don't show status messages") 82 | 83 | return cmd 84 | } 85 | 86 | // Validate ensures that all required arguments and flag values are provided 87 | func (o *SplitYAMLOptions) Validate() error { 88 | return nil 89 | } 90 | 91 | func (o *SplitYAMLOptions) Run() error { 92 | var inputReader io.Reader 93 | if o.inputFilename == "-" { 94 | inputReader = o.ioStreams.In 95 | } else { 96 | var err error 97 | inputReader, err = os.Open(o.inputFilename) 98 | if err != nil { 99 | return err 100 | } 101 | } 102 | 103 | filenameTemplate, err := template.New("").Parse(o.template) 104 | if err != nil { 105 | return err 106 | } 107 | 108 | saveResources, err := saveresources.New(saveresources.Options{ 109 | OutputPath: o.outputPath, 110 | FilenameTemplate: filenameTemplate, 111 | OnStartFile: func(filename string) { 112 | if !o.quiet { 113 | fmt.Fprintf(o.ioStreams.Out, "%s\n", filename) 114 | } 115 | }, 116 | }) 117 | if err != nil { 118 | return err 119 | } 120 | 121 | // Warn user if input appears to be a terminal 122 | if !o.quiet && inputReader == os.Stdin { 123 | fi, err := os.Stdin.Stat() 124 | if err != nil { 125 | return err 126 | } 127 | if (fi.Mode() & os.ModeCharDevice) != 0 { 128 | _, _ = o.ioStreams.ErrOut.Write([]byte( 129 | "NOTE: kubectl-split-yaml is currently reading from stdin.\n" + 130 | " Other options include passing a filename, e.g:\n" + 131 | " $ kubectl split-yaml -f resources.yaml\n" + 132 | " or piping input, e.g:\n" + 133 | " $ kubectl get all -o yaml | kubectl split-yaml\n" + 134 | "Press Ctrl+C to exit\n", 135 | )) 136 | } 137 | } 138 | 139 | return walkresources.WalkReader(inputReader, func(resource map[interface{}]interface{}) error { 140 | return saveResources.Save(resource) 141 | }) 142 | } 143 | -------------------------------------------------------------------------------- /internal/saveresources/saveresources.go: -------------------------------------------------------------------------------- 1 | package saveresources 2 | 3 | import ( 4 | "bytes" 5 | "errors" 6 | "html/template" 7 | "os" 8 | "path/filepath" 9 | "regexp" 10 | 11 | "github.com/go-yaml/yaml" 12 | ) 13 | 14 | const defaultNamespace = "default" 15 | 16 | var unsafeBasenameCharsRegexp *regexp.Regexp 17 | 18 | func init() { 19 | unsafeBasenameCharsRegexp = regexp.MustCompile("[^-.0-9a-zA-Z]") 20 | } 21 | 22 | type Options struct { 23 | OutputPath string 24 | FilenameTemplate *template.Template 25 | OnStartFile func(filename string) 26 | } 27 | 28 | func New(options Options) (*SaveResources, error) { 29 | options.FilenameTemplate.Option("missingkey=error") 30 | 31 | s := &SaveResources{ 32 | outputPath: options.OutputPath, 33 | filenameTemplate: options.FilenameTemplate, 34 | onStartFile: options.OnStartFile, 35 | } 36 | 37 | if err := s.testFilenameTemplate(); err != nil { 38 | return nil, err 39 | } 40 | 41 | return s, nil 42 | } 43 | 44 | type SaveResources struct { 45 | outputPath string 46 | filenameTemplate *template.Template 47 | onStartFile func(filename string) 48 | } 49 | 50 | func (s *SaveResources) Save(resource map[interface{}]interface{}) error { 51 | filename, err := s.getFilenameForResource(resource) 52 | if err != nil { 53 | return err 54 | } 55 | 56 | if s.onStartFile != nil { 57 | s.onStartFile(filename) 58 | } 59 | 60 | if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil { 61 | return err 62 | } 63 | 64 | f, err := os.Create(filename) 65 | if err != nil { 66 | return err 67 | } 68 | defer f.Close() 69 | 70 | encoder := yaml.NewEncoder(f) 71 | defer encoder.Close() 72 | 73 | if err := encoder.Encode(resource); err != nil { 74 | return err 75 | } 76 | 77 | return nil 78 | } 79 | 80 | func (s *SaveResources) testFilenameTemplate() error { 81 | _, err := s.getFilename("v1", "Test", "namespace", "name") 82 | return err 83 | } 84 | 85 | func (s *SaveResources) getFilename(apiVersion, kind, namespace, name string) (string, error) { 86 | if namespace == "" { 87 | namespace = defaultNamespace 88 | } 89 | filename := &bytes.Buffer{} 90 | err := s.filenameTemplate.Execute(filename, map[string]interface{}{ 91 | "apiVersion": sanitiseBasename(apiVersion), 92 | "kind": sanitiseBasename(kind), 93 | "namespace": sanitiseBasename(namespace), 94 | "name": sanitiseBasename(name), 95 | }) 96 | if err != nil { 97 | return "", err 98 | } 99 | return filepath.Join(s.outputPath, filename.String()), nil 100 | } 101 | 102 | func (s *SaveResources) getFilenameForResource(resource map[interface{}]interface{}) (string, error) { 103 | apiVersion, ok := resource["apiVersion"].(string) 104 | if !ok { 105 | return "", errors.New("apiVersion is missing or not a string") 106 | } 107 | 108 | kind, ok := resource["kind"].(string) 109 | if !ok { 110 | return "", errors.New("kind is missing or not a string") 111 | } 112 | 113 | metadata, ok := resource["metadata"].(map[interface{}]interface{}) 114 | if !ok { 115 | return "", errors.New("metadata is missing or not a map") 116 | } 117 | 118 | namespace, ok := metadata["namespace"].(string) 119 | if !ok { 120 | if _, ok := metadata["namespace"]; ok { 121 | return "", errors.New("namespace is not a string") 122 | } 123 | } 124 | 125 | name, ok := metadata["name"].(string) 126 | if !ok { 127 | return "", errors.New("name is missing or not a string") 128 | } 129 | 130 | return s.getFilename(apiVersion, kind, namespace, name) 131 | } 132 | 133 | func sanitiseBasename(s string) string { 134 | return unsafeBasenameCharsRegexp.ReplaceAllString(s, "_") 135 | } 136 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/ketall-list-resource/input.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: List 3 | items: 4 | - items: 5 | - apiVersion: v1 6 | kind: Test 7 | metadata: 8 | name: test0 9 | - apiVersion: v1 10 | kind: Test 11 | metadata: 12 | name: test1 13 | - items: 14 | - apiVersion: v1 15 | kind: Test 16 | metadata: 17 | name: test2 18 | - apiVersion: v1 19 | kind: Test 20 | metadata: 21 | name: test3 22 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/ketall-list-resource/output0.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test0 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/ketall-list-resource/output1.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test1 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/ketall-list-resource/output2.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test2 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/ketall-list-resource/output3.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test3 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/list-resource/input.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: List 3 | items: 4 | - apiVersion: v1 5 | kind: Test 6 | metadata: 7 | name: test0 8 | - apiVersion: v1 9 | kind: Test 10 | metadata: 11 | name: test1 12 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/list-resource/output0.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test0 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/list-resource/output1.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test1 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/multi-doc-resources/input.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test0 5 | --- 6 | apiVersion: v1 7 | kind: Test 8 | metadata: 9 | name: test1 10 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/multi-doc-resources/output0.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test0 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/multi-doc-resources/output1.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test1 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/recursive-list-resource/input.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: List 3 | items: 4 | - apiVersion: v1 5 | kind: List 6 | items: 7 | - apiVersion: v1 8 | kind: Test 9 | metadata: 10 | name: test0 11 | - apiVersion: v1 12 | kind: Test 13 | metadata: 14 | name: test1 15 | - apiVersion: v1 16 | kind: List 17 | items: 18 | - apiVersion: v1 19 | kind: Test 20 | metadata: 21 | name: test2 22 | - apiVersion: v1 23 | kind: Test 24 | metadata: 25 | name: test3 26 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/recursive-list-resource/output0.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test0 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/recursive-list-resource/output1.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test1 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/recursive-list-resource/output2.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test2 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/recursive-list-resource/output3.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test3 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/single-resource/input.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test0 5 | -------------------------------------------------------------------------------- /internal/walkresources/testdata/single-resource/output0.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Test 3 | metadata: 4 | name: test0 5 | -------------------------------------------------------------------------------- /internal/walkresources/walkresources.go: -------------------------------------------------------------------------------- 1 | package walkresources 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io" 7 | 8 | "gopkg.in/yaml.v2" 9 | ) 10 | 11 | var ErrNotAResource = errors.New("object is not a resource (missing apiVersion or kind)") 12 | var ErrNotAListResource = errors.New("object is not a list resource") 13 | var ErrUnexpectedType = errors.New("unexpected type") 14 | 15 | var errNotAKetallItem = errors.New("not a ketall item") 16 | 17 | type callback = func(map[interface{}]interface{}) error 18 | 19 | // WalkReader takes YAML docs from a Reader and calls callback() for each 20 | // Kubernetes resource 21 | func WalkReader(reader io.Reader, callback callback) error { 22 | decoder := yaml.NewDecoder(reader) 23 | for { 24 | doc := map[interface{}]interface{}{} 25 | if err := decoder.Decode(doc); err != nil { 26 | if errors.Is(err, io.EOF) { 27 | break 28 | } 29 | return err 30 | } 31 | 32 | if err := WalkObj(doc, callback); err != nil { 33 | return err 34 | } 35 | } 36 | 37 | return nil 38 | } 39 | 40 | // WalkObj takes a parsed YAML object and calls callback() for each Kubernetes 41 | // resource 42 | func WalkObj(obj interface{}, callback callback) error { 43 | if objMap, ok := obj.(map[interface{}]interface{}); ok { 44 | return walkObjMap(objMap, callback) 45 | } 46 | 47 | return newErrUnexpectedType(obj) 48 | } 49 | 50 | // walkObjMap delegates to walkList() if it looks like a v1/List object, or 51 | // calls callback() if not 52 | func walkObjMap(objMap map[interface{}]interface{}, callback callback) error { 53 | apiVersion, kind, ok := getResourceType(objMap) 54 | if !ok { 55 | return ErrNotAResource 56 | } 57 | 58 | if apiVersion == "v1" && kind == "List" { 59 | return walkList(objMap, callback) 60 | } 61 | 62 | return callback(objMap) 63 | } 64 | 65 | // walkList calls callback() for each resource in the list. 66 | // Has special handling for the ketall kubectl plugin 67 | func walkList(obj map[interface{}]interface{}, callback callback) error { 68 | items, ok := obj["items"].([]interface{}) 69 | if !ok { 70 | return ErrNotAListResource 71 | } 72 | 73 | for _, itemIntf := range items { 74 | item, ok := itemIntf.(map[interface{}]interface{}) 75 | if !ok { 76 | return newErrUnexpectedType(item) 77 | } 78 | 79 | // Try to handle ketall plugin output 80 | if err := walkKetallItem(item, callback); err != nil { 81 | if errors.Is(err, errNotAKetallItem) { 82 | // This isn't a ketall item. Treat as a standard resource 83 | if err := WalkObj(item, callback); err != nil { 84 | return err 85 | } 86 | } else { 87 | return err 88 | } 89 | } 90 | } 91 | return nil 92 | } 93 | 94 | // walkKetallItem attempts to handle an item output from the ketall kubectl 95 | // plugin 96 | func walkKetallItem(obj map[interface{}]interface{}, callback callback) error { 97 | // ketall plugin output is inconsistent with kubectl v1/List objects. 98 | // 99 | // kubectl would represent two items as: 100 | // apiVersion: v1 101 | // kind: List 102 | // items: 103 | // - AAA 104 | // - BBB 105 | // 106 | // ketall represents the same two items as: 107 | // apiVersion: v1 108 | // kind: List 109 | // items: 110 | // - items: 111 | // - AAA 112 | // - BBB 113 | // 114 | // Notice the extra items objects with no apiVersion or kind. 115 | // 116 | // This function tries to callback ketall resources found within a 117 | // top-level item. Returns errNotAKetallItem if unsuccessful, signalling 118 | // the item should be processed as a standard Kubernetes resource. 119 | 120 | if _, ok := obj["apiVersion"].(string); ok { 121 | return errNotAKetallItem 122 | } 123 | 124 | if _, ok := obj["kind"].(string); ok { 125 | return errNotAKetallItem 126 | } 127 | 128 | itemIntfs, ok := obj["items"].([]interface{}) 129 | if !ok { 130 | return errNotAKetallItem 131 | } 132 | 133 | for _, itemIntf := range itemIntfs { 134 | item, ok := itemIntf.(map[interface{}]interface{}) 135 | if !ok { 136 | return newErrUnexpectedType(itemIntf) 137 | } 138 | 139 | if err := callback(item); err != nil { 140 | return err 141 | } 142 | } 143 | return nil 144 | } 145 | 146 | func newErrUnexpectedType(value interface{}) error { 147 | return fmt.Errorf("%w: %T", ErrUnexpectedType, value) 148 | } 149 | 150 | func getResourceType(obj map[interface{}]interface{}) (string, string, bool) { 151 | apiVersion, ok := obj["apiVersion"].(string) 152 | if !ok { 153 | return "", "", false 154 | } 155 | 156 | kind, ok := obj["kind"].(string) 157 | if !ok { 158 | return "", "", false 159 | } 160 | 161 | return apiVersion, kind, true 162 | } 163 | -------------------------------------------------------------------------------- /internal/walkresources/walkresources_test.go: -------------------------------------------------------------------------------- 1 | package walkresources 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io" 7 | "io/ioutil" 8 | "os" 9 | "path/filepath" 10 | "reflect" 11 | "testing" 12 | 13 | "gopkg.in/yaml.v2" 14 | ) 15 | 16 | type expectedCall = map[interface{}]interface{} 17 | 18 | func TestData(t *testing.T) { 19 | dataPath := "testdata" 20 | 21 | fileInfos, err := ioutil.ReadDir(dataPath) 22 | if err != nil { 23 | t.Error(err) 24 | } 25 | 26 | for _, fileInfo := range fileInfos { 27 | if fileInfo.IsDir() { 28 | t.Run(fileInfo.Name(), func(t *testing.T) { 29 | if err := testDataFolder(filepath.Join(dataPath, fileInfo.Name())); err != nil { 30 | t.Error(err) 31 | } 32 | }) 33 | } 34 | } 35 | } 36 | 37 | func testDataFolder(path string) error { 38 | decodeFile := func(filename string, dest interface{}) error { 39 | f, err := os.Open(filename) 40 | if err != nil { 41 | return err 42 | } 43 | defer f.Close() 44 | 45 | decoder := yaml.NewDecoder(f) 46 | return decoder.Decode(dest) 47 | } 48 | 49 | inputFilename := filepath.Join(path, "input.yaml") 50 | input, err := os.Open(inputFilename) 51 | if err != nil { 52 | return err 53 | } 54 | defer input.Close() 55 | 56 | expectedCalls := []expectedCall{} 57 | for i := 0; ; i++ { 58 | outputFilename := filepath.Join(path, fmt.Sprintf("output%d.yaml", i)) 59 | if _, err := os.Stat(outputFilename); err != nil { 60 | if os.IsNotExist(err) { 61 | break 62 | } 63 | return err 64 | } 65 | output := map[interface{}]interface{}{} 66 | if err := decodeFile(outputFilename, output); err != nil { 67 | return fmt.Errorf("%s: %w", outputFilename, err) 68 | } 69 | expectedCalls = append(expectedCalls, expectedCall(output)) 70 | } 71 | 72 | return expectWalkCallbacks(input, expectedCalls) 73 | } 74 | 75 | func expectWalkCallbacks(input io.Reader, expectedCalls []expectedCall) error { 76 | nextCallIndex := 0 77 | 78 | err := WalkReader(input, func(actual map[interface{}]interface{}) error { 79 | callIndex := nextCallIndex 80 | 81 | if callIndex >= len(expectedCalls) { 82 | return errors.New("too many calls") 83 | } 84 | 85 | expected := expectedCalls[callIndex] 86 | nextCallIndex++ 87 | 88 | if !reflect.DeepEqual(actual, expected) { 89 | return fmt.Errorf( 90 | "mismatch for call %[1]d\n"+ 91 | "Actual: (%[2]T) %#[2]v\n"+ 92 | "Expected: (%[3]T) %#[3]v", 93 | callIndex, actual, expected, 94 | ) 95 | } 96 | return nil 97 | }) 98 | if err != nil { 99 | return err 100 | } 101 | 102 | remainingCalls := len(expectedCalls) - nextCallIndex 103 | if remainingCalls > 0 { 104 | return fmt.Errorf("expected %d more call(s)", remainingCalls) 105 | } 106 | 107 | return nil 108 | } 109 | --------------------------------------------------------------------------------