Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sockaddr templates in start-join and retry-join configuration #3865

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions agent/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,28 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
}
}

// expand addresses in start join lan configuration
startJoinLAN := make([]string, len(c.StartJoinAddrsLAN))

for _, a := range c.StartJoinAddrsLAN {
addrs := b.expandOptionalAddrs("start_join", &a)

if addrs != nil {
startJoinLAN = append(startJoinLAN, addrs...)
}
}

// expand addresses in retry join lan configuration
retryJoinLAN := make([]string, len(c.RetryJoinLAN))

for _, a := range c.RetryJoinLAN {
addrs := b.expandOptionalAddrs("retry_join", &a)

if addrs != nil {
retryJoinLAN = append(retryJoinLAN, addrs...)
}
}

// expand dns recursors
uniq := map[string]bool{}
dnsRecursors := []string{}
Expand Down Expand Up @@ -677,7 +699,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
RejoinAfterLeave: b.boolVal(c.RejoinAfterLeave),
RetryJoinIntervalLAN: b.durationVal("retry_interval", c.RetryJoinIntervalLAN),
RetryJoinIntervalWAN: b.durationVal("retry_interval_wan", c.RetryJoinIntervalWAN),
RetryJoinLAN: c.RetryJoinLAN,
RetryJoinLAN: retryJoinLan,
RetryJoinMaxAttemptsLAN: b.intVal(c.RetryJoinMaxAttemptsLAN),
RetryJoinMaxAttemptsWAN: b.intVal(c.RetryJoinMaxAttemptsWAN),
RetryJoinWAN: c.RetryJoinWAN,
Expand All @@ -695,7 +717,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
Services: services,
SessionTTLMin: b.durationVal("session_ttl_min", c.SessionTTLMin),
SkipLeaveOnInt: skipLeaveOnInt,
StartJoinAddrsLAN: c.StartJoinAddrsLAN,
StartJoinAddrsLAN: startJoinLAN,
StartJoinAddrsWAN: c.StartJoinAddrsWAN,
SyslogFacility: b.stringVal(c.SyslogFacility),
TLSCipherSuites: b.tlsCipherSuites("tls_cipher_suites", c.TLSCipherSuites),
Expand Down Expand Up @@ -1110,6 +1132,32 @@ func (b *Builder) expandAddrs(name string, s *string) []net.Addr {
return addrs
}

// expandOptionalAddrs expands the go-sockaddr template in s and returns the
// result as a list of strings. If s does not contain a go-sockaddr template,
// the result list will contain the input string as a single element with no
// error set. In contrast to expandAddrs, expandOptionalAddrs does not validate
// if the result contains valid addresses and returns a list of strings.
// However, if the expansion of the go-sockaddr template fails an error is set.
func (b *Builder) expandOptionalAddrs(name string, s *string) []string {
if s == nil || *s == "" {
return nil
}

x, err := template.Parse(*s)
if err != nil {
b.err = multierror.Append(b.err, fmt.Errorf("%s: error parsing %q: %s", name, s, err))
return nil
}

if x != *s {
// A template has been expanded, split the results from go-sockaddr
return strings.Fields(x)
} else {
// No template has been expanded, pass through the input
return []string{*s}
}
}

// expandIPs expands the go-sockaddr template in s and returns a list of
// *net.IPAddr. If one of the expanded addresses is a unix socket
// address an error is set and nil is returned.
Expand Down Expand Up @@ -1159,7 +1207,7 @@ func (b *Builder) expandFirstAddr(name string, s *string) net.Addr {
return addrs[0]
}

// expandFirstIP exapnds the go-sockaddr template in s and returns the
// expandFirstIP expands the go-sockaddr template in s and returns the
// first address if it is not a unix socket address. If the template
// expands to multiple addresses an error is set and nil is returned.
func (b *Builder) expandFirstIP(name string, s *string) *net.IPAddr {
Expand Down