From 175122ab762f12979c35e5136761b2d01ca3f7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20P=C3=A9rez?= Date: Sat, 24 Aug 2024 21:54:08 +0200 Subject: [PATCH] fix: Recursively update images on nested subcharts regardless of depth (#67) --- cmd/dt/relocate_test.go | 48 +++++++++++++++++++ pkg/relocator/chart.go | 3 +- testdata/scenarios/recursive-chart/Chart.yaml | 13 +++++ .../recursive-chart/charts/chartB/Chart.yaml | 12 +++++ .../charts/chartB/charts/chartC/Chart.yaml | 8 ++++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 testdata/scenarios/recursive-chart/Chart.yaml create mode 100644 testdata/scenarios/recursive-chart/charts/chartB/Chart.yaml create mode 100644 testdata/scenarios/recursive-chart/charts/chartB/charts/chartC/Chart.yaml diff --git a/cmd/dt/relocate_test.go b/cmd/dt/relocate_test.go index 681e2f8..ca445a8 100644 --- a/cmd/dt/relocate_test.go +++ b/cmd/dt/relocate_test.go @@ -67,3 +67,51 @@ func (suite *CmdSuite) TestRelocateCommand() { } }) } + +func (suite *CmdSuite) TestRelocateCommandRecursively() { + s, err := tu.NewTestServer() + suite.Require().NoError(err) + defer s.Close() + + images, err := s.LoadImagesFromFile("../../testdata/images.json") + suite.Require().NoError(err) + + sb := suite.sb + require := suite.Require() + serverURL := s.ServerURL + scenarioName := "recursive-chart" + chartName := "test" + + scenarioDir := fmt.Sprintf("../../testdata/scenarios/%s", scenarioName) + + renderLockedChart := func(chartDir string, _ string, serverURL string) string { + + require.NoError(tu.RenderScenario(scenarioDir, chartDir, + map[string]interface{}{"ServerURL": serverURL, "Images": images, "Name": chartName, "RepositoryURL": serverURL}, + )) + + return chartDir + } + suite.T().Run("Relocate Helm chart", func(t *testing.T) { + relocateURL := "custom.repo.example.com" + originChart := renderLockedChart(sb.TempFile(), scenarioName, serverURL) + + cmd := dt("charts", "relocate", originChart, relocateURL) + cmd.AssertSuccess(suite.T()) + + err := filepath.Walk(originChart, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() && filepath.Base(path) == "Chart.yaml" { + content, err := os.ReadFile(path) + if err != nil { + return err + } + suite.Assert().Contains(string(content), relocateURL, "File %s does not contain relocated URL", path) + } + return nil + }) + suite.Require().NoError(err) + }) +} diff --git a/pkg/relocator/chart.go b/pkg/relocator/chart.go index dba6ae1..f01eb52 100644 --- a/pkg/relocator/chart.go +++ b/pkg/relocator/chart.go @@ -95,11 +95,12 @@ func RelocateChartDir(chartPath string, prefix string, opts ...RelocateOption) e if cfg.Recursive { for _, dep := range chart.Dependencies() { - if err := relocateChart(dep, prefix, cfg); err != nil { + if err := RelocateChartDir(dep.ChartDir(), prefix, opts...); err != nil { allErrors = errors.Join(allErrors, fmt.Errorf("failed to relocate Helm SubChart %q: %v", dep.Chart().ChartFullPath(), err)) } } } + return allErrors } diff --git a/testdata/scenarios/recursive-chart/Chart.yaml b/testdata/scenarios/recursive-chart/Chart.yaml new file mode 100644 index 0000000..191f5c8 --- /dev/null +++ b/testdata/scenarios/recursive-chart/Chart.yaml @@ -0,0 +1,13 @@ +name: chartA +version: 1.0.0 +annotations: + category: CMS + licenses: Apache-2.0 + images: | + - name: imagea + image: registry-1.docker.io/bitnami/imagea:1.0.8-debian-12-r7 + +dependencies: + - name: chartB + repository: oci://registry-1.docker.io/bitnamicharts + version: 2.0.0 diff --git a/testdata/scenarios/recursive-chart/charts/chartB/Chart.yaml b/testdata/scenarios/recursive-chart/charts/chartB/Chart.yaml new file mode 100644 index 0000000..7a6b01e --- /dev/null +++ b/testdata/scenarios/recursive-chart/charts/chartB/Chart.yaml @@ -0,0 +1,12 @@ +name: chartB +version: 2.0.0 +annotations: + category: CMS + licenses: Apache-2.0 + images: | + - name: imageb + image: registry-1.docker.io/bitnami/imageb:1.0.8-debian-12-r7 +dependencies: + - name: chartC + repository: oci://registry-1.docker.io/bitnamicharts + version: 3.0.0 diff --git a/testdata/scenarios/recursive-chart/charts/chartB/charts/chartC/Chart.yaml b/testdata/scenarios/recursive-chart/charts/chartB/charts/chartC/Chart.yaml new file mode 100644 index 0000000..c974ec0 --- /dev/null +++ b/testdata/scenarios/recursive-chart/charts/chartB/charts/chartC/Chart.yaml @@ -0,0 +1,8 @@ +name: chartC +version: 3.0.0 +annotations: + category: CMS + licenses: Apache-2.0 + images: | + - name: imagec + image: registry-1.docker.io/bitnami/imagec:1.0.8-debian-12-r7