Skip to content

Commit

Permalink
Merge pull request #7690 from hashicorp/b-inspect-proxy-output
Browse files Browse the repository at this point in the history
two fixes for inspect on connect proxy
  • Loading branch information
shoenig committed Apr 20, 2020
2 parents d4ebc73 + eef81c3 commit a6cb4a0
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 25 deletions.
1 change: 1 addition & 0 deletions contributing/checklist-jobspec.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [ ] Add structs/fields to `nomad/structs` package
* Validation happens in this package and must be implemented
* Implement other methods and tests from `api/` package
* Note that analogous struct field names should match with `api/` package
* [ ] Add conversion between `api/` and `nomad/structs` in `command/agent/job_endpoint.go`
* [ ] Add check for job diff in `nomad/structs/diff.go`
* Note that fields must be listed in alphabetical order in `FieldDiff` slices in `nomad/structs/diff_test.go`
Expand Down
18 changes: 18 additions & 0 deletions jobspec/parse_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,24 @@ func parseProxy(o *ast.ObjectItem) (*api.ConsulProxy, error) {
}

var proxy api.ConsulProxy
var m map[string]interface{}
if err := hcl.DecodeObject(&m, o.Val); err != nil {
return nil, err
}

delete(m, "upstreams")
delete(m, "expose")
delete(m, "config")

dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &proxy,
})
if err != nil {
return nil, err
}
if err := dec.Decode(m); err != nil {
return nil, fmt.Errorf("proxy: %v", err)
}

var listVal *ast.ObjectList
if ot, ok := o.Val.(*ast.ObjectType); ok {
Expand Down
116 changes: 94 additions & 22 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,28 +894,6 @@ func TestParse(t *testing.T) {
},
false,
},
{
"service-connect-sidecar_task-name.hcl",
&api.Job{
ID: helper.StringToPtr("sidecar_task_name"),
Name: helper.StringToPtr("sidecar_task_name"),
Type: helper.StringToPtr("service"),
TaskGroups: []*api.TaskGroup{{
Name: helper.StringToPtr("group"),
Services: []*api.Service{{
Name: "example",
Connect: &api.ConsulConnect{
Native: false,
SidecarService: &api.ConsulSidecarService{},
SidecarTask: &api.SidecarTask{
Name: "my-sidecar",
},
},
}},
}},
},
false,
},
{
"reschedule-job.hcl",
&api.Job{
Expand Down Expand Up @@ -1051,6 +1029,7 @@ func TestParse(t *testing.T) {
SidecarService: &api.ConsulSidecarService{
Tags: []string{"side1", "side2"},
Proxy: &api.ConsulProxy{
LocalServicePort: 8080,
Upstreams: []*api.ConsulUpstream{
{
DestinationName: "other-service",
Expand Down Expand Up @@ -1172,6 +1151,99 @@ func TestParse(t *testing.T) {
},
false,
},
{
"tg-service-connect-sidecar_task-name.hcl",
&api.Job{
ID: helper.StringToPtr("sidecar_task_name"),
Name: helper.StringToPtr("sidecar_task_name"),
Type: helper.StringToPtr("service"),
TaskGroups: []*api.TaskGroup{{
Name: helper.StringToPtr("group"),
Services: []*api.Service{{
Name: "example",
Connect: &api.ConsulConnect{
Native: false,
SidecarService: &api.ConsulSidecarService{},
SidecarTask: &api.SidecarTask{
Name: "my-sidecar",
},
},
}},
}},
},
false,
},
{
"tg-service-connect-proxy.hcl",
&api.Job{
ID: helper.StringToPtr("service-connect-proxy"),
Name: helper.StringToPtr("service-connect-proxy"),
Type: helper.StringToPtr("service"),
TaskGroups: []*api.TaskGroup{{
Name: helper.StringToPtr("group"),
Services: []*api.Service{{
Name: "example",
Connect: &api.ConsulConnect{
Native: false,
SidecarService: &api.ConsulSidecarService{
Proxy: &api.ConsulProxy{
LocalServiceAddress: "10.0.1.2",
LocalServicePort: 8080,
ExposeConfig: &api.ConsulExposeConfig{
Path: []*api.ConsulExposePath{{
Path: "/metrics",
Protocol: "http",
LocalPathPort: 9001,
ListenerPort: "metrics",
}, {
Path: "/health",
Protocol: "http",
LocalPathPort: 9002,
ListenerPort: "health",
}},
},
Upstreams: []*api.ConsulUpstream{{
DestinationName: "upstream1",
LocalBindPort: 2001,
}, {
DestinationName: "upstream2",
LocalBindPort: 2002,
}},
Config: map[string]interface{}{
"foo": "bar",
},
},
},
},
}},
}},
},
false,
},
{
"tg-service-connect-local-service.hcl",
&api.Job{
ID: helper.StringToPtr("connect-proxy-local-service"),
Name: helper.StringToPtr("connect-proxy-local-service"),
Type: helper.StringToPtr("service"),
TaskGroups: []*api.TaskGroup{{
Name: helper.StringToPtr("group"),
Services: []*api.Service{{
Name: "example",
Connect: &api.ConsulConnect{
Native: false,
SidecarService: &api.ConsulSidecarService{
Proxy: &api.ConsulProxy{
LocalServiceAddress: "10.0.1.2",
LocalServicePort: 9876,
},
},
},
}},
}},
},
false,
},
{
"tg-service-check-expose.hcl",
&api.Job{
Expand Down
17 changes: 17 additions & 0 deletions jobspec/test-fixtures/tg-service-connect-local-service.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
job "connect-proxy-local-service" {
type = "service"

group "group" {
service {
name = "example"
connect {
sidecar_service {
proxy {
local_service_port = 9876
local_service_address = "10.0.1.2"
}
}
}
}
}
}
42 changes: 42 additions & 0 deletions jobspec/test-fixtures/tg-service-connect-proxy.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
job "service-connect-proxy" {
type = "service"

group "group" {
service {
name = "example"
connect {
sidecar_service {
proxy {
local_service_port = 8080
local_service_address = "10.0.1.2"
upstreams {
destination_name = "upstream1"
local_bind_port = 2001
}
upstreams {
destination_name = "upstream2"
local_bind_port = 2002
}
expose {
path {
path = "/metrics"
protocol = "http"
local_path_port = 9001
listener_port = "metrics"
}
path {
path = "/health"
protocol = "http"
local_path_port = 9002
listener_port = "health"
}
}
config {
foo = "bar"
}
}
}
}
}
}
}
9 changes: 6 additions & 3 deletions nomad/structs/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,9 @@ type ConsulProxy struct {

// Expose configures the consul proxy.expose stanza to "open up" endpoints
// used by task-group level service checks using HTTP or gRPC protocols.
Expose *ConsulExposeConfig
//
// Use json tag to match with field name in api/
Expose *ConsulExposeConfig `json:"ExposeConfig"`

// Config is a proxy configuration. It is opaque to Nomad and passed
// directly to Consul.
Expand All @@ -905,7 +907,7 @@ func (p *ConsulProxy) Copy() *ConsulProxy {
newP := &ConsulProxy{
LocalServiceAddress: p.LocalServiceAddress,
LocalServicePort: p.LocalServicePort,
Expose: p.Expose,
Expose: p.Expose.Copy(),
}

if n := len(p.Upstreams); n > 0 {
Expand Down Expand Up @@ -1009,7 +1011,8 @@ func (u *ConsulUpstream) Equals(o *ConsulUpstream) bool {

// ExposeConfig represents a Consul Connect expose jobspec stanza.
type ConsulExposeConfig struct {
Paths []ConsulExposePath
// Use json tag to match with field name in api/
Paths []ConsulExposePath `json:"Path"`
}

type ConsulExposePath struct {
Expand Down

0 comments on commit a6cb4a0

Please sign in to comment.