-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit abeb563
Showing
16 changed files
with
557 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
version: 2 | ||
|
||
updates: | ||
- package-ecosystem: github-actions | ||
directory: / | ||
schedule: | ||
interval: weekly | ||
day: friday | ||
rebase-strategy: disabled | ||
labels: | ||
- dependencies | ||
|
||
- package-ecosystem: gomod | ||
directory: / | ||
schedule: | ||
interval: weekly | ||
day: friday | ||
rebase-strategy: disabled | ||
labels: | ||
- dependencies |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: CD | ||
|
||
on: # yamllint disable-line rule:truthy | ||
workflow_dispatch: | ||
push: | ||
tags: | ||
- '**' | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | ||
cancel-in-progress: true | ||
|
||
defaults: | ||
run: | ||
shell: bash -Eeuxo pipefail {0} | ||
|
||
jobs: | ||
|
||
goreleaser: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version-file: 'go.mod' | ||
|
||
- name: Set up Syft | ||
run: curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin | ||
|
||
- name: Run GoReleaser | ||
uses: goreleaser/goreleaser-action@v5 | ||
with: | ||
distribution: goreleaser | ||
version: latest | ||
args: release --clean | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
name: CI | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: [main] | ||
paths: | ||
- "**.go" | ||
pull_request: | ||
branches: [main] | ||
paths: | ||
- "**.go" | ||
|
||
permissions: | ||
contents: read | ||
|
||
defaults: | ||
run: | ||
shell: bash -Eeuo pipefail {0} | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
run: | ||
name: CI | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 10 | ||
|
||
steps: | ||
|
||
- name: Check out code | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Restore cached CI Dependencies | ||
id: cache-ci-dependencies-restore | ||
uses: actions/cache/restore@v4 | ||
with: | ||
path: | | ||
/usr/local/bin/gotestfmt | ||
key: ${{ runner.os }}-ci-dependencies | ||
|
||
- name: Install Go | ||
id: cache-go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version-file: 'go.mod' | ||
|
||
- name: Install `gotestfmt` | ||
if: steps.cache-ci-dependencies-restore.outputs.cache-hit != 'true' | ||
run: go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest && mv /home/runner/go/bin/gotestfmt /usr/local/bin/ | ||
|
||
- name: Save CI dependencies | ||
id: cache-ci-dependencies-save | ||
if: steps.cache-ci-dependencies-restore.outputs.cache-hit != 'true' | ||
uses: actions/cache/save@v4 | ||
with: | ||
path: | | ||
/usr/local/bin/gotestfmt | ||
key: ${{ steps.cache-ci-dependencies-restore.outputs.cache-primary-key }} | ||
|
||
- name: Go mod download | ||
if: steps.cache-go.outputs.cache-hit != 'true' | ||
run: go mod download -x | ||
|
||
- name: Go Build | ||
run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /dev/null | ||
|
||
- name: Run tests | ||
run: | | ||
go test -json -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
args: "--timeout=2m" | ||
skip-pkg-cache: true | ||
skip-build-cache: true | ||
skip-cache: true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
*.test | ||
*.out | ||
go.work | ||
cmake-build-*/ | ||
out/ | ||
.idea/ | ||
k8s-yaml-extract |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
builds: | ||
- env: | ||
- CGO_ENABLED=0 | ||
goos: | ||
- linux | ||
- darwin | ||
|
||
archives: | ||
- format: tar.gz | ||
name_template: >- | ||
{{ .ProjectName }}_ | ||
{{- title .Os }}_ | ||
{{- if eq .Arch "amd64" }}x86_64 | ||
{{- else if eq .Arch "386" }}i386 | ||
{{- else }}{{ .Arch }}{{ end }} | ||
{{- if .Arm }}v{{ .Arm }}{{ end }} | ||
checksum: | ||
name_template: 'checksums.txt' | ||
snapshot: | ||
name_template: "{{ incpatch .Version }}-next" | ||
changelog: | ||
sort: asc | ||
filters: | ||
exclude: | ||
- '^docs:' | ||
- '^test:' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) [2024] [Nathan S. Martins] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# k8s-yaml-extract | ||
|
||
`k8s-yaml-extract`, is a powerful command-line interface (CLI) tool built for extraction of Kubernetes YAML manifests. This tool streamlines the process of filtering YAML files based on specified criteria, offering enhanced flexibility and control. | ||
|
||
Key Features: | ||
|
||
--name Flag: Extract YAMLs matching a specific resource name. ( REQUIRED ) | ||
--kind Flag: Extract YAMLs based on a particular Kubernetes resource kind. | ||
|
||
Combined Filtering: Use both --name and --kind flags simultaneously for granular extraction. | ||
|
||
|
||
## Examples | ||
|
||
It can work just by piping in from STDIN: | ||
|
||
`kustomize build example/ | k8s-yaml-extract --kind=deployment` | ||
|
||
`kustomize build example/ | k8s-yaml-extract --name=my-app --kind=deployment` | ||
|
||
or a static existing file as a parameter: | ||
|
||
`k8s-yaml-extract --name=my-app --kind=deployment example/out.yaml` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"sigs.k8s.io/kustomize/kyaml/kio" | ||
"sigs.k8s.io/kustomize/kyaml/yaml" | ||
"strings" | ||
) | ||
|
||
var extractKinds string | ||
var extractName string | ||
|
||
func init() { | ||
rootCmd.Flags().StringVar(&extractKinds, "kind", "", "the kind of object you want to extract") | ||
_ = rootCmd.MarkFlagRequired("kind") | ||
rootCmd.Flags().StringVar(&extractName, "name", "", "the name of object you want to extract") | ||
} | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "k8s-yaml-extract --kind=Deployment [--name=deploy-name]", | ||
Short: "This CLI tool allows extracting Kubernetes YAMLs from YAML lists based on specified criteria", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
// catch any panics | ||
defer func() { | ||
if err := recover(); err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "unexpected panic: \n\t%v\n", err) | ||
os.Exit(1) | ||
} | ||
}() | ||
|
||
var inputReader = cmd.InOrStdin() | ||
|
||
// the argument received looks like a file, we try to open it | ||
if len(args) > 0 { | ||
if args[0] != "-" && args[0] != "" { | ||
file, err := os.Open(args[0]) | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "failed open file: %v", err) | ||
os.Exit(1) | ||
} | ||
inputReader = file | ||
} | ||
} | ||
|
||
rw := kio.ByteReadWriter{ | ||
Reader: inputReader, | ||
Writer: os.Stdout, | ||
WrapBareSeqNode: true, | ||
PreserveSeqIndent: true, | ||
} | ||
|
||
err := kio.Pipeline{ | ||
Inputs: []kio.Reader{&rw}, | ||
Filters: []kio.Filter{ | ||
ExtractFilter{}, | ||
}, | ||
Outputs: []kio.Writer{&rw}, | ||
}.Execute() | ||
|
||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "[ERROR] while parsing file: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
os.Exit(0) | ||
}, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
type ExtractFilter struct{} | ||
|
||
func (f ExtractFilter) Filter(slice []*yaml.RNode) ([]*yaml.RNode, error) { | ||
// only add to slice what you want to change | ||
var newSlices []*yaml.RNode | ||
|
||
for i := range slice { | ||
kindNode, err := slice[i].Pipe(yaml.Get("kind")) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while getting RNode kind: %w", err) | ||
} | ||
if kindNode == nil { | ||
continue | ||
} | ||
|
||
kind := kindNode.YNode().Value | ||
if !strings.EqualFold(kind, extractKinds) { | ||
continue | ||
} | ||
|
||
if extractName != "" && !strings.EqualFold(slice[i].GetName(), extractName) { | ||
continue | ||
} | ||
|
||
newSlices = append(newSlices, slice[i]) | ||
} | ||
|
||
return newSlices, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: my-app | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: my-app | ||
template: | ||
metadata: | ||
labels: | ||
app: my-app | ||
spec: | ||
containers: | ||
- name: my-app | ||
image: nginx:latest | ||
ports: | ||
- containerPort: 80 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: networking.k8s.io/v1 | ||
kind: Ingress | ||
metadata: | ||
name: my-app-ingress | ||
spec: | ||
rules: | ||
- host: my-app.example.com | ||
http: | ||
paths: | ||
- path: / | ||
pathType: Prefix | ||
backend: | ||
service: | ||
name: my-app | ||
port: | ||
number: 80 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
resources: | ||
- deployment.yaml | ||
- service.yaml | ||
- ingress.yaml |
Oops, something went wrong.