Skip to content

Commit

Permalink
connect: handle grpc_address as gosockaddr/template string (#24280)
Browse files Browse the repository at this point in the history
* connect: handle grpc_address as gosockaddr/template string

This PR fixes a bug where the consul.grpc_address could not be set using
a go-sockaddr/template string. This was inconsistent with how we do accept
such strings for consul.address values.

* add changelog
  • Loading branch information
shoenig authored Nov 7, 2024
1 parent b58abf4 commit 4ef4beb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/24280.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
connect: Able to accept go-sockaddr address for consul grpc address
```
15 changes: 12 additions & 3 deletions client/allocrunner/consul_grpc_sock_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/hashicorp/go-hclog"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-secure-stdlib/listenerutil"
"github.com/hashicorp/go-set/v3"
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
Expand Down Expand Up @@ -132,7 +133,7 @@ func (h *consulGRPCSocketHook) Prerun() error {

var mErr *multierror.Error
for _, proxy := range h.proxies {
if err := proxy.run(h.alloc); err != nil {
if err := proxy.run(); err != nil {
mErr = multierror.Append(mErr, err)
}
}
Expand All @@ -156,7 +157,7 @@ func (h *consulGRPCSocketHook) Update(req *interfaces.RunnerUpdateRequest) error

var mErr *multierror.Error
for _, proxy := range h.proxies {
if err := proxy.run(h.alloc); err != nil {
if err := proxy.run(); err != nil {
mErr = multierror.Append(mErr, err)
}
}
Expand Down Expand Up @@ -216,7 +217,7 @@ func newGRPCSocketProxy(
// hasn't been told to stop.
//
// NOT safe for concurrent use.
func (p *grpcSocketProxy) run(alloc *structs.Allocation) error {
func (p *grpcSocketProxy) run() error {
// Only run once.
if p.runOnce {
return nil
Expand All @@ -240,6 +241,7 @@ func (p *grpcSocketProxy) run(alloc *structs.Allocation) error {
}

destAddr := p.config.GRPCAddr

if destAddr == "" {
// No GRPCAddr defined. Use Addr but replace port with the gRPC
// default of 8502.
Expand All @@ -248,6 +250,13 @@ func (p *grpcSocketProxy) run(alloc *structs.Allocation) error {
return fmt.Errorf("error parsing Consul address %q: %v", p.config.Addr, err)
}
destAddr = net.JoinHostPort(host, p.consulGRPCFallbackPort)
} else {
// GRPCAddr may be sockaddr/template string, parse it.
ipStr, err := listenerutil.ParseSingleIPTemplate(destAddr)
if err != nil {
return fmt.Errorf("unable to parse address template %q: %v", destAddr, err)
}
destAddr = ipStr
}

socketFile := allocdir.AllocGRPCSocket
Expand Down

0 comments on commit 4ef4beb

Please sign in to comment.