Skip to content

Commit 7a57b61

Browse files
committed
feat(mdns): allow specifying iface for mDNS
1 parent fca5ddd commit 7a57b61

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

cmd/serf/command/agent/command.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (c *Command) readConfig() *Config {
8888
"tag pair, specified as key=value")
8989
cmdFlags.StringVar(&cmdConfig.Discover, "discover", "", "mDNS discovery name")
9090
cmdFlags.StringVar(&cmdConfig.Interface, "iface", "", "interface to bind to")
91+
cmdFlags.StringVar(&cmdConfig.MDNS.Interface, "mdns-iface", "", "interface to use for mDNS")
9192
cmdFlags.StringVar(&cmdConfig.TagsFile, "tags-file", "", "tag persistence file")
9293
cmdFlags.BoolVar(&cmdConfig.EnableSyslog, "syslog", false,
9394
"enable logging to syslog facility")
@@ -176,6 +177,18 @@ func (c *Command) readConfig() *Config {
176177
return nil
177178
}
178179

180+
if config.MDNS.Interface != "" {
181+
if config.Discover == "" {
182+
c.Ui.Error("mDNS interface specified without enabling mDNS discovery")
183+
return nil
184+
}
185+
186+
if _, err := net.InterfaceByName(config.MDNS.Interface); err != nil {
187+
c.Ui.Error(fmt.Sprintf("Invalid mDNS network interface: %s", err))
188+
return nil
189+
}
190+
}
191+
179192
// Backward compatibility hack for 'Role'
180193
if config.Role != "" {
181194
c.Ui.Output("Deprecation warning: 'Role' has been replaced with 'Tags'")
@@ -432,7 +445,9 @@ func (c *Command) startAgent(config *Config, agent *Agent,
432445
local := agent.Serf().Memberlist().LocalNode()
433446

434447
// Get the bind interface if any
435-
iface, _ := config.NetworkInterface()
448+
iface, _ := config.MDNSNetworkInterface()
449+
450+
c.logger.Printf("[INFO] agent: Starting mDNS listener on interface %s", iface.Name)
436451

437452
_, err := NewAgentMDNS(agent, logOutput, config.ReplayOnJoin,
438453
config.NodeName, config.Discover, iface, local.Addr, int(local.Port))
@@ -734,7 +749,10 @@ Options:
734749
-bind if the interface is known but not the address.
735750
If both are provided, then Serf verifies that the
736751
interface has the bind address that is provided. This
737-
flag also sets the multicast device used for -discover.
752+
flag also sets the multicast device used for -discover,
753+
if mdns-iface is not specified.
754+
-mdns-iface Network interface to use for mDNS. If not provided, the
755+
-iface value is used.
738756
-advertise=0.0.0.0 Address to advertise to the other cluster members
739757
-config-file=foo Path to a JSON file to read configuration from.
740758
This can be specified multiple times.

cmd/serf/command/agent/config.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ func DefaultConfig() *Config {
4545

4646
type dirEnts []os.FileInfo
4747

48+
type MDNSConfig struct {
49+
// Interface is used to provide a binding interface to use for mDNS.
50+
// if not set, iface will be used.
51+
Interface string `mapstructure:"interface"`
52+
}
53+
4854
// Config is the configuration that can be set for an Agent. Some of these
4955
// configurations are exposed as command-line flags to `serf agent`, whereas
5056
// many of the more advanced configurations can only be set by creating
@@ -154,10 +160,12 @@ type Config struct {
154160
// allows Serf agents to join each other with zero configuration.
155161
Discover string `mapstructure:"discover"`
156162

163+
MDNS MDNSConfig `mapstructure:"mdns"`
164+
157165
// Interface is used to provide a binding interface to use. It can be
158166
// used instead of providing a bind address, as Serf will discover the
159167
// address of the provided interface. It is also used to set the multicast
160-
// device used with `-discover`.
168+
// device used with `-discover`, if `mdns-iface` is not set
161169
Interface string `mapstructure:"interface"`
162170

163171
// ReconnectIntervalRaw is the string reconnect interval time. This interval
@@ -292,6 +300,16 @@ func (c *Config) NetworkInterface() (*net.Interface, error) {
292300
return net.InterfaceByName(c.Interface)
293301
}
294302

303+
func (c *Config) MDNSNetworkInterface() (*net.Interface, error) {
304+
if c.MDNS.Interface == "" && c.Interface == "" {
305+
return nil, nil
306+
} else if c.MDNS.Interface != "" {
307+
return net.InterfaceByName(c.MDNS.Interface)
308+
} else {
309+
return net.InterfaceByName(c.Interface)
310+
}
311+
}
312+
295313
// DecodeConfig reads the configuration from the given reader in JSON
296314
// format and decodes it into a proper Config structure.
297315
func DecodeConfig(r io.Reader) (*Config, error) {
@@ -436,6 +454,11 @@ func MergeConfig(a, b *Config) *Config {
436454
if b.Interface != "" {
437455
result.Interface = b.Interface
438456
}
457+
458+
if b.MDNS.Interface != "" {
459+
result.MDNS.Interface = b.MDNS.Interface
460+
}
461+
439462
if b.ReconnectInterval != 0 {
440463
result.ReconnectInterval = b.ReconnectInterval
441464
}

0 commit comments

Comments
 (0)