Skip to content

Commit

Permalink
jobspec: parse multi expose.path instead of explicit slice
Browse files Browse the repository at this point in the history
  • Loading branch information
shoenig committed Mar 30, 2020
1 parent 12e58cc commit 4e5524e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 15 deletions.
2 changes: 1 addition & 1 deletion api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ type ConsulUpstream struct {
}

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

Expand Down
2 changes: 1 addition & 1 deletion command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ func apiConsulExposeConfigToStructs(in *api.ConsulExposeConfig) *structs.ConsulE
return nil
}
return &structs.ConsulExposeConfig{
Paths: apiConsulExposePathsToStructs(in.Paths),
Paths: apiConsulExposePathsToStructs(in.Path),
}
}

Expand Down
4 changes: 2 additions & 2 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2631,7 +2631,7 @@ func TestConversion_apiConsulExposeConfigToStructs(t *testing.T) {
require.Equal(t, &structs.ConsulExposeConfig{
Paths: []structs.ConsulExposePath{{Path: "/health"}},
}, apiConsulExposeConfigToStructs(&api.ConsulExposeConfig{
Paths: []*api.ConsulExposePath{{Path: "/health"}},
Path: []*api.ConsulExposePath{{Path: "/health"}},
}))
}

Expand Down Expand Up @@ -2670,7 +2670,7 @@ func TestConversion_apiConnectSidecarServiceProxyToStructs(t *testing.T) {
DestinationName: "upstream",
}},
ExposeConfig: &api.ConsulExposeConfig{
Paths: []*api.ConsulExposePath{{
Path: []*api.ConsulExposePath{{
Path: "/health",
}},
},
Expand Down
48 changes: 40 additions & 8 deletions jobspec/parse_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,39 +403,71 @@ func parseProxy(o *ast.ObjectItem) (*api.ConsulProxy, error) {
}

func parseExpose(eo *ast.ObjectItem) (*api.ConsulExposeConfig, error) {
valid := []string{
"path", // an array of path blocks
// todo(shoenig) checks boolean
}

if err := helper.CheckHCLKeys(eo.Val, valid); err != nil {
return nil, multierror.Prefix(err, "expose ->")
}

var expose api.ConsulExposeConfig

var listVal *ast.ObjectList
if eoType, ok := eo.Val.(*ast.ObjectType); ok {
listVal = eoType.List
} else {
return nil, fmt.Errorf("expose: should be an object")
}

// Parse the expose block

po := listVal.Filter("path") // array
if len(po.Items) > 0 {
expose.Path = make([]*api.ConsulExposePath, len(po.Items))
for i := range po.Items {
p, err := parseExposePath(po.Items[i])
if err != nil {
return nil, err
}
expose.Path[i] = p
}
}

return &expose, nil
}

func parseExposePath(epo *ast.ObjectItem) (*api.ConsulExposePath, error) {
valid := []string{
"paths",
"path",
"protocol",
"local_path_port",
"listener_port",
}

if err := helper.CheckHCLKeys(listVal, valid); err != nil {
return nil, multierror.Prefix(err, "expose ->")
if err := helper.CheckHCLKeys(epo.Val, valid); err != nil {
return nil, multierror.Prefix(err, "path ->")
}

var path api.ConsulExposePath
var m map[string]interface{}
if err := hcl.DecodeObject(&m, eo.Val); err != nil {
if err := hcl.DecodeObject(&m, epo.Val); err != nil {
return nil, err
}

// Build the expose block
var expose api.ConsulExposeConfig
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &expose,
Result: &path,
})
if err != nil {
return nil, err
}

if err := dec.Decode(m); err != nil {
return nil, err
}

return &expose, nil
return &path, nil
}

func parseUpstream(uo *ast.ObjectItem) (*api.ConsulUpstream, error) {
Expand Down
7 changes: 6 additions & 1 deletion jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,11 +1130,16 @@ func TestParse(t *testing.T) {
SidecarService: &api.ConsulSidecarService{
Proxy: &api.ConsulProxy{
ExposeConfig: &api.ConsulExposeConfig{
Paths: []*api.ConsulExposePath{{
Path: []*api.ConsulExposePath{{
Path: "/health",
Protocol: "http",
LocalPathPort: 2222,
ListenerPort: "healthcheck",
}, {
Path: "/metrics",
Protocol: "grpc",
LocalPathPort: 3000,
ListenerPort: "metrics",
}},
},
},
Expand Down
11 changes: 9 additions & 2 deletions jobspec/test-fixtures/tg-service-proxy-expose.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ job "group_service_proxy_expose" {
sidecar_service {
proxy {
expose {
paths = [{
path = {
path = "/health"
protocol = "http"
local_path_port = 2222
listener_port = "healthcheck"
}]
}

path = {
path = "/metrics"
protocol = "grpc"
local_path_port = 3000
listener_port = "metrics"
}
}
}
}
Expand Down

0 comments on commit 4e5524e

Please sign in to comment.