Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
Add capability to disable subcomponent per environment (#286)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
marcel-dias authored May 14, 2020
1 parent 2b389c9 commit 19220cb
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 6 additions & 0 deletions core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
1 change: 1 addition & 0 deletions core/componentConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
32 changes: 32 additions & 0 deletions core/componentConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
33 changes: 33 additions & 0 deletions core/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 11 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
13 changes: 13 additions & 0 deletions testdata/disabled/component.yaml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions testdata/disabled/config/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
subcomponents:
elasticsearch:
config:
fod: rad
foo: rad
zoo: zaa
7 changes: 7 additions & 0 deletions testdata/disabled/config/disabled.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
subcomponents:
cloud-native:
disabled: true
elasticsearch:
disabled: true
config:
fod: bar
18 changes: 18 additions & 0 deletions testdata/generate-disabled/component.yaml
Original file line number Diff line number Diff line change
@@ -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

11 changes: 11 additions & 0 deletions testdata/generate-disabled/config/disabled.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
subcomponents:
pod-info:
disabled: true
config:
env: local
mysql:
disabled: true
config:
env: local
bookinfo:
disabled: true

0 comments on commit 19220cb

Please sign in to comment.