diff --git a/dagger.go b/dagger.go index e4cf878..1b105af 100644 --- a/dagger.go +++ b/dagger.go @@ -2,7 +2,9 @@ package daggerhelm import ( "context" + "errors" "fmt" + "log" "os" "path" "text/template" @@ -12,11 +14,17 @@ import ( const CONTAINER_IMAGE = "docker.io/alpine/k8s:1.26.3" -func Compile(ctx context.Context, client *dagger.Client, chartPath string, valuesFilePath string) (*dagger.Directory, error) { +func Compile( + ctx context.Context, + client *dagger.Client, + chartPath string, + valuesFilePath string, +) (*dagger.Directory, error) { chart := client.Host().Directory(chartPath) tmpdir := os.TempDir() + tmpdir = path.Join(tmpdir, "helm-dagger") err := os.MkdirAll(tmpdir, 0755) if err != nil { return nil, fmt.Errorf("failed to create tmpdir: %v", err) @@ -34,7 +42,16 @@ func Compile(ctx context.Context, client *dagger.Client, chartPath string, value // } //}() - tmpl, err := template.New("helm-dagger").ParseFiles(valuesFilePath) + if _, err := os.Stat(valuesFilePath); errors.Is(err, os.ErrNotExist) { + return nil, fmt.Errorf("the values file does not exist: %v", err) + } + + contents, err := os.ReadFile(valuesFilePath) + if err != nil { + return nil, fmt.Errorf("failed to read values file: %v", err) + } + + tmpl, err := template.New(valuesFilePath).Delims("[[", "]]").Parse(string(contents)) if err != nil { return nil, fmt.Errorf("failed to build template for values.yaml, err: %v", err) } @@ -53,22 +70,21 @@ func Compile(ctx context.Context, client *dagger.Client, chartPath string, value return nil, fmt.Errorf("failed to read tmpdir. err: %v", err) } - valuesFileDirPath := path.Dir(tmpdir) + valuesFileDirPath := tmpdir valuesFileRelativePath := files[0].Name() + log.Println(valuesFileRelativePath) valuesFile := client.Host().Directory(valuesFileDirPath).File(valuesFileRelativePath) - helmoutput := client.Container().From(CONTAINER_IMAGE).WithWorkdir("/mnt").WithDirectory("chart", chart).WithFile("values.yaml", valuesFile).WithExec([]string{"helm", "template", ".", "--output-dir", "/mnt/output"}) + helmoutput := client.Container(). + From(CONTAINER_IMAGE). + WithWorkdir("/mnt"). + WithDirectory("chart", chart). + WithFile("chart/values.yaml", valuesFile). + WithWorkdir("/mnt/chart"). + WithExec([]string{"helm", "template", ".", "--output-dir", "/mnt/output"}) if exitCode, err := helmoutput.ExitCode(ctx); err != nil || exitCode != 0 { return nil, fmt.Errorf("failed to render helm chart: %v", err) } - exported, err := helmoutput.Directory("/mnt/output").Export(ctx, "dist") - if err != nil { - return nil, fmt.Errorf("failed to export helm template: %v", err) - } - if !exported { - return nil, fmt.Errorf("nothing was exported") - } - - return nil, nil + return helmoutput.Directory("/mnt/output"), nil } diff --git a/internal/accept_test/compile_test.go b/internal/accept_test/compile_test.go index 75d30ca..1c39dd1 100644 --- a/internal/accept_test/compile_test.go +++ b/internal/accept_test/compile_test.go @@ -16,11 +16,26 @@ func TestCompile(t *testing.T) { ctx := context.Background() client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) assert.NoError(t, err) + t.Cleanup(func() { + _ = os.RemoveAll("test_data/dist") + }) t.Run("can generate nginx helm chart", func(t *testing.T) { + + _ = os.RemoveAll("test_data/dist/nginx") + ctx := context.Background() - _, err := daggerhelm.Compile(ctx, client, "test_data/nginx", "test_data/nginx.values.yaml") + helmoutput, err := daggerhelm.Compile(ctx, client, "test_data/nginx", "test_data/nginx.values.yaml") + + exported, err := helmoutput.Export(ctx, "test_data/dist") assert.NoError(t, err) + assert.True(t, exported) + + dir, err := os.ReadDir("test_data/dist/nginx/templates") + + assert.NoError(t, err) + + assert.Equal(t, 2, len(dir)) }) } diff --git a/internal/accept_test/test_data/nginx.values.yaml b/internal/accept_test/test_data/nginx.values.yaml index 5cc8ecd..ac9c3fe 100644 --- a/internal/accept_test/test_data/nginx.values.yaml +++ b/internal/accept_test/test_data/nginx.values.yaml @@ -13,7 +13,6 @@ global: ## - myRegistryKeySecretName ## imagePullSecrets: [] - ## @section Common parameters ## @param nameOverride String to partially override nginx.fullname template (will maintain the release name) @@ -40,7 +39,6 @@ commonLabels: {} ## @param commonAnnotations Add annotations to all the deployed resources ## commonAnnotations: {} - ## Enable diagnostic mode in the deployment(s)/statefulset(s) ## diagnosticMode: @@ -55,7 +53,6 @@ diagnosticMode: ## args: - infinity - ## @section NGINX parameters ## Bitnami NGINX image version @@ -112,7 +109,6 @@ extraEnvVarsCM: "" ## @param extraEnvVarsSecret Secret with extra environment variables ## extraEnvVarsSecret: "" - ## @section NGINX deployment parameters ## @param replicaCount Number of NGINX replicas to deploy @@ -255,7 +251,6 @@ resources: ## cpu: 100m ## memory: 128Mi requests: {} - ## NGINX containers' lifecycleHooks ## ref: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ ## ref: https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/ @@ -263,15 +258,15 @@ resources: ## lines, adjust them as necessary, and remove the curly braces on 'lifecycle:{}'. ## @param lifecycleHooks Optional lifecycleHooks for the NGINX container lifecycleHooks: {} - ## Example: - ## postStart: - ## exec: - ## command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"] - ## Example: - ## preStop: - ## exec: - ## command: ["/bin/sleep", "20"] - ## command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"] +## Example: +## postStart: +## exec: +## command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"] +## Example: +## preStop: +## exec: +## command: ["/bin/sleep", "20"] +## command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"] ## NGINX containers' startup probe. ## ref: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes @@ -377,12 +372,10 @@ serviceAccount: ## containerPort: 1234 ## sidecars: [] - ## @param sidecarSingleProcessNamespace Enable sharing the process namespace with sidecars ## This will switch pod.spec.shareProcessNamespace parameter ## sidecarSingleProcessNamespace: false - ## @param initContainers Extra init containers ## initContainers: [] @@ -399,7 +392,6 @@ pdb: ## @param pdb.maxUnavailable Max number of pods that can be unavailable after the eviction ## maxUnavailable: 0 - ## @section Custom NGINX application parameters ## Get the server static content from a git repository @@ -506,7 +498,6 @@ staticSiteConfigmap: "" ## NOTE: This will override staticSiteConfigmap ## staticSitePVC: "" - ## @section Traffic Exposure parameters ## NGINX Service properties @@ -749,7 +740,6 @@ healthIngress: ## Useful when looking for additional customization, such as using different backend ## extraRules: [] - ## @section Metrics parameters ## Prometheus Exporter / Metrics diff --git a/shuttletask/format.go b/shuttletask/format.go index 126b8e9..40fa7a7 100644 --- a/shuttletask/format.go +++ b/shuttletask/format.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "log" "os" "dagger.io/dagger" @@ -41,6 +42,9 @@ func format(ctx context.Context, optsFuncs ...OptsFunc) error { } } + log.Println("dagger") + log.Printf("%+v", os.Environ()) + client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) if err != nil { return fmt.Errorf("could not connect to dagger: %v", err) diff --git a/shuttletask/go.mod b/shuttletask/go.mod index d0e8283..ca74733 100644 --- a/shuttletask/go.mod +++ b/shuttletask/go.mod @@ -2,10 +2,9 @@ module shuttletask go 1.20 - require ( dagger.io/dagger v0.5.2 - github.com/stretchr/testify v1.8.1 + github.com/stretchr/testify v1.8.2 ) require ( @@ -16,8 +15,8 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/vektah/gqlparser/v2 v2.5.1 // indirect golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect - golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect + golang.org/x/sys v0.2.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/lunarway/shuttle => github.com/kjuulh/shuttle v0.0.0-20230406144450-c44ea3d9ff41 +replace github.com/lunarway/shuttle => github.com/kjuulh/shuttle v0.0.0-20230406204032-939c1779c5f3 diff --git a/shuttletask/go.sum b/shuttletask/go.sum index ad2af6d..8140790 100644 --- a/shuttletask/go.sum +++ b/shuttletask/go.sum @@ -55,8 +55,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= github.com/vektah/gqlparser/v2 v2.4.0/go.mod h1:flJWIR04IMQPGz+BXLrORkrARBxv/rtyIAFvd/MceW0= github.com/vektah/gqlparser/v2 v2.4.5/go.mod h1:flJWIR04IMQPGz+BXLrORkrARBxv/rtyIAFvd/MceW0= @@ -92,8 +92,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/shuttletask/testdata/test_module/very_long_line.go b/shuttletask/testdata/test_module/very_long_line.go index 694a446..eb17348 100644 --- a/shuttletask/testdata/test_module/very_long_line.go +++ b/shuttletask/testdata/test_module/very_long_line.go @@ -1,5 +1,7 @@ package testmodule -func SomeFunction(with, lots, of, parameters, more, more, more, more, more string) (and string, lots string, of string, someerror string, returns string) { +func SomeFunction( + with, lots, of, parameters, more, more, more, more, more string, +) (and string, lots string, of string, someerror string, returns string) { return "", "", "", "", "" }