From d38d04c0d86f6708ee85e2951844df23e6360039 Mon Sep 17 00:00:00 2001 From: Andrej Petras Date: Mon, 17 Jun 2024 22:09:02 +0200 Subject: [PATCH] feat: add helm repos from deps, update project dependencies --- .github/workflows/default.yaml | 2 +- .github/workflows/master.yaml | 2 +- .github/workflows/release.yaml | 2 +- Dockerfile | 21 ++++++++++----- cmd/helm.go | 49 ++++++++++++++++++++++++++++++++++ cmd/helm_build.go | 5 ++++ cmd/helm_deps_update.go | 5 ++++ cmd/helm_deps_validate.go | 5 ++++ cmd/helm_lock_update.go | 6 +++++ go.mod | 8 +++--- go.sum | 9 ++++--- 11 files changed, 95 insertions(+), 19 deletions(-) diff --git a/.github/workflows/default.yaml b/.github/workflows/default.yaml index 8a7ecd6..0de1b99 100644 --- a/.github/workflows/default.yaml +++ b/.github/workflows/default.yaml @@ -14,7 +14,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: 1.21 + go-version: 1.22 - name: golangci-lint uses: golangci/golangci-lint-action@v3 - uses: docker/login-action@v3 diff --git a/.github/workflows/master.yaml b/.github/workflows/master.yaml index dbe5ae9..67e93fe 100644 --- a/.github/workflows/master.yaml +++ b/.github/workflows/master.yaml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: 1.21 + go-version: 1.22 - uses: golangci/golangci-lint-action@v3 - uses: docker/login-action@v3 with: diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c2039c8..abbc4ab 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: 1.21 + go-version: 1.22 - uses: golangci/golangci-lint-action@v3 - uses: docker/login-action@v3 with: diff --git a/Dockerfile b/Dockerfile index f3e0519..937d665 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,21 @@ -FROM alpine/helm:3.10.2 as helm +FROM alpine/helm:3.15.2 as helm -FROM debian:10.11-slim +FROM debian:12-slim COPY --from=helm /usr/bin/helm /usr/bin/helm RUN apt-get update \ - && apt-get install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common curl ca-certificates \ - && curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - \ - && add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" \ - && apt-get update \ - && apt-get install -y docker-ce + && apt-get install -y ca-certificates curl \ + && install -m 0755 -d /etc/apt/keyrings \ + && curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc \ + && chmod a+r /etc/apt/keyrings/docker.asc + +RUN echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \ + $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ + tee /etc/apt/sources.list.d/docker.list > /dev/null + +RUN apt-get update \ + && apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin COPY samo /usr/bin/samo diff --git a/cmd/helm.go b/cmd/helm.go index f9e5a2d..c3390f9 100644 --- a/cmd/helm.go +++ b/cmd/helm.go @@ -28,6 +28,7 @@ type helmFlags struct { Dir string `mapstructure:"helm-dir"` Registry string `mapstructure:"helm-registry"` AbsoluteDir bool `mapstructure:"helm-absolute-dir"` + AddRepoDeps bool `mapstructure:"helm-add-repo-deps"` } func createHelmCmd() *cobra.Command { @@ -48,6 +49,7 @@ func createHelmCmd() *cobra.Command { addStringFlag(cmd, "helm-push-type", "", "harbor", "helm repository push type. Values: upload,harbor [deprecated]") addStringFlag(cmd, "helm-registry", "", "", "helm OCI registry") addBoolFlag(cmd, "helm-absolute-dir", "", false, "helm chart absolute directory (skip add project name in path)") + addBoolFlag(cmd, "helm-add-repo-deps", "", false, "add https repositories from dependencies") addChildCmd(cmd, createHelmBuildCmd()) addChildCmd(cmd, createHelmPushCmd()) @@ -77,6 +79,53 @@ func helmClean(flags helmFlags) { } } +func helmAddRepoDeps(h *chart.Chart) { + + index := false + for _, d := range h.Metadata.Dependencies { + repo := d.Repository + if len(repo) > 0 && strings.HasPrefix(repo, "https://") { + helmAddRepository(repo) + index = true + } + } + + // update index of the added repository + if index { + tools.ExecCmd("helm", "repo", "update") + } +} + +func helmAddRepository(repo string) { + + // create name from URL + name := strings.TrimPrefix(repo, "https://") + index := strings.Index(name, "/") + if index >= 0 { + name = name[:index] + } + name = envRegexp.ReplaceAllString(name, "_") + + // add repository + var command []string + command = append(command, "repo", "add") + command = append(command, name, repo) + tools.ExecCmd("helm", command...) +} + +var envRegexp = createRegexp() + +// initialize yaml file configuration source +func createRegexp() *regexp.Regexp { + + // initialize the regex for EnvConfigSource + tmp, err := regexp.Compile("[^a-zA-Z0-9]+") + if err != nil { + panic(err) + } + return tmp +} + // deprecated func helmAddRepo(flags helmFlags) { if len(flags.Repo) == 0 { diff --git a/cmd/helm_build.go b/cmd/helm_build.go index f0faf16..41c8b08 100644 --- a/cmd/helm_build.go +++ b/cmd/helm_build.go @@ -50,6 +50,11 @@ func helmBuild(project *Project, flags helmBuildFlags) { helmClean(flags.Helm) // add and update custom helm repo helmAddRepo(flags.Helm) + // add repo from chart dependencies + if flags.Helm.AddRepoDeps { + chart := loadChart(project, flags.Helm) + helmAddRepoDeps(chart) + } // filter resources to output dir buildHelmChart(flags, project) diff --git a/cmd/helm_deps_update.go b/cmd/helm_deps_update.go index c570fc5..1d26341 100644 --- a/cmd/helm_deps_update.go +++ b/cmd/helm_deps_update.go @@ -41,6 +41,11 @@ func helmDepsUpdate(project *Project, flags helmDepsUpdateFlags) { c := loadChart(project, flags.Helm) + // add repo from chart dependencies + if flags.Helm.AddRepoDeps { + helmAddRepoDeps(c) + } + notFound := true update := false for _, d := range c.Metadata.Dependencies { diff --git a/cmd/helm_deps_validate.go b/cmd/helm_deps_validate.go index 212cc61..2e92264 100644 --- a/cmd/helm_deps_validate.go +++ b/cmd/helm_deps_validate.go @@ -35,6 +35,11 @@ func helmDepsValidate(project *Project, flags helmDepsValidateFlags) { chart := loadChart(project, flags.Helm) failed := false + // add repo from chart dependencies + if flags.Helm.AddRepoDeps { + helmAddRepoDeps(chart) + } + log.Info("Dependencies validation", log.F("validate-type", flags.ValidateType).F("chart", chart.Name()).F("version", chart.Metadata.Version)) fmt.Println("Dependencies:") diff --git a/cmd/helm_lock_update.go b/cmd/helm_lock_update.go index 2d95dd9..a3c8c4a 100644 --- a/cmd/helm_lock_update.go +++ b/cmd/helm_lock_update.go @@ -32,6 +32,12 @@ func createHelmLockUpdateCmd() *cobra.Command { func helmLockUpdate(project *Project, flags helmLockUpdateFlags) { + // add repo from chart dependencies + if flags.Helm.AddRepoDeps { + chart := loadChart(project, flags.Helm) + helmAddRepoDeps(chart) + } + dir := helmDir(project, flags.Helm) // update helm Chart.lock diff --git a/go.mod b/go.mod index a0f304f..094ff77 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/lorislab/samo -go 1.21 - -toolchain go1.21.4 +go 1.22.0 require ( github.com/Masterminds/semver/v3 v3.2.1 @@ -14,7 +12,7 @@ require ( github.com/spf13/viper v1.19.0 go.hein.dev/go-version v0.1.0 gopkg.in/yaml.v2 v2.4.0 - helm.sh/helm/v3 v3.14.3 + helm.sh/helm/v3 v3.15.2 ) require ( @@ -28,7 +26,7 @@ require ( golang.org/x/sys v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect - sigs.k8s.io/yaml v1.3.0 // indirect + sigs.k8s.io/yaml v1.4.0 // indirect ) require github.com/rs/zerolog v1.33.0 diff --git a/go.sum b/go.sum index 0e67b7a..43830a7 100644 --- a/go.sum +++ b/go.sum @@ -45,6 +45,7 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= @@ -233,9 +234,9 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -helm.sh/helm/v3 v3.14.3 h1:HmvRJlwyyt9HjgmAuxHbHv3PhMz9ir/XNWHyXfmnOP4= -helm.sh/helm/v3 v3.14.3/go.mod h1:v6myVbyseSBJTzhmeE39UcPLNv6cQK6qss3dvgAySaE= +helm.sh/helm/v3 v3.15.2 h1:/3XINUFinJOBjQplGnjw92eLGpgXXp1L8chWPkCkDuw= +helm.sh/helm/v3 v3.15.2/go.mod h1:FzSIP8jDQaa6WAVg9F+OkKz7J0ZmAga4MABtTbsb9WQ= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= -sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= -sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= +sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= +sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=