From 8495c90bce306e345e6839035419a45492636ddf Mon Sep 17 00:00:00 2001 From: Utku Ozdemir Date: Sun, 21 Apr 2024 13:49:51 +0200 Subject: [PATCH] feat: read helm chart directly from the embedded directory Signed-off-by: Utku Ozdemir --- Taskfile.yml | 7 ---- helm/helm.go | 73 ++++++++++++++++++++++++++++++++++++++++ helm/helm_test.go | 22 ++++++++++++ migrator/helm-chart.tgz | Bin 4954 -> 0 bytes migrator/migrator.go | 9 ++--- 5 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 helm/helm.go create mode 100644 helm/helm_test.go delete mode 100644 migrator/helm-chart.tgz diff --git a/Taskfile.yml b/Taskfile.yml index e9d44c23e..f62112f68 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -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: diff --git a/helm/helm.go b/helm/helm.go new file mode 100644 index 000000000..c70845f05 --- /dev/null +++ b/helm/helm.go @@ -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 +} diff --git a/helm/helm_test.go b/helm/helm_test.go new file mode 100644 index 000000000..8368067a7 --- /dev/null +++ b/helm/helm_test.go @@ -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") +} diff --git a/migrator/helm-chart.tgz b/migrator/helm-chart.tgz deleted file mode 100644 index 63468afca8fc9c00e041c4fc8cc0210e8c2f0e67..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4954 zcmV-g6Q%4QiwG0|00000|0w_~VMtOiV@ORlOnEsqVl!4SWK%V1T2nbTPgYhoO;>Dc zVQyr3R8em|NM&qo0PHfY&2TyZ8_Q>j!M zA|VMeNw5LXM_b;Xv43d)*cJdDAb9BT(Mgw3l~@9ScYt^Y-h+VT+>OYbA%XkH3&ezX zg`)7$-Il>%Fc=>ms=tH5!2UZNJ{vt69v%&cgJ%ch(X&T`;lb$W;1LY&n2yGha)H>R z!L4!G%zY&V#W58~5E}D|1Av$yGvtd!I`s(clL*Z*pTN=q0B|8j5uQL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TbIN$ z$|5D*DEtpDu0s5L79!#DUsJ^J6Zq!CJNr_g6$oT|*J!>Dju-ZgO)GGe?bqbPaXN2( za-Kf@fkrq%b3A$a6#lmMl}X<@&fXsUT;A>EI7`S*3VanI62cPx$wF8j+>2K)y;?!jw`uH-Aw{eF>rKXnPYv zan44ahzC4X*r2GkA*@1}dE|Ysd=0g;@@oK%?Q0c)>z&PCLuG=qFbOoTqEP4V*C427 z)vo5&*d>I7t6B5mVhFx!~`>idJ+f!Nmtn2^d0Q1hrXoOs^Y zT_>Fy-pzWaxwpeq8|f;vS?9Btq;?(lZXzglVA&#ahlhz9_{=?}?Jvn_W%e`BnS+K?vI;pql5M*Vl{etjclx4kTG*}Ka@Nq- zW;R!{>s;a12CjkA)@#|im@ZM5h&D3z+uv4JV(c>a_4^Xbew{|QXI?qkn_Tx$5cDP= zdgVovy^l8~idBZWH7VXga>J#PbvRZIyJ6Ygb<<=Wp2}7i{>x}o1ECw(-Gq))7|4q& zHS1L9#ukY6Je5^fqqc(G&QDS3+Ca8=DXZ>ZE#YnFqcq*M2}{RuH!9AHRM0t&B?KJ6 zjL|$MmuDFv<&v)t3misnKz$D9LvJ|rMrBmc2x`TYT~Hc)?ibDbS#*Z?vwly$hN%9o Y?8>g}%Ifm}0RRC1|9GOHl>mkS0PdQv!~g&Q diff --git a/migrator/migrator.go b/migrator/migrator.go index 720f494db..a84c0d9bf 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -1,16 +1,14 @@ 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" @@ -18,9 +16,6 @@ import ( "github.com/utkuozdemir/pv-migrate/util" ) -//go:embed helm-chart.tgz -var chartBytes []byte - const ( attemptIDLength = 5 ) @@ -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) }