tailcfg: make SelfNodeV4MasqAddrForThisPeer a pointer

This makes `omitempty` actually work, and saves bytes in each map response.

Updates tailscale/corp#8020

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali
2023-04-13 10:12:31 -07:00
committed by Maisem Ali
parent a5fd51ebdc
commit 64bbf1738e
11 changed files with 38 additions and 22 deletions

View File

@@ -289,7 +289,7 @@ type Node struct {
// This only applies to traffic originating from the current node to the
// peer or any of its subnets. Traffic originating from subnet routes will
// not be masqueraded (e.g. in case of --snat-subnet-routes).
SelfNodeV4MasqAddrForThisPeer netip.Addr `json:",omitempty"`
SelfNodeV4MasqAddrForThisPeer *netip.Addr `json:",omitempty"`
// IsWireGuardOnly indicates that this is a non-Tailscale WireGuard peer, it
// is not expected to speak Disco or DERP, and it must have Endpoints in
@@ -1705,7 +1705,7 @@ func (n *Node) Equal(n2 *Node) bool {
bytes.Equal(n.KeySignature, n2.KeySignature) &&
n.Machine == n2.Machine &&
n.DiscoKey == n2.DiscoKey &&
eqBoolPtr(n.Online, n2.Online) &&
eqPtr(n.Online, n2.Online) &&
eqCIDRs(n.Addresses, n2.Addresses) &&
eqCIDRs(n.AllowedIPs, n2.AllowedIPs) &&
eqCIDRs(n.PrimaryRoutes, n2.PrimaryRoutes) &&
@@ -1722,11 +1722,11 @@ func (n *Node) Equal(n2 *Node) bool {
n.ComputedNameWithHost == n2.ComputedNameWithHost &&
eqStrings(n.Tags, n2.Tags) &&
n.Expired == n2.Expired &&
n.SelfNodeV4MasqAddrForThisPeer == n2.SelfNodeV4MasqAddrForThisPeer &&
eqPtr(n.SelfNodeV4MasqAddrForThisPeer, n2.SelfNodeV4MasqAddrForThisPeer) &&
n.IsWireGuardOnly == n2.IsWireGuardOnly
}
func eqBoolPtr(a, b *bool) bool {
func eqPtr[T comparable](a, b *T) bool {
if a == b { // covers nil
return true
}
@@ -1734,7 +1734,6 @@ func eqBoolPtr(a, b *bool) bool {
return false
}
return *a == *b
}
func eqStrings(a, b []string) bool {

View File

@@ -63,6 +63,10 @@ func (src *Node) Clone() *Node {
*dst.Online = *src.Online
}
dst.Capabilities = append(src.Capabilities[:0:0], src.Capabilities...)
if dst.SelfNodeV4MasqAddrForThisPeer != nil {
dst.SelfNodeV4MasqAddrForThisPeer = new(netip.Addr)
*dst.SelfNodeV4MasqAddrForThisPeer = *src.SelfNodeV4MasqAddrForThisPeer
}
return dst
}
@@ -98,7 +102,7 @@ var _NodeCloneNeedsRegeneration = Node(struct {
ComputedNameWithHost string
DataPlaneAuditLogID string
Expired bool
SelfNodeV4MasqAddrForThisPeer netip.Addr
SelfNodeV4MasqAddrForThisPeer *netip.Addr
IsWireGuardOnly bool
}{})

View File

@@ -18,6 +18,7 @@ import (
. "tailscale.com/tailcfg"
"tailscale.com/tstest"
"tailscale.com/types/key"
"tailscale.com/types/ptr"
"tailscale.com/util/must"
"tailscale.com/version"
)
@@ -537,12 +538,12 @@ func TestNodeEqual(t *testing.T) {
},
{
&Node{},
&Node{SelfNodeV4MasqAddrForThisPeer: netip.MustParseAddr("100.64.0.1")},
&Node{SelfNodeV4MasqAddrForThisPeer: ptr.To(netip.MustParseAddr("100.64.0.1"))},
false,
},
{
&Node{SelfNodeV4MasqAddrForThisPeer: netip.MustParseAddr("100.64.0.1")},
&Node{SelfNodeV4MasqAddrForThisPeer: netip.MustParseAddr("100.64.0.1")},
&Node{SelfNodeV4MasqAddrForThisPeer: ptr.To(netip.MustParseAddr("100.64.0.1"))},
&Node{SelfNodeV4MasqAddrForThisPeer: ptr.To(netip.MustParseAddr("100.64.0.1"))},
true,
},
}

View File

@@ -176,9 +176,14 @@ func (v NodeView) ComputedName() string { return v.ж.ComputedName
func (v NodeView) ComputedNameWithHost() string { return v.ж.ComputedNameWithHost }
func (v NodeView) DataPlaneAuditLogID() string { return v.ж.DataPlaneAuditLogID }
func (v NodeView) Expired() bool { return v.ж.Expired }
func (v NodeView) SelfNodeV4MasqAddrForThisPeer() netip.Addr {
return v.ж.SelfNodeV4MasqAddrForThisPeer
func (v NodeView) SelfNodeV4MasqAddrForThisPeer() *netip.Addr {
if v.ж.SelfNodeV4MasqAddrForThisPeer == nil {
return nil
}
x := *v.ж.SelfNodeV4MasqAddrForThisPeer
return &x
}
func (v NodeView) IsWireGuardOnly() bool { return v.ж.IsWireGuardOnly }
func (v NodeView) Equal(v2 NodeView) bool { return v.ж.Equal(v2.ж) }
@@ -214,7 +219,7 @@ var _NodeViewNeedsRegeneration = Node(struct {
ComputedNameWithHost string
DataPlaneAuditLogID string
Expired bool
SelfNodeV4MasqAddrForThisPeer netip.Addr
SelfNodeV4MasqAddrForThisPeer *netip.Addr
IsWireGuardOnly bool
}{})