diff --git a/_proto/hapi/chart/metadata.proto b/_proto/hapi/chart/metadata.proto index da194e9d3..6a6e7ebdc 100644 --- a/_proto/hapi/chart/metadata.proto +++ b/_proto/hapi/chart/metadata.proto @@ -64,4 +64,10 @@ message Metadata { // The API Version of this chart. string apiVersion = 10; + + // The condition to check to enable chart + string condition = 11; + + // The tags to check to enable chart + string tags = 12; } diff --git a/docs/charts.md b/docs/charts.md index 2a119ab48..096e2e4ff 100644 --- a/docs/charts.md +++ b/docs/charts.md @@ -203,6 +203,80 @@ Managing charts with `requirements.yaml` is a good way to easily keep charts updated, and also share requirements information throughout a team. +#### Tags and Condition fields in requirements.yaml + +In addition to the other fields above, each requirements entry may contain +the optional fields `tags` and `condition`. + +All charts are loaded by default. If `tags` or `condition` fields are present, +they will be evaluated and used to control loading for the chart(s) they are applied to. + +Condition - The condition field holds one or more YAML paths (delimited by commas). +If this path exists in the top parent's values and resolves to a boolean value, +the chart will be enabled or disabled based on that boolean value. Only the first +valid path found in the list is evaluated and if no paths exist then the condition has no effect. + +Tags - The tags field is a YAML list of labels to associate with this chart. +In the top parent's values, all charts with tags can be enabled or disabled by +specifying the tag and a boolean value. + +```` +# parentchart/requirements.yaml +dependencies: + - name: subchart1 + repository: http://localhost:10191 + version: 0.1.0 + condition: subchart1.enabled, global.subchart1.enabled + tags: + - front-end + - subchart1 + + - name: subchart2 + repository: http://localhost:10191 + version: 0.1.0 + condition: subchart2.enabled,global.subchart2.enabled + tags: + - back-end + - subchart1 + +```` +```` +# parentchart/values.yaml + +subchart1: + enabled: true +tags: + front-end: false + back-end: true +```` + +In the above example all charts with the tag `front-end` would be disabled but since the +`subchart1.enabled` path evaluates to 'true' in the parent's values, the condition will override the +`front-end` tag and `subchart1` will be enabled. + +Since `subchart2` is tagged with `back-end` and that tag evaluates to `true`, `subchart2` will be +enabled. Also note that although `subchart2` has a condition specified in `requirements.yaml`, there +is no corresponding path and value in the parent's values so that condition has no effect. + +##### Using the CLI with Tags and Conditions + +The `--set` parameter can be used as usual to alter tag and condition values. + +```` +helm install --set tags.front-end=true --set subchart2.enabled=false + +```` + +##### Tags and Condition Resolution + + + * **Conditions (when set in values) always override tags.** The first condition + path that exists wins and subsequent ones for that chart are ignored. + * Tags are evaluated as 'if any of the chart's tags are true then enable the chart'. + * Tags and conditions values must be set in the top parent's values. + * The `tags:` key in values must be a top level key. Globals and nested `tags:` tables + are not currently supported. + ## Templates and Values Helm Chart templates are written in the diff --git a/pkg/chartutil/requirements.go b/pkg/chartutil/requirements.go index 8871edbb8..3a31042d6 100644 --- a/pkg/chartutil/requirements.go +++ b/pkg/chartutil/requirements.go @@ -17,10 +17,11 @@ package chartutil import ( "errors" + "log" + "strings" "time" "github.com/ghodss/yaml" - "k8s.io/helm/pkg/proto/hapi/chart" ) @@ -55,8 +56,17 @@ type Dependency struct { // Appending `index.yaml` to this string should result in a URL that can be // used to fetch the repository index. Repository string `json:"repository"` + // A yaml path that resolves to a boolean, used for enabling/disabling charts (e.g. subchart1.enabled ) + Condition string `json:"condition"` + // Tags can be used to group charts for enabling/disabling together + Tags []string `json:"tags"` + // Enabled bool determines if chart should be loaded + Enabled bool `json:"enabled"` } +// ErrNoRequirementsFile to detect error condition +type ErrNoRequirementsFile error + // Requirements is a list of requirements for a chart. // // Requirements are charts upon which this chart depends. This expresses @@ -106,3 +116,153 @@ func LoadRequirementsLock(c *chart.Chart) (*RequirementsLock, error) { r := &RequirementsLock{} return r, yaml.Unmarshal(data, r) } + +// ProcessRequirementsConditions disables charts based on condition path value in values +func ProcessRequirementsConditions(reqs *Requirements, cvals Values) { + var cond string + var conds []string + if reqs == nil || len(reqs.Dependencies) == 0 { + return + } + for _, r := range reqs.Dependencies { + var hasTrue, hasFalse bool + cond = string(r.Condition) + // check for list + if len(cond) > 0 { + if strings.Contains(cond, ",") { + conds = strings.Split(strings.TrimSpace(cond), ",") + } else { + conds = []string{strings.TrimSpace(cond)} + } + for _, c := range conds { + if len(c) > 0 { + // retrieve value + vv, err := cvals.PathValue(c) + if err == nil { + // if not bool, warn + if bv, ok := vv.(bool); ok { + if bv { + hasTrue = true + } else { + hasFalse = true + } + } else { + log.Printf("Warning: Condition path '%s' for chart %s returned non-bool value", c, r.Name) + } + } else if _, ok := err.(ErrNoValue); !ok { + // this is a real error + log.Printf("Warning: PathValue returned error %v", err) + + } + if vv != nil { + // got first value, break loop + break + } + } + } + if !hasTrue && hasFalse { + r.Enabled = false + } else if hasTrue { + r.Enabled = true + + } + } + + } + +} + +// ProcessRequirementsTags disables charts based on tags in values +func ProcessRequirementsTags(reqs *Requirements, cvals Values) { + vt, err := cvals.Table("tags") + if err != nil { + return + + } + if reqs == nil || len(reqs.Dependencies) == 0 { + return + } + for _, r := range reqs.Dependencies { + if len(r.Tags) > 0 { + tags := r.Tags + + var hasTrue, hasFalse bool + for _, k := range tags { + if b, ok := vt[k]; ok { + // if not bool, warn + if bv, ok := b.(bool); ok { + if bv { + hasTrue = true + } else { + hasFalse = true + } + } else { + log.Printf("Warning: Tag '%s' for chart %s returned non-bool value", k, r.Name) + } + } + } + if !hasTrue && hasFalse { + r.Enabled = false + } else if hasTrue || !hasTrue && !hasFalse { + r.Enabled = true + + } + + } + } + +} + +// ProcessRequirementsEnabled removes disabled charts from dependencies +func ProcessRequirementsEnabled(c *chart.Chart, v *chart.Config) error { + reqs, err := LoadRequirements(c) + if err != nil { + // if not just missing requirements file, return error + if nerr, ok := err.(ErrNoRequirementsFile); !ok { + return nerr + } + + // no requirements to process + return nil + } + // set all to true + for _, lr := range reqs.Dependencies { + lr.Enabled = true + } + cvals, err := CoalesceValues(c, v) + if err != nil { + return err + } + // flag dependencies as enabled/disabled + ProcessRequirementsTags(reqs, cvals) + ProcessRequirementsConditions(reqs, cvals) + + // make a map of charts to remove + rm := map[string]bool{} + for _, r := range reqs.Dependencies { + if !r.Enabled { + // remove disabled chart + rm[r.Name] = true + } + } + // don't keep disabled charts in new slice + cd := []*chart.Chart{} + copy(cd, c.Dependencies[:0]) + for _, n := range c.Dependencies { + if _, ok := rm[n.Metadata.Name]; !ok { + cd = append(cd, n) + } + + } + // recursively call self to process sub dependencies + for _, t := range cd { + err := ProcessRequirementsEnabled(t, v) + // if its not just missing requirements file, return error + if nerr, ok := err.(ErrNoRequirementsFile); !ok && err != nil { + return nerr + } + } + c.Dependencies = cd + + return nil +} diff --git a/pkg/chartutil/requirements_test.go b/pkg/chartutil/requirements_test.go index afcc75621..b9a5ae12a 100644 --- a/pkg/chartutil/requirements_test.go +++ b/pkg/chartutil/requirements_test.go @@ -15,7 +15,10 @@ limitations under the License. package chartutil import ( + "sort" "testing" + + "k8s.io/helm/pkg/proto/hapi/chart" ) func TestLoadRequirements(t *testing.T) { @@ -33,3 +36,173 @@ func TestLoadRequirementsLock(t *testing.T) { } verifyRequirementsLock(t, c) } +func TestRequirementsTagsNonValue(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // tags with no effect + v := &chart.Config{Raw: "tags:\n nothinguseful: false\n\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart", "subchart1", "subcharta", "subchartb"} + + verifyRequirementsEnabled(t, c, v, e) +} +func TestRequirementsTagsDisabledL1(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // tags disabling a group + v := &chart.Config{Raw: "tags:\n front-end: false\n\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart"} + + verifyRequirementsEnabled(t, c, v, e) +} +func TestRequirementsTagsEnabledL1(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // tags disabling a group and enabling a different group + v := &chart.Config{Raw: "tags:\n front-end: false\n\n back-end: true\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart", "subchart2", "subchartb", "subchartc"} + + verifyRequirementsEnabled(t, c, v, e) +} +func TestRequirementsTagsDisabledL2(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // tags disabling only children + v := &chart.Config{Raw: "tags:\n subcharta: false\n\n subchartb: false\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart", "subchart1"} + + verifyRequirementsEnabled(t, c, v, e) +} +func TestRequirementsTagsDisabledL1Mixed(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // tags disabling all parents/children with additional tag re-enabling a parent + v := &chart.Config{Raw: "tags:\n front-end: false\n\n subchart1: true\n\n back-end: false\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart", "subchart1"} + + verifyRequirementsEnabled(t, c, v, e) +} +func TestRequirementsConditionsNonValue(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // tags with no effect + v := &chart.Config{Raw: "subchart1:\n nothinguseful: false\n\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart", "subchart1", "subcharta", "subchartb"} + + verifyRequirementsEnabled(t, c, v, e) +} +func TestRequirementsConditionsEnabledL1Both(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // conditions enabling the parent charts, effectively enabling children + v := &chart.Config{Raw: "subchart1:\n enabled: true\nsubchart2:\n enabled: true\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart", "subchart1", "subchart2", "subcharta", "subchartb", "subchartb", "subchartc"} + + verifyRequirementsEnabled(t, c, v, e) +} +func TestRequirementsConditionsDisabledL1Both(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // conditions disabling the parent charts, effectively disabling children + v := &chart.Config{Raw: "subchart1:\n enabled: false\nsubchart2:\n enabled: false\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart"} + + verifyRequirementsEnabled(t, c, v, e) +} + +func TestRequirementsConditionsSecond(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // conditions a child using the second condition path of child's condition + v := &chart.Config{Raw: "subchart1:\n subcharta:\n enabled: false\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart", "subchart1", "subchartb"} + + verifyRequirementsEnabled(t, c, v, e) +} +func TestRequirementsCombinedDisabledL2(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // tags enabling a parent/child group with condition disabling one child + v := &chart.Config{Raw: "subchartc:\n enabled: false\ntags:\n back-end: true\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart", "subchart1", "subchart2", "subcharta", "subchartb", "subchartb"} + + verifyRequirementsEnabled(t, c, v, e) +} +func TestRequirementsCombinedDisabledL1(t *testing.T) { + c, err := Load("testdata/subpop") + if err != nil { + t.Fatalf("Failed to load testdata: %s", err) + } + // tags will not enable a child if parent is explicitly disabled with condition + v := &chart.Config{Raw: "subchart1:\n enabled: false\ntags:\n front-end: true\n"} + // expected charts including duplicates in alphanumeric order + e := []string{"parentchart"} + + verifyRequirementsEnabled(t, c, v, e) +} + +func verifyRequirementsEnabled(t *testing.T, c *chart.Chart, v *chart.Config, e []string) { + out := []*chart.Chart{} + err := ProcessRequirementsEnabled(c, v) + if err != nil { + t.Errorf("Error processing enabled requirements %v", err) + } + out = extractCharts(c, out) + // build list of chart names + p := []string{} + for _, r := range out { + p = append(p, r.Metadata.Name) + } + //sort alphanumeric and compare to expectations + sort.Strings(p) + if len(p) != len(e) { + t.Errorf("Error slice lengths do not match got %v, expected %v", len(p), len(e)) + return + } + for i := range p { + if p[i] != e[i] { + t.Errorf("Error slice values do not match got %v, expected %v", p[i], e[i]) + } + } +} + +// extractCharts recursively searches chart dependencies returning all charts found +func extractCharts(c *chart.Chart, out []*chart.Chart) []*chart.Chart { + + if len(c.Metadata.Name) > 0 { + out = append(out, c) + } + for _, d := range c.Dependencies { + out = extractCharts(d, out) + } + return out +} diff --git a/pkg/chartutil/testdata/subpop/Chart.yaml b/pkg/chartutil/testdata/subpop/Chart.yaml new file mode 100644 index 000000000..bbb0941c3 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: parentchart +version: 0.1.0 diff --git a/pkg/chartutil/testdata/subpop/README.md b/pkg/chartutil/testdata/subpop/README.md new file mode 100644 index 000000000..e43fbfe9c --- /dev/null +++ b/pkg/chartutil/testdata/subpop/README.md @@ -0,0 +1,18 @@ +## Subpop + +This chart is for testing the processing of enabled/disabled charts +via conditions and tags. + +Currently there are three levels: + +```` +parent +-1 tags: front-end, subchart1 +--A tags: front-end, subchartA +--B tags: front-end, subchartB +-2 tags: back-end, subchart2 +--B tags: back-end, subchartB +--C tags: back-end, subchartC +```` + +Tags and conditions are currently in requirements.yaml files. \ No newline at end of file diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/Chart.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/Chart.yaml new file mode 100644 index 000000000..f58d82818 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: subchart1 +version: 0.1.0 diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartA/Chart.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartA/Chart.yaml new file mode 100644 index 000000000..be3edcefb --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartA/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: subcharta +version: 0.1.0 diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartA/templates/service.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartA/templates/service.yaml new file mode 100644 index 000000000..fdf75aa91 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartA/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartA/values.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartA/values.yaml new file mode 100644 index 000000000..5e5b21065 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartA/values.yaml @@ -0,0 +1,21 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartB/Chart.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartB/Chart.yaml new file mode 100644 index 000000000..c3c6bbaf0 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartB/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: subchartb +version: 0.1.0 diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartB/templates/service.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartB/templates/service.yaml new file mode 100644 index 000000000..fdf75aa91 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartB/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartB/values.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartB/values.yaml new file mode 100644 index 000000000..5e5b21065 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/charts/subchartB/values.yaml @@ -0,0 +1,21 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/requirements.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/requirements.yaml new file mode 100644 index 000000000..94d278234 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/requirements.yaml @@ -0,0 +1,15 @@ +dependencies: + - name: subcharta + repository: http://localhost:10191 + version: 0.1.0 + condition: subcharta.enabled,subchart1.subcharta.enabled + tags: + - front-end + - subcharta + - name: subchartb + repository: http://localhost:10191 + version: 0.1.0 + condition: subchartb.enabled + tags: + - front-end + - subchartb diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml new file mode 100644 index 000000000..fdf75aa91 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/pkg/chartutil/testdata/subpop/charts/subchart1/values.yaml b/pkg/chartutil/testdata/subpop/charts/subchart1/values.yaml new file mode 100644 index 000000000..5e5b21065 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart1/values.yaml @@ -0,0 +1,21 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + diff --git a/pkg/chartutil/testdata/subpop/charts/subchart2/Chart.yaml b/pkg/chartutil/testdata/subpop/charts/subchart2/Chart.yaml new file mode 100644 index 000000000..2c76b0786 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart2/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: subchart2 +version: 0.1.0 diff --git a/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartB/Chart.yaml b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartB/Chart.yaml new file mode 100644 index 000000000..c3c6bbaf0 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartB/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: subchartb +version: 0.1.0 diff --git a/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartB/templates/service.yaml b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartB/templates/service.yaml new file mode 100644 index 000000000..0935aadce --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartB/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: subchart2-{{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: subchart2-{{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartB/values.yaml b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartB/values.yaml new file mode 100644 index 000000000..5e5b21065 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartB/values.yaml @@ -0,0 +1,21 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + diff --git a/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartC/Chart.yaml b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartC/Chart.yaml new file mode 100644 index 000000000..dcc45c088 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartC/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: subchartc +version: 0.1.0 diff --git a/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartC/templates/service.yaml b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartC/templates/service.yaml new file mode 100644 index 000000000..fdf75aa91 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartC/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartC/values.yaml b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartC/values.yaml new file mode 100644 index 000000000..5e5b21065 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart2/charts/subchartC/values.yaml @@ -0,0 +1,21 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + diff --git a/pkg/chartutil/testdata/subpop/charts/subchart2/requirements.yaml b/pkg/chartutil/testdata/subpop/charts/subchart2/requirements.yaml new file mode 100644 index 000000000..1f0023a08 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart2/requirements.yaml @@ -0,0 +1,15 @@ +dependencies: + - name: subchartb + repository: http://localhost:10191 + version: 0.1.0 + condition: subchartb.enabled,subchart2.subchartb.enabled + tags: + - back-end + - subchartb + - name: subchartc + repository: http://localhost:10191 + version: 0.1.0 + condition: subchartc.enabled + tags: + - back-end + - subchartc diff --git a/pkg/chartutil/testdata/subpop/charts/subchart2/templates/service.yaml b/pkg/chartutil/testdata/subpop/charts/subchart2/templates/service.yaml new file mode 100644 index 000000000..fdf75aa91 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart2/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/pkg/chartutil/testdata/subpop/charts/subchart2/values.yaml b/pkg/chartutil/testdata/subpop/charts/subchart2/values.yaml new file mode 100644 index 000000000..5e5b21065 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/charts/subchart2/values.yaml @@ -0,0 +1,21 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + diff --git a/pkg/chartutil/testdata/subpop/noreqs/Chart.yaml b/pkg/chartutil/testdata/subpop/noreqs/Chart.yaml new file mode 100644 index 000000000..bbb0941c3 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/noreqs/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: parentchart +version: 0.1.0 diff --git a/pkg/chartutil/testdata/subpop/noreqs/templates/service.yaml b/pkg/chartutil/testdata/subpop/noreqs/templates/service.yaml new file mode 100644 index 000000000..fdf75aa91 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/noreqs/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/pkg/chartutil/testdata/subpop/noreqs/values.yaml b/pkg/chartutil/testdata/subpop/noreqs/values.yaml new file mode 100644 index 000000000..4ed3b7ad3 --- /dev/null +++ b/pkg/chartutil/testdata/subpop/noreqs/values.yaml @@ -0,0 +1,26 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + + +# switch-like +tags: + front-end: true + back-end: false diff --git a/pkg/chartutil/testdata/subpop/requirements.yaml b/pkg/chartutil/testdata/subpop/requirements.yaml new file mode 100644 index 000000000..9840e047d --- /dev/null +++ b/pkg/chartutil/testdata/subpop/requirements.yaml @@ -0,0 +1,15 @@ +dependencies: + - name: subchart1 + repository: http://localhost:10191 + version: 0.1.0 + condition: subchart1.enabled + tags: + - front-end + - subchart1 + - name: subchart2 + repository: http://localhost:10191 + version: 0.1.0 + condition: subchart2.enabled + tags: + - back-end + - subchart2 diff --git a/pkg/chartutil/testdata/subpop/values.yaml b/pkg/chartutil/testdata/subpop/values.yaml new file mode 100644 index 000000000..85fc7b49d --- /dev/null +++ b/pkg/chartutil/testdata/subpop/values.yaml @@ -0,0 +1,7 @@ +# parent/values.yaml + +# switch-like +tags: + front-end: true + back-end: false + diff --git a/pkg/chartutil/values.go b/pkg/chartutil/values.go index 499cb89b0..cc60860cd 100644 --- a/pkg/chartutil/values.go +++ b/pkg/chartutil/values.go @@ -31,6 +31,9 @@ import ( // ErrNoTable indicates that a chart does not have a matching table. type ErrNoTable error +// ErrNoValue indicates that Values does not contain a key with a value +type ErrNoValue error + // GlobalKey is the name of the Values key that is used for storing global vars. const GlobalKey = "global" @@ -376,3 +379,43 @@ func istable(v interface{}) bool { _, ok := v.(map[string]interface{}) return ok } + +// PathValue takes a yaml path with . notation and returns the value if exists +func (v Values) PathValue(ypath string) (interface{}, error) { + if len(ypath) == 0 { + return nil, fmt.Errorf("yaml path string cannot be zero length") + } + yps := strings.Split(ypath, ".") + if len(yps) == 1 { + // if exists must be root key not table + vals := v.AsMap() + k := yps[0] + if _, ok := vals[k]; ok && !istable(vals[k]) { + // key found + return vals[yps[0]], nil + } + // key not found + return nil, ErrNoValue(fmt.Errorf("%v is not a value", k)) + } + // join all elements of YAML path except last to get string table path + ypsLen := len(yps) + table := yps[:ypsLen-1] + st := strings.Join(table, ".") + // get the last element as a string key + key := yps[ypsLen-1:] + sk := string(key[0]) + // get our table for table path + t, err := v.Table(st) + if err != nil { + //no table + return nil, ErrNoValue(fmt.Errorf("%v is not a value", sk)) + } + // check table for key and ensure value is not a table + if k, ok := t[sk]; ok && !istable(k) { + // key found + return k, nil + } + + // key not found + return nil, ErrNoValue(fmt.Errorf("key not found: %s", sk)) +} diff --git a/pkg/chartutil/values_test.go b/pkg/chartutil/values_test.go index 2c519b099..1fe8b5c8b 100644 --- a/pkg/chartutil/values_test.go +++ b/pkg/chartutil/values_test.go @@ -407,3 +407,37 @@ func TestCoalesceTables(t *testing.T) { t.Errorf("Expected boat string, got %v", dst["boat"]) } } +func TestPathValue(t *testing.T) { + doc := ` +title: "Moby Dick" +chapter: + one: + title: "Loomings" + two: + title: "The Carpet-Bag" + three: + title: "The Spouter Inn" +` + d, err := ReadValues([]byte(doc)) + if err != nil { + t.Fatalf("Failed to parse the White Whale: %s", err) + } + + if v, err := d.PathValue("chapter.one.title"); err != nil { + t.Errorf("Got error instead of title: %s\n%v", err, d) + } else if v != "Loomings" { + t.Errorf("No error but got wrong value for title: %s\n%v", err, d) + } + if _, err := d.PathValue("chapter.one.doesntexist"); err == nil { + t.Errorf("Non-existent key should return error: %s\n%v", err, d) + } + if _, err := d.PathValue("chapter.doesntexist.one"); err == nil { + t.Errorf("Non-existent key in middle of path should return error: %s\n%v", err, d) + } + if v, err := d.PathValue("title"); err == nil { + if v != "Moby Dick" { + t.Errorf("Failed to return values for root key title") + } + } + +} diff --git a/pkg/helm/client.go b/pkg/helm/client.go index c8ade3467..03df61c6d 100644 --- a/pkg/helm/client.go +++ b/pkg/helm/client.go @@ -92,6 +92,11 @@ func (h *Client) InstallReleaseFromChart(chart *chart.Chart, ns string, opts ... return nil, err } } + err := chartutil.ProcessRequirementsEnabled(req.Chart, req.Values) + if err != nil { + return nil, err + } + return h.install(ctx, req) } @@ -156,6 +161,11 @@ func (h *Client) UpdateReleaseFromChart(rlsName string, chart *chart.Chart, opts return nil, err } } + err := chartutil.ProcessRequirementsEnabled(req.Chart, req.Values) + if err != nil { + return nil, err + } + return h.update(ctx, req) } diff --git a/pkg/proto/hapi/chart/metadata.pb.go b/pkg/proto/hapi/chart/metadata.pb.go index 41e0999f4..322719e3d 100644 --- a/pkg/proto/hapi/chart/metadata.pb.go +++ b/pkg/proto/hapi/chart/metadata.pb.go @@ -71,6 +71,10 @@ type Metadata struct { Icon string `protobuf:"bytes,9,opt,name=icon" json:"icon,omitempty"` // The API Version of this chart. ApiVersion string `protobuf:"bytes,10,opt,name=apiVersion" json:"apiVersion,omitempty"` + // The condition to check to enable chart + Condition string `protobuf:"bytes,11,opt,name=condition" json:"condition,omitempty"` + // The tags to check to enable chart + Tags []string `protobuf:"bytes,12,opt,name=tags" json:"tags,omitempty"` } func (m *Metadata) Reset() { *m = Metadata{} } @@ -94,24 +98,25 @@ func init() { func init() { proto.RegisterFile("hapi/chart/metadata.proto", fileDescriptor2) } var fileDescriptor2 = []byte{ - // 290 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x6c, 0x91, 0x4d, 0x4b, 0xf4, 0x30, - 0x14, 0x85, 0xdf, 0x4e, 0xbf, 0x6f, 0x37, 0xc3, 0xe5, 0x65, 0x88, 0x2e, 0xa4, 0x74, 0xd5, 0x55, - 0x07, 0x14, 0xc4, 0xb5, 0x20, 0x2e, 0x74, 0x3a, 0x52, 0xfc, 0x00, 0x77, 0xb1, 0x0d, 0x36, 0x68, - 0x93, 0x92, 0x44, 0xc5, 0xff, 0xe8, 0x8f, 0x92, 0xa6, 0x9d, 0x99, 0x2e, 0xdc, 0xdd, 0x73, 0x9e, - 0x9e, 0xdb, 0x9c, 0x04, 0x8e, 0x5a, 0xda, 0xf3, 0x75, 0xdd, 0x52, 0x65, 0xd6, 0x1d, 0x33, 0xb4, - 0xa1, 0x86, 0x16, 0xbd, 0x92, 0x46, 0x22, 0x0c, 0xa8, 0xb0, 0x28, 0x3b, 0x07, 0xd8, 0x50, 0x2e, - 0x0c, 0xe5, 0x82, 0x29, 0x44, 0xf0, 0x04, 0xed, 0x18, 0x71, 0x52, 0x27, 0x8f, 0x2b, 0x3b, 0xe3, - 0x7f, 0xf0, 0x59, 0x47, 0xf9, 0x3b, 0x59, 0x58, 0x73, 0x14, 0xd9, 0xcf, 0x02, 0xa2, 0xcd, 0xb4, - 0xf6, 0xcf, 0x18, 0x82, 0xd7, 0xca, 0x8e, 0x4d, 0x29, 0x3b, 0x23, 0x81, 0x50, 0xcb, 0x0f, 0x55, - 0x33, 0x4d, 0xdc, 0xd4, 0xcd, 0xe3, 0x6a, 0x27, 0x07, 0xf2, 0xc9, 0x94, 0xe6, 0x52, 0x10, 0xcf, - 0x06, 0x76, 0x12, 0x53, 0x48, 0x1a, 0xa6, 0x6b, 0xc5, 0x7b, 0x33, 0x50, 0xdf, 0xd2, 0xb9, 0x85, - 0xc7, 0x10, 0xbd, 0xb1, 0xef, 0x2f, 0xa9, 0x1a, 0x4d, 0x02, 0xbb, 0x76, 0xaf, 0xf1, 0x02, 0x92, - 0x6e, 0x5f, 0x4f, 0x93, 0x30, 0x75, 0xf3, 0xe4, 0x74, 0x55, 0x1c, 0x2e, 0xa0, 0x38, 0xb4, 0xaf, - 0xe6, 0x9f, 0xe2, 0x0a, 0x02, 0x26, 0x5e, 0xb9, 0x60, 0x24, 0xb2, 0xbf, 0x9c, 0xd4, 0xd0, 0x8b, - 0xd7, 0x52, 0x90, 0x78, 0xec, 0x35, 0xcc, 0x78, 0x02, 0x40, 0x7b, 0xfe, 0x38, 0x15, 0x00, 0x4b, - 0x66, 0x4e, 0x96, 0x42, 0x70, 0x35, 0xa6, 0x13, 0x08, 0x1f, 0xca, 0x9b, 0x72, 0xfb, 0x54, 0x2e, - 0xff, 0x61, 0x0c, 0xfe, 0xf5, 0xf6, 0xfe, 0xee, 0x76, 0xe9, 0x5c, 0x86, 0xcf, 0xbe, 0x3d, 0xce, - 0x4b, 0x60, 0x9f, 0xe8, 0xec, 0x37, 0x00, 0x00, 0xff, 0xff, 0x65, 0x86, 0x8b, 0xda, 0xbf, 0x01, - 0x00, 0x00, + // 311 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x6c, 0x91, 0xcd, 0x4b, 0xc3, 0x40, + 0x10, 0xc5, 0xed, 0x47, 0x92, 0x66, 0xe2, 0xa1, 0x0c, 0x52, 0x56, 0x11, 0x09, 0x3d, 0xf5, 0x94, + 0x82, 0x82, 0x78, 0x16, 0xc4, 0x83, 0xb6, 0x95, 0xe2, 0x07, 0x78, 0x5b, 0x93, 0xa5, 0x5d, 0x34, + 0xbb, 0x61, 0x77, 0x55, 0xfc, 0xe7, 0x45, 0x76, 0x92, 0x36, 0x39, 0x78, 0x9b, 0xf7, 0x5e, 0xe6, + 0x0d, 0xbf, 0x2c, 0x1c, 0x6f, 0x79, 0x25, 0xe7, 0xf9, 0x96, 0x1b, 0x37, 0x2f, 0x85, 0xe3, 0x05, + 0x77, 0x3c, 0xab, 0x8c, 0x76, 0x1a, 0xc1, 0x47, 0x19, 0x45, 0xd3, 0x4b, 0x80, 0x05, 0x97, 0xca, + 0x71, 0xa9, 0x84, 0x41, 0x84, 0xa1, 0xe2, 0xa5, 0x60, 0xbd, 0xb4, 0x37, 0x8b, 0xd7, 0x34, 0xe3, + 0x11, 0x04, 0xa2, 0xe4, 0xf2, 0x83, 0xf5, 0xc9, 0xac, 0xc5, 0xf4, 0xb7, 0x0f, 0xa3, 0x45, 0x53, + 0xfb, 0xef, 0x1a, 0xc2, 0x70, 0xab, 0x4b, 0xd1, 0x6c, 0xd1, 0x8c, 0x0c, 0x22, 0xab, 0x3f, 0x4d, + 0x2e, 0x2c, 0x1b, 0xa4, 0x83, 0x59, 0xbc, 0xde, 0x49, 0x9f, 0x7c, 0x09, 0x63, 0xa5, 0x56, 0x6c, + 0x48, 0x0b, 0x3b, 0x89, 0x29, 0x24, 0x85, 0xb0, 0xb9, 0x91, 0x95, 0xf3, 0x69, 0x40, 0x69, 0xd7, + 0xc2, 0x13, 0x18, 0xbd, 0x8b, 0x9f, 0x6f, 0x6d, 0x0a, 0xcb, 0x42, 0xaa, 0xdd, 0x6b, 0xbc, 0x82, + 0xa4, 0xdc, 0xe3, 0x59, 0x16, 0xa5, 0x83, 0x59, 0x72, 0x3e, 0xc9, 0xda, 0x1f, 0x90, 0xb5, 0xf4, + 0xeb, 0xee, 0xa7, 0x38, 0x81, 0x50, 0xa8, 0x8d, 0x54, 0x82, 0x8d, 0xe8, 0x64, 0xa3, 0x3c, 0x97, + 0xcc, 0xb5, 0x62, 0x71, 0xcd, 0xe5, 0x67, 0x3c, 0x03, 0xe0, 0x95, 0x7c, 0x6e, 0x00, 0x80, 0x92, + 0x8e, 0x83, 0xa7, 0x10, 0xe7, 0x5a, 0x15, 0x92, 0x08, 0x12, 0x8a, 0x5b, 0xc3, 0x37, 0x3a, 0xbe, + 0xb1, 0xec, 0xb0, 0x6e, 0xf4, 0xf3, 0x34, 0x85, 0xf0, 0xa6, 0xbe, 0x97, 0x40, 0xf4, 0xb4, 0xbc, + 0x5b, 0xae, 0x5e, 0x96, 0xe3, 0x03, 0x8c, 0x21, 0xb8, 0x5d, 0x3d, 0x3e, 0xdc, 0x8f, 0x7b, 0xd7, + 0xd1, 0x6b, 0x40, 0x00, 0x6f, 0x21, 0x3d, 0xea, 0xc5, 0x5f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x5a, + 0xa8, 0xcd, 0xe1, 0xf1, 0x01, 0x00, 0x00, } diff --git a/pkg/resolver/resolver_test.go b/pkg/resolver/resolver_test.go index edc160a63..4a4f853b7 100644 --- a/pkg/resolver/resolver_test.go +++ b/pkg/resolver/resolver_test.go @@ -141,7 +141,7 @@ func TestResolve(t *testing.T) { } func TestHashReq(t *testing.T) { - expect := "sha256:e70e41f8922e19558a8bf62f591a8b70c8e4622e3c03e5415f09aba881f13885" + expect := "sha256:c8250374210bd909cef274be64f871bd4e376d4ecd34a1589b5abf90b68866ba" req := &chartutil.Requirements{ Dependencies: []*chartutil.Dependency{ {Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts"},