From 8cab19f2f9f20c191fa32981db9debcfca636845 Mon Sep 17 00:00:00 2001 From: Martin Buchleitner Date: Mon, 4 Oct 2021 12:45:13 +0200 Subject: [PATCH 1/2] fix: adding write/queryoptions to nomad calls to apply namespace/region from file --- levant/auto_revert.go | 2 +- levant/deploy.go | 12 +++++++----- levant/dispatch.go | 2 +- levant/failure_inspector.go | 4 ++-- levant/plan.go | 26 +++++++++++++++++++++++++- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/levant/auto_revert.go b/levant/auto_revert.go index 4200838d4..d5469ab3b 100644 --- a/levant/auto_revert.go +++ b/levant/auto_revert.go @@ -14,7 +14,7 @@ func (l *levantDeployment) autoRevert(jobID, depID *string) { i := 0 for i := 0; i < 5; i++ { - dep, _, err := l.nomad.Jobs().LatestDeployment(*jobID, nil) + dep, _, err := l.nomad.Jobs().LatestDeployment(*jobID, setQueryOptions(l.options)) if err != nil { log.Error().Msgf("levant/auto_revert: unable to query latest deployment of job %s", *jobID) return diff --git a/levant/deploy.go b/levant/deploy.go index 01b0c33eb..217b69dd7 100644 --- a/levant/deploy.go +++ b/levant/deploy.go @@ -20,8 +20,9 @@ const ( // levantDeployment is the all deployment related objects for this Levant // deployment invocation. type levantDeployment struct { - nomad *nomad.Client - config *DeployConfig + nomad *nomad.Client + config *DeployConfig + options *nomad.WriteOptions } // DeployConfig is the set of config structs required to run a Levant deploy. @@ -43,6 +44,7 @@ func newLevantDeployment(config *DeployConfig, nomadClient *nomad.Client) (*leva dep := &levantDeployment{} dep.config = config + dep.options = setWriteOptions(dep.config.Template) if nomadClient == nil { dep.nomad, err = client.NewNomadClient(config.Client.Addr) @@ -91,7 +93,7 @@ func TriggerDeployment(config *DeployConfig, nomadClient *nomad.Client) bool { func (l *levantDeployment) preDeployValidate() (success bool) { // Validate the job to check it is syntactically correct. - if _, _, err := l.nomad.Jobs().Validate(l.config.Template.Job, nil); err != nil { + if _, _, err := l.nomad.Jobs().Validate(l.config.Template.Job, l.options); err != nil { log.Error().Err(err).Msg("levant/deploy: job validation failed") return } @@ -120,7 +122,7 @@ func (l *levantDeployment) deploy() (success bool) { l.config.Template.Job.VaultToken = &l.config.Deploy.VaultToken - eval, _, err := l.nomad.Jobs().Register(l.config.Template.Job, nil) + eval, _, err := l.nomad.Jobs().Register(l.config.Template.Job, l.options) if err != nil { log.Error().Err(err).Msg("levant/deploy: unable to register job with Nomad") return @@ -178,7 +180,7 @@ func (l *levantDeployment) deploy() (success bool) { return } - dep, _, err := l.nomad.Deployments().Info(depID, nil) + dep, _, err := l.nomad.Deployments().Info(depID, setQueryOptions(l.options)) if err != nil { log.Error().Err(err).Msgf("levant/deploy: unable to query deployment %s for auto-revert check", depID) return diff --git a/levant/dispatch.go b/levant/dispatch.go index 1c68de1b6..52a13f268 100644 --- a/levant/dispatch.go +++ b/levant/dispatch.go @@ -38,7 +38,7 @@ func TriggerDispatch(job string, metaMap map[string]string, payload []byte, addr func (l *levantDeployment) dispatch(job string, metaMap map[string]string, payload []byte) bool { // Initiate the dispatch with the passed meta parameters. - eval, _, err := l.nomad.Jobs().Dispatch(job, metaMap, payload, nil) + eval, _, err := l.nomad.Jobs().Dispatch(job, metaMap, payload, l.options) if err != nil { log.Error().Msgf("levant/dispatch: %v", err) return false diff --git a/levant/failure_inspector.go b/levant/failure_inspector.go index 166092752..8af907de7 100644 --- a/levant/failure_inspector.go +++ b/levant/failure_inspector.go @@ -14,7 +14,7 @@ func (l *levantDeployment) checkFailedDeployment(depID *string) { var allocIDS []string - allocs, _, err := l.nomad.Deployments().Allocations(*depID, nil) + allocs, _, err := l.nomad.Deployments().Allocations(*depID, setQueryOptions(l.options)) if err != nil { log.Error().Msgf("levant/failure_inspector: unable to query deployment allocations for deployment %v", depID) @@ -54,7 +54,7 @@ func (l *levantDeployment) allocInspector(allocID string, wg *sync.WaitGroup) { // Inform the wait group we have finished our task upon completion. defer wg.Done() - resp, _, err := l.nomad.Allocations().Info(allocID, nil) + resp, _, err := l.nomad.Allocations().Info(allocID, setQueryOptions(l.options)) if err != nil { log.Error().Msgf("levant/failure_inspector: unable to query alloc %v: %v", allocID, err) return diff --git a/levant/plan.go b/levant/plan.go index 3d4a5f1e0..c9cff8ff6 100644 --- a/levant/plan.go +++ b/levant/plan.go @@ -18,6 +18,8 @@ const ( type levantPlan struct { nomad *nomad.Client config *PlanConfig + + options *nomad.WriteOptions } // PlanConfig is the set of config structs required to run a Levant plan. @@ -35,12 +37,33 @@ func newPlan(config *PlanConfig) (*levantPlan, error) { plan.config = config plan.nomad, err = client.NewNomadClient(config.Client.Addr) + + plan.options = setWriteOptions(plan.config.Template) + if err != nil { return nil, err } return plan, nil } +func setWriteOptions(template *structs.TemplateConfig) *nomad.WriteOptions { + options := &nomad.WriteOptions{} + if template.Job.Namespace != nil { + options.Namespace = *template.Job.Namespace + } + if template.Job.Region != nil { + options.Region = *template.Job.Region + } + return options +} + +func setQueryOptions(wopt *nomad.WriteOptions) *nomad.QueryOptions { + qopt := &nomad.QueryOptions{} + qopt.Namespace = wopt.Namespace + qopt.Region = wopt.Region + return qopt +} + // TriggerPlan initiates a Levant plan run. func TriggerPlan(config *PlanConfig) (bool, bool) { @@ -74,7 +97,8 @@ func (lp *levantPlan) plan() (bool, error) { log.Debug().Msg("levant/plan: triggering Nomad plan") // Run a plan using the rendered job. - resp, _, err := lp.nomad.Jobs().Plan(lp.config.Template.Job, true, nil) + resp, _, err := lp.nomad.Jobs().Plan(lp.config.Template.Job, true, lp.options) + log.Info().Msg(fmt.Sprintf("%#v", resp.Diff)) if err != nil { log.Error().Err(err).Msg("levant/plan: unable to run a job plan") return false, err From 7f2eed4dc57ea99f76aa5c0b7dbc28639b2846d4 Mon Sep 17 00:00:00 2001 From: Martin Buchleitner Date: Mon, 4 Oct 2021 12:54:40 +0200 Subject: [PATCH 2/2] refactor: using namespace and region also from env vars --- levant/plan.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/levant/plan.go b/levant/plan.go index c9cff8ff6..e792e02e6 100644 --- a/levant/plan.go +++ b/levant/plan.go @@ -2,6 +2,7 @@ package levant import ( "fmt" + "os" "github.com/hashicorp/levant/client" "github.com/hashicorp/levant/levant/structs" @@ -48,12 +49,21 @@ func newPlan(config *PlanConfig) (*levantPlan, error) { func setWriteOptions(template *structs.TemplateConfig) *nomad.WriteOptions { options := &nomad.WriteOptions{} + if template.Job.Namespace != nil { options.Namespace = *template.Job.Namespace } + if os.Getenv("NOMAD_NAMESPACE") != "" { + log.Info().Msgf("levant/plan: using namespace from env-var: %s", os.Getenv("NOMAD_NAMESPACE")) + options.Namespace = os.Getenv("NOMAD_NAMESPACE") + } if template.Job.Region != nil { options.Region = *template.Job.Region } + if os.Getenv("NOMAD_REGION") != "" { + log.Info().Msgf("levant/plan: using region from env-var: %s", os.Getenv("NOMAD_REGION")) + options.Namespace = os.Getenv("NOMAD_REGION") + } return options } @@ -98,7 +108,6 @@ func (lp *levantPlan) plan() (bool, error) { // Run a plan using the rendered job. resp, _, err := lp.nomad.Jobs().Plan(lp.config.Template.Job, true, lp.options) - log.Info().Msg(fmt.Sprintf("%#v", resp.Diff)) if err != nil { log.Error().Err(err).Msg("levant/plan: unable to run a job plan") return false, err