Skip to content

Commit

Permalink
feat: advertise DNS services to peers
Browse files Browse the repository at this point in the history
  • Loading branch information
tinyzimmer committed Jul 29, 2023
1 parent f066030 commit 489886d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
17 changes: 16 additions & 1 deletion pkg/cmd/nodecmd/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ func (o *Options) Overlay(opts ...any) error {
}
}
if primaryEndpoint.IsValid() {
// Determine the raft and wireguard ports so we can set our
// advertise addresses.
var raftPort, wireguardPort uint16
for _, inOpts := range opts {
if vopt, ok := inOpts.(*raft.Options); ok {
Expand Down Expand Up @@ -321,7 +323,20 @@ func (o *Options) Overlay(opts ...any) error {
v.TURN.PublicIP = primaryEndpoint.String()
}
}

if v.MeshDNS.Enabled && v.MeshDNS.ListenUDP != "" && !o.DisableFeatureAdvertisement {
// Set the advertise DNS port
dnsAddr, err := netip.ParseAddrPort(v.MeshDNS.ListenUDP)
if err != nil {
return fmt.Errorf("failed to parse listen address: %w", err)
}
for _, inOpts := range opts {
if vopt, ok := inOpts.(*mesh.Options); ok {
if vopt.Mesh.DNSPort == 0 {
vopt.Mesh.DNSPort = int(dnsAddr.Port())
}
}
}
}
}
}
return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/mesh/mesh_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (s *meshStore) joinWithConn(ctx context.Context, c *grpc.ClientConn, featur
PublicKey: key.PublicKey().String(),
RaftPort: int32(s.raft.ListenPort()),
GrpcPort: int32(s.opts.Mesh.GRPCPort),
MeshdnsPort: int32(s.opts.Mesh.DNSPort),
PrimaryEndpoint: s.opts.Mesh.PrimaryEndpoint,
WireguardEndpoints: s.opts.WireGuard.Endpoints,
ZoneAwarenessId: s.opts.Mesh.ZoneAwarenessID,
Expand Down
5 changes: 5 additions & 0 deletions pkg/mesh/options_mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
MaxJoinRetriesEnvVar = "MESH_MAX_JOIN_RETRIES"
JoinTimeoutEnvVar = "MESH_JOIN_TIMEOUT"
GRPCAdvertisePortEnvVar = "MESH_GRPC_PORT"
DNSAdvertisePortEnvVar = "MESH_DNS_PORT"
PrimaryEndpointEnvVar = "MESH_PRIMARY_ENDPOINT"
NodeRoutesEnvVar = "MESH_ROUTES"
NodeDirectPeersEnvVar = "MESH_DIRECT_PEERS"
Expand Down Expand Up @@ -64,6 +65,8 @@ type MeshOptions struct {
DirectPeers []string `json:"direct-peers,omitempty" yaml:"direct-peers,omitempty" toml:"direct-peers,omitempty"`
// GRPCPort is the port to advertise for gRPC.
GRPCPort int `json:"grpc-port,omitempty" yaml:"grpc-port,omitempty" toml:"grpc-port,omitempty"`
// DNSPort is the port to advertise for DNS.
DNSPort int `json:"dns-port,omitempty" yaml:"dns-port,omitempty" toml:"dns-port,omitempty"`
// NoIPv4 disables IPv4 usage.
NoIPv4 bool `json:"no-ipv4,omitempty" yaml:"no-ipv4,omitempty" toml:"no-ipv4,omitempty"`
// NoIPv6 disables IPv6 usage.
Expand Down Expand Up @@ -121,6 +124,8 @@ func (o *MeshOptions) BindFlags(fl *flag.FlagSet) {
"Join the cluster as a voter. Default behavior is to join as an observer.")
fl.IntVar(&o.GRPCPort, "mesh.grpc-port", util.GetEnvIntDefault(GRPCAdvertisePortEnvVar, 8443),
"GRPC advertise port.")
fl.IntVar(&o.DNSPort, "mesh.dns-port", util.GetEnvIntDefault(DNSAdvertisePortEnvVar, 0),
"DNS advertise port. This is set automatically when advertising is enabled and the mesh-dns server is running. Default is 0 (disabled).")
fl.StringVar(&o.PrimaryEndpoint, "mesh.primary-endpoint", util.GetEnvDefault(PrimaryEndpointEnvVar, ""),
`The primary endpoint to broadcast when joining a cluster.
This is only necessary if the node intends on being publicly accessible.`)
Expand Down
7 changes: 2 additions & 5 deletions pkg/meshdb/peers/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,8 @@ func (p *peers) ListByFeature(ctx context.Context, feature v1.Feature) ([]Node,
}
out := make([]Node, 0)
for _, node := range nodes {
for _, f := range node.Features {
if f == feature {
out = append(out, node)
break
}
if node.HasFeature(feature) {
out = append(out, node)
}
}
return out, nil
Expand Down
18 changes: 0 additions & 18 deletions pkg/net/inspect/inspect.go

This file was deleted.

6 changes: 6 additions & 0 deletions pkg/services/node/server_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ func (s *Server) Join(ctx context.Context, req *v1.JoinRequest) (*v1.JoinRespons
log.Warn("could not lookup DNS servers", slog.String("error", err.Error()))
} else {
for _, peer := range dnsServers {
if peer.ID == req.GetId() {
continue
}
switch {
// Prefer the IPv4 address
case peer.PrivateDNSAddrV4().IsValid():
Expand Down Expand Up @@ -437,6 +440,9 @@ func (s *Server) Join(ctx context.Context, req *v1.JoinRequest) (*v1.JoinRespons
return nil, status.Errorf(codes.Internal, "failed to list peers by ICE feature: %v", err)
}
for _, peer := range peers {
if peer.ID == req.GetId() {
continue
}
// We only return peers that are publicly accessible for now.
// This should be configurable in the future.
publicAddr := peer.PublicRPCAddr()
Expand Down

0 comments on commit 489886d

Please sign in to comment.