fix oci:// chart (#477)
* fix oci:// chart Signed-off-by: Quan TRAN <itscaro@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									3ac0cee50a
								
							
						
					
					
						commit
						e919b4b1d2
					
				|  | @ -3306,22 +3306,12 @@ func (st *HelmState) Reverse() { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (st *HelmState) getOCIChart(release *ReleaseSpec, tempDir string, helm helmexec.Interface) (*string, error) { | func (st *HelmState) getOCIChart(release *ReleaseSpec, tempDir string, helm helmexec.Interface) (*string, error) { | ||||||
| 	repo, name := st.GetRepositoryAndNameFromChartName(release.Chart) | 	qualifiedChartName, chartName, chartVersion := st.getOCIQualifiedChartName(release) | ||||||
| 	if repo == nil { | 
 | ||||||
|  | 	if qualifiedChartName == "" { | ||||||
| 		return nil, nil | 		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{ | 	pathElems := []string{ | ||||||
| 		tempDir, | 		tempDir, | ||||||
| 	} | 	} | ||||||
|  | @ -3334,7 +3324,7 @@ func (st *HelmState) getOCIChart(release *ReleaseSpec, tempDir string, helm helm | ||||||
| 		pathElems = append(pathElems, release.KubeContext) | 		pathElems = append(pathElems, release.KubeContext) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	pathElems = append(pathElems, release.Name, name, chartVersion) | 	pathElems = append(pathElems, release.Name, chartName, chartVersion) | ||||||
| 
 | 
 | ||||||
| 	chartPath := path.Join(pathElems...) | 	chartPath := path.Join(pathElems...) | ||||||
| 
 | 
 | ||||||
|  | @ -3357,6 +3347,30 @@ func (st *HelmState) getOCIChart(release *ReleaseSpec, tempDir string, helm helm | ||||||
| 	return &chartPath, nil | 	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) { | func (st *HelmState) FullFilePath() (string, error) { | ||||||
| 	var wd string | 	var wd string | ||||||
| 	var err error | 	var err error | ||||||
|  |  | ||||||
|  | @ -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) | ||||||
|  | 			} | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
							
								
								
									
										6
									
								
								test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/config.yaml
								
								
								
									vendored
								
								
									Normal file
								
							
							
						
						
									
										6
									
								
								test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/config.yaml
								
								
								
									vendored
								
								
									Normal file
								
							|  | @ -0,0 +1,6 @@ | ||||||
|  | localDockerRegistry: | ||||||
|  |   enabled: true | ||||||
|  |   port: 5000 | ||||||
|  | chartifyTempDir: temp2 | ||||||
|  | helmfileArgs: | ||||||
|  | - fetch | ||||||
							
								
								
									
										14
									
								
								test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/input.yaml
								
								
								
									vendored
								
								
									Normal file
								
							
							
						
						
									
										14
									
								
								test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/input.yaml
								
								
								
									vendored
								
								
									Normal file
								
							|  | @ -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 | ||||||
							
								
								
									
										4
									
								
								test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/output.yaml
								
								
								
									vendored
								
								
									Normal file
								
							
							
						
						
									
										4
									
								
								test/e2e/template/helmfile/testdata/snapshot/issue_473_oci_chart_url_fetch/output.yaml
								
								
								
									vendored
								
								
									Normal file
								
							|  | @ -0,0 +1,4 @@ | ||||||
|  | Pulling localhost:5000/myrepo/raw:0.1.0 | ||||||
|  | Pulled: localhost:5000/myrepo/raw:0.1.0 | ||||||
|  | Digest: $DIGEST | ||||||
|  | 
 | ||||||
		Loading…
	
		Reference in New Issue