Merge pull request #2534 from sushilkm/issues/2508

First alias would be dependency rename
pull/2555/head
Taylor Thomas 8 years ago committed by GitHub
commit e3250e3319

@ -234,11 +234,11 @@ team.
In addition to the other fields above, each requirements entry may contain In addition to the other fields above, each requirements entry may contain
the optional field `alias`. the optional field `alias`.
Adding an alias for a dependency chart would add another copy Adding an alias for a dependency chart would put
of the chart as a new depdendency using alias as name of new dependency. a chart in dependencies using alias as name of new dependency.
One can use `alias` in cases where they need multiple copies of same chart One can use `alias` in cases where they need to access a chart
as dependencies all independent of one another. with other name(s).
```` ````
# parentchart/requirements.yaml # parentchart/requirements.yaml
@ -246,16 +246,21 @@ dependencies:
- name: subchart - name: subchart
repository: http://localhost:10191 repository: http://localhost:10191
version: 0.1.0 version: 0.1.0
alias: alias: new-subchart-1
- one-more-subchart - name: subchart
- another-subchart repository: http://localhost:10191
version: 0.1.0
alias: new-subchart-2
- name: subchart
repository: http://localhost:10191
version: 0.1.0
```` ````
In the above example we will get 3 depenendencies in all for `parentchart` In the above example we will get 3 depenendencies in all for `parentchart`
``` ```
subchart subchart
one-more-subchart new-subchart-1
another-subchart new-subchart-2
``` ```
Manual way of achieving this is copy/pasting same chart in Manual way of achieving this is copy/pasting same chart in

@ -66,7 +66,7 @@ type Dependency struct {
// string or pair of child/parent sublist items. // string or pair of child/parent sublist items.
ImportValues []interface{} `json:"import-values"` ImportValues []interface{} `json:"import-values"`
// Alias usable alias to be used for the chart // Alias usable alias to be used for the chart
Alias []string `json:"alias"` Alias string `json:"alias"`
} }
// ErrNoRequirementsFile to detect error condition // ErrNoRequirementsFile to detect error condition
@ -218,7 +218,7 @@ func ProcessRequirementsTags(reqs *Requirements, cvals Values) {
} }
func copyChartAsAlias(charts []*chart.Chart, dependentChart, aliasChart string) *chart.Chart { func getAliasDependency(charts []*chart.Chart, aliasChart *Dependency) *chart.Chart {
var chartFound chart.Chart var chartFound chart.Chart
for _, existingChart := range charts { for _, existingChart := range charts {
if existingChart == nil { if existingChart == nil {
@ -227,13 +227,17 @@ func copyChartAsAlias(charts []*chart.Chart, dependentChart, aliasChart string)
if existingChart.Metadata == nil { if existingChart.Metadata == nil {
continue continue
} }
if existingChart.Metadata.Name != dependentChart { if existingChart.Metadata.Name != aliasChart.Name {
continue
}
if existingChart.Metadata.Version != aliasChart.Version {
continue continue
} }
chartFound = *existingChart chartFound = *existingChart
newMetadata := *existingChart.Metadata newMetadata := *existingChart.Metadata
newMetadata.Name = aliasChart if aliasChart.Alias != "" {
newMetadata.Name = aliasChart.Alias
}
chartFound.Metadata = &newMetadata chartFound.Metadata = &newMetadata
return &chartFound return &chartFound
} }
@ -253,19 +257,16 @@ func ProcessRequirementsEnabled(c *chart.Chart, v *chart.Config) error {
return nil return nil
} }
var chartDependencies []*chart.Chart
for _, req := range reqs.Dependencies { for _, req := range reqs.Dependencies {
for _, alias := range req.Alias { if chartDependency := getAliasDependency(c.Dependencies, req); chartDependency != nil {
aliasDependency := copyChartAsAlias(c.Dependencies, req.Name, alias) chartDependencies = append(chartDependencies, chartDependency)
if aliasDependency == nil { }
break if req.Alias != "" {
} req.Name = req.Alias
c.Dependencies = append(c.Dependencies, aliasDependency)
origReqName := req.Name
req.Name = alias
reqs.Dependencies = append(reqs.Dependencies, req)
req.Name = origReqName
} }
} }
c.Dependencies = chartDependencies
// set all to true // set all to true
for _, lr := range reqs.Dependencies { for _, lr := range reqs.Dependencies {

@ -321,22 +321,36 @@ func verifyRequirementsImportValues(t *testing.T, c *chart.Chart, v *chart.Confi
} }
} }
func TestCopyChartAsAlias(t *testing.T) { func TestGetAliasDependency(t *testing.T) {
c, err := Load("testdata/frobnitz") c, err := Load("testdata/frobnitz")
if err != nil { if err != nil {
t.Fatalf("Failed to load testdata: %s", err) t.Fatalf("Failed to load testdata: %s", err)
} }
req, err := LoadRequirements(c)
if aliasChart := copyChartAsAlias(c.Dependencies, "mariners", "another-mariner"); aliasChart != nil { if err != nil {
t.Fatalf("expected no chart but got %s", aliasChart.Metadata.Name) t.Fatalf("Failed to load requirement for testdata: %s", err)
}
if len(req.Dependencies) == 0 {
t.Fatalf("There are no requirements to test")
} }
aliasChart := copyChartAsAlias(c.Dependencies, "mariner", "another-mariner") // Success case
aliasChart := getAliasDependency(c.Dependencies, req.Dependencies[0])
if aliasChart == nil { if aliasChart == nil {
t.Fatal("Failed to find dependent chart") t.Fatalf("Failed to get dependency chart for alias %s", req.Dependencies[0].Name)
}
if req.Dependencies[0].Alias != "" {
if aliasChart.Metadata.Name != req.Dependencies[0].Alias {
t.Fatalf("Dependency chart name should be %s but got %s", req.Dependencies[0].Alias, aliasChart.Metadata.Name)
}
} else if aliasChart.Metadata.Name != req.Dependencies[0].Name {
t.Fatalf("Dependency chart name should be %s but got %s", req.Dependencies[0].Name, aliasChart.Metadata.Name)
} }
if aliasChart.Metadata.Name != "another-mariner" {
t.Fatal(`Failed to update chart-name for alias "dependent chart`) // Failure case
req.Dependencies[0].Name = "something-else"
if aliasChart := getAliasDependency(c.Dependencies, req.Dependencies[0]); aliasChart != nil {
t.Fatalf("expected no chart but got %s", aliasChart.Metadata.Name)
} }
} }
@ -364,15 +378,15 @@ func TestDependentChartAliases(t *testing.T) {
t.Fatalf("Cannot load requirements for test chart, %v", err) t.Fatalf("Cannot load requirements for test chart, %v", err)
} }
var expectedDependencyCharts int // var expectedDependencyCharts int
for _, reqmt := range reqmts.Dependencies { // for _, reqmt := range reqmts.Dependencies {
expectedDependencyCharts++ // expectedDependencyCharts++
if len(reqmt.Alias) >= 0 { // if len(reqmt.Alias) >= 0 {
expectedDependencyCharts += len(reqmt.Alias) // expectedDependencyCharts += len(reqmt.Alias)
} // }
} // }
if len(c.Dependencies) != expectedDependencyCharts { if len(c.Dependencies) != len(reqmts.Dependencies) {
t.Fatalf("Expected number of chart dependencies %d, but got %d", expectedDependencyCharts, len(c.Dependencies)) t.Fatalf("Expected number of chart dependencies %d, but got %d", len(reqmts.Dependencies), len(c.Dependencies))
} }
} }

@ -5,6 +5,8 @@ dependencies:
- name: mariner - name: mariner
version: "4.3.2" version: "4.3.2"
repository: https://example.com/charts repository: https://example.com/charts
alias: alias: mariners2
- mariners1 - name: mariner
- mariners2 version: "4.3.2"
repository: https://example.com/charts
alias: mariners1

@ -146,7 +146,7 @@ func TestResolve(t *testing.T) {
} }
func TestHashReq(t *testing.T) { func TestHashReq(t *testing.T) {
expect := "sha256:917e251ddba291096889f81eb7de713ab4e1afbbb07c576dfd7d66ba9300b12b" expect := "sha256:45b06fcc4496c705bf3d634f8a2ff84e6a6f0bdcaf010614b8886572d1e52b99"
req := &chartutil.Requirements{ req := &chartutil.Requirements{
Dependencies: []*chartutil.Dependency{ Dependencies: []*chartutil.Dependency{
{Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts"}, {Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts"},

Loading…
Cancel
Save