feat: working helm template

This commit is contained in:
Kasper Juul Hermansen 2023-04-07 00:06:38 +02:00
parent 44575dd763
commit 3ab20c8a87
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
7 changed files with 68 additions and 42 deletions

View File

@ -2,7 +2,9 @@ package daggerhelm
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"log"
"os" "os"
"path" "path"
"text/template" "text/template"
@ -12,11 +14,17 @@ import (
const CONTAINER_IMAGE = "docker.io/alpine/k8s:1.26.3" 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) chart := client.Host().Directory(chartPath)
tmpdir := os.TempDir() tmpdir := os.TempDir()
tmpdir = path.Join(tmpdir, "helm-dagger")
err := os.MkdirAll(tmpdir, 0755) err := os.MkdirAll(tmpdir, 0755)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create tmpdir: %v", err) 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 { if err != nil {
return nil, fmt.Errorf("failed to build template for values.yaml, err: %v", err) 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) return nil, fmt.Errorf("failed to read tmpdir. err: %v", err)
} }
valuesFileDirPath := path.Dir(tmpdir) valuesFileDirPath := tmpdir
valuesFileRelativePath := files[0].Name() valuesFileRelativePath := files[0].Name()
log.Println(valuesFileRelativePath)
valuesFile := client.Host().Directory(valuesFileDirPath).File(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 { if exitCode, err := helmoutput.ExitCode(ctx); err != nil || exitCode != 0 {
return nil, fmt.Errorf("failed to render helm chart: %v", err) return nil, fmt.Errorf("failed to render helm chart: %v", err)
} }
exported, err := helmoutput.Directory("/mnt/output").Export(ctx, "dist") return helmoutput.Directory("/mnt/output"), nil
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
} }

View File

@ -16,11 +16,26 @@ func TestCompile(t *testing.T) {
ctx := context.Background() ctx := context.Background()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
assert.NoError(t, err) assert.NoError(t, err)
t.Cleanup(func() {
_ = os.RemoveAll("test_data/dist")
})
t.Run("can generate nginx helm chart", func(t *testing.T) { t.Run("can generate nginx helm chart", func(t *testing.T) {
_ = os.RemoveAll("test_data/dist/nginx")
ctx := context.Background() 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.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))
}) })
} }

View File

@ -13,7 +13,6 @@ global:
## - myRegistryKeySecretName ## - myRegistryKeySecretName
## ##
imagePullSecrets: [] imagePullSecrets: []
## @section Common parameters ## @section Common parameters
## @param nameOverride String to partially override nginx.fullname template (will maintain the release name) ## @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 ## @param commonAnnotations Add annotations to all the deployed resources
## ##
commonAnnotations: {} commonAnnotations: {}
## Enable diagnostic mode in the deployment(s)/statefulset(s) ## Enable diagnostic mode in the deployment(s)/statefulset(s)
## ##
diagnosticMode: diagnosticMode:
@ -55,7 +53,6 @@ diagnosticMode:
## ##
args: args:
- infinity - infinity
## @section NGINX parameters ## @section NGINX parameters
## Bitnami NGINX image version ## Bitnami NGINX image version
@ -112,7 +109,6 @@ extraEnvVarsCM: ""
## @param extraEnvVarsSecret Secret with extra environment variables ## @param extraEnvVarsSecret Secret with extra environment variables
## ##
extraEnvVarsSecret: "" extraEnvVarsSecret: ""
## @section NGINX deployment parameters ## @section NGINX deployment parameters
## @param replicaCount Number of NGINX replicas to deploy ## @param replicaCount Number of NGINX replicas to deploy
@ -255,7 +251,6 @@ resources:
## cpu: 100m ## cpu: 100m
## memory: 128Mi ## memory: 128Mi
requests: {} requests: {}
## NGINX containers' lifecycleHooks ## NGINX containers' lifecycleHooks
## ref: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ ## ref: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/
## ref: https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/ ## 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:{}'. ## lines, adjust them as necessary, and remove the curly braces on 'lifecycle:{}'.
## @param lifecycleHooks Optional lifecycleHooks for the NGINX container ## @param lifecycleHooks Optional lifecycleHooks for the NGINX container
lifecycleHooks: {} lifecycleHooks: {}
## Example: ## Example:
## postStart: ## postStart:
## exec: ## exec:
## command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"] ## command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
## Example: ## Example:
## preStop: ## preStop:
## exec: ## exec:
## command: ["/bin/sleep", "20"] ## command: ["/bin/sleep", "20"]
## command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"] ## command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]
## NGINX containers' startup probe. ## NGINX containers' startup probe.
## ref: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes ## ref: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes
@ -377,12 +372,10 @@ serviceAccount:
## containerPort: 1234 ## containerPort: 1234
## ##
sidecars: [] sidecars: []
## @param sidecarSingleProcessNamespace Enable sharing the process namespace with sidecars ## @param sidecarSingleProcessNamespace Enable sharing the process namespace with sidecars
## This will switch pod.spec.shareProcessNamespace parameter ## This will switch pod.spec.shareProcessNamespace parameter
## ##
sidecarSingleProcessNamespace: false sidecarSingleProcessNamespace: false
## @param initContainers Extra init containers ## @param initContainers Extra init containers
## ##
initContainers: [] initContainers: []
@ -399,7 +392,6 @@ pdb:
## @param pdb.maxUnavailable Max number of pods that can be unavailable after the eviction ## @param pdb.maxUnavailable Max number of pods that can be unavailable after the eviction
## ##
maxUnavailable: 0 maxUnavailable: 0
## @section Custom NGINX application parameters ## @section Custom NGINX application parameters
## Get the server static content from a git repository ## Get the server static content from a git repository
@ -506,7 +498,6 @@ staticSiteConfigmap: ""
## NOTE: This will override staticSiteConfigmap ## NOTE: This will override staticSiteConfigmap
## ##
staticSitePVC: "" staticSitePVC: ""
## @section Traffic Exposure parameters ## @section Traffic Exposure parameters
## NGINX Service properties ## NGINX Service properties
@ -749,7 +740,6 @@ healthIngress:
## Useful when looking for additional customization, such as using different backend ## Useful when looking for additional customization, such as using different backend
## ##
extraRules: [] extraRules: []
## @section Metrics parameters ## @section Metrics parameters
## Prometheus Exporter / Metrics ## Prometheus Exporter / Metrics

View File

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"os" "os"
"dagger.io/dagger" "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)) client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil { if err != nil {
return fmt.Errorf("could not connect to dagger: %v", err) return fmt.Errorf("could not connect to dagger: %v", err)

View File

@ -2,10 +2,9 @@ module shuttletask
go 1.20 go 1.20
require ( require (
dagger.io/dagger v0.5.2 dagger.io/dagger v0.5.2
github.com/stretchr/testify v1.8.1 github.com/stretchr/testify v1.8.2
) )
require ( require (
@ -16,8 +15,8 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vektah/gqlparser/v2 v2.5.1 // indirect github.com/vektah/gqlparser/v2 v2.5.1 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // 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 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

View File

@ -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.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.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.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 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/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.0/go.mod h1:flJWIR04IMQPGz+BXLrORkrARBxv/rtyIAFvd/MceW0=
github.com/vektah/gqlparser/v2 v2.4.5/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-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-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-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 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/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.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

View File

@ -1,5 +1,7 @@
package testmodule 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 "", "", "", "", "" return "", "", "", "", ""
} }