From 10f16f231ad26983a7b5efd939078e8e70e0216c Mon Sep 17 00:00:00 2001 From: Ardika Bagus Date: Fri, 3 Nov 2023 14:22:35 +0700 Subject: [PATCH 1/5] chore: add LICENSE Signed-off-by: Ardika Bagus --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d106165 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Ardika Bagus + +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. \ No newline at end of file From 514ef962d080e18b6810dd2a61d02cd7ae2caa89 Mon Sep 17 00:00:00 2001 From: Ardika Bagus Date: Fri, 3 Nov 2023 14:22:52 +0700 Subject: [PATCH 2/5] ci: add CI related Signed-off-by: Ardika Bagus --- .github/workflows/release.yaml | 42 +++++++++++++++++++++++++++++++++ .github/workflows/test.yaml | 40 +++++++++++++++++++++++++++++++ .goreleaser.yml | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 .github/workflows/release.yaml create mode 100644 .github/workflows/test.yaml create mode 100644 .goreleaser.yml diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..30c701a --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,42 @@ +name: Release + +on: + push: + tags: + - "*" + +permissions: + contents: write + +jobs: + test: + uses: ./.github/workflows/test.yaml + + release: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: ^1.21.x + + - name: Get Tag + id: tag + uses: devops-actions/action-get-tag@v1.0.2 + with: + strip_v: true + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + distribution: goreleaser + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + RELEASE_VERSION: ${{ steps.tag.outputs.tag }} diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..7e2e884 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,40 @@ +name: Test + +on: + pull_request: + branches: + - main + paths: + - "**/*.go" + + workflow_call: + +jobs: + audit: + name: Auditing the code + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: ^1.21.x + + - name: Install make + run: | + sudo apt update -y + sudo apt install make curl git -y + + - name: Run audit + run: | + make lint + make test + + - name: Upload coverage + uses: codecov/codecov-action@v1 + with: + file: ./output/coverage.out diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..fc56246 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,43 @@ +version: 1 +before: + hooks: + - go mod tidy + +builds: + - binary: helmize + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + - windows + goarch: + - amd64 + - arm64 + goarm: + - "6" + - "7" + ldflags: + - -X github.com/ardikabs/helmize/cmd.Version={{.Version}} + - -X github.com/ardikabs/helmize/cmd.GitCommit={{.Commit}} + ignore: + - goos: windows + goarch: arm64 + +archives: + - format: binary + +changelog: + filters: + exclude: + - "^Release version" + - "^Merge pull request" + +checksum: + name_template: "checksums.txt" + +release: + github: + owner: ardikabs + name: helmize + name_template: "Version {{.Version}}" From f459629a0dc7d58a4742b8488149e8c0ad338076 Mon Sep 17 00:00:00 2001 From: Ardika Bagus Date: Fri, 3 Nov 2023 14:28:22 +0700 Subject: [PATCH 3/5] feat: preparation for first release move internal package to pkg Signed-off-by: Ardika Bagus --- Makefile | 22 ++++--- README.md | 5 ++ cmd/root.go | 57 ++++++++++++++++--- examples/direct/releases-1.yaml | 23 ++++++++ examples/direct/releases-2.yaml | 22 +++++++ .../{ => krm}/local-chart/kustomization.yaml | 0 examples/{ => krm}/local-chart/release.yaml | 0 .../local-chart/test-chart/Chart.yaml | 0 .../test-chart/templates/configmap.yaml | 0 .../local-chart/test-chart/values.yaml | 0 examples/{ => krm}/local-chart/values.yaml | 0 .../multi-releases/kustomization.yaml | 0 .../{ => krm}/multi-releases/release-1.yaml | 0 .../multi-releases/release-1/values.yaml | 0 .../{ => krm}/multi-releases/release-2.yaml | 0 .../multi-releases/release-2/values.yaml | 0 .../{ => krm}/multi-releases/release-3.yaml | 0 examples/{ => krm}/simple/kustomization.yaml | 0 examples/{ => krm}/simple/release.yaml | 0 examples/{ => krm}/with-glob/config.env | 0 .../{ => krm}/with-glob/kustomization.yaml | 0 examples/{ => krm}/with-glob/release.yaml | 0 examples/{ => krm}/with-glob/secret.env | 0 .../{ => krm}/with-glob/values.d/common.yaml | 0 .../values.d/disabled/autoscaling.yaml | 0 .../with-glob/values.d/enabled/command.yaml | 0 examples/{ => krm}/with-glob/values.yaml | 0 .../with-oci-repo/kustomization.yaml | 0 examples/{ => krm}/with-oci-repo/release.yaml | 0 go.mod | 2 +- main.go | 2 +- {internal => pkg}/errs/errs.go | 0 {internal => pkg}/helm/client.go | 0 {internal => pkg}/helm/clientopts.go | 0 {internal => pkg}/helm/helm.go | 11 ++-- {internal => pkg}/helm/helm_test.go | 0 {internal => pkg}/helm/release.go | 2 +- {internal => pkg}/helm/repo.go | 2 +- {internal/krm => pkg/processor}/processor.go | 30 ++++++---- .../krm => pkg/processor}/processor_test.go | 8 +-- .../processor}/testdata/default/input.yaml | 0 .../processor}/testdata/default/want.yaml | 0 .../invalid-input-apiVersion/input.yaml | 0 .../testdata/invalid-input-kind/input.yaml | 0 .../testdata/invalid-input-noname/input.yaml | 0 .../invalid-input-nonamespace/input.yaml | 0 .../testdata/invalid-input-norepo/input.yaml | 0 {internal => pkg}/util/util.go | 0 48 files changed, 141 insertions(+), 45 deletions(-) create mode 100644 examples/direct/releases-1.yaml create mode 100644 examples/direct/releases-2.yaml rename examples/{ => krm}/local-chart/kustomization.yaml (100%) rename examples/{ => krm}/local-chart/release.yaml (100%) rename examples/{ => krm}/local-chart/test-chart/Chart.yaml (100%) rename examples/{ => krm}/local-chart/test-chart/templates/configmap.yaml (100%) rename examples/{ => krm}/local-chart/test-chart/values.yaml (100%) rename examples/{ => krm}/local-chart/values.yaml (100%) rename examples/{ => krm}/multi-releases/kustomization.yaml (100%) rename examples/{ => krm}/multi-releases/release-1.yaml (100%) rename examples/{ => krm}/multi-releases/release-1/values.yaml (100%) rename examples/{ => krm}/multi-releases/release-2.yaml (100%) rename examples/{ => krm}/multi-releases/release-2/values.yaml (100%) rename examples/{ => krm}/multi-releases/release-3.yaml (100%) rename examples/{ => krm}/simple/kustomization.yaml (100%) rename examples/{ => krm}/simple/release.yaml (100%) rename examples/{ => krm}/with-glob/config.env (100%) rename examples/{ => krm}/with-glob/kustomization.yaml (100%) rename examples/{ => krm}/with-glob/release.yaml (100%) rename examples/{ => krm}/with-glob/secret.env (100%) rename examples/{ => krm}/with-glob/values.d/common.yaml (100%) rename examples/{ => krm}/with-glob/values.d/disabled/autoscaling.yaml (100%) rename examples/{ => krm}/with-glob/values.d/enabled/command.yaml (100%) rename examples/{ => krm}/with-glob/values.yaml (100%) rename examples/{ => krm}/with-oci-repo/kustomization.yaml (100%) rename examples/{ => krm}/with-oci-repo/release.yaml (100%) rename {internal => pkg}/errs/errs.go (100%) rename {internal => pkg}/helm/client.go (100%) rename {internal => pkg}/helm/clientopts.go (100%) rename {internal => pkg}/helm/helm.go (95%) rename {internal => pkg}/helm/helm_test.go (100%) rename {internal => pkg}/helm/release.go (98%) rename {internal => pkg}/helm/repo.go (98%) rename {internal/krm => pkg/processor}/processor.go (64%) rename {internal/krm => pkg/processor}/processor_test.go (90%) rename {internal/krm => pkg/processor}/testdata/default/input.yaml (100%) rename {internal/krm => pkg/processor}/testdata/default/want.yaml (100%) rename {internal/krm => pkg/processor}/testdata/invalid-input-apiVersion/input.yaml (100%) rename {internal/krm => pkg/processor}/testdata/invalid-input-kind/input.yaml (100%) rename {internal/krm => pkg/processor}/testdata/invalid-input-noname/input.yaml (100%) rename {internal/krm => pkg/processor}/testdata/invalid-input-nonamespace/input.yaml (100%) rename {internal/krm => pkg/processor}/testdata/invalid-input-norepo/input.yaml (100%) rename {internal => pkg}/util/util.go (100%) diff --git a/Makefile b/Makefile index 520a537..af1a846 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ GitCommit := $(shell git rev-parse HEAD) LDFLAGS := "-s -w -X github.com/ardikabs/helmize/cmd.Version=$(Version) -X github.com/ardikabs/helmize/cmd.GitCommit=$(GitCommit)" OUTDIR := bin -GOLANGCI_VERSION = 1.31.0 +GOLANGCI_VERSION = 1.53.3 ## help: print this help message .PHONY: help @@ -17,26 +17,24 @@ bin/golangci-lint: bin/golangci-lint-${GOLANGCI_VERSION} bin/golangci-lint-${GOLANGCI_VERSION}: @mkdir -p bin - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b ./bin/ v${GOLANGCI_VERSION} + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(OUTDIR) v$(GOLANGCI_VERSION) @mv bin/golangci-lint "$@" -## audit: tidy and vendor dependencies and format, vet, lint and test all code +## audit: format, tidying, vet, lint and test all code .PHONY: audit -audit: fmt vendor lint vet test +audit: fmt mod lint vet test ## fmt: formatting the code fmt: @echo 'Formatting code...' @go fmt $(shell go list ./... | grep -v /vendor/|xargs echo) -## vendor: tidy and vendor dependencies -.PHONY: vendor -vendor: +## mod: tidying module dependencies +.PHONY: mod +mod: @echo 'Tidying and verifying module dependencies...' @go mod tidy @go mod verify - @echo 'Vendoring dependencies...' - @go mod vendor ## lint: linting the code .PHONY: lint @@ -44,16 +42,16 @@ lint: bin/golangci-lint @echo 'Linting code...' bin/golangci-lint run -## test: run unit test +## test: running unit test .PHONY: test test: @echo 'Running tests...' @mkdir -p output - @go test $(shell go list ./... | grep -v /vendor/|xargs echo) -cover -coverprofile=./output/coverage.out -race && \ + @go test $(shell go list ./... | grep -v /vendor/|xargs echo) -v -covermode=atomic -cover -coverprofile=./output/coverage.out -race && \ go tool cover -html=./output/coverage.out -o ./output/coverage.html && \ go tool cover -func=./output/coverage.out -## vet: run vetting the code +## vet: vetting the code .PHONY: vet vet: @echo 'Vetting code...' diff --git a/README.md b/README.md index 71e299f..9d72a15 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # Helmize +[![Go Reference](https://pkg.go.dev/badge/github.com/ardikabs/helmize.svg)](https://pkg.go.dev/github.com/ardikabs/helmize) +[![Go Report Card](https://goreportcard.com/badge/github.com/ardikabs/helmize)](https://goreportcard.com/report/github.com/ardikabs/helmize) +[![Test Status](https://github.com/ardikabs/helmize/actions/workflows/test.yml/badge.svg?branch=main)] +[![Codecov](https://codecov.io/gh/ardikabs/helmize/branch/main/graph/badge.svg)](https://codecov.io/gh/ardikabs/helmize) + > A KRM function to enable Helm on Kustomize with Glob support The motivation for this project is quite simple. While [Helm integration](https://kubectl.docs.kubernetes.io/references/kustomize/builtins/#_helmchartinflationgenerator_) in Kustomize is already available, it falls short in scenarios where dynamic use of Glob is required to fetch all necessary values.yaml files. In such cases, it lacks support. diff --git a/cmd/root.go b/cmd/root.go index 94e96f4..e8bd04f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,40 +4,79 @@ import ( "os" "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" - "github.com/ardikabs/helmize/internal/krm" + "github.com/ardikabs/helmize/pkg/processor" "github.com/lithammer/dedent" "github.com/spf13/cobra" ) +var files []string + func MakeRoot() *cobra.Command { var cmd = &cobra.Command{ Use: "helmize", Short: "helmize is a KRM Function to enable Helm on Kustomize with Glob support", Example: dedent.Dedent(` - Helmize is intended to be used as KRM function only, - thus a standalone usage is not supported. + Initially Helmize used as KRM function only, but it also support for direct use through flag. + + > KRM usage: + + Within "generators" fields through annotations [config.kubernetes.io/function] + + annotations: + config.kubernetes.io/function: | + exec: + path: helmize - KRM usage: + $ kustomize build --enable-alpha-plugins --enable-exec /path/to/kustomization_dir - $ kustomize build --enable-alpha-plugins --enable-exec . + > Direct usage: + + $ helmize [-f HELM_RELEASE_FILE] `), } cmd.Args = cobra.MaximumNArgs(0) cmd.RunE = run + + cmd.Flags().StringArrayVarP(&files, "file", "f", []string{}, "specify HelmRelease in a YAML file (can specify multiple times)") return cmd } func run(cmd *cobra.Command, args []string) error { - stdinStat, _ := os.Stdin.Stat() + // Direct usage + if len(files) > 0 { + var generatedObjects fn.KubeObjects + for _, file := range files { + raw, err := os.ReadFile(file) + if err != nil { + return err + } + + kubeObjects, err := fn.ParseKubeObjects(raw) + if err != nil { + return err + } + + generated, err := processor.ProcessKubeObjects(kubeObjects) + if err != nil { + return err + } + generatedObjects = append(generatedObjects, generated...) + } + + os.Stdout.Write([]byte(generatedObjects.String())) + return nil + } + + // KRM usage + stdinStat, _ := os.Stdin.Stat() // Check the StdIn content. if (stdinStat.Mode() & os.ModeCharDevice) != 0 { - cmd.Help() - os.Exit(1) + return cmd.Help() } - if err := fn.AsMain(fn.ResourceListProcessorFunc(krm.Process)); err != nil { + if err := fn.AsMain(fn.ResourceListProcessorFunc(processor.ProcessResourceList)); err != nil { os.Exit(1) } diff --git a/examples/direct/releases-1.yaml b/examples/direct/releases-1.yaml new file mode 100644 index 0000000..8d0c880 --- /dev/null +++ b/examples/direct/releases-1.yaml @@ -0,0 +1,23 @@ +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: test-1 + namespace: default +spec: + chart: minecraft + repo: + name: minecraft + url: https://itzg.github.io/minecraft-server-charts + version: 3.1.3 +--- +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: "test-2" + namespace: default +spec: + chart: minecraft + repo: + name: minecraft + url: https://itzg.github.io/minecraft-server-charts + version: 3.1.4 diff --git a/examples/direct/releases-2.yaml b/examples/direct/releases-2.yaml new file mode 100644 index 0000000..d98d3a2 --- /dev/null +++ b/examples/direct/releases-2.yaml @@ -0,0 +1,22 @@ +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: test-3 + namespace: default +spec: + chart: minecraft + repo: + name: minecraft + url: https://itzg.github.io/minecraft-server-charts + version: 3.1.3 +--- +apiVersion: toolkit.ardikabs.com/v1alpha1 +kind: HelmRelease +metadata: + name: test-4 + namespace: default +spec: + chart: minecraft + repo: + name: minecraft + url: https://itzg.github.io/minecraft-server-charts diff --git a/examples/local-chart/kustomization.yaml b/examples/krm/local-chart/kustomization.yaml similarity index 100% rename from examples/local-chart/kustomization.yaml rename to examples/krm/local-chart/kustomization.yaml diff --git a/examples/local-chart/release.yaml b/examples/krm/local-chart/release.yaml similarity index 100% rename from examples/local-chart/release.yaml rename to examples/krm/local-chart/release.yaml diff --git a/examples/local-chart/test-chart/Chart.yaml b/examples/krm/local-chart/test-chart/Chart.yaml similarity index 100% rename from examples/local-chart/test-chart/Chart.yaml rename to examples/krm/local-chart/test-chart/Chart.yaml diff --git a/examples/local-chart/test-chart/templates/configmap.yaml b/examples/krm/local-chart/test-chart/templates/configmap.yaml similarity index 100% rename from examples/local-chart/test-chart/templates/configmap.yaml rename to examples/krm/local-chart/test-chart/templates/configmap.yaml diff --git a/examples/local-chart/test-chart/values.yaml b/examples/krm/local-chart/test-chart/values.yaml similarity index 100% rename from examples/local-chart/test-chart/values.yaml rename to examples/krm/local-chart/test-chart/values.yaml diff --git a/examples/local-chart/values.yaml b/examples/krm/local-chart/values.yaml similarity index 100% rename from examples/local-chart/values.yaml rename to examples/krm/local-chart/values.yaml diff --git a/examples/multi-releases/kustomization.yaml b/examples/krm/multi-releases/kustomization.yaml similarity index 100% rename from examples/multi-releases/kustomization.yaml rename to examples/krm/multi-releases/kustomization.yaml diff --git a/examples/multi-releases/release-1.yaml b/examples/krm/multi-releases/release-1.yaml similarity index 100% rename from examples/multi-releases/release-1.yaml rename to examples/krm/multi-releases/release-1.yaml diff --git a/examples/multi-releases/release-1/values.yaml b/examples/krm/multi-releases/release-1/values.yaml similarity index 100% rename from examples/multi-releases/release-1/values.yaml rename to examples/krm/multi-releases/release-1/values.yaml diff --git a/examples/multi-releases/release-2.yaml b/examples/krm/multi-releases/release-2.yaml similarity index 100% rename from examples/multi-releases/release-2.yaml rename to examples/krm/multi-releases/release-2.yaml diff --git a/examples/multi-releases/release-2/values.yaml b/examples/krm/multi-releases/release-2/values.yaml similarity index 100% rename from examples/multi-releases/release-2/values.yaml rename to examples/krm/multi-releases/release-2/values.yaml diff --git a/examples/multi-releases/release-3.yaml b/examples/krm/multi-releases/release-3.yaml similarity index 100% rename from examples/multi-releases/release-3.yaml rename to examples/krm/multi-releases/release-3.yaml diff --git a/examples/simple/kustomization.yaml b/examples/krm/simple/kustomization.yaml similarity index 100% rename from examples/simple/kustomization.yaml rename to examples/krm/simple/kustomization.yaml diff --git a/examples/simple/release.yaml b/examples/krm/simple/release.yaml similarity index 100% rename from examples/simple/release.yaml rename to examples/krm/simple/release.yaml diff --git a/examples/with-glob/config.env b/examples/krm/with-glob/config.env similarity index 100% rename from examples/with-glob/config.env rename to examples/krm/with-glob/config.env diff --git a/examples/with-glob/kustomization.yaml b/examples/krm/with-glob/kustomization.yaml similarity index 100% rename from examples/with-glob/kustomization.yaml rename to examples/krm/with-glob/kustomization.yaml diff --git a/examples/with-glob/release.yaml b/examples/krm/with-glob/release.yaml similarity index 100% rename from examples/with-glob/release.yaml rename to examples/krm/with-glob/release.yaml diff --git a/examples/with-glob/secret.env b/examples/krm/with-glob/secret.env similarity index 100% rename from examples/with-glob/secret.env rename to examples/krm/with-glob/secret.env diff --git a/examples/with-glob/values.d/common.yaml b/examples/krm/with-glob/values.d/common.yaml similarity index 100% rename from examples/with-glob/values.d/common.yaml rename to examples/krm/with-glob/values.d/common.yaml diff --git a/examples/with-glob/values.d/disabled/autoscaling.yaml b/examples/krm/with-glob/values.d/disabled/autoscaling.yaml similarity index 100% rename from examples/with-glob/values.d/disabled/autoscaling.yaml rename to examples/krm/with-glob/values.d/disabled/autoscaling.yaml diff --git a/examples/with-glob/values.d/enabled/command.yaml b/examples/krm/with-glob/values.d/enabled/command.yaml similarity index 100% rename from examples/with-glob/values.d/enabled/command.yaml rename to examples/krm/with-glob/values.d/enabled/command.yaml diff --git a/examples/with-glob/values.yaml b/examples/krm/with-glob/values.yaml similarity index 100% rename from examples/with-glob/values.yaml rename to examples/krm/with-glob/values.yaml diff --git a/examples/with-oci-repo/kustomization.yaml b/examples/krm/with-oci-repo/kustomization.yaml similarity index 100% rename from examples/with-oci-repo/kustomization.yaml rename to examples/krm/with-oci-repo/kustomization.yaml diff --git a/examples/with-oci-repo/release.yaml b/examples/krm/with-oci-repo/release.yaml similarity index 100% rename from examples/with-oci-repo/release.yaml rename to examples/krm/with-oci-repo/release.yaml diff --git a/go.mod b/go.mod index 0f1fe86..3475c6f 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20230427202446-3255accc518d + github.com/lithammer/dedent v1.1.0 github.com/spf13/cobra v1.7.0 github.com/sters/yaml-diff v1.3.1 github.com/stretchr/testify v1.8.4 @@ -76,7 +77,6 @@ require ( github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect github.com/lib/pq v1.10.9 // indirect github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect - github.com/lithammer/dedent v1.1.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect diff --git a/main.go b/main.go index 83f9978..5ed6061 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,6 @@ func main() { root.AddCommand(cmd.MakeVersion()) if err := root.Execute(); err != nil { - os.Exit(1) + os.Exit(2) } } diff --git a/internal/errs/errs.go b/pkg/errs/errs.go similarity index 100% rename from internal/errs/errs.go rename to pkg/errs/errs.go diff --git a/internal/helm/client.go b/pkg/helm/client.go similarity index 100% rename from internal/helm/client.go rename to pkg/helm/client.go diff --git a/internal/helm/clientopts.go b/pkg/helm/clientopts.go similarity index 100% rename from internal/helm/clientopts.go rename to pkg/helm/clientopts.go diff --git a/internal/helm/helm.go b/pkg/helm/helm.go similarity index 95% rename from internal/helm/helm.go rename to pkg/helm/helm.go index 07bb483..a500ffd 100644 --- a/internal/helm/helm.go +++ b/pkg/helm/helm.go @@ -9,8 +9,8 @@ import ( "path/filepath" "strings" - "github.com/ardikabs/helmize/internal/errs" - "github.com/ardikabs/helmize/internal/util" + "github.com/ardikabs/helmize/pkg/errs" + "github.com/ardikabs/helmize/pkg/util" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/cli" @@ -24,10 +24,9 @@ import ( var settings = cli.New() type DefaultHelmRenderer struct { - writer io.Writer - client *clientFactory - repo HelmRepo - isOCIRepo bool + writer io.Writer + client *clientFactory + repo HelmRepo } func NewHelmRenderer(writer io.Writer, helmRepo HelmRepo) (*DefaultHelmRenderer, error) { diff --git a/internal/helm/helm_test.go b/pkg/helm/helm_test.go similarity index 100% rename from internal/helm/helm_test.go rename to pkg/helm/helm_test.go diff --git a/internal/helm/release.go b/pkg/helm/release.go similarity index 98% rename from internal/helm/release.go rename to pkg/helm/release.go index 9e97359..2530012 100644 --- a/internal/helm/release.go +++ b/pkg/helm/release.go @@ -5,7 +5,7 @@ import ( "fmt" "io" - "github.com/ardikabs/helmize/internal/errs" + "github.com/ardikabs/helmize/pkg/errs" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) diff --git a/internal/helm/repo.go b/pkg/helm/repo.go similarity index 98% rename from internal/helm/repo.go rename to pkg/helm/repo.go index fbd0f2e..a4ac498 100644 --- a/internal/helm/repo.go +++ b/pkg/helm/repo.go @@ -5,7 +5,7 @@ import ( "fmt" "os" - "github.com/ardikabs/helmize/internal/errs" + "github.com/ardikabs/helmize/pkg/errs" "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/registry" diff --git a/internal/krm/processor.go b/pkg/processor/processor.go similarity index 64% rename from internal/krm/processor.go rename to pkg/processor/processor.go index 31803e8..9cbbba1 100644 --- a/internal/krm/processor.go +++ b/pkg/processor/processor.go @@ -1,4 +1,4 @@ -package krm +package processor import ( "bytes" @@ -6,7 +6,7 @@ import ( "strings" "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" - "github.com/ardikabs/helmize/internal/helm" + "github.com/ardikabs/helmize/pkg/helm" "helm.sh/helm/v3/pkg/releaseutil" "sigs.k8s.io/yaml" ) @@ -15,21 +15,32 @@ func wrapErr(err error) error { return fmt.Errorf("%w\n", err) } -func Process(rl *fn.ResourceList) (bool, error) { +func ProcessResourceList(rl *fn.ResourceList) (bool, error) { + generatedObjects, err := ProcessKubeObjects(rl.Items) + if err != nil { + return false, err + } + + rl.Items = generatedObjects + return true, nil +} + +func ProcessKubeObjects(objs fn.KubeObjects) (fn.KubeObjects, error) { var generatedObjects fn.KubeObjects - for _, manifest := range rl.Items { + + for _, manifest := range objs { release := new(helm.Release) if err := yaml.Unmarshal([]byte(manifest.String()), &release); err != nil { - return false, wrapErr(err) + return nil, wrapErr(err) } if err := release.Validate(); err != nil { - return false, wrapErr(err) + return nil, wrapErr(err) } var buf bytes.Buffer if err := release.Render(&buf); err != nil { - return false, wrapErr(err) + return nil, wrapErr(err) } for _, manifest := range releaseutil.SplitManifests(buf.String()) { @@ -38,13 +49,12 @@ func Process(rl *fn.ResourceList) (bool, error) { if strings.Contains(err.Error(), "expected exactly one object") { continue } - return false, wrapErr(err) + return nil, wrapErr(err) } generatedObjects = append(generatedObjects, object) } } - rl.Items = generatedObjects - return true, nil + return generatedObjects, nil } diff --git a/internal/krm/processor_test.go b/pkg/processor/processor_test.go similarity index 90% rename from internal/krm/processor_test.go rename to pkg/processor/processor_test.go index 739f97f..a30de54 100644 --- a/internal/krm/processor_test.go +++ b/pkg/processor/processor_test.go @@ -1,4 +1,4 @@ -package krm_test +package processor_test import ( "os" @@ -6,8 +6,8 @@ import ( "testing" "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" - "github.com/ardikabs/helmize/internal/errs" - "github.com/ardikabs/helmize/internal/krm" + "github.com/ardikabs/helmize/pkg/errs" + "github.com/ardikabs/helmize/pkg/processor" "github.com/sters/yaml-diff/yamldiff" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -48,7 +48,7 @@ func TestKRM_Parse(t *testing.T) { in, err := os.ReadFile(filepath.Join("testdata", tc.name, "input.yaml")) require.NoError(t, err) - out, err := fn.Run(fn.ResourceListProcessorFunc(krm.Process), in) + out, err := fn.Run(fn.ResourceListProcessorFunc(processor.ProcessResourceList), in) if tc.wantErr != nil { require.ErrorIs(t, err, tc.wantErr) return diff --git a/internal/krm/testdata/default/input.yaml b/pkg/processor/testdata/default/input.yaml similarity index 100% rename from internal/krm/testdata/default/input.yaml rename to pkg/processor/testdata/default/input.yaml diff --git a/internal/krm/testdata/default/want.yaml b/pkg/processor/testdata/default/want.yaml similarity index 100% rename from internal/krm/testdata/default/want.yaml rename to pkg/processor/testdata/default/want.yaml diff --git a/internal/krm/testdata/invalid-input-apiVersion/input.yaml b/pkg/processor/testdata/invalid-input-apiVersion/input.yaml similarity index 100% rename from internal/krm/testdata/invalid-input-apiVersion/input.yaml rename to pkg/processor/testdata/invalid-input-apiVersion/input.yaml diff --git a/internal/krm/testdata/invalid-input-kind/input.yaml b/pkg/processor/testdata/invalid-input-kind/input.yaml similarity index 100% rename from internal/krm/testdata/invalid-input-kind/input.yaml rename to pkg/processor/testdata/invalid-input-kind/input.yaml diff --git a/internal/krm/testdata/invalid-input-noname/input.yaml b/pkg/processor/testdata/invalid-input-noname/input.yaml similarity index 100% rename from internal/krm/testdata/invalid-input-noname/input.yaml rename to pkg/processor/testdata/invalid-input-noname/input.yaml diff --git a/internal/krm/testdata/invalid-input-nonamespace/input.yaml b/pkg/processor/testdata/invalid-input-nonamespace/input.yaml similarity index 100% rename from internal/krm/testdata/invalid-input-nonamespace/input.yaml rename to pkg/processor/testdata/invalid-input-nonamespace/input.yaml diff --git a/internal/krm/testdata/invalid-input-norepo/input.yaml b/pkg/processor/testdata/invalid-input-norepo/input.yaml similarity index 100% rename from internal/krm/testdata/invalid-input-norepo/input.yaml rename to pkg/processor/testdata/invalid-input-norepo/input.yaml diff --git a/internal/util/util.go b/pkg/util/util.go similarity index 100% rename from internal/util/util.go rename to pkg/util/util.go From afc3abbed9d555d56829ccc69f87916838161702 Mon Sep 17 00:00:00 2001 From: Ardika Bagus Date: Fri, 3 Nov 2023 14:45:59 +0700 Subject: [PATCH 4/5] ci: add packaging as a form of container image Signed-off-by: Ardika Bagus --- .github/workflows/release.yaml | 16 +++++++++++- .goreleaser.yml | 45 ++++++++++++++++++++++++++++++++++ Dockerfile.goreleaser | 5 ++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 Dockerfile.goreleaser diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 30c701a..cc37e7a 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -3,10 +3,11 @@ name: Release on: push: tags: - - "*" + - "v*.*.*" permissions: contents: write + packages: write jobs: test: @@ -31,6 +32,19 @@ jobs: with: strip_v: true + - uses: docker/setup-qemu-action@v2 + + - uses: docker/setup-buildx-action@v2 + with: + endpoint: builders + + - name: Log in to the GitHub Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Run GoReleaser uses: goreleaser/goreleaser-action@v2 with: diff --git a/.goreleaser.yml b/.goreleaser.yml index fc56246..14b1c86 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -41,3 +41,48 @@ release: owner: ardikabs name: helmize name_template: "Version {{.Version}}" + +dockers: + - goos: linux + goarch: amd64 + use: buildx + dockerfile: .goreleaser.Dockerfile + image_templates: + - "ghcr.io/ardikabs/helmize:latest-amd64" + - "ghcr.io/ardikabs/helmize:{{ .Major }}-amd64" + - "ghcr.io/ardikabs/helmize:{{ .Major }}.{{ .Minor }}-amd64" + - "ghcr.io/ardikabs/helmize:{{ .Major }}.{{ .Minor }}.{{ .Patch }}-amd64" + build_flag_templates: + - "--builder=builders" + - "--platform=linux/amd64" + + - goos: linux + goarch: arm64 + use: buildx + dockerfile: .goreleaser.Dockerfile + image_templates: + - "ghcr.io/ardikabs/helmize:latest-amd64" + - "ghcr.io/ardikabs/helmize:{{ .Major }}-amd64" + - "ghcr.io/ardikabs/helmize:{{ .Major }}.{{ .Minor }}-amd64" + - "ghcr.io/ardikabs/helmize:{{ .Major }}.{{ .Minor }}.{{ .Patch }}-amd64" + build_flag_templates: + - "--builder=builders" + - "--platform=linux/arm64" + +docker_manifests: + - name_template: ghcr.io/ardikabs/helmize:latest + image_templates: + - ghcr.io/ardikabs/helmize:latest-amd64 + - ghcr.io/ardikabs/helmize:latest-arm64 + - name_template: ghcr.io/ardikabs/helmize:v{{ .Major }} + image_templates: + - ghcr.io/ardikabs/helmize:{{ .Major }}-amd64 + - ghcr.io/ardikabs/helmize:{{ .Major }}-arm64 + - name_template: ghcr.io/ardikabs/helmize:v{{ .Major }}.{{ .Minor }} + image_templates: + - ghcr.io/ardikabs/helmize:{{ .Major }}.{{ .Minor }}-amd64 + - ghcr.io/ardikabs/helmize:{{ .Major }}.{{ .Minor }}-arm64 + - name_template: ghcr.io/ardikabs/helmize:v{{ .Major }}.{{ .Minor }}.{{ .Patch }} + image_templates: + - ghcr.io/ardikabs/helmize:{{ .Major }}.{{ .Minor }}.{{ .Patch }}-amd64 + - ghcr.io/ardikabs/helmize:{{ .Major }}.{{ .Minor }}.{{ .Patch }}-arm64 diff --git a/Dockerfile.goreleaser b/Dockerfile.goreleaser new file mode 100644 index 0000000..017a58b --- /dev/null +++ b/Dockerfile.goreleaser @@ -0,0 +1,5 @@ +FROM ghcr.io/ardikabs/etc/shell + +USER nobody +COPY helmize /usr/bin/helmize +CMD ["/usr/bin/helmize"] \ No newline at end of file From cce24c53032248faed1dc62137edfd870fab05c2 Mon Sep 17 00:00:00 2001 From: Ardika Bagus Date: Fri, 3 Nov 2023 14:47:45 +0700 Subject: [PATCH 5/5] ci: test happens when pushing to main branch Signed-off-by: Ardika Bagus --- .github/workflows/test.yaml | 3 +++ .golangci.yml | 3 ++- pkg/helm/helm_test.go | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7e2e884..621fdf4 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,6 +1,9 @@ name: Test on: + push: + branches: + - main pull_request: branches: - main diff --git a/.golangci.yml b/.golangci.yml index 07d26a5..22ce4e3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,4 +1,5 @@ # Options for analysis running. run: tests: true - go: "1.20" + timeout: 5m + go: "1.21" diff --git a/pkg/helm/helm_test.go b/pkg/helm/helm_test.go index 0e7dc00..7f544c6 100644 --- a/pkg/helm/helm_test.go +++ b/pkg/helm/helm_test.go @@ -4,7 +4,7 @@ import ( "bytes" "testing" - "github.com/ardikabs/helmize/internal/helm" + "github.com/ardikabs/helmize/pkg/helm" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/ptr"