Skip to content

Commit ee3b43e

Browse files
committed
jobspec: parse multi expose.path instead of explicit slice
1 parent 2a9749c commit ee3b43e

File tree

6 files changed

+59
-15
lines changed

6 files changed

+59
-15
lines changed

api/services.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ type ConsulUpstream struct {
182182
}
183183

184184
type ConsulExposeConfig struct {
185-
Paths []*ConsulExposePath `mapstructure:"paths"`
185+
Path []*ConsulExposePath `mapstructure:"path"`
186186
// todo(shoenig): add magic for 'checks' option
187187
}
188188

command/agent/job_endpoint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ func apiConsulExposeConfigToStructs(in *api.ConsulExposeConfig) *structs.ConsulE
12351235
return nil
12361236
}
12371237
return &structs.ConsulExposeConfig{
1238-
Paths: apiConsulExposePathsToStructs(in.Paths),
1238+
Paths: apiConsulExposePathsToStructs(in.Path),
12391239
}
12401240
}
12411241

command/agent/job_endpoint_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2631,7 +2631,7 @@ func TestConversion_apiConsulExposeConfigToStructs(t *testing.T) {
26312631
require.Equal(t, &structs.ConsulExposeConfig{
26322632
Paths: []structs.ConsulExposePath{{Path: "/health"}},
26332633
}, apiConsulExposeConfigToStructs(&api.ConsulExposeConfig{
2634-
Paths: []*api.ConsulExposePath{{Path: "/health"}},
2634+
Path: []*api.ConsulExposePath{{Path: "/health"}},
26352635
}))
26362636
}
26372637

@@ -2670,7 +2670,7 @@ func TestConversion_apiConnectSidecarServiceProxyToStructs(t *testing.T) {
26702670
DestinationName: "upstream",
26712671
}},
26722672
ExposeConfig: &api.ConsulExposeConfig{
2673-
Paths: []*api.ConsulExposePath{{
2673+
Path: []*api.ConsulExposePath{{
26742674
Path: "/health",
26752675
}},
26762676
},

jobspec/parse_service.go

+40-8
Original file line numberDiff line numberDiff line change
@@ -403,39 +403,71 @@ func parseProxy(o *ast.ObjectItem) (*api.ConsulProxy, error) {
403403
}
404404

405405
func parseExpose(eo *ast.ObjectItem) (*api.ConsulExposeConfig, error) {
406+
valid := []string{
407+
"path", // an array of path blocks
408+
// todo(shoenig) checks boolean
409+
}
410+
411+
if err := helper.CheckHCLKeys(eo.Val, valid); err != nil {
412+
return nil, multierror.Prefix(err, "expose ->")
413+
}
414+
415+
var expose api.ConsulExposeConfig
416+
406417
var listVal *ast.ObjectList
407418
if eoType, ok := eo.Val.(*ast.ObjectType); ok {
408419
listVal = eoType.List
409420
} else {
410421
return nil, fmt.Errorf("expose: should be an object")
411422
}
412423

424+
// Parse the expose block
425+
426+
po := listVal.Filter("path") // array
427+
if len(po.Items) > 0 {
428+
expose.Path = make([]*api.ConsulExposePath, len(po.Items))
429+
for i := range po.Items {
430+
p, err := parseExposePath(po.Items[i])
431+
if err != nil {
432+
return nil, err
433+
}
434+
expose.Path[i] = p
435+
}
436+
}
437+
438+
return &expose, nil
439+
}
440+
441+
func parseExposePath(epo *ast.ObjectItem) (*api.ConsulExposePath, error) {
413442
valid := []string{
414-
"paths",
443+
"path",
444+
"protocol",
445+
"local_path_port",
446+
"listener_port",
415447
}
416448

417-
if err := helper.CheckHCLKeys(listVal, valid); err != nil {
418-
return nil, multierror.Prefix(err, "expose ->")
449+
if err := helper.CheckHCLKeys(epo.Val, valid); err != nil {
450+
return nil, multierror.Prefix(err, "path ->")
419451
}
420452

453+
var path api.ConsulExposePath
421454
var m map[string]interface{}
422-
if err := hcl.DecodeObject(&m, eo.Val); err != nil {
455+
if err := hcl.DecodeObject(&m, epo.Val); err != nil {
423456
return nil, err
424457
}
425458

426-
// Build the expose block
427-
var expose api.ConsulExposeConfig
428459
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
429-
Result: &expose,
460+
Result: &path,
430461
})
431462
if err != nil {
432463
return nil, err
433464
}
465+
434466
if err := dec.Decode(m); err != nil {
435467
return nil, err
436468
}
437469

438-
return &expose, nil
470+
return &path, nil
439471
}
440472

441473
func parseUpstream(uo *ast.ObjectItem) (*api.ConsulUpstream, error) {

jobspec/parse_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,16 @@ func TestParse(t *testing.T) {
11301130
SidecarService: &api.ConsulSidecarService{
11311131
Proxy: &api.ConsulProxy{
11321132
ExposeConfig: &api.ConsulExposeConfig{
1133-
Paths: []*api.ConsulExposePath{{
1133+
Path: []*api.ConsulExposePath{{
11341134
Path: "/health",
11351135
Protocol: "http",
11361136
LocalPathPort: 2222,
11371137
ListenerPort: "healthcheck",
1138+
}, {
1139+
Path: "/metrics",
1140+
Protocol: "grpc",
1141+
LocalPathPort: 3000,
1142+
ListenerPort: "metrics",
11381143
}},
11391144
},
11401145
},

jobspec/test-fixtures/tg-service-proxy-expose.hcl

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ job "group_service_proxy_expose" {
66
sidecar_service {
77
proxy {
88
expose {
9-
paths = [{
9+
path = {
1010
path = "/health"
1111
protocol = "http"
1212
local_path_port = 2222
1313
listener_port = "healthcheck"
14-
}]
14+
}
15+
16+
path = {
17+
path = "/metrics"
18+
protocol = "grpc"
19+
local_path_port = 3000
20+
listener_port = "metrics"
21+
}
1522
}
1623
}
1724
}

0 commit comments

Comments
 (0)