Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PL-2834): support mappings per chart and release #195

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions api/v1alpha1/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ type ReleaseMetadata struct {
}

type ReleaseChart struct {
Ref string `yaml:"ref,omitempty" json:"ref,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
RepoUrl string `yaml:"repoUrl,omitempty" json:"repoUrl,omitempty"`
Ref string `yaml:"ref,omitempty" json:"ref,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
RepoUrl string `yaml:"repoUrl,omitempty" json:"repoUrl,omitempty"`
Mappings map[string]any `yaml:"mappings,omitempty" json:"mappings,omitempty"`
}

func (chart ReleaseChart) Validate(validRefs []string) error {
Expand Down
4 changes: 4 additions & 0 deletions internal/release/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ func HydrateValues(release *v1alpha1.Release, chart *helm.ChartFS, mappings *con
return nil, fmt.Errorf("hydrating object values: %w", err)
}

for key, value := range chart.Mappings {
setInMap(values, splitIntoPathSegments(key), value)
}

if mappings != nil && !slices.Contains(mappings.ReleaseIgnoreList, release.Name) {
for mapping, value := range mappings.Mappings {
setInMap(values, splitIntoPathSegments(mapping), value)
Expand Down
82 changes: 78 additions & 4 deletions internal/release/render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
func TestRenderRelease(t *testing.T) {
type RenderTestParams struct {
Release *v1alpha1.Release
Chart helm.Chart
ChartFS func(*xfs.FSMock) func(*testing.T)
IO internal.IO
SetupHelmMock func(*helm.PullRendererMock) func(*testing.T)
Expand Down Expand Up @@ -70,7 +71,7 @@ func TestRenderRelease(t *testing.T) {
t,
helm.RenderOpts{
ReleaseName: "release",
Values: map[string]interface{}{},
Values: map[string]any{},
ChartPath: "path/to/chart",
},
mock.RenderCalls()[0].Opts,
Expand Down Expand Up @@ -165,8 +166,8 @@ func TestRenderRelease(t *testing.T) {
t,
helm.RenderOpts{
ReleaseName: "release",
Values: map[string]interface{}{
"corsOrigins": []interface{}{"origin1.com", "origin2.com"},
Values: map[string]any{
"corsOrigins": []any{"origin1.com", "origin2.com"},
"env": "env",
"version": "v1.2.3",
},
Expand All @@ -178,6 +179,78 @@ func TestRenderRelease(t *testing.T) {
},
},
},
{
Name: "with chart level mappings",
Params: RenderTestParams{
Release: func() *v1alpha1.Release {
rel := buildRelease("env", "release")
rel.Spec.Values = map[string]any{}
rel.Spec.Version = "v9.9.9"
return rel
}(),
Chart: helm.Chart{
Mappings: map[string]any{
"test.mapping": "{{ .Release.Spec.Version }}",
},
},
SetupHelmMock: func(mock *helm.PullRendererMock) func(*testing.T) {
return func(t *testing.T) {
require.Len(t, mock.RenderCalls(), 1)
require.Equal(
t,
helm.RenderOpts{
ReleaseName: "release",
Values: map[string]any{
"test": map[string]any{
"mapping": "v9.9.9",
},
},
ChartPath: "path/to/chart",
},
mock.RenderCalls()[0].Opts,
)
}
},
ValueMapping: &config.ValueMapping{},
},
},
{
Name: "chart mappings take priority over global mappings",
Params: RenderTestParams{
Release: func() *v1alpha1.Release {
rel := buildRelease("env", "release")
rel.Spec.Values = map[string]any{}
rel.Spec.Version = "v9.9.9"
return rel
}(),
Chart: helm.Chart{
Mappings: map[string]any{"test.mapping": "{{ .Release.Spec.Version }}"},
},
SetupHelmMock: func(mock *helm.PullRendererMock) func(*testing.T) {
return func(t *testing.T) {
require.Len(t, mock.RenderCalls(), 1)
require.Equal(
t,
helm.RenderOpts{
ReleaseName: "release",
Values: map[string]any{
"test": map[string]any{
"mapping": "v9.9.9",
},
},
ChartPath: "path/to/chart",
},
mock.RenderCalls()[0].Opts,
)
}
},
ValueMapping: &config.ValueMapping{
Mappings: map[string]any{
"test.mapping": "GLOBAL",
},
},
},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -211,7 +284,8 @@ func TestRenderRelease(t *testing.T) {
result, err := Render(context.Background(), RenderParams{
Release: tc.Params.Release,
Chart: &helm.ChartFS{
FS: fsMock,
Chart: tc.Params.Chart,
FS: fsMock,
},
ValueMapping: tc.Params.ValueMapping,
Helm: helmMock,
Expand Down
22 changes: 16 additions & 6 deletions pkg/helm/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"maps"
"net/url"
"os"
"path"
Expand All @@ -24,9 +25,10 @@ type ChartCache struct {
}

type Chart struct {
RepoURL string `yaml:"repoUrl"`
Name string `yaml:"name"`
Version string `yaml:"version"`
RepoURL string `yaml:"repoUrl"`
Name string `yaml:"name"`
Version string `yaml:"version"`
Mappings map[string]any `yaml:"mappings"`
}

func (chart Chart) ToURL() (*url.URL, error) {
Expand Down Expand Up @@ -54,9 +56,10 @@ type ChartFS struct {
func (cache ChartCache) GetReleaseChart(release *v1alpha1.Release) (Chart, error) {
if repoURL := release.Spec.Chart.RepoUrl; repoURL != "" {
return Chart{
RepoURL: repoURL,
Name: release.Spec.Chart.Name,
Version: release.Spec.Chart.Version,
RepoURL: repoURL,
Name: release.Spec.Chart.Name,
Version: release.Spec.Chart.Version,
Mappings: release.Spec.Chart.Mappings,
}, nil
}

Expand All @@ -70,6 +73,13 @@ func (cache ChartCache) GetReleaseChart(release *v1alpha1.Release) (Chart, error
chart.Version,
)

chart.Mappings = func() map[string]any {
mappings := make(map[string]any)
maps.Copy(mappings, chart.Mappings)
maps.Copy(mappings, release.Spec.Chart.Mappings)
return mappings
}()

return chart, nil
}

Expand Down
Loading