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

[f-nested-deps] Remove requirement to have root pack name in overrides #426

Merged
merged 6 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions fixtures/v2/override_files/deps_test_1/test1.hcl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
deps_test_1.job_name = "A"
deps_test_1.child1.job_name = "A.1"
deps_test_1.child1.gc.job_name = "A.1.a"
deps_test_1.child2.job_name = "A.2"
deps_test_1.child2.gc.job_name = "A.2.a"
job_name = "A"
child1.job_name = "A.1"
child1.gc.job_name = "A.1.a"
child2.job_name = "A.2"
child2.gc.job_name = "A.2.a"
2 changes: 1 addition & 1 deletion fixtures/v2/override_files/simple_raw_exec/test_01.hcl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
simple_raw_exec.job_name = "sre"
job_name = "sre"
4 changes: 2 additions & 2 deletions fixtures/v2/override_files/simple_raw_exec/test_02.hcl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
simple_raw_exec.env = {
env = {
"NOMAD_TOKEN" = "some awesome token"
"NOMAD_ADDR" = "http://127.0.0.1:4646"
}

simple_raw_exec.job_name = "sre"
job_name = "sre"
2 changes: 1 addition & 1 deletion fixtures/v2/variable_test/heredoc.vars.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
# ends immediately after the end-of-heredoc marker
# https://github.com/hashicorp/nomad-pack/pull/191

variable_test_pack.input = <<EOF
input = <<EOF
heredoc
EOF
2 changes: 1 addition & 1 deletion fixtures/v2/variable_test/input.vars.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

variable_test_pack.input = "varfile"
input = "varfile"
16 changes: 8 additions & 8 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func TestCLI_PackStop_Conflicts(t *testing.T) {
must.NoError(t, err)
} else {
deploymentName := fmt.Sprintf("--name=%s", tC.deploymentName)
varJobName := fmt.Sprintf("--var=%s.job_name=%s", testPack, tC.jobName)
varJobName := fmt.Sprintf("--var=job_name=%s", tC.jobName)
if tC.namespace != "" {
namespaceFlag := fmt.Sprintf("--namespace=%s", tC.namespace)
expectGoodPackDeploy(t, runTestPackCmd(t, s, []string{"run", getTestPackPath(t, testPack), deploymentName, varJobName, namespaceFlag}))
Expand Down Expand Up @@ -392,16 +392,16 @@ func TestCLI_PackDestroy_WithOverrides(t *testing.T) {
jobNames := []string{"foo", "bar"}
for _, j := range jobNames {
expectGoodPackDeploy(t, runTestPackCmd(
t, s, []string{"run", testPack, "--var=" + testPack + ".job_name=" + j, "--registry=" + reg.Name}))
t, s, []string{"run", testPack, "--var=job_name=" + j, "--registry=" + reg.Name}))
}

// Stop nonexistent job
result := runTestPackCmd(t, s, []string{"destroy", testPack, "--var=" + testPack + ".job_name=baz", "--registry=" + reg.Name})
result := runTestPackCmd(t, s, []string{"destroy", testPack, "--var=job_name=baz", "--registry=" + reg.Name})
must.Eq(t, 1, result.exitCode, must.Sprintf(
"expected exitcode 1; got %v\ncmdOut:%v", result.exitCode, result.cmdOut.String()))

// Stop job with var override
result = runTestPackCmd(t, s, []string{"destroy", testPack, "--var=" + testPack + ".job_name=foo", "--registry=" + reg.Name})
result = runTestPackCmd(t, s, []string{"destroy", testPack, "--var=job_name=foo", "--registry=" + reg.Name})
must.Zero(t, result.exitCode, must.Sprintf(
"expected exitcode 0; got %v\ncmdOut:%v", result.exitCode, result.cmdOut.String()))

Expand Down Expand Up @@ -527,7 +527,7 @@ func TestCLI_CLIFlag_Namespace(t *testing.T) {
expect map[string]int
}{
{
desc: "flags vs unspecified",
desc: "client flag vs unspecified",
args: []string{
`--namespace=flag`,
},
Expand All @@ -541,7 +541,7 @@ func TestCLI_CLIFlag_Namespace(t *testing.T) {
{
desc: "flags vs job",
args: []string{
`--var=` + testPack + `.namespace=job`,
`--var=namespace=job`,
`--namespace=flag`,
},
env: make(map[string]string),
Expand Down Expand Up @@ -670,7 +670,7 @@ func TestCLI_EnvConfig_Namespace(t *testing.T) {
{
desc: "env vs job",
args: []string{
`--var=` + testPack + `.namespace=job`,
`--var=namespace=job`,
},
expect: map[string]int{
"job": 1,
Expand All @@ -693,7 +693,7 @@ func TestCLI_EnvConfig_Namespace(t *testing.T) {
desc: "env vs flag vs job",
args: []string{
`--namespace=flag`,
`--var=` + testPack + `.namespace=job`,
`--var=namespace=job`,
},
expect: map[string]int{
"job": 1,
Expand Down
4 changes: 1 addition & 3 deletions internal/cli/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ package cli

import (
"fmt"
"path"

"github.com/hashicorp/nomad-pack/internal/pkg/cache"
"github.com/hashicorp/nomad-pack/internal/pkg/flag"
"github.com/hashicorp/nomad-pack/internal/pkg/loader"
"github.com/hashicorp/nomad-pack/internal/pkg/variable/parser"
"github.com/hashicorp/nomad-pack/internal/pkg/variable/parser/config"
"github.com/hashicorp/nomad-pack/sdk/pack"
"github.com/mitchellh/go-glint"
"github.com/zclconf/go-cty/cty"
)
Expand Down Expand Up @@ -56,7 +54,7 @@ func (c *InfoCommand) Run(args []string) int {
}

variableParser, err := parser.NewParser(&config.ParserConfig{
ParentPackID: pack.ID(path.Base(packPath)),
ParentPack: p,
RootVariableFiles: p.RootVariableFiles(),
IgnoreMissingVars: c.baseCommand.ignoreMissingVars,
})
Expand Down
12 changes: 6 additions & 6 deletions internal/creator/templates/pack_readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ search for Consul services with the tags found in the default value of the

<!-- Include information on the variables from your pack -->

- `message` (string) - The message your application will respond with
- `count` (number) - The number of app instances to deploy
- `message` (string:"Hello World!") - The message your application will respond with
- `count` (number:2) - The number of app instances to deploy
- `job_name` (string) - The name to use as the job name which overrides using
the pack name
- `datacenters` (list of strings) - A list of datacenters in the region which
- `datacenters` (list of strings:["dc1"]) - A list of datacenters in the region which
are eligible for task placement
- `region` (string) - The region where jobs will be deployed
- `register_consul_service` (bool) - If you want to register a consul service
- `register_consul_service` (bool: true) - If you want to register a Consul service
for the job
- `consul_service_tags` (list of string) - The consul service name for the
- `consul_service_tags` (list of string) - The Consul service name for the
{{.PackName}} application
- `consul_service_name` (string) - The consul service name for the {{.PackName}}
- `consul_service_name` (string) - The Consul service name for the {{.PackName}}
application

[pack-registry]: https://github.com/hashicorp/nomad-pack-community-registry
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (pm *PackManager) ProcessVariableFiles() (*parser.ParsedVariables, []*error

pCfg := &config.ParserConfig{
Version: config.V2,
ParentPackID: pack.ID(parentName),
ParentPack: pm.loadedPack,
RootVariableFiles: loadedPack.RootVariableFiles(),
EnvOverrides: pm.cfg.VariableEnvVars,
FileOverrides: pm.cfg.VariableFiles,
Expand Down
32 changes: 16 additions & 16 deletions internal/pkg/varfile/fixture/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ import (
)

const GoodConfigfileHCL = `# variable answers
simple_raw_exec.child1.username="foo"
simple_raw_exec.child1.password="bar"
simple_raw_exec.rootuser="admin"
child1.username="foo"
child1.password="bar"
rootuser="admin"
`

const GoodConfigfileJSON = `{
"simple_raw_exec.child1.username": "foo",
"simple_raw_exec.child1.password": "bar",
"simple_raw_exec.rootuser": "admin"
"child1.username": "foo",
"child1.password": "bar",
"rootuser": "admin"
}`

const BadMissingEqualOneLine = `mypack.foo "bar"`
const BadMissingEqualSecondLine = `mypack.foo = "bar"
const BadMissingEqualOneLine = `foo "bar"`
const BadMissingEqualSecondLine = `foo = "bar"
bad value`
const BadMissingEqualInternalLine = `mypack.foo = "bar"
const BadMissingEqualInternalLine = `foo = "bar"
bad value
mypack.bar = "baz"`
bar = "baz"`

const BadJSONMissingStartBrace = `"mypack.foo": "bar" }`
const BadJSONMissingEndBrace = `{ "mypack.foo": "bar"`
const BadJSONMissingComma = `{ "mypack.foo": "bar" "mypack.bar": "baz" }`
const BadJSONMissingQuote = `{ "mypack.foo": "bar", mypack.bar": "baz" }`
const BadJSONMissingColon = `{ "mypack.foo": "bar", mypack.bar" "baz" }`
const BadJSONMissingStartBrace = `"foo": "bar" }`
const BadJSONMissingEndBrace = `{ "foo": "bar"`
const BadJSONMissingComma = `{ "foo": "bar" "bar": "baz" }`
const BadJSONMissingQuote = `{ "foo": "bar", bar": "baz" }`
const BadJSONMissingColon = `{ "foo": "bar", bar" "baz" }`
const JSONEmpty = ""
const JSONEmptyObject = "{}"

var JSONFiles = map[pack.ID][]*pack.File{
"myPack": {
"mypack": {
{
Name: "tc1.json",
Content: []byte(BadJSONMissingStartBrace),
Expand Down
40 changes: 33 additions & 7 deletions internal/pkg/varfile/varfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
"github.com/hashicorp/nomad-pack/sdk/pack/variables"
)

func DecodeVariableOverrides(files []*pack.File) DecodeResult {
// TODO: this is only used in a test - remove?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into the test package

func DecodeVariableOverrides(root *pack.Pack, files []*pack.File) DecodeResult {
decodeResult := DecodeResult{}
for _, file := range files {
fileDecodeResult := DecodeResult{
Overrides: make(variables.Overrides),
}
fileDecodeResult.HCLFiles, fileDecodeResult.Diags = Decode(file.Name, file.Content, nil, &fileDecodeResult.Overrides)

fileDecodeResult.HCLFiles, fileDecodeResult.Diags = Decode(root, file.Name, file.Content, nil, &fileDecodeResult.Overrides)
decodeResult.Merge(fileDecodeResult)
}
return decodeResult
Expand Down Expand Up @@ -117,8 +119,8 @@ func (d *DecodeResult) Merge(in DecodeResult) {

// Decode parses, decodes, and evaluates expressions in the given HCL source
// code, in a single step.
func Decode(filename string, src []byte, ctx *hcl.EvalContext, target *variables.Overrides) (map[string]*hcl.File, hcl.Diagnostics) {
fm, diags := decode(filename, src, ctx, target)
func Decode(root *pack.Pack, filename string, src []byte, ctx *hcl.EvalContext, target *variables.Overrides) (map[string]*hcl.File, hcl.Diagnostics) {
fm, diags := decode(root, filename, src, ctx, target)
var fd = fixableDiags(diags)

fm.Fixup() // the hcl.File that we will return to the diagnostic printer will have our modifications
Expand All @@ -129,7 +131,7 @@ func Decode(filename string, src []byte, ctx *hcl.EvalContext, target *variables

// Decode parses, decodes, and evaluates expressions in the given HCL source
// code, in a single step.
func decode(filename string, src []byte, ctx *hcl.EvalContext, target *variables.Overrides) (diagFileMap, hcl.Diagnostics) {
func decode(root *pack.Pack, filename string, src []byte, ctx *hcl.EvalContext, target *variables.Overrides) (diagFileMap, hcl.Diagnostics) {
var file *hcl.File
var diags hcl.Diagnostics

Expand Down Expand Up @@ -204,6 +206,8 @@ func decode(filename string, src []byte, ctx *hcl.EvalContext, target *variables
steps = strings.Split(k.AsString(), ".")

case 1:
// NOTE: traversalToName is recursive

// In the HCL case, we have to read the traversal to get the path parts.
steps = traversalToName(keyVars[0])

Expand All @@ -217,9 +221,30 @@ func decode(filename string, src []byte, ctx *hcl.EvalContext, target *variables
oRange := hcl.RangeBetween(kv.Key.Range(), kv.Value.Range())
fixupRange(&oRange)

var path pack.ID
var name variables.ID

// NOTE: This implementation assumes a single element variable value,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's fine for now but maybe make an issue so that we could revisit later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created issue #435 to capture this

// which specifically breaks setting a sub-element value. The more
// correct way is to perform the traversal based on the provided root
// pack

if len(steps) < 2 {
name = variables.ID(steps[len(steps)-1])
path = root.ID()
} else {
name = variables.ID(steps[len(steps)-1])
path = pack.ID(strings.Join(
append(
[]string{root.ID().String()},
steps[0:len(steps)-1]...,
),
"."))
}

val := variables.Override{
Name: variables.ID(steps[len(steps)-1]),
Path: pack.ID(strings.Join(steps[0:len(steps)-1], ".")),
Name: name,
Path: path,
Value: value,
Type: value.Type(),
Range: oRange,
Expand All @@ -228,6 +253,7 @@ func decode(filename string, src []byte, ctx *hcl.EvalContext, target *variables
}

if len(vals) > 0 {
// What is this doing?
(*target)[pack.ID(filename)] = vals
}
return fm, diags
Expand Down
34 changes: 31 additions & 3 deletions internal/pkg/varfile/varfile_pack_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
package varfile_test

import (
"bytes"
"os"
"strings"
"testing"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/nomad-pack/internal/pkg/varfile"
"github.com/hashicorp/nomad-pack/internal/pkg/varfile/fixture"
"github.com/hashicorp/nomad-pack/sdk/pack"
"github.com/hashicorp/nomad-pack/sdk/pack/variables"
"github.com/shoenig/test/must"
)

func testpack(p ...string) *pack.Pack {
name := strings.Join(p, ".")
if name == "" {
name = "example"
}

return &pack.Pack{
Metadata: &pack.Metadata{
Pack: &pack.MetadataPack{
Name: name,
},
},
}
}

func TestVarfile_ProcessPackVarfiles(t *testing.T) {
root := testpack("mypack")
ovrds := make(variables.Overrides)
fm, d := varfile.Decode("foo.hcl", []byte(`foo="bar"`), nil, &ovrds)
fm, d := varfile.Decode(root, "foo.hcl", []byte(`foo="bar"`), nil, &ovrds)
if d.HasErrors() {
dw := hcl.NewDiagnosticTextWriter(os.Stderr, fm, 40, false)
t.Log(dw.WriteDiagnostics(d))
Expand All @@ -24,7 +43,16 @@ func TestVarfile_ProcessPackVarfiles(t *testing.T) {
}

func TestVarfile_DecodeVariableOverrides(t *testing.T) {
dr := varfile.DecodeVariableOverrides(fixture.JSONFiles["myPack"])
dw := hcl.NewDiagnosticTextWriter(os.Stderr, dr.HCLFiles, 80, false)
root := testpack("mypack")
dr := varfile.DecodeVariableOverrides(root, fixture.JSONFiles["mypack"])
must.NotNil(t, dr.Diags)
must.Len(t, 4, dr.Diags)
var b bytes.Buffer
dw := hcl.NewDiagnosticTextWriter(&b, dr.HCLFiles, 80, false)
for _, d := range dr.Diags {
dw.WriteDiagnostics(hcl.Diagnostics{d})
must.StrHasPrefix(t, "Error:", b.String())
b.Reset()
}
dw.WriteDiagnostics(dr.Diags)
}
Loading