From e919b4b1d2a07b364d3dabdf227ac89081cc6547 Mon Sep 17 00:00:00 2001 From: Quan TRAN Date: Tue, 1 Nov 2022 06:50:05 +0100 Subject: [PATCH] fix oci:// chart (#477) * fix oci:// chart Signed-off-by: Quan TRAN --- pkg/state/state.go | 42 +++++++---- pkg/state/state_test.go | 69 +++++++++++++++++++ .../issue_473_oci_chart_url_fetch/config.yaml | 6 ++ .../issue_473_oci_chart_url_fetch/input.yaml | 14 ++++ .../issue_473_oci_chart_url_fetch/output.yaml | 4 ++ 5 files changed, 121 insertions(+), 14 deletions(-) create mode 100644 test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/config.yaml create mode 100644 test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/input.yaml create mode 100644 test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/output.yaml diff --git a/pkg/state/state.go b/pkg/state/state.go index 474918c7..f3de6536 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -3306,22 +3306,12 @@ func (st *HelmState) Reverse() { } func (st *HelmState) getOCIChart(release *ReleaseSpec, tempDir string, helm helmexec.Interface) (*string, error) { - repo, name := st.GetRepositoryAndNameFromChartName(release.Chart) - if repo == nil { + qualifiedChartName, chartName, chartVersion := st.getOCIQualifiedChartName(release) + + if qualifiedChartName == "" { return nil, nil } - if !repo.OCI { - return nil, nil - } - - chartVersion := "latest" - if release.Version != "" { - chartVersion = release.Version - } - - qualifiedChartName := fmt.Sprintf("%s/%s:%s", repo.URL, name, chartVersion) - pathElems := []string{ tempDir, } @@ -3334,7 +3324,7 @@ func (st *HelmState) getOCIChart(release *ReleaseSpec, tempDir string, helm helm pathElems = append(pathElems, release.KubeContext) } - pathElems = append(pathElems, release.Name, name, chartVersion) + pathElems = append(pathElems, release.Name, chartName, chartVersion) chartPath := path.Join(pathElems...) @@ -3357,6 +3347,30 @@ func (st *HelmState) getOCIChart(release *ReleaseSpec, tempDir string, helm helm return &chartPath, nil } +func (st *HelmState) getOCIQualifiedChartName(release *ReleaseSpec) (qualifiedChartName, chartName, chartVersion string) { + chartVersion = "latest" + if release.Version != "" { + chartVersion = release.Version + } + + if strings.HasPrefix(release.Chart, "oci://") { + split := strings.Split(release.Chart, "/") + chartName = split[len(split)-1] + qualifiedChartName = strings.Replace(fmt.Sprintf("%s:%s", release.Chart, chartVersion), "oci://", "", 1) + } else { + var repo *RepositorySpec + repo, chartName = st.GetRepositoryAndNameFromChartName(release.Chart) + if repo == nil { + return + } + if !repo.OCI { + return + } + qualifiedChartName = fmt.Sprintf("%s/%s:%s", repo.URL, chartName, chartVersion) + } + return +} + func (st *HelmState) FullFilePath() (string, error) { var wd string var err error diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go index 31e8717d..0d408eeb 100644 --- a/pkg/state/state_test.go +++ b/pkg/state/state_test.go @@ -2743,3 +2743,72 @@ func TestFullFilePath(t *testing.T) { }) } } + +func TestGetOCIQualifiedChartName(t *testing.T) { + tests := []struct { + state HelmState + expected []struct { + qualifiedChartName string + chartName string + chartVersion string + } + }{ + { + state: HelmState{ + ReleaseSetSpec: ReleaseSetSpec{ + Repositories: []RepositorySpec{}, + Releases: []ReleaseSpec{ + { + Chart: "oci://registry/chart-path/chart-name", + Version: "0.1.2", + }, + }, + }, + }, + expected: []struct { + qualifiedChartName string + chartName string + chartVersion string + }{ + {"registry/chart-path/chart-name:0.1.2", "chart-name", "0.1.2"}, + }, + }, + { + state: HelmState{ + ReleaseSetSpec: ReleaseSetSpec{ + Repositories: []RepositorySpec{ + { + Name: "oci-repo", + URL: "registry/chart-path", + OCI: true, + }, + }, + Releases: []ReleaseSpec{ + { + Chart: "oci-repo/chart-name", + Version: "0.1.2", + }, + }, + }, + }, + expected: []struct { + qualifiedChartName string + chartName string + chartVersion string + }{ + {"registry/chart-path/chart-name:0.1.2", "chart-name", "0.1.2"}, + }, + }, + } + + for _, tt := range tests { + t.Run(fmt.Sprintf("%+v", tt.expected), func(t *testing.T) { + for i, r := range tt.state.Releases { + qualifiedChartName, chartName, chartVersion := tt.state.getOCIQualifiedChartName(&r) + require.Equalf(t, qualifiedChartName, tt.expected[i].qualifiedChartName, "qualifiedChartName got = %v, want %v", qualifiedChartName, tt.expected[i].qualifiedChartName) + require.Equalf(t, chartName, tt.expected[i].chartName, "chartName got = %v, want %v", chartName, tt.expected[i].chartName) + require.Equalf(t, chartVersion, tt.expected[i].chartVersion, "chartVersion got = %v, want %v", chartVersion, tt.expected[i].chartVersion) + } + }) + } +} diff --git a/test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/config.yaml b/test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/config.yaml new file mode 100644 index 00000000..20aefc41 --- /dev/null +++ b/test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/config.yaml @@ -0,0 +1,6 @@ +localDockerRegistry: + enabled: true + port: 5000 +chartifyTempDir: temp2 +helmfileArgs: +- fetch diff --git a/test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/input.yaml b/test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/input.yaml new file mode 100644 index 00000000..c534fb4f --- /dev/null +++ b/test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/input.yaml @@ -0,0 +1,14 @@ +releases: +- name: foo + chart: oci://localhost:5000/myrepo/raw + version: 0.1.0 + values: + - templates: + - | + apiVersion: v1 + kind: ConfigMap + metadata: + name: {{`{{ .Release.Name }}`}} + namespace: {{`{{ .Release.Namespace }}`}} + data: + foo: FOO \ No newline at end of file diff --git a/test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/output.yaml b/test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/output.yaml new file mode 100644 index 00000000..b40f397e --- /dev/null +++ b/test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/output.yaml @@ -0,0 +1,4 @@ +Pulling localhost:5000/myrepo/raw:0.1.0 +Pulled: localhost:5000/myrepo/raw:0.1.0 +Digest: $DIGEST +