Skip to content

Commit

Permalink
feat: read helm chart directly from the embedded directory
Browse files Browse the repository at this point in the history
Signed-off-by: Utku Ozdemir <[email protected]>
  • Loading branch information
utkuozdemir committed Apr 21, 2024
1 parent 4958ad5 commit 8495c90
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 14 deletions.
7 changes: 0 additions & 7 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ tasks:
- golangci-lint run ./...
- helm lint helm/pv-migrate

update-chart:
desc: update the helm chart
cmds:
- helm-docs -c helm/pv-migrate/
- helm package helm/pv-migrate/
- mv pv-migrate-*.tgz migrator/helm-chart.tgz

clean:
desc: clean
cmds:
Expand Down
73 changes: 73 additions & 0 deletions helm/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package helm

import (
"embed"
"fmt"
"io/fs"
"path/filepath"

"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
)

// chartFS is the embedded Helm chart.
//
// Note: The prefix "all:" is important here, as otherwise the files starting with "." or "_" will be ignored.
//
// See: https://github.com/golang/go/issues/44393
//
//go:embed all:pv-migrate
var chartFS embed.FS

const rootDir = "pv-migrate"

// LoadChart loads the embedded Helm chart.
func LoadChart() (*chart.Chart, error) {
files, err := chartAsBufferedFiles()
if err != nil {
return nil, fmt.Errorf("failed to get chart files: %w", err)
}

helmChart, err := loader.LoadFiles(files)
if err != nil {
return nil, fmt.Errorf("failed to load chart: %w", err)
}

return helmChart, nil
}

func chartAsBufferedFiles() ([]*loader.BufferedFile, error) {
var files []*loader.BufferedFile

err := fs.WalkDir(chartFS, rootDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
return nil
}

data, err := chartFS.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file %q in chart: %w", path, err)
}

relativePath, err := filepath.Rel(rootDir, path)
if err != nil {
return fmt.Errorf("failed to relativize path %q: %w", path, err)
}

files = append(files, &loader.BufferedFile{
Name: relativePath,
Data: data,
})

return nil
})
if err != nil {
return nil, fmt.Errorf("failed to walk chart directory: %w", err)
}

return files, nil
}
22 changes: 22 additions & 0 deletions helm/helm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package helm_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/utkuozdemir/pv-migrate/helm"
)

func TestLoadChart(t *testing.T) {
t.Parallel()

chart, err := helm.LoadChart()
require.NoError(t, err)

assert.Equal(t, "pv-migrate", chart.Metadata.Name)
assert.NotEmpty(t, chart.Metadata.Version, "chart version should not be empty")
assert.NotEmpty(t, chart.Values, "chart values should not be empty")
assert.NotEmpty(t, chart.Templates, "chart templates should not be empty")
}
Binary file removed migrator/helm-chart.tgz
Binary file not shown.
9 changes: 2 additions & 7 deletions migrator/migrator.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package migrator

import (
"bytes"
"context"
_ "embed" // we embed the helm chart
"errors"
"fmt"
"strings"

log "github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/chart/loader"

"github.com/utkuozdemir/pv-migrate/helm"
"github.com/utkuozdemir/pv-migrate/k8s"
"github.com/utkuozdemir/pv-migrate/migration"
"github.com/utkuozdemir/pv-migrate/pvc"
"github.com/utkuozdemir/pv-migrate/strategy"
"github.com/utkuozdemir/pv-migrate/util"
)

//go:embed helm-chart.tgz
var chartBytes []byte

const (
attemptIDLength = 5
)
Expand Down Expand Up @@ -97,7 +92,7 @@ func (m *Migrator) Run(ctx context.Context, request *migration.Request) error {
}

func (m *Migrator) buildMigration(ctx context.Context, request *migration.Request) (*migration.Migration, error) {
chart, err := loader.LoadArchive(bytes.NewReader(chartBytes))
chart, err := helm.LoadChart()
if err != nil {
return nil, fmt.Errorf("failed to load helm chart: %w", err)
}
Expand Down

0 comments on commit 8495c90

Please sign in to comment.