Skip to content

Commit

Permalink
fix: Change config option to autodiscover.mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jskrill committed Oct 25, 2023
1 parent ff1abd7 commit 0569d74
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 127 deletions.
30 changes: 22 additions & 8 deletions runatlantis.io/docs/repo-level-atlantis-yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ in your repo.
If you have many directories with Terraform configuration, each directory will
need to be defined.

This behavior can be overriden by setting `discover_projects_with_missing_config` to
`true` in which case Atlantis will still try to discover projects which were not
This behavior can be overriden by setting `autodiscover.mode` to
`enabled` in which case Atlantis will still try to discover projects which were not
explicitly configured. If the directory of any discovered project conflicts with a
manually configured project, the manually configured project will take precedence.
:::
Expand All @@ -54,7 +54,8 @@ manually configured project, the manually configured project will take precedenc
version: 3
automerge: true
delete_source_branch_on_merge: true
discover_projects_with_missing_config: true
autoplan:
mode: auto
parallel_plan: true
parallel_apply: true
abort_on_execution_order_fail: true
Expand Down Expand Up @@ -288,12 +289,25 @@ in each group one by one.
If any plan/apply fails and `abort_on_execution_order_fail` is set to true on a repo level, all the
following groups will be aborted. For this example, if project2 fails then project1 will not run.

### Discovering projects without explicit config
### Autodiscovery Config
```yaml
version: 3
discover_projects_with_missing_config: true
projects:
- dir: project1
autodiscover:
mode: "auto"
```
The above is the default configuration for `autodiscover.mode`. When `autodiscover.mode` is auto,
projects will be discovered only if the repo has no `projects` configured.

```yaml
autodiscover:
mode: "disabled"
```
With the config above, Atlantis will never try to discover projects, even when there are no
`projects` configured. This is useful if dynamically generating Atlantis config in pre_workflow hooks.
See [Dynamic Repo Config Generation](pre-workflow-hooks.html#dynamic-repo-config-generation).

```yaml
autodiscover:
mode: "enabled"
```
With the config above, Atlantis will unconditionally try to discover projects based on modified_files,
even when the directory of the project is missing from the configured `projects` in the repo configuration.
Expand Down
67 changes: 44 additions & 23 deletions server/core/config/parser_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ workflows:
- run: old 'shell parsing'
`,
exp: valid.RepoCfg{
Version: 2,
Version: 2,
AutoDiscover: raw.DefaultAutoDiscover(),
Workflows: map[string]valid.Workflow{
"custom": {
Name: "custom",
Expand Down Expand Up @@ -190,9 +191,10 @@ workflows:
version: 3
projects:`,
exp: valid.RepoCfg{
Version: 3,
Projects: nil,
Workflows: map[string]valid.Workflow{},
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: nil,
Workflows: map[string]valid.Workflow{},
},
},
{
Expand All @@ -210,7 +212,8 @@ version: 3
projects:
- dir: .`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand All @@ -235,7 +238,8 @@ projects:
- dir: "."
`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand All @@ -259,7 +263,8 @@ projects:
when_modified: ["**/*.tf*"]
`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand All @@ -281,7 +286,8 @@ projects:
- dir: "."
`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand All @@ -304,7 +310,8 @@ projects:
workflows: ~
`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -332,7 +339,8 @@ workflows:
steps:
`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -361,7 +369,8 @@ projects:
workflows:
myworkflow: ~`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -395,7 +404,8 @@ projects:
workflows:
myworkflow: ~`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -429,7 +439,8 @@ projects:
workflows:
myworkflow: ~`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -463,7 +474,8 @@ projects:
workflows:
myworkflow: ~`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -497,7 +509,8 @@ projects:
workflows:
myworkflow: ~`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -531,7 +544,8 @@ projects:
workflows:
myworkflow: ~`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -565,7 +579,8 @@ projects:
workflows:
myworkflow: ~`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -599,7 +614,8 @@ projects:
workflows:
myworkflow: ~`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -710,7 +726,8 @@ projects:
dir: .
workspace: workspace`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Name: String("myname"),
Expand Down Expand Up @@ -762,7 +779,8 @@ workflows:
- state_rm
`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -861,7 +879,8 @@ workflows:
extra_args: ["a", "b"]
`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -952,7 +971,8 @@ workflows:
- run: echo apply "arg 4"
`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down Expand Up @@ -1045,7 +1065,8 @@ workflows:
value: env_value
`,
exp: valid.RepoCfg{
Version: 3,
Version: 3,
AutoDiscover: raw.DefaultAutoDiscover(),
Projects: []valid.Project{
{
Dir: ".",
Expand Down
38 changes: 38 additions & 0 deletions server/core/config/raw/autodiscover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package raw

import (
validation "github.com/go-ozzo/ozzo-validation"
"github.com/runatlantis/atlantis/server/core/config/valid"
)

var DefaultAutoDiscoverMode = valid.AutoDiscoverAutoMode

type AutoDiscover struct {
Mode *valid.AutoDiscoverMode `yaml:"mode,omitempty"`
}

func (a AutoDiscover) ToValid() *valid.AutoDiscover {
var v valid.AutoDiscover

if a.Mode != nil {
v.Mode = *a.Mode
} else {
v.Mode = DefaultAutoDiscoverMode
}

return &v
}

func (a AutoDiscover) Validate() error {
res := validation.ValidateStruct(&a,
// If a.Mode is nil, this should still pass validation.
validation.Field(&a.Mode, validation.In(valid.AutoDiscoverAutoMode, valid.AutoDiscoverDisabledMode, valid.AutoDiscoverEnabledMode)),
)
return res
}

func DefaultAutoDiscover() *valid.AutoDiscover {
return &valid.AutoDiscover{
Mode: DefaultAutoDiscoverMode,
}
}
Loading

0 comments on commit 0569d74

Please sign in to comment.