Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use namespace/region from job template #428

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion levant/auto_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions levant/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion levant/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions levant/failure_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion levant/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package levant

import (
"fmt"
"os"

"github.com/hashicorp/levant/client"
"github.com/hashicorp/levant/levant/structs"
Expand All @@ -18,6 +19,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.
Expand All @@ -35,12 +38,42 @@ 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 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
}

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) {

Expand Down Expand Up @@ -74,7 +107,7 @@ 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)
if err != nil {
log.Error().Err(err).Msg("levant/plan: unable to run a job plan")
return false, err
Expand Down