feat: working helm template

This commit is contained in:
2023-04-07 00:06:38 +02:00
parent 44575dd763
commit 3ab20c8a87
7 changed files with 68 additions and 42 deletions

View File

@@ -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
}