diff --git a/pkg/action/install.go b/pkg/action/install.go index caeefca68..ad9eb448d 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -181,17 +181,6 @@ func (i *Install) Run(chrt *chart.Chart, vals map[string]interface{}) (*release. return nil, err } - // Pre-install anything in the crd/ directory. We do this before Helm - // contacts the upstream server and builds the capabilities object. - if crds := chrt.CRDObjects(); !i.ClientOnly && !i.SkipCRDs && len(crds) > 0 { - // On dry run, bail here - if i.DryRun { - i.cfg.Log("WARNING: This chart or one of its subcharts contains CRDs. Rendering may fail or contain inaccuracies.") - } else if err := i.installCRDs(crds); err != nil { - return nil, err - } - } - if i.ClientOnly { // Add mock objects in here so it doesn't use Kube API server // NOTE(bacongobbler): used for `helm template` @@ -210,6 +199,17 @@ func (i *Install) Run(chrt *chart.Chart, vals map[string]interface{}) (*release. return nil, err } + // Pre-install anything in the crd/ directory. We do this before Helm + // contacts the upstream server and builds the capabilities object. + if crds := chrt.CRDObjects(); !i.ClientOnly && !i.SkipCRDs && len(crds) > 0 { + // On dry run, bail here + if i.DryRun { + i.cfg.Log("WARNING: This chart or one of its subcharts contains CRDs. Rendering may fail or contain inaccuracies.") + } else if err := i.installCRDs(crds); err != nil { + return nil, err + } + } + // Make sure if Atomic is set, that wait is set as well. This makes it so // the user doesn't have to specify both i.Wait = i.Wait || i.Atomic diff --git a/pkg/chart/chart.go b/pkg/chart/chart.go index a3bed63a3..95cc2cf8c 100644 --- a/pkg/chart/chart.go +++ b/pkg/chart/chart.go @@ -162,7 +162,11 @@ func (ch *Chart) CRDObjects() []CRD { } // Get CRDs from dependencies, too. for _, dep := range ch.Dependencies() { - crds = append(crds, dep.CRDObjects()...) + for _, req := range ch.Metadata.Dependencies { + if dep.Name() == req.Name && req.Enabled { + crds = append(crds, dep.CRDObjects()...) + } + } } return crds } diff --git a/pkg/chart/chart_test.go b/pkg/chart/chart_test.go index ef8cec3ad..fe01a6f7b 100644 --- a/pkg/chart/chart_test.go +++ b/pkg/chart/chart_test.go @@ -209,3 +209,81 @@ func TestCRDObjects(t *testing.T) { crds := chrt.CRDObjects() is.Equal(expected, crds) } + +func TestCRDObjectsWithDependecy(t *testing.T) { + chrt := createTestChartWithCrdAndDependency(true) + expected := []CRD{ + { + Name: "crds/foo.yaml", + Filename: "crds/foo.yaml", + File: &File{ + Name: "crds/foo.yaml", + Data: []byte("hello"), + }, + }, + { + Name: "crds/baz.yaml", + Filename: "dependency1/crds/baz.yaml", + File: &File{ + Name: "crds/baz.yaml", + Data: []byte("hello"), + }, + }, + } + + is := assert.New(t) + crds := chrt.CRDObjects() + is.Equal(expected, crds) +} + +func TestCRDObjectsWithDependecyDisabled(t *testing.T) { + chrt := createTestChartWithCrdAndDependency(false) + expected := []CRD{ + { + Name: "crds/foo.yaml", + Filename: "crds/foo.yaml", + File: &File{ + Name: "crds/foo.yaml", + Data: []byte("hello"), + }, + }, + } + + is := assert.New(t) + crds := chrt.CRDObjects() + is.Equal(expected, crds) +} + +func createTestChartWithCrdAndDependency(dependencyEnabled bool) Chart { + chrt := Chart{ + Files: []*File{ + { + Name: "crds/foo.yaml", + Data: []byte("hello"), + }, + }, + dependencies: []*Chart{ + { + Metadata: &Metadata{ + Name: "dependency1", + }, + Files: []*File{ + { + Name: "crds/baz.yaml", + Data: []byte("hello"), + }, + }, + }, + }, + Metadata: &Metadata{ + Dependencies: []*Dependency{ + { + Name: "dependency1", + Enabled: dependencyEnabled, + }, + }, + }, + } + + return chrt +}