Skip to content

Commit

Permalink
Merge pull request #56 from puzzle/enhance-lint
Browse files Browse the repository at this point in the history
Assure all dependencies are present before linting
  • Loading branch information
RetGal authored Jan 7, 2025
2 parents 12aa1da + c56879e commit 823716b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
27 changes: 26 additions & 1 deletion helm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,14 @@ func (h *Helm) Lint(
// +optional
args []string,
) (string, error) {
c := h.createContainer(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
Expand All @@ -196,6 +203,24 @@ func (h *Helm) Lint(
return out, nil
}

func (h *Helm) hasMissingDependencies(
// method call context
ctx context.Context,
// directory that contains the Helm Chart
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", "helm dep update"}).Directory("charts")
}

func (h *Helm) createContainer(
// directory that contains the Helm Chart
directory *dagger.Directory,
Expand Down
15 changes: 15 additions & 0 deletions tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}
30 changes: 30 additions & 0 deletions tests/testdata/mydependentchart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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/

0 comments on commit 823716b

Please sign in to comment.