2019-08-09 19:18:53 +00:00
|
|
|
package jobspec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/hcl"
|
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2019-10-24 14:41:54 +00:00
|
|
|
// ParseNetwork parses a collection containing exactly one NetworkResource
|
|
|
|
func ParseNetwork(o *ast.ObjectList) (*api.NetworkResource, error) {
|
2019-08-09 19:18:53 +00:00
|
|
|
if len(o.Items) > 1 {
|
|
|
|
return nil, fmt.Errorf("only one 'network' resource allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"mode",
|
|
|
|
"mbits",
|
2020-04-28 03:11:06 +00:00
|
|
|
"dns",
|
2019-08-09 19:18:53 +00:00
|
|
|
"port",
|
|
|
|
}
|
2020-09-03 11:34:04 +00:00
|
|
|
if err := checkHCLKeys(o.Items[0].Val, valid); err != nil {
|
2019-08-09 19:18:53 +00:00
|
|
|
return nil, multierror.Prefix(err, "network ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
var r api.NetworkResource
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Items[0].Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-28 03:11:06 +00:00
|
|
|
|
|
|
|
delete(m, "dns")
|
2019-08-09 19:18:53 +00:00
|
|
|
if err := mapstructure.WeakDecode(m, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var networkObj *ast.ObjectList
|
|
|
|
if ot, ok := o.Items[0].Val.(*ast.ObjectType); ok {
|
|
|
|
networkObj = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("should be an object")
|
|
|
|
}
|
|
|
|
if err := parsePorts(networkObj, &r); err != nil {
|
|
|
|
return nil, multierror.Prefix(err, "network, ports ->")
|
|
|
|
}
|
|
|
|
|
2020-04-28 03:11:06 +00:00
|
|
|
// Filter dns
|
|
|
|
if dns := networkObj.Filter("dns"); len(dns.Items) > 0 {
|
|
|
|
if len(dns.Items) > 1 {
|
|
|
|
return nil, multierror.Prefix(fmt.Errorf("cannot have more than 1 dns stanza"), "network ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := parseDNS(dns.Items[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, multierror.Prefix(err, "network ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
r.DNS = d
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:18:53 +00:00
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePorts(networkObj *ast.ObjectList, nw *api.NetworkResource) error {
|
|
|
|
portsObjList := networkObj.Filter("port")
|
|
|
|
knownPortLabels := make(map[string]bool)
|
|
|
|
for _, port := range portsObjList.Items {
|
|
|
|
if len(port.Keys) == 0 {
|
|
|
|
return fmt.Errorf("ports must be named")
|
|
|
|
}
|
2020-04-28 03:11:06 +00:00
|
|
|
|
|
|
|
// check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"static",
|
|
|
|
"to",
|
2020-06-16 15:53:10 +00:00
|
|
|
"host_network",
|
2020-04-28 03:11:06 +00:00
|
|
|
}
|
2020-09-03 11:34:04 +00:00
|
|
|
if err := checkHCLKeys(port.Val, valid); err != nil {
|
2020-04-28 03:11:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:18:53 +00:00
|
|
|
label := port.Keys[0].Token.Value().(string)
|
|
|
|
if !reDynamicPorts.MatchString(label) {
|
|
|
|
return errPortLabel
|
|
|
|
}
|
|
|
|
l := strings.ToLower(label)
|
|
|
|
if knownPortLabels[l] {
|
|
|
|
return fmt.Errorf("found a port label collision: %s", label)
|
|
|
|
}
|
|
|
|
var p map[string]interface{}
|
|
|
|
var res api.Port
|
|
|
|
if err := hcl.DecodeObject(&p, port.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(p, &res); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.Label = label
|
|
|
|
if res.Value > 0 {
|
|
|
|
nw.ReservedPorts = append(nw.ReservedPorts, res)
|
|
|
|
} else {
|
|
|
|
nw.DynamicPorts = append(nw.DynamicPorts, res)
|
|
|
|
}
|
|
|
|
knownPortLabels[l] = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-28 03:11:06 +00:00
|
|
|
|
|
|
|
func parseDNS(dns *ast.ObjectItem) (*api.DNSConfig, error) {
|
|
|
|
valid := []string{
|
|
|
|
"servers",
|
|
|
|
"searches",
|
|
|
|
"options",
|
|
|
|
}
|
|
|
|
|
2020-09-03 11:34:04 +00:00
|
|
|
if err := checkHCLKeys(dns.Val, valid); err != nil {
|
2020-04-28 03:11:06 +00:00
|
|
|
return nil, multierror.Prefix(err, "dns ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
var dnsCfg api.DNSConfig
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, dns.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mapstructure.WeakDecode(m, &dnsCfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dnsCfg, nil
|
|
|
|
}
|