From f98540b8790d50fcdef8e53cd65b85454d90e19e Mon Sep 17 00:00:00 2001 From: Reto Galante Date: Wed, 18 Dec 2024 16:29:57 +0100 Subject: [PATCH 1/5] Assure all dependencies are present before linting --- helm/main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/helm/main.go b/helm/main.go index 662616d..7104324 100644 --- a/helm/main.go +++ b/helm/main.go @@ -187,7 +187,7 @@ func (h *Helm) Lint( // +optional args []string, ) (string, error) { - c := h.createContainer(directory) + c := h.createContainer(directory).WithMountedDirectory("./charts", h.dependencyUpdate(ctx, directory)) out, err := c.WithExec([]string{"sh", "-c", fmt.Sprintf("%s %s", "helm lint", strings.Join(args, " "))}).Stdout(ctx) if err != nil { return "", err @@ -196,6 +196,16 @@ func (h *Helm) Lint( return out, nil } +func (h *Helm) dependencyUpdate( + // method call context + ctx context.Context, + // directory that contains the Helm Chart + directory *dagger.Directory, +) (*dagger.Directory) { + c := h.createContainer(directory) + return c.WithExec([]string{"sh", "-c", "helm dep update"}).Directory("./charts") +} + func (h *Helm) createContainer( // directory that contains the Helm Chart directory *dagger.Directory, From a22306af165c3107a7317e9d03764ef999f220b1 Mon Sep 17 00:00:00 2001 From: Reto Galante Date: Wed, 18 Dec 2024 17:45:37 +0100 Subject: [PATCH 2/5] Create charts directory --- helm/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helm/main.go b/helm/main.go index 7104324..a2edc07 100644 --- a/helm/main.go +++ b/helm/main.go @@ -203,7 +203,7 @@ func (h *Helm) dependencyUpdate( directory *dagger.Directory, ) (*dagger.Directory) { c := h.createContainer(directory) - return c.WithExec([]string{"sh", "-c", "helm dep update"}).Directory("./charts") + return c.WithExec([]string{"sh", "-c", "mkdir charts && helm dep update"}).Directory("charts") } func (h *Helm) createContainer( From ed7c45eb0d7f8b8f63965b863993920f300aa62f Mon Sep 17 00:00:00 2001 From: Reto Galante Date: Tue, 7 Jan 2025 10:08:13 +0100 Subject: [PATCH 3/5] Add missing dependencies detection --- helm/main.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/helm/main.go b/helm/main.go index a2edc07..bc38104 100644 --- a/helm/main.go +++ b/helm/main.go @@ -187,7 +187,14 @@ func (h *Helm) Lint( // +optional args []string, ) (string, error) { - c := h.createContainer(directory).WithMountedDirectory("./charts", h.dependencyUpdate(ctx, directory)) + var c *dagger.Container + + if h.hasMissingDependencies(ctx, directory) { + c = h.createContainer(directory).WithMountedDirectory("./charts", h.dependencyUpdate(directory)) + } else { + c = h.createContainer(directory) + } + out, err := c.WithExec([]string{"sh", "-c", fmt.Sprintf("%s %s", "helm lint", strings.Join(args, " "))}).Stdout(ctx) if err != nil { return "", err @@ -196,14 +203,22 @@ func (h *Helm) Lint( return out, nil } -func (h *Helm) dependencyUpdate( +func (h *Helm) hasMissingDependencies( // method call context ctx context.Context, // directory that contains the Helm Chart directory *dagger.Directory, -) (*dagger.Directory) { +) bool { + _, err := h.createContainer(directory).WithExec([]string{"sh", "-c", "helm dep list | grep missing"}).Stdout(ctx) + return err == nil +} + +func (h *Helm) dependencyUpdate( + // directory that contains the Helm Chart + directory *dagger.Directory, +) *dagger.Directory { c := h.createContainer(directory) - return c.WithExec([]string{"sh", "-c", "mkdir charts && helm dep update"}).Directory("charts") + return c.WithExec([]string{"sh", "-c", "helm dep update"}).Directory("charts") } func (h *Helm) createContainer( From e85577eae695846a39038c23fe789afd36031086 Mon Sep 17 00:00:00 2001 From: Reto Galante Date: Tue, 7 Jan 2025 10:28:16 +0100 Subject: [PATCH 4/5] Add test for dependencies detection --- tests/main.go | 15 +++++++++++ tests/testdata/mydependentchart/Chart.yaml | 30 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 tests/testdata/mydependentchart/Chart.yaml diff --git a/tests/main.go b/tests/main.go index cf85a00..d389dbc 100644 --- a/tests/main.go +++ b/tests/main.go @@ -19,6 +19,7 @@ func (m *Go) All(ctx context.Context) error { p.Go(m.HelmLint) p.Go(m.HelmLintWithArg) p.Go(m.HelmLintWithArgs) + p.Go(m.HelmLintWithMissingDependencies) return p.Wait() } @@ -123,3 +124,17 @@ func (m *Go) HelmLintWithArgs( return nil } + +func (m *Go) HelmLintWithMissingDependencies( + // method call context + ctx context.Context, +) error { + directory := dag.CurrentModule().Source().Directory("./testdata/mydependentchart/") + _, err := dag.Helm().Lint(ctx, directory) + + if err != nil { + return err + } + + return nil +} diff --git a/tests/testdata/mydependentchart/Chart.yaml b/tests/testdata/mydependentchart/Chart.yaml new file mode 100644 index 0000000..30cb06d --- /dev/null +++ b/tests/testdata/mydependentchart/Chart.yaml @@ -0,0 +1,30 @@ +apiVersion: v2 +name: dagger-module-helm-test +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.1 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" + +# This is an optional list containing all dependencies. +dependencies: + - name: dependency-track + version: 1.8.1 + repository: https://puzzle.github.io/dependencytrack-helm/ From c56879e2c0d9dac9158fa5eee37ae13b95775574 Mon Sep 17 00:00:00 2001 From: Reto Galante Date: Tue, 7 Jan 2025 11:26:04 +0100 Subject: [PATCH 5/5] Fix indention --- tests/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/main.go b/tests/main.go index d389dbc..c1fe4f7 100644 --- a/tests/main.go +++ b/tests/main.go @@ -19,7 +19,7 @@ func (m *Go) All(ctx context.Context) error { p.Go(m.HelmLint) p.Go(m.HelmLintWithArg) p.Go(m.HelmLintWithArgs) - p.Go(m.HelmLintWithMissingDependencies) + p.Go(m.HelmLintWithMissingDependencies) return p.Wait() }