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

daemon, maps/ipcache: Replace usage of net.IP* for ingress IPs #26045

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions daemon/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import (
"github.com/cilium/cilium/pkg/hubble/observer"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/identity/identitymanager"
"github.com/cilium/cilium/pkg/ip"
"github.com/cilium/cilium/pkg/ipam"
ipamMetadata "github.com/cilium/cilium/pkg/ipam/metadata"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
Expand Down Expand Up @@ -561,7 +560,7 @@ func newDaemon(ctx context.Context, cleaner *daemonCleanup, params *daemonParams

// Collect old CIDR identities
var oldNIDs []identity.NumericIdentity
var oldIngressIPs []*net.IPNet
var oldIngressIPs []netip.Prefix
if option.Config.RestoreState && !option.Config.DryMode {
if err := ipcachemap.IPCacheMap().DumpWithCallback(func(key bpf.MapKey, value bpf.MapValue) {
k := key.(*ipcachemap.Key)
Expand All @@ -571,12 +570,13 @@ func newDaemon(ctx context.Context, cleaner *daemonCleanup, params *daemonParams
d.restoredCIDRs = append(d.restoredCIDRs, k.Prefix())
oldNIDs = append(oldNIDs, nid)
} else if nid == identity.ReservedIdentityIngress && v.TunnelEndpoint.IsZero() {
oldIngressIPs = append(oldIngressIPs, k.IPNet())
ip := k.IPNet().IP
tklauser marked this conversation as resolved.
Show resolved Hide resolved
if ip.To4() != nil {
node.SetIngressIPv4(ip)
prefix := k.Prefix()
oldIngressIPs = append(oldIngressIPs, prefix)
addr := prefix.Addr()
if addr.Is4() {
node.SetIngressIPv4(addr.AsSlice())
} else {
node.SetIngressIPv6(ip)
node.SetIngressIPv6(addr.AsSlice())
}
}
}); err != nil && !os.IsNotExist(err) {
Expand Down Expand Up @@ -744,7 +744,7 @@ func newDaemon(ctx context.Context, cleaner *daemonCleanup, params *daemonParams
// Upsert restored local Ingress IPs
for _, ingressIP := range oldIngressIPs {
d.ipcache.UpsertLabels(
ip.IPNetToPrefix(ingressIP),
ingressIP,
labels.LabelIngress,
source.Restored,
ipcachetypes.NewResourceID(ipcachetypes.ResourceKindDaemon, "", ""),
Expand Down
15 changes: 0 additions & 15 deletions pkg/ip/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,6 @@ func ParsePrefixes(cidrs []string) (valid []netip.Prefix, invalid []string, erro
return valid, invalid, errors
}

// IPNetToPrefix is a convenience helper for migrating from the older 'net'
// standard library types to the newer 'netip' types. Use this to plug the
// new types in newer code into older types in older code during the migration.
func IPNetToPrefix(prefix *net.IPNet) netip.Prefix {
if prefix == nil {
return netip.Prefix{}
}
addr, ok := AddrFromIP(prefix.IP)
if !ok {
return netip.Prefix{}
}
ones, _ := prefix.Mask.Size()
return netip.PrefixFrom(addr, ones)
}

// AddrToIPNet is a convenience helper to convert a netip.Addr to a *net.IPNet
// with a mask corresponding to the addresses's bit length.
func AddrToIPNet(addr netip.Addr) *net.IPNet {
Expand Down
16 changes: 0 additions & 16 deletions pkg/ip/cidr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,3 @@ func TestNetsContainsAny(t *testing.T) {
})
}
}

func TestIPNetToPrefix(t *testing.T) {
_, v4CIDR, err := net.ParseCIDR("1.1.1.1/32")
assert.NoError(t, err)
_, v6CIDR, err := net.ParseCIDR("::ff/128")
assert.NoError(t, err)
assert.Equal(t, netip.MustParsePrefix(v4CIDR.String()), IPNetToPrefix(v4CIDR))
assert.Equal(t, netip.MustParsePrefix(v6CIDR.String()), IPNetToPrefix(v6CIDR))

_, v4CIDR, err = net.ParseCIDR("1.1.1.1/1")
assert.NoError(t, err)
assert.Equal(t, netip.MustParsePrefix(v4CIDR.String()), IPNetToPrefix(v4CIDR))

assert.Equal(t, netip.Prefix{}, IPNetToPrefix(nil))
assert.Equal(t, netip.Prefix{}, IPNetToPrefix(&net.IPNet{}))
}
14 changes: 0 additions & 14 deletions pkg/maps/ipcache/ipcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,6 @@ func (k Key) String() string {

func (k *Key) New() bpf.MapKey { return &Key{} }

func (k Key) IPNet() *net.IPNet {
cidr := &net.IPNet{}
prefixLen := k.Prefixlen - getStaticPrefixBits()
switch k.Family {
case bpf.EndpointKeyIPv4:
cidr.IP = net.IP(k.IP[:net.IPv4len])
cidr.Mask = net.CIDRMask(int(prefixLen), 32)
case bpf.EndpointKeyIPv6:
cidr.IP = net.IP(k.IP[:net.IPv6len])
cidr.Mask = net.CIDRMask(int(prefixLen), 128)
}
return cidr
}

func (k Key) Prefix() netip.Prefix {
var addr netip.Addr
prefixLen := int(k.Prefixlen - getStaticPrefixBits())
Expand Down