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

Update nomad 1.0 #158

Merged
merged 6 commits into from Oct 28, 2020
Merged
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
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -18,6 +18,7 @@ install:
- bash scripts/gogetcookie.sh
- bash scripts/getnomad.sh
- bash scripts/getvault.sh
- bash scripts/getconsul.sh


before_script:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -16,7 +16,7 @@ require (
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/go-version v1.2.0
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f
github.com/hashicorp/nomad/api v0.0.0-20200812181322-71e8a68d9948
github.com/hashicorp/nomad/api v0.0.0-20201028165800-38e23b62a770
github.com/hashicorp/terraform-plugin-sdk v1.15.0
github.com/hashicorp/vault v0.10.4
github.com/mitchellh/mapstructure v1.1.2
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Expand Up @@ -120,10 +120,8 @@ github.com/hashicorp/hcl/v2 v2.0.0 h1:efQznTz+ydmQXq3BOnRa3AXzvCeTq1P4dKj/z5GLlY
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/nomad/api v0.0.0-20200708195712-3b59bc78ef12 h1:8j4+AQZzZtTY8yD8b/qqoQgJGIoRq26w48xzuD2RXio=
github.com/hashicorp/nomad/api v0.0.0-20200708195712-3b59bc78ef12/go.mod h1:DCi2k47yuUDzf2qWAK8E1RVmWgz/lc0jZQeEnICTxmY=
github.com/hashicorp/nomad/api v0.0.0-20200812181322-71e8a68d9948 h1:JFdomVvt63WWly1LZekDHfuEOYASNpVOC27Fzacewmk=
github.com/hashicorp/nomad/api v0.0.0-20200812181322-71e8a68d9948/go.mod h1:DCi2k47yuUDzf2qWAK8E1RVmWgz/lc0jZQeEnICTxmY=
github.com/hashicorp/nomad/api v0.0.0-20201028165800-38e23b62a770 h1:usjfwUJVNtQtlLyJB5LVY1/HiD7DYHiI3e/JhtF0kWo=
github.com/hashicorp/nomad/api v0.0.0-20201028165800-38e23b62a770/go.mod h1:bCQhFF3AFAZqORXM7okqLdpsrrag/uX9zfHDhwZwPag=
github.com/hashicorp/terraform-config-inspect v0.0.0-20191115094559-17f92b0546e8 h1:+RyjwU+Gnd/aTJBPZVDNm903eXVjjqhbaR4Ypx3xYyY=
github.com/hashicorp/terraform-config-inspect v0.0.0-20191115094559-17f92b0546e8/go.mod h1:p+ivJws3dpqbp1iP84+npOyAmTTOLMgCzrXd3GSdn/A=
github.com/hashicorp/terraform-exec v0.1.1 h1:soHqDz5365aZVt9pyTT7ArOXSpMqaHGMYq4VhI+HNkE=
Expand Down Expand Up @@ -336,6 +334,8 @@ google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
4 changes: 0 additions & 4 deletions nomad/core/README.md
Expand Up @@ -19,9 +19,5 @@ If your update requires a newer version of Nomad, you should also update all
other dependencies that are currently in this directory and the commit hash
above.

The only modification you should do from the original files is to change
`import` paths from `github.com/hashicorp/nomad` to
`github.com/github.com/hashicorp/terraform-provider-nomad/nomad/core`.

You can also copy just a few chucks of the original file to avoid bringing
extra dependencie.
104 changes: 0 additions & 104 deletions nomad/core/helper/funcs.go
@@ -1,110 +1,6 @@
package helper

import (
"fmt"
"reflect"
"strings"
"time"

multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl/hcl/ast"
)

// boolToPtr returns the pointer to a boolean
func BoolToPtr(b bool) *bool {
return &b
}

// StringToPtr returns the pointer to a string
func StringToPtr(str string) *string {
return &str
}

// TimeToPtr returns the pointer to a time stamp
func TimeToPtr(t time.Duration) *time.Duration {
return &t
}

func CheckHCLKeys(node ast.Node, valid []string) error {
var list *ast.ObjectList
switch n := node.(type) {
case *ast.ObjectList:
list = n
case *ast.ObjectType:
list = n.List
default:
return fmt.Errorf("cannot check HCL keys of type %T", n)
}

validMap := make(map[string]struct{}, len(valid))
for _, v := range valid {
validMap[v] = struct{}{}
}

var result error
for _, item := range list.Items {
key := item.Keys[0].Token.Value().(string)
if _, ok := validMap[key]; !ok {
result = multierror.Append(result, fmt.Errorf(
"invalid key: %s", key))
}
}

return result
}

// UnusedKeys returns a pretty-printed error if any `hcl:",unusedKeys"` is not empty
func UnusedKeys(obj interface{}) error {
val := reflect.ValueOf(obj)
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
return unusedKeysImpl([]string{}, val)
}

func unusedKeysImpl(path []string, val reflect.Value) error {
stype := val.Type()
for i := 0; i < stype.NumField(); i++ {
ftype := stype.Field(i)
fval := val.Field(i)
tags := strings.Split(ftype.Tag.Get("hcl"), ",")
name := tags[0]
tags = tags[1:]

if fval.Kind() == reflect.Ptr {
fval = reflect.Indirect(fval)
}

// struct? recurse. Add the struct's key to the path
if fval.Kind() == reflect.Struct {
err := unusedKeysImpl(append([]string{name}, path...), fval)
if err != nil {
return err
}
continue
}

// Search the hcl tags for "unusedKeys"
unusedKeys := false
for _, p := range tags {
if p == "unusedKeys" {
unusedKeys = true
break
}
}

if unusedKeys {
ks, ok := fval.Interface().([]string)
if ok && len(ks) != 0 {
ps := ""
if len(path) > 0 {
ps = strings.Join(path, ".") + " "
}
return fmt.Errorf("%sunexpected keys %s",
ps,
strings.Join(ks, ", "))
}
}
}
return nil
}
113 changes: 113 additions & 0 deletions nomad/core/jobspec/helper.go
@@ -0,0 +1,113 @@
package jobspec

// These functions are copied from helper/funcs.go
// added here to avoid jobspec depending on any other package

import (
"fmt"
"reflect"
"strings"
"time"

multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl/hcl/ast"
)

// stringToPtr returns the pointer to a string
func stringToPtr(str string) *string {
return &str
}

// timeToPtr returns the pointer to a time.Duration.
func timeToPtr(t time.Duration) *time.Duration {
return &t
}

// boolToPtr returns the pointer to a boolean
func boolToPtr(b bool) *bool {
return &b
}

func checkHCLKeys(node ast.Node, valid []string) error {
var list *ast.ObjectList
switch n := node.(type) {
case *ast.ObjectList:
list = n
case *ast.ObjectType:
list = n.List
default:
return fmt.Errorf("cannot check HCL keys of type %T", n)
}

validMap := make(map[string]struct{}, len(valid))
for _, v := range valid {
validMap[v] = struct{}{}
}

var result error
for _, item := range list.Items {
key := item.Keys[0].Token.Value().(string)
if _, ok := validMap[key]; !ok {
result = multierror.Append(result, fmt.Errorf(
"invalid key: %s", key))
}
}

return result
}

// UnusedKeys returns a pretty-printed error if any `hcl:",unusedKeys"` is not empty
func unusedKeys(obj interface{}) error {
val := reflect.ValueOf(obj)
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
return unusedKeysImpl([]string{}, val)
}

func unusedKeysImpl(path []string, val reflect.Value) error {
stype := val.Type()
for i := 0; i < stype.NumField(); i++ {
ftype := stype.Field(i)
fval := val.Field(i)
tags := strings.Split(ftype.Tag.Get("hcl"), ",")
name := tags[0]
tags = tags[1:]

if fval.Kind() == reflect.Ptr {
fval = reflect.Indirect(fval)
}

// struct? recurse. Add the struct's key to the path
if fval.Kind() == reflect.Struct {
err := unusedKeysImpl(append([]string{name}, path...), fval)
if err != nil {
return err
}
continue
}

// Search the hcl tags for "unusedKeys"
unusedKeys := false
for _, p := range tags {
if p == "unusedKeys" {
unusedKeys = true
break
}
}

if unusedKeys {
ks, ok := fval.Interface().([]string)
if ok && len(ks) != 0 {
ps := ""
if len(path) > 0 {
ps = strings.Join(path, ".") + " "
}
return fmt.Errorf("%sunexpected keys %s",
ps,
strings.Join(ks, ", "))
}
}
}
return nil
}
19 changes: 9 additions & 10 deletions nomad/core/jobspec/parse.go
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/terraform-provider-nomad/nomad/core/helper"
"github.com/mitchellh/mapstructure"
)

Expand Down Expand Up @@ -48,7 +47,7 @@ func Parse(r io.Reader) (*api.Job, error) {
valid := []string{
"job",
}
if err := helper.CheckHCLKeys(list, valid); err != nil {
if err := checkHCLKeys(list, valid); err != nil {
return nil, err
}

Expand Down Expand Up @@ -100,7 +99,7 @@ func parseReschedulePolicy(final **api.ReschedulePolicy, list *ast.ObjectList) e
"max_delay",
"delay_function",
}
if err := helper.CheckHCLKeys(obj.Val, valid); err != nil {
if err := checkHCLKeys(obj.Val, valid); err != nil {
return err
}

Expand Down Expand Up @@ -140,7 +139,7 @@ func parseConstraints(result *[]*api.Constraint, list *ast.ObjectList) error {
"version",
"semver",
}
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
if err := checkHCLKeys(o.Val, valid); err != nil {
return err
}

Expand Down Expand Up @@ -230,7 +229,7 @@ func parseAffinities(result *[]*api.Affinity, list *ast.ObjectList) error {
"semver",
"weight",
}
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
if err := checkHCLKeys(o.Val, valid); err != nil {
return err
}

Expand Down Expand Up @@ -307,7 +306,7 @@ func parseSpread(result *[]*api.Spread, list *ast.ObjectList) error {
"weight",
"target",
}
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
if err := checkHCLKeys(o.Val, valid); err != nil {
return err
}

Expand Down Expand Up @@ -370,7 +369,7 @@ func parseSpreadTarget(result *[]*api.SpreadTarget, list *ast.ObjectList) error
"percent",
"value",
}
if err := helper.CheckHCLKeys(listVal, valid); err != nil {
if err := checkHCLKeys(listVal, valid); err != nil {
return multierror.Prefix(err, fmt.Sprintf("'%s' ->", n))
}

Expand Down Expand Up @@ -433,7 +432,7 @@ func parseUpdate(result **api.UpdateStrategy, list *ast.ObjectList) error {
"auto_promote",
"canary",
}
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
if err := checkHCLKeys(o.Val, valid); err != nil {
return err
}

Expand Down Expand Up @@ -469,7 +468,7 @@ func parseMigrate(result **api.MigrateStrategy, list *ast.ObjectList) error {
"min_healthy_time",
"healthy_deadline",
}
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
if err := checkHCLKeys(o.Val, valid); err != nil {
return err
}

Expand Down Expand Up @@ -512,7 +511,7 @@ func parseVault(result *api.Vault, list *ast.ObjectList) error {
"change_mode",
"change_signal",
}
if err := helper.CheckHCLKeys(listVal, valid); err != nil {
if err := checkHCLKeys(listVal, valid); err != nil {
return multierror.Prefix(err, "vault ->")
}

Expand Down