Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Execute helm dependency build #154

Merged
merged 27 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
cfedbde
fix(clean-up): Call env cleanup method when executing render and sync…
fritzduchardt Nov 1, 2023
fad2197
Merge remote-tracking branch 'origin/dev' into dev
fritzduchardt Nov 26, 2023
3725d6e
Merge remote-tracking branch 'origin/dev' into dev
fritzduchardt Dec 9, 2023
68f28b4
Merge remote-tracking branch 'origin/dev' into dev
fritzduchardt Dec 17, 2023
004ef70
Merge remote-tracking branch 'origin/dev' into dev
fritzduchardt Dec 21, 2023
ae83e61
Merge remote-tracking branch 'origin/dev' into dev
fritzduchardt Dec 22, 2023
2d06bc9
fix(clean-up): Call env cleanup method when executing render and sync…
fritzduchardt Nov 1, 2023
3b0ab67
Introduce helm dependencies build
fritzduchardt Dec 17, 2023
1808fc9
Refactor Argo and Helm config
fritzduchardt Dec 21, 2023
f798f50
Add logic to helm build as well as add repositories
fritzduchardt Dec 23, 2023
508434f
Formatting
fritzduchardt Dec 23, 2023
4bbd59a
Merge remote-tracking branch 'origin/dev' into dev
fritzduchardt Dec 24, 2023
0c57e5c
Merge remote-tracking branch 'origin/dev' into dev
fritzduchardt Dec 25, 2023
f069d95
Merge remote-tracking branch 'origin/dev' into dev
fritzduchardt Dec 25, 2023
6d98c85
Improve registry slugs. Build Argo Config back.
fritzduchardt Dec 26, 2023
31c7b33
Merge remote-tracking branch 'origin/dev' into execute-helm-dependenc…
fritzduchardt Dec 26, 2023
572b4a0
Merge remote-tracking branch 'origin/dev' into execute-helm-dependenc…
fritzduchardt Dec 26, 2023
7df2cba
Merge remote-tracking branch 'origin/dev' into dev
fritzduchardt Dec 26, 2023
b1bcf4e
Merge branch 'dev' into execute-helm-dependency-build
fritzduchardt Dec 27, 2023
9b964c2
Merge branch 'dev' into execute-helm-dependency-build
fritzduchardt Dec 27, 2023
9e7f8ae
Remove argocd namespace
fritzduchardt Dec 27, 2023
3cff9c0
Add support for helm oci registries
fritzduchardt Dec 27, 2023
feb7d8b
Build back helmconfig refactoring
fritzduchardt Dec 27, 2023
560aa10
Improve helm repo update logic
fritzduchardt Dec 28, 2023
9ac6ca3
revert ArgoCD-related changes
Zebradil Dec 28, 2023
5f3eb9d
Merge branch 'dev' into execute-helm-dependency-build
Zebradil Dec 28, 2023
e53d1ac
Take oci registries out of "helm repo add".
fritzduchardt Dec 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions internal/myks/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ type Application struct {
}

type HelmConfig struct {
Namespace string `yaml:"namespace"`
KubeVersion string `yaml:"kubeVersion"`
IncludeCRDs bool `yaml:"includeCRDs"`
Capabilities []string `yaml:"capabilities"`
Namespace string `yaml:"namespace"`
KubeVersion string `yaml:"kubeVersion"`
IncludeCRDs bool `yaml:"includeCRDs"`
Capabilities []string `yaml:"capabilities"`
BuildDependencies bool `yaml:"buildDependencies"`
}

var (
Expand Down
2 changes: 2 additions & 0 deletions internal/myks/assets/data-schema.ytt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ helm:
kubeVersion: ""
#! If defined, passed as a value of `--namespace` for `helm template`.
namespace: ""
#! If true, run "helm dependency build" before rendering. This is required for helm charts with dependencies that are pulled from git repositories.
buildDependencies: false
#! Configuration of the step that renders ytt-packages.
yttPkg:
#! A ytt-package can be rendered as a whole, or can contain multiple sub-packages that should be rendered separately.
Expand Down
46 changes: 46 additions & 0 deletions internal/myks/render_helm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package myks

import (
"fmt"
"path/filepath"
"strings"

Expand Down Expand Up @@ -73,6 +74,14 @@ func (h *Helm) Render(_ string) (string, error) {
var outputs []string

for _, chartDir := range chartDirs {

if helmConfig.BuildDependencies {
err = h.helmBuild(chartDir)
if err != nil {
return "", err
}
}

chartName := filepath.Base(chartDir)
var helmValuesFile string
if helmValuesFile, err = h.app.prepareValuesFile("helm", chartName); err != nil {
Expand Down Expand Up @@ -111,6 +120,43 @@ func (h *Helm) Render(_ string) (string, error) {
return strings.Join(outputs, "---\n"), nil
}

func (h *Helm) helmBuild(chartDir string) error {
chartPath := filepath.Join(chartDir, "Chart.yaml")
if exists, _ := isExist(chartDir); !exists {
return fmt.Errorf("can't locate Chart.yaml at: %s", chartPath)
}

chart, err := unmarshalYamlToMap(chartPath)
if err != nil {
return fmt.Errorf("failure to unmarshal Chart.yaml at: %s", chartPath)
}

helmCache := h.app.expandTempPath("helm-cache")
cacheArgs := []string{
"--repository-cache", filepath.Join(helmCache, "repository"),
"--repository-config", filepath.Join(helmCache, "repositories.yaml"),
}
dependencies := chart["dependencies"].([]interface{})
for _, dependency := range dependencies {
depMap := dependency.(map[string]interface{})
repo := depMap["repository"].(string)
if strings.HasPrefix(repo, "http") || strings.HasPrefix(repo, "oci://") {
args := []string{"repo", "add", createURLSlug(repo), repo, "--force-update"}
_, err := h.app.runCmd(helmStepName, "helm repo add", "helm", nil, append(args, cacheArgs...))
if err != nil {
return fmt.Errorf("failed to add repository %s in %s ", repo, chartPath)
}
}
}

buildArgs := []string{"dependencies", "build", chartDir, "--skip-refresh"}
_, err = h.app.runCmd(helmStepName, "helm dependencies build", "helm", nil, append(buildArgs, cacheArgs...))
if err != nil {
return fmt.Errorf("failed to build dependencies for chart %s", chartDir)
}
return nil
}

func (h *Helm) getHelmConfig() (HelmConfig, error) {
dataValuesYaml, err := h.app.ytt(helmStepName, "get helm config", h.app.yttDataFiles, "--data-values-inspect")
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/myks/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,11 @@ func mapToSlice(env map[string]string) []string {
}
return envSlice
}

func createURLSlug(url string) string {
url = strings.TrimPrefix(url, "http://")
url = strings.TrimPrefix(url, "https://")
url = strings.TrimPrefix(url, "oci://")
url = strings.ReplaceAll(url, "/", "-")
return url
}
24 changes: 24 additions & 0 deletions internal/myks/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,27 @@ func Test_extract(t *testing.T) {
})
}
}

func Test_createURLSlug(t *testing.T) {
type args struct {
input string
}
tests := []struct {
name string
args args
want string
}{
{"bitnami", args{"https://charts.bitnami.com/bitnami"}, "charts.bitnami.com-bitnami"},
{"stable", args{"https://charts.helm.sh/stable"}, "charts.helm.sh-stable"},
{"grafana", args{"https://grafana.github.io/helm-charts"}, "grafana.github.io-helm-charts"},
{"nginx", args{"https://helm.nginx.com/stable"}, "helm.nginx.com-stable"},
{"oci", args{"oci://registry-1.docker.io/bitnamicharts"}, "registry-1.docker.io-bitnamicharts"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := createURLSlug(tt.args.input); got != tt.want {
t.Errorf("createURLSlug() = %v, want %v", got, tt.want)
}
})
}
}
Loading