Skip to content

Commit

Permalink
[DEVCON-6849] First part of new flow of adhoc mode (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
smonero authored Mar 8, 2024
1 parent 274776f commit 2a0da82
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 9 deletions.
67 changes: 67 additions & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cmd

import (
"github.com/pkg/errors"
cfgParser "github.com/runatlantis/atlantis/server/config"
"github.com/runatlantis/atlantis/server/config/valid"
"github.com/runatlantis/atlantis/server/legacy"
"github.com/runatlantis/atlantis/server/logging"
adhoc "github.com/runatlantis/atlantis/server/neptune/adhoc"
adhocconfig "github.com/runatlantis/atlantis/server/neptune/adhoc/config"
neptune "github.com/runatlantis/atlantis/server/neptune/temporalworker/config"
)

type Adhoc struct{}

func (a *Adhoc) NewServer(userConfig legacy.UserConfig, config legacy.Config) (ServerStarter, error) {
ctxLogger, err := logging.NewLoggerFromLevel(userConfig.ToLogLevel())
if err != nil {
return nil, errors.Wrap(err, "failed to build context logger")
}

globalCfg := valid.NewGlobalCfg(userConfig.DataDir)
validator := &cfgParser.ParserValidator{}

if userConfig.RepoConfig != "" {
globalCfg, err = validator.ParseGlobalCfg(userConfig.RepoConfig, globalCfg)
if err != nil {
return nil, errors.Wrapf(err, "parsing %s file", userConfig.RepoConfig)
}
}

parsedURL, err := legacy.ParseAtlantisURL(userConfig.AtlantisURL)
if err != nil {
return nil, errors.Wrapf(err,
"parsing atlantis url %q", userConfig.AtlantisURL)
}

appConfig, err := createGHAppConfig(userConfig)
if err != nil {
return nil, err
}

cfg := &adhocconfig.Config{
AuthCfg: neptune.AuthConfig{
SslCertFile: userConfig.SSLCertFile,
SslKeyFile: userConfig.SSLKeyFile,
},
ServerCfg: neptune.ServerConfig{
URL: parsedURL,
Version: config.AtlantisVersion,
Port: userConfig.Port,
},
TerraformCfg: neptune.TerraformConfig{
DefaultVersion: userConfig.DefaultTFVersion,
DownloadURL: userConfig.TFDownloadURL,
LogFilters: globalCfg.TerraformLogFilter,
},
DataDir: userConfig.DataDir,
TemporalCfg: globalCfg.Temporal,
GithubCfg: globalCfg.Github,
App: appConfig,
CtxLogger: ctxLogger,
StatsNamespace: userConfig.StatsNamespace,
Metrics: globalCfg.Metrics,
}
return adhoc.NewServer(cfg)
}
9 changes: 8 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ var stringFlags = map[string]stringFlag{
"default: Runs atlantis with default event handler that processes events within same.\n" +
"gateway: Runs atlantis with gateway event handler that publishes events through sns.\n" +
"worker: Runs atlantis with a sqs handler that polls for events in the queue to process.\n" +
"hybrid: Runs atlantis with both a gateway event handler and sqs handler to perform both gateway and worker behaviors.",
"hybrid: Runs atlantis with both a gateway event handler and sqs handler to perform both gateway and worker behaviors.\n" +
"adhoc: Runs atlantis in an admin mode that allows for running adhoc terraform commands.",
defaultValue: "",
},
LyftWorkerQueueURLFlag: {
Expand Down Expand Up @@ -344,6 +345,7 @@ func NewServerCmd(v *viper.Viper, version string) *ServerCmd {
GatewayCreator: &GatewayCreator{},
WorkerCreator: &WorkerCreator{},
TemporalWorkerCreator: &TemporalWorker{},
AdhocCreator: &Adhoc{},
},
Viper: v,
AtlantisVersion: version,
Expand Down Expand Up @@ -374,6 +376,7 @@ type ServerCreatorProxy struct {
GatewayCreator ServerCreator
WorkerCreator ServerCreator
TemporalWorkerCreator ServerCreator
AdhocCreator ServerCreator
}

func (d *ServerCreatorProxy) NewServer(userConfig server.UserConfig, config server.Config) (ServerStarter, error) {
Expand All @@ -389,6 +392,10 @@ func (d *ServerCreatorProxy) NewServer(userConfig server.UserConfig, config serv
return d.TemporalWorkerCreator.NewServer(userConfig, config)
}

if userConfig.ToLyftMode() == server.Adhoc {
return d.AdhocCreator.NewServer(userConfig, config)
}

return d.WorkerCreator.NewServer(userConfig, config)
}

Expand Down
12 changes: 6 additions & 6 deletions server/config/raw/global_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ type GlobalCfg struct {
Persistence Persistence `yaml:"persistence" json:"persistence"`
RevisionSetter RevisionSetter `yaml:"revision_setter" json:"revision_setter"`
Admin Admin `yaml:"admin" json:"admin"`
TerraformAdminMode TerraformAdminMode `yaml:"terraform_admin_mode" json:"terraform_admin_mode"`
AdhocMode AdhocMode `yaml:"adhoc_mode" json:"adhoc_mode"`
}

type TerraformAdminMode struct {
type AdhocMode struct {
Repo string `yaml:"repo" json:"repo"`
Root string `yaml:"root" json:"root"`
}

func (t TerraformAdminMode) ToValid() valid.TerraformAdminMode {
return valid.TerraformAdminMode{
func (t AdhocMode) ToValid() valid.AdhocMode {
return valid.AdhocMode{
Repo: t.Repo,
Root: t.Root,
}
}

func (t TerraformAdminMode) Validate() error {
func (t AdhocMode) Validate() error {
// We don't need to validate the inputs so we can just return nil
return nil
}
Expand Down Expand Up @@ -214,7 +214,7 @@ func (g GlobalCfg) ToValid(defaultCfg valid.GlobalCfg) valid.GlobalCfg {
Github: g.Github.ToValid(),
Admin: g.Admin.ToValid(),
RevisionSetter: g.RevisionSetter.ToValid(),
TerraformAdminMode: g.TerraformAdminMode.ToValid(),
AdhocMode: g.AdhocMode.ToValid(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions server/config/valid/global_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ type GlobalCfg struct {
Github Github
RevisionSetter RevisionSetter
Admin Admin
TerraformAdminMode TerraformAdminMode
AdhocMode AdhocMode
}

type TerraformAdminMode struct {
type AdhocMode struct {
Repo string
Root string
}
Expand Down
3 changes: 3 additions & 0 deletions server/legacy/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
Gateway
Worker
TemporalWorker
Adhoc
)

// UserConfig holds config values passed in by the user.
Expand Down Expand Up @@ -104,6 +105,8 @@ func (u UserConfig) ToLyftMode() Mode {
return Worker
case "temporalworker":
return TemporalWorker
case "adhoc":
return Adhoc
}
return Default
}
8 changes: 8 additions & 0 deletions server/legacy/user_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ func TestUserConfig_ToLyftMode(t *testing.T) {
"",
server.Default,
},
{
"adhoc",
server.Adhoc,
},
{
"temporalworker",
server.TemporalWorker,
},
}

for _, c := range cases {
Expand Down
25 changes: 25 additions & 0 deletions server/neptune/adhoc/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package adhocconfig

import (
"github.com/palantir/go-githubapp/githubapp"
"github.com/runatlantis/atlantis/server/config/valid"
"github.com/runatlantis/atlantis/server/logging"
neptune "github.com/runatlantis/atlantis/server/neptune/temporalworker/config"
)

// Config is TerraformAdmin (Adhoc mode) specific user config
type Config struct {
AuthCfg neptune.AuthConfig
ServerCfg neptune.ServerConfig
FeatureConfig neptune.FeatureConfig
TemporalCfg valid.Temporal
GithubCfg valid.Github
TerraformCfg neptune.TerraformConfig
Metrics valid.Metrics

StatsNamespace string

DataDir string
CtxLogger logging.Logger
App githubapp.Config
}
Loading

0 comments on commit 2a0da82

Please sign in to comment.