From 156d48bc3bdab4cf148a4f4c56231f2b3b2479c3 Mon Sep 17 00:00:00 2001
From: Sushil Kumar <Sushil.Kumar@suse.com>
Date: Tue, 23 May 2017 14:57:08 -0700
Subject: [PATCH] Adds alias for dependencies

Fixes https://github.com/kubernetes/helm/issues/2383

Sample `requirements.yaml` I used for test purpose was for wordpress chart

```dependencies:
- name: mariadb
  version: 0.5.10
  repository: https://kubernetes-charts.storage.googleapis.com/
  alias:
  - db1
  - db2
  - db3
```
---
 pkg/chartutil/requirements.go | 56 +++++++++++++++++++++++++++++++++++
 pkg/resolver/resolver_test.go |  2 +-
 2 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/pkg/chartutil/requirements.go b/pkg/chartutil/requirements.go
index 5f9fa3b3d..26d13f3a5 100644
--- a/pkg/chartutil/requirements.go
+++ b/pkg/chartutil/requirements.go
@@ -65,6 +65,8 @@ type Dependency struct {
 	// ImportValues holds the mapping of source values to parent key to be imported. Each item can be a
 	// string or pair of child/parent sublist items.
 	ImportValues []interface{} `json:"import-values"`
+	// Alias usable alias to be used for the chart
+	Alias []string `json:"alias"`
 }
 
 // ErrNoRequirementsFile to detect error condition
@@ -216,6 +218,45 @@ func ProcessRequirementsTags(reqs *Requirements, cvals Values) {
 
 }
 
+func copyChartAsAlias(charts []*chart.Chart, dependentChart, aliasChart string) *chart.Chart {
+	var chartFound *chart.Chart
+	for _, existingChart := range charts {
+		if existingChart == nil {
+			continue
+		}
+		if existingChart.Metadata == nil {
+			continue
+		}
+		if existingChart.Metadata.Name != dependentChart {
+			continue
+		}
+
+		chartFound = new(chart.Chart)
+		chartFound.Metadata = &chart.Metadata{
+			Name:          aliasChart,
+			Home:          existingChart.Metadata.Home,
+			Sources:       existingChart.Metadata.Sources,
+			Version:       existingChart.Metadata.Version,
+			Description:   existingChart.Metadata.Description,
+			Keywords:      existingChart.Metadata.Keywords,
+			Maintainers:   existingChart.Metadata.Maintainers,
+			Engine:        existingChart.Metadata.Engine,
+			Icon:          existingChart.Metadata.Icon,
+			ApiVersion:    existingChart.Metadata.ApiVersion,
+			Condition:     existingChart.Metadata.Condition,
+			Tags:          existingChart.Metadata.Tags,
+			AppVersion:    existingChart.Metadata.AppVersion,
+			Deprecated:    existingChart.Metadata.Deprecated,
+			TillerVersion: existingChart.Metadata.TillerVersion,
+		}
+		chartFound.Templates = existingChart.Templates
+		chartFound.Dependencies = existingChart.Dependencies
+		chartFound.Values = existingChart.Values
+		chartFound.Files = existingChart.Files
+	}
+	return chartFound
+}
+
 // ProcessRequirementsEnabled removes disabled charts from dependencies
 func ProcessRequirementsEnabled(c *chart.Chart, v *chart.Config) error {
 	reqs, err := LoadRequirements(c)
@@ -228,6 +269,21 @@ func ProcessRequirementsEnabled(c *chart.Chart, v *chart.Config) error {
 		// no requirements to process
 		return nil
 	}
+
+	for _, req := range reqs.Dependencies {
+		for _, alias := range req.Alias {
+			aliasDependency := copyChartAsAlias(c.Dependencies, req.Name, alias)
+			if aliasDependency == nil {
+				break
+			}
+			c.Dependencies = append(c.Dependencies, aliasDependency)
+			origReqName := req.Name
+			req.Name = alias
+			reqs.Dependencies = append(reqs.Dependencies, req)
+			req.Name = origReqName
+		}
+	}
+
 	// set all to true
 	for _, lr := range reqs.Dependencies {
 		lr.Enabled = true
diff --git a/pkg/resolver/resolver_test.go b/pkg/resolver/resolver_test.go
index ef5a3bee0..9453a979f 100644
--- a/pkg/resolver/resolver_test.go
+++ b/pkg/resolver/resolver_test.go
@@ -146,7 +146,7 @@ func TestResolve(t *testing.T) {
 }
 
 func TestHashReq(t *testing.T) {
-	expect := "sha256:1feffe2016ca113f64159d91c1f77d6a83bcd23510b171d9264741bf9d63f741"
+	expect := "sha256:917e251ddba291096889f81eb7de713ab4e1afbbb07c576dfd7d66ba9300b12b"
 	req := &chartutil.Requirements{
 		Dependencies: []*chartutil.Dependency{
 			{Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts"},
-- 
GitLab