-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read helm chart directly from the embedded directory
Signed-off-by: Utku Ozdemir <[email protected]>
- Loading branch information
1 parent
4958ad5
commit 8495c90
Showing
5 changed files
with
97 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters