Skip to content

Commit

Permalink
fix: Recursively update images on nested subcharts regardless of depth (
Browse files Browse the repository at this point in the history
  • Loading branch information
mpermar committed Aug 24, 2024
1 parent 5e9b6d7 commit 175122a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
48 changes: 48 additions & 0 deletions cmd/dt/relocate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
3 changes: 2 additions & 1 deletion pkg/relocator/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 13 additions & 0 deletions testdata/scenarios/recursive-chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions testdata/scenarios/recursive-chart/charts/chartB/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 175122a

Please sign in to comment.