From 19220cbade85c210b3edce381847d6a2c4e9d9ae Mon Sep 17 00:00:00 2001 From: Marcel Dias Date: Thu, 14 May 2020 01:16:30 -0300 Subject: [PATCH] Add capability to disable subcomponent per environment (#286) * Add capability to disable subcomponent per environment * Fix identation * Add example of how to disable subcomponent * Add generate test with disabled subcomponents * Move Config.Disabled check from enqueue func to main subcomponent iteration * Fix tests * Fix tests * build trigger --- cmd/generate_test.go | 13 ++++++++ core/component.go | 6 ++++ core/componentConfig.go | 1 + core/componentConfig_test.go | 32 ++++++++++++++++++ core/component_test.go | 33 +++++++++++++++++++ docs/config.md | 11 +++++++ testdata/disabled/component.yaml | 13 ++++++++ testdata/disabled/config/default.yaml | 6 ++++ testdata/disabled/config/disabled.yaml | 7 ++++ testdata/generate-disabled/component.yaml | 18 ++++++++++ .../generate-disabled/config/disabled.yaml | 11 +++++++ 11 files changed, 151 insertions(+) create mode 100644 testdata/disabled/component.yaml create mode 100644 testdata/disabled/config/default.yaml create mode 100644 testdata/disabled/config/disabled.yaml create mode 100644 testdata/generate-disabled/component.yaml create mode 100644 testdata/generate-disabled/config/disabled.yaml diff --git a/cmd/generate_test.go b/cmd/generate_test.go index e7fbebb..e213d36 100644 --- a/cmd/generate_test.go +++ b/cmd/generate_test.go @@ -69,3 +69,16 @@ func TestGenerateWithHooks(t *testing.T) { assert.Nil(t, err) } + +func TestGenerateDisabledSubcomponent(t *testing.T) { + components, err := Generate("../testdata/generate-disabled", []string{"disabled"}, false) + + expectedLengths := map[string]int{ + "disabled-stack": 0, + } + + assert.Nil(t, err) + assert.Equal(t, 1, len(components)) + + checkComponentLengthsAgainstExpected(t, components, expectedLengths) +} diff --git a/core/component.go b/core/component.go index da8cb4a..39e914c 100644 --- a/core/component.go +++ b/core/component.go @@ -396,6 +396,12 @@ func WalkComponentTree(startingPath string, environments []string, iterator comp results <- WalkResult{Error: err} } + // Do not add to the queue if component or subcomponent is Disabled. + if subcomponent.Config.Disabled { + logger.Info(emoji.Sprintf(":prohibited: Subcomponent '%s' is disabled", subcomponent.Name)) + continue + } + // Depending if the subcomponent is inlined or not; prepare the component to either load // config/path info from filesystem (non-inlined) or inherit from parent (inlined) if subcomponent.ComponentType == "component" || subcomponent.ComponentType == "" { diff --git a/core/componentConfig.go b/core/componentConfig.go index 541a90e..17f8c9e 100644 --- a/core/componentConfig.go +++ b/core/componentConfig.go @@ -20,6 +20,7 @@ type ComponentConfig struct { Serialization string `yaml:"-" json:"-"` Namespace string `yaml:"namespace,omitempty" json:"namespace,omitempty"` InjectNamespace bool `yaml:"injectNamespace,omitempty" json:"injectNamespace,omitempty"` + Disabled bool `yaml:"disabled,omitempty" json:"disabled,omitempty"` Config map[string]interface{} `yaml:"config,omitempty" json:"config,omitempty"` Subcomponents map[string]ComponentConfig `yaml:"subcomponents,omitempty" json:"subcomponents,omitempty"` } diff --git a/core/componentConfig_test.go b/core/componentConfig_test.go index ea63776..c344c0a 100644 --- a/core/componentConfig_test.go +++ b/core/componentConfig_test.go @@ -142,3 +142,35 @@ func TestWriteJSON(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 132, len(configContents)) } + +func TestDisabledDefault(t *testing.T) { + config := NewComponentConfig("../testdata/disabled") + + err := config.Load("default") + assert.Nil(t, err) + + cloudNativeSubcomponent := config.Subcomponents["cloud-native"] + elasticsearchSubcomponent := config.Subcomponents["elasticsearch"] + + cloudNativeDisabled := cloudNativeSubcomponent.Disabled + assert.Equal(t, false, cloudNativeDisabled) + + elasticsearchDisabled := elasticsearchSubcomponent.Disabled + assert.Equal(t, false, elasticsearchDisabled) +} + +func TestDisabledTrue(t *testing.T) { + config := NewComponentConfig("../testdata/disabled") + + err := config.Load("disabled") + assert.Nil(t, err) + + cloudNativeSubcomponent := config.Subcomponents["cloud-native"] + elasticsearchSubcomponent := config.Subcomponents["elasticsearch"] + + cloudNativeDisabled := cloudNativeSubcomponent.Disabled + assert.Equal(t, true, cloudNativeDisabled) + + elasticsearchDisabled := elasticsearchSubcomponent.Disabled + assert.Equal(t, true, elasticsearchDisabled) +} \ No newline at end of file diff --git a/core/component_test.go b/core/component_test.go index 4842791..586ac25 100644 --- a/core/component_test.go +++ b/core/component_test.go @@ -140,3 +140,36 @@ func TestWriteComponent(t *testing.T) { err = component.Write() assert.Nil(t, err) } + +func TestLoadDisabledComponentDefaultValue(t *testing.T) { + component := Component{ + PhysicalPath: "../testdata/disabled", + LogicalPath: "", + } + + component, err := component.LoadComponent() + assert.Nil(t, err) + + err = component.LoadConfig([]string{"default"}) + assert.Nil(t, err) + + assert.Equal(t, false, component.Config.Subcomponents["cloud-native"].Disabled) + assert.Equal(t, false, component.Config.Subcomponents["elasticsearch"].Disabled) + +} + +func TestLoadDisabledComponent(t *testing.T) { + component := Component{ + PhysicalPath: "../testdata/disabled", + LogicalPath: "", + } + + component, err := component.LoadComponent() + assert.Nil(t, err) + + err = component.LoadConfig([]string{"disabled"}) + assert.Nil(t, err) + + assert.Equal(t, true, component.Config.Subcomponents["cloud-native"].Disabled) + assert.Equal(t, true, component.Config.Subcomponents["elasticsearch"].Disabled) +} diff --git a/docs/config.md b/docs/config.md index f7ed814..25cae7c 100644 --- a/docs/config.md +++ b/docs/config.md @@ -86,3 +86,14 @@ subcomponents: istio-crd: namespace: istio-system ``` + +### Disable subcomponents per environment + +It is possible to disable subcomponent per environment with a simple `disabled: true` in the +environment config file + +```yaml +subcomponents: + redis: + disabled: true +``` diff --git a/testdata/disabled/component.yaml b/testdata/disabled/component.yaml new file mode 100644 index 0000000..3dc7641 --- /dev/null +++ b/testdata/disabled/component.yaml @@ -0,0 +1,13 @@ +name: disabled +type: component +subcomponents: +- name: cloud-native + type: component + source: https://github.com/timfpark/fabrikate-cloud-native + method: git + version: 8ad79e73e0665e347e1553ad7ca32b6e590e007a +- name: elasticsearch + type: helm + source: https://github.com/helm/charts + method: git + path: stable/elasticsearch diff --git a/testdata/disabled/config/default.yaml b/testdata/disabled/config/default.yaml new file mode 100644 index 0000000..73005a6 --- /dev/null +++ b/testdata/disabled/config/default.yaml @@ -0,0 +1,6 @@ +subcomponents: + elasticsearch: + config: + fod: rad + foo: rad + zoo: zaa \ No newline at end of file diff --git a/testdata/disabled/config/disabled.yaml b/testdata/disabled/config/disabled.yaml new file mode 100644 index 0000000..b433084 --- /dev/null +++ b/testdata/disabled/config/disabled.yaml @@ -0,0 +1,7 @@ +subcomponents: + cloud-native: + disabled: true + elasticsearch: + disabled: true + config: + fod: bar \ No newline at end of file diff --git a/testdata/generate-disabled/component.yaml b/testdata/generate-disabled/component.yaml new file mode 100644 index 0000000..1c9270d --- /dev/null +++ b/testdata/generate-disabled/component.yaml @@ -0,0 +1,18 @@ +name: disabled-stack +type: component +subcomponents: + - name: pod-info + type: helm + method: git + source: https://github.com/stefanprodan/podinfo + path: charts/podinfo + - name: mysql + type: helm + method: git + source: https://github.com/helm/charts + path: stable/mysql + - name: bookinfo # Istio BookInfo application - wrapped in Fabrikate component + source: https://github.com/microsoft/fabrikate-definitions.git + path: definitions/fabrikate-bookinfo + method: git + diff --git a/testdata/generate-disabled/config/disabled.yaml b/testdata/generate-disabled/config/disabled.yaml new file mode 100644 index 0000000..11c64cb --- /dev/null +++ b/testdata/generate-disabled/config/disabled.yaml @@ -0,0 +1,11 @@ +subcomponents: + pod-info: + disabled: true + config: + env: local + mysql: + disabled: true + config: + env: local + bookinfo: + disabled: true