hclv1: parse service upstreams

This commit is contained in:
Mahmood Ali 2021-07-22 15:15:50 -04:00
parent 7ac28d74e6
commit 22f786d375
3 changed files with 63 additions and 4 deletions

View File

@ -891,6 +891,9 @@ func parseUpstream(uo *ast.ObjectItem) (*api.ConsulUpstream, error) {
valid := []string{
"destination_name",
"local_bind_port",
"local_bind_address",
"datacenter",
"mesh_gateway",
}
if err := checkHCLKeys(uo.Val, valid); err != nil {
@ -903,6 +906,8 @@ func parseUpstream(uo *ast.ObjectItem) (*api.ConsulUpstream, error) {
return nil, err
}
delete(m, "mesh_gateway")
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
WeaklyTypedInput: true,
@ -916,9 +921,51 @@ func parseUpstream(uo *ast.ObjectItem) (*api.ConsulUpstream, error) {
return nil, err
}
var listVal *ast.ObjectList
if ot, ok := uo.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return nil, fmt.Errorf("'%s': should be an object", upstream.DestinationName)
}
if mgO := listVal.Filter("mesh_gateway"); len(mgO.Items) > 0 {
if len(mgO.Items) > 1 {
return nil, fmt.Errorf("upstream '%s': cannot have more than 1 mesh_gateway", upstream.DestinationName)
}
mgw, err := parseMeshGateway(mgO.Items[0])
if err != nil {
return nil, multierror.Prefix(err, fmt.Sprintf("'%s',", upstream.DestinationName))
}
upstream.MeshGateway = mgw
}
return &upstream, nil
}
func parseMeshGateway(gwo *ast.ObjectItem) (*api.ConsulMeshGateway, error) {
valid := []string{
"mode",
}
if err := checkHCLKeys(gwo.Val, valid); err != nil {
return nil, multierror.Prefix(err, "mesh_gateway ->")
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, gwo.Val); err != nil {
return nil, err
}
var mgw api.ConsulMeshGateway
if err := mapstructure.WeakDecode(m, &mgw); err != nil {
return nil, err
}
return &mgw, nil
}
func parseChecks(service *api.Service, checkObjs *ast.ObjectList) error {
service.Checks = make([]api.ServiceCheck, len(checkObjs.Items))
for idx, co := range checkObjs.Items {

View File

@ -1115,8 +1115,14 @@ func TestParse(t *testing.T) {
LocalServicePort: 8080,
Upstreams: []*api.ConsulUpstream{
{
DestinationName: "other-service",
LocalBindPort: 4567,
DestinationName: "other-service",
LocalBindPort: 4567,
LocalBindAddress: "0.0.0.0",
Datacenter: "dc1",
MeshGateway: &api.ConsulMeshGateway{
Mode: "local",
},
},
},
},

View File

@ -34,8 +34,14 @@ job "foo" {
local_service_port = 8080
upstreams {
destination_name = "other-service"
local_bind_port = 4567
destination_name = "other-service"
local_bind_port = 4567
local_bind_address = "0.0.0.0"
datacenter = "dc1"
mesh_gateway {
mode = "local"
}
}
}
}