Skip to content

Commit

Permalink
Move TransportDialer out of config package
Browse files Browse the repository at this point in the history
and into dependency
  • Loading branch information
tvoran committed Oct 13, 2021
1 parent 9860065 commit 03d3cad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
16 changes: 3 additions & 13 deletions config/transport.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package config

import (
"context"
"fmt"
"net"
"runtime"
"time"

"github.com/hashicorp/consul-template/dependency"
)

const (
Expand Down Expand Up @@ -41,7 +41,7 @@ type TransportConfig struct {
// CustomDialer overrides the default net.Dial with a custom dialer. This is
// useful for instance with Vault Agent Templating to direct Consul Template
// requests through an internal cache.
CustomDialer TransportDialer `mapstructure:"-"`
CustomDialer dependency.TransportDialer `mapstructure:"-"`

// DialKeepAlive is the amount of time for keep-alives.
DialKeepAlive *time.Duration `mapstructure:"dial_keep_alive"`
Expand All @@ -68,16 +68,6 @@ type TransportConfig struct {
TLSHandshakeTimeout *time.Duration `mapstructure:"tls_handshake_timeout"`
}

// TransportDialer is an interface that allows passing a custom dialer function
// to an HTTP client's transport config
type TransportDialer interface {
// Dial is intended to match https://pkg.go.dev/net#Dialer.Dial
Dial(network, address string) (net.Conn, error)

// DialContext is intended to match https://pkg.go.dev/net#Dialer.DialContext
DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

// DefaultTransportConfig returns a configuration that is populated with the
// default values.
func DefaultTransportConfig() *TransportConfig {
Expand Down
17 changes: 13 additions & 4 deletions dependency/client_set.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dependency

import (
"context"
"crypto/tls"
"fmt"
"log"
Expand All @@ -12,8 +13,6 @@ import (
consulapi "github.com/hashicorp/consul/api"
rootcerts "github.com/hashicorp/go-rootcerts"
vaultapi "github.com/hashicorp/vault/api"

"github.com/hashicorp/consul-template/config"
)

// ClientSet is a collection of clients that dependencies use to communicate
Expand All @@ -37,6 +36,16 @@ type vaultClient struct {
httpClient *http.Client
}

// TransportDialer is an interface that allows passing a custom dialer function
// to an HTTP client's transport config
type TransportDialer interface {
// Dial is intended to match https://pkg.go.dev/net#Dialer.Dial
Dial(network, address string) (net.Conn, error)

// DialContext is intended to match https://pkg.go.dev/net#Dialer.DialContext
DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

// CreateConsulClientInput is used as input to the CreateConsulClient function.
type CreateConsulClientInput struct {
Address string
Expand Down Expand Up @@ -76,7 +85,7 @@ type CreateVaultClientInput struct {
SSLCAPath string
ServerName string

TransportCustomDialer config.TransportDialer
TransportCustomDialer TransportDialer
TransportDialKeepAlive time.Duration
TransportDialTimeout time.Duration
TransportDisableKeepAlives bool
Expand Down Expand Up @@ -205,7 +214,7 @@ func (c *ClientSet) CreateVaultClient(i *CreateVaultClientInput) error {
}

// This transport will attempt to keep connections open to the Vault server.
var dialer config.TransportDialer
var dialer TransportDialer
dialer = &net.Dialer{
Timeout: i.TransportDialTimeout,
KeepAlive: i.TransportDialKeepAlive,
Expand Down

0 comments on commit 03d3cad

Please sign in to comment.