Skip to content

Commit

Permalink
feat: Nomad variables files and HCL2 by default (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilgdn authored Jan 9, 2023
1 parent 7149401 commit 242260b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 32 deletions.
13 changes: 8 additions & 5 deletions command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ General Options:
Nomad cluster. You can repeat this flag multiple times to supply multiple var-files.
[default: levant.(json|yaml|yml|tf)]
-hcl2
Use HCL2 jopspec parser.
-disable-hcl2
Do not use HCL2 jopspec parser.
-nomad-var-file=<file>
Nomad variables file (cannot be used with -disable-hcl2)
`
return strings.TrimSpace(helpText)
}
Expand Down Expand Up @@ -128,9 +131,10 @@ func (c *DeployCommand) Run(args []string) int {
flags.StringVar(&format, "log-format", "HUMAN", "")
flags.StringVar(&config.Deploy.VaultToken, "vault-token", "", "")
flags.BoolVar(&config.Deploy.EnvVault, "vault", false, "")
flags.BoolVar(&config.Template.HCL2, "hcl2", false, "")
flags.BoolVar(&config.Template.DisableHCL2, "disable-hcl2", false, "")

flags.Var((*helper.FlagStringSlice)(&config.Template.VariableFiles), "var-file", "")
flags.Var((*helper.FlagStringSlice)(&config.Template.NomadVariableFiles), "nomad-var-file", "")

if err = flags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -162,8 +166,7 @@ func (c *DeployCommand) Run(args []string) int {
return 1
}

config.Template.Job, err = template.RenderJob(config.Template.TemplateFile,
config.Template.VariableFiles, config.Client.ConsulAddr, &c.Meta.flagVars, config.Template.HCL2)
config.Template.Job, err = template.RenderJob(config.Template, config.Client.ConsulAddr, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
Expand Down
5 changes: 3 additions & 2 deletions command/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"testing"

"github.com/hashicorp/levant/levant/structs"
"github.com/hashicorp/levant/template"
)

Expand Down Expand Up @@ -30,7 +31,7 @@ func TestDeploy_checkCanaryAutoPromote(t *testing.T) {
}

for i, c := range cases {
job, err := template.RenderJob(c.File, []string{}, "", &fVars, false)
job, err := template.RenderJob(&structs.TemplateConfig{TemplateFile: c.File, VariableFiles: []string{}}, "", &fVars)
if err != nil {
t.Fatalf("case %d failed: %v", i, err)
}
Expand Down Expand Up @@ -61,7 +62,7 @@ func TestDeploy_checkForceBatch(t *testing.T) {
}

for i, c := range cases {
job, err := template.RenderJob(c.File, []string{}, "", &fVars, false)
job, err := template.RenderJob(&structs.TemplateConfig{TemplateFile: c.File, VariableFiles: []string{}}, "", &fVars)
if err != nil {
t.Fatalf("case %d failed: %v", i, err)
}
Expand Down
15 changes: 9 additions & 6 deletions command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ General Options:
Nomad cluster. You can repeat this flag multiple times to supply multiple var-files.
[default: levant.(json|yaml|yml|tf)]
-hcl2
Use HCL2 jopspec parser.
-disable-hcl2
Do not use HCL2 jopspec parser.
-nomad-var-file=<file>
Nomad variables file (cannot be used with -disable-hcl2)
`
return strings.TrimSpace(helpText)
}
Expand Down Expand Up @@ -100,8 +104,9 @@ func (c *PlanCommand) Run(args []string) int {
flags.BoolVar(&config.Plan.IgnoreNoChanges, "ignore-no-changes", false, "")
flags.StringVar(&level, "log-level", "INFO", "")
flags.StringVar(&format, "log-format", "HUMAN", "")
flags.BoolVar(&config.Template.HCL2, "hcl2", false, "")
flags.BoolVar(&config.Template.DisableHCL2, "disable-hcl2", false, "")
flags.Var((*helper.FlagStringSlice)(&config.Template.VariableFiles), "var-file", "")
flags.Var((*helper.FlagStringSlice)(&config.Template.NomadVariableFiles), "nomad-var-file", "")

if err = flags.Parse(args); err != nil {
return 1
Expand All @@ -127,9 +132,7 @@ func (c *PlanCommand) Run(args []string) int {
return 1
}

config.Template.Job, err = template.RenderJob(config.Template.TemplateFile,
config.Template.VariableFiles, config.Client.ConsulAddr, &c.Meta.flagVars, config.Template.HCL2)

config.Template.Job, err = template.RenderJob(config.Template, config.Client.ConsulAddr, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
Expand Down
7 changes: 5 additions & 2 deletions levant/structs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ type TemplateConfig struct {
// templateFile before deployment.
VariableFiles []string

// HCL2 is a boolean flag that enables using jobspec2 parser
HCL2 bool
// DisableHCL2 is a boolean flag that allows to disable HCL2 jobspec parser
DisableHCL2 bool

// NomadVariableFiles contains a list of Nomad variables files
NomadVariableFiles []string
}

// ScaleConfig contains all the scaling specific configuration options.
Expand Down
16 changes: 9 additions & 7 deletions template/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/levant/client"
"github.com/hashicorp/levant/helper"
"github.com/hashicorp/levant/levant/structs"
nomad "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/jobspec"
"github.com/hashicorp/nomad/jobspec2"
Expand All @@ -20,19 +21,20 @@ import (

// RenderJob takes in a template and variables performing a render of the
// template followed by Nomad jobspec parse.
func RenderJob(templateFile string, variableFiles []string, addr string, flagVars *map[string]interface{}, hcl2 bool) (job *nomad.Job, err error) {
func RenderJob(templateConfig *structs.TemplateConfig, addr string, flagVars *map[string]interface{}) (job *nomad.Job, err error) {
var tpl *bytes.Buffer
tpl, err = RenderTemplate(templateFile, variableFiles, addr, flagVars)
tpl, err = RenderTemplate(templateConfig.TemplateFile, templateConfig.VariableFiles, addr, flagVars)
if err != nil {
return
}

if hcl2 {
if !templateConfig.DisableHCL2 {
return jobspec2.ParseWithConfig(&jobspec2.ParseConfig{
Path: templateFile,
Body: tpl.Bytes(),
AllowFS: true,
Strict: true,
Path: templateConfig.TemplateFile,
Body: tpl.Bytes(),
AllowFS: true,
Strict: true,
VarFiles: templateConfig.NomadVariableFiles,
})
}

Expand Down
42 changes: 33 additions & 9 deletions template/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"

"github.com/hashicorp/levant/levant/structs"
nomad "github.com/hashicorp/nomad/api"
)

Expand All @@ -25,7 +26,12 @@ func TestTemplater_RenderTemplate(t *testing.T) {
fVars := make(map[string]interface{})

// Test basic TF template render.
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf"}, "", &fVars, false)
config := &structs.TemplateConfig{
TemplateFile: "test-fixtures/single_templated.nomad",
VariableFiles: []string{"test-fixtures/test.tf"},
DisableHCL2: true,
}
job, err = RenderJob(config, "", &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -37,7 +43,8 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test basic YAML template render.
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.yaml"}, "", &fVars, false)
config = &structs.TemplateConfig{TemplateFile: "test-fixtures/single_templated.nomad", VariableFiles: []string{"test-fixtures/test.yaml"}}
job, err = RenderJob(config, "", &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -49,7 +56,11 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test multiple var-files
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.yaml", "test-fixtures/test-overwrite.yaml"}, "", &fVars, false)
config = &structs.TemplateConfig{
TemplateFile: "test-fixtures/single_templated.nomad",
VariableFiles: []string{"test-fixtures/test.yaml", "test-fixtures/test-overwrite.yaml"},
}
job, err = RenderJob(config, "", &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -58,7 +69,11 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test multiple var-files of different types
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"}, "", &fVars, false)
config = &structs.TemplateConfig{
TemplateFile: "test-fixtures/single_templated.nomad",
VariableFiles: []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"},
}
job, err = RenderJob(config, "", &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -68,7 +83,12 @@ func TestTemplater_RenderTemplate(t *testing.T) {

// Test multiple var-files with var-args
fVars["job_name"] = testJobNameOverwrite2
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"}, "", &fVars, false)

config = &structs.TemplateConfig{
TemplateFile: "test-fixtures/single_templated.nomad",
VariableFiles: []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"},
}
job, err = RenderJob(config, "", &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -77,7 +97,8 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test empty var-args and empty variable file render.
job, err = RenderJob("test-fixtures/none_templated.nomad", []string{}, "", &fVars, false)
config = &structs.TemplateConfig{TemplateFile: "test-fixtures/none_templated.nomad", VariableFiles: []string{}}
job, err = RenderJob(config, "", &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -86,8 +107,9 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test var-args only render.
config = &structs.TemplateConfig{TemplateFile: "test-fixtures/single_templated.nomad", VariableFiles: []string{}}
fVars = map[string]interface{}{"job_name": testJobName, "task_resource_cpu": "1313"}
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{}, "", &fVars, false)
job, err = RenderJob(config, "", &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -99,8 +121,9 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test var-args only render with HCL2 spec
config = &structs.TemplateConfig{TemplateFile: "test-fixtures/single_templated_connect.nomad"}
fVars = map[string]interface{}{"job_name": testJobName, "task_resource_cpu": "1313", "upstream_datacenter": "dc2"}
job, err = RenderJob("test-fixtures/single_templated_connect.nomad", []string{}, "", &fVars, true)
job, err = RenderJob(config, "", &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -116,9 +139,10 @@ func TestTemplater_RenderTemplate(t *testing.T) {

// Test var-args and variables file render.
delete(fVars, "job_name")
config = &structs.TemplateConfig{TemplateFile: "test-fixtures/multi_templated.nomad", VariableFiles: []string{"test-fixtures/test.yaml"}}
fVars["datacentre"] = testDCName
os.Setenv(testEnvName, testEnvValue)
job, err = RenderJob("test-fixtures/multi_templated.nomad", []string{"test-fixtures/test.yaml"}, "", &fVars, false)
job, err = RenderJob(config, "", &fVars)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion test/acctest/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ func (c DeployTestStepRunner) Run(s *TestState) error {
}
c.Vars["job_name"] = s.JobName

job, err := template.RenderJob("fixtures/"+c.FixtureName, []string{}, "", &c.Vars, false)
job, err := template.RenderJob(
&structs.TemplateConfig{TemplateFile: "fixtures/" + c.FixtureName, VariableFiles: []string{}}, "", &c.Vars,
)
if err != nil {
return fmt.Errorf("error rendering template: %s", err)
}
Expand Down

0 comments on commit 242260b

Please sign in to comment.