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

Simplify leases code #1773

Merged
merged 1 commit into from
May 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 35 additions & 89 deletions pkg/lease/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,48 +67,23 @@ type LeaseWatchResult struct {
}

type LeaseWatcher struct {
OwnLease *Lease
Leases []Lease
OwnLease *Lease //Lease with the subnet of the local node
Leases []Lease //Leases with subnets from other nodes
}

// Reset is called by etcd-subnet when using a snapshot
func (lw *LeaseWatcher) Reset(leases []Lease) []Event {
batch := []Event{}

for _, nl := range leases {
if lw.OwnLease != nil && nl.EnableIPv4 && !nl.EnableIPv6 &&
nl.Subnet.Equal(lw.OwnLease.Subnet) {
continue
} else if lw.OwnLease != nil && !nl.EnableIPv4 && nl.EnableIPv6 &&
nl.IPv6Subnet.Equal(lw.OwnLease.IPv6Subnet) {
continue
} else if lw.OwnLease != nil && nl.EnableIPv4 && nl.EnableIPv6 &&
nl.Subnet.Equal(lw.OwnLease.Subnet) &&
nl.IPv6Subnet.Equal(lw.OwnLease.IPv6Subnet) {
continue
} else if lw.OwnLease != nil && !nl.EnableIPv4 && !nl.EnableIPv6 &&
nl.Subnet.Equal(lw.OwnLease.Subnet) {
//TODO - dual-stack temporarily only compatible with kube subnet manager
found := false
if sameSubnet(nl.EnableIPv4, nl.EnableIPv6, *lw.OwnLease, nl) {
continue
}

found := false
for i, ol := range lw.Leases {
if ol.EnableIPv4 && !ol.EnableIPv6 && ol.Subnet.Equal(nl.Subnet) {
lw.Leases = deleteLease(lw.Leases, i)
found = true
break
} else if ol.EnableIPv4 && !ol.EnableIPv6 && ol.IPv6Subnet.Equal(nl.IPv6Subnet) {
lw.Leases = deleteLease(lw.Leases, i)
found = true
break
} else if ol.EnableIPv4 && ol.EnableIPv6 && ol.Subnet.Equal(nl.Subnet) &&
ol.IPv6Subnet.Equal(nl.IPv6Subnet) {
lw.Leases = deleteLease(lw.Leases, i)
found = true
break
} else if !ol.EnableIPv4 && !ol.EnableIPv6 && ol.Subnet.Equal(nl.Subnet) {
//TODO - dual-stack temporarily only compatible with kube subnet manager
lw.Leases = deleteLease(lw.Leases, i)
if sameSubnet(ol.EnableIPv4, ol.EnableIPv6, ol, nl) {
lw.Leases = append(lw.Leases[:i], lw.Leases[i+1:]...)
found = true
break
}
Expand All @@ -120,23 +95,7 @@ func (lw *LeaseWatcher) Reset(leases []Lease) []Event {
}
}

// everything left in sm.leases has been deleted
for _, l := range lw.Leases {
if lw.OwnLease != nil && l.EnableIPv4 && !l.EnableIPv6 &&
l.Subnet.Equal(lw.OwnLease.Subnet) {
continue
} else if lw.OwnLease != nil && !l.EnableIPv4 && l.EnableIPv6 &&
l.IPv6Subnet.Equal(lw.OwnLease.IPv6Subnet) {
continue
} else if lw.OwnLease != nil && l.EnableIPv4 && l.EnableIPv6 &&
l.Subnet.Equal(lw.OwnLease.Subnet) &&
l.IPv6Subnet.Equal(lw.OwnLease.IPv6Subnet) {
continue
} else if lw.OwnLease != nil && !l.EnableIPv4 && !l.EnableIPv6 &&
l.Subnet.Equal(lw.OwnLease.Subnet) {
//TODO - dual-stack temporarily only compatible with kube subnet manager
continue
}
batch = append(batch, Event{EventRemoved, l})
}

Expand All @@ -147,23 +106,12 @@ func (lw *LeaseWatcher) Reset(leases []Lease) []Event {
return batch
}

// Update reads the leases in the events and depending on Type, adds them or removes them
func (lw *LeaseWatcher) Update(events []Event) []Event {
batch := []Event{}

for _, e := range events {
if lw.OwnLease != nil && e.Lease.EnableIPv4 && !e.Lease.EnableIPv6 &&
e.Lease.Subnet.Equal(lw.OwnLease.Subnet) {
continue
} else if lw.OwnLease != nil && !e.Lease.EnableIPv4 && e.Lease.EnableIPv6 &&
e.Lease.IPv6Subnet.Equal(lw.OwnLease.IPv6Subnet) {
continue
} else if lw.OwnLease != nil && e.Lease.EnableIPv4 && e.Lease.EnableIPv6 &&
e.Lease.Subnet.Equal(lw.OwnLease.Subnet) &&
e.Lease.IPv6Subnet.Equal(lw.OwnLease.IPv6Subnet) {
continue
} else if lw.OwnLease != nil && !e.Lease.EnableIPv4 && !e.Lease.EnableIPv6 &&
e.Lease.Subnet.Equal(lw.OwnLease.Subnet) {
//TODO - dual-stack temporarily only compatible with kube subnet manager
if sameSubnet(e.Lease.EnableIPv4, e.Lease.EnableIPv6, *lw.OwnLease, e.Lease) {
continue
}

Expand All @@ -179,44 +127,25 @@ func (lw *LeaseWatcher) Update(events []Event) []Event {
return batch
}

// add updates lw.Leases, adding the passed lease (either overwriting or appending). It makes lw.Leases a set
func (lw *LeaseWatcher) add(lease *Lease) Event {
for i, l := range lw.Leases {
if l.EnableIPv4 && !l.EnableIPv6 && l.Subnet.Equal(lease.Subnet) {
lw.Leases[i] = *lease
return Event{EventAdded, lw.Leases[i]}
} else if !l.EnableIPv4 && l.EnableIPv6 && l.IPv6Subnet.Equal(lease.IPv6Subnet) {
lw.Leases[i] = *lease
return Event{EventAdded, lw.Leases[i]}
} else if l.EnableIPv4 && l.EnableIPv6 && l.Subnet.Equal(lease.Subnet) &&
l.IPv6Subnet.Equal(lease.IPv6Subnet) {
lw.Leases[i] = *lease
return Event{EventAdded, lw.Leases[i]}
} else if !l.EnableIPv4 && !l.EnableIPv6 && l.Subnet.Equal(lease.Subnet) {
//TODO - dual-stack temporarily only compatible with kube subnet manager
if sameSubnet(l.EnableIPv4, l.EnableIPv6, l, *lease) {
lw.Leases[i] = *lease
return Event{EventAdded, lw.Leases[i]}
}
}

lw.Leases = append(lw.Leases, *lease)

return Event{EventAdded, lw.Leases[len(lw.Leases)-1]}
}

// remove updates lw.Leases, removing the passed lease
func (lw *LeaseWatcher) remove(lease *Lease) Event {
for i, l := range lw.Leases {
if l.EnableIPv4 && !l.EnableIPv6 && l.Subnet.Equal(lease.Subnet) {
lw.Leases = deleteLease(lw.Leases, i)
return Event{EventRemoved, l}
} else if !l.EnableIPv4 && l.EnableIPv6 && l.IPv6Subnet.Equal(lease.IPv6Subnet) {
lw.Leases = deleteLease(lw.Leases, i)
return Event{EventRemoved, l}
} else if l.EnableIPv4 && l.EnableIPv6 && l.Subnet.Equal(lease.Subnet) &&
l.IPv6Subnet.Equal(lease.IPv6Subnet) {
lw.Leases = deleteLease(lw.Leases, i)
return Event{EventRemoved, l}
} else if !l.EnableIPv4 && !l.EnableIPv6 && l.Subnet.Equal(lease.Subnet) {
//TODO - dual-stack temporarily only compatible with kube subnet manager
lw.Leases = deleteLease(lw.Leases, i)
if sameSubnet(l.EnableIPv4, l.EnableIPv6, l, *lease) {
lw.Leases = append(lw.Leases[:i], lw.Leases[i+1:]...)
return Event{EventRemoved, l}
}
}
Expand All @@ -225,7 +154,24 @@ func (lw *LeaseWatcher) remove(lease *Lease) Event {
return Event{EventRemoved, *lease}
}

func deleteLease(l []Lease, i int) []Lease {
l = append(l[:i], l[i+1:]...)
return l
// sameSubnet checks if the subnets are the same in ipv4-only, ipv6-only and dualStack cases
func sameSubnet(ipv4Enabled, ipv6Enabled bool, firstLease, secondLease Lease) bool {
// ipv4 only case
if ipv4Enabled && !ipv6Enabled && firstLease.Subnet.Equal(secondLease.Subnet) {
return true
}
// ipv6 only case
if !ipv4Enabled && ipv6Enabled && firstLease.IPv6Subnet.Equal(secondLease.IPv6Subnet) {
return true
}
// dualStack case
if ipv4Enabled && ipv6Enabled && firstLease.Subnet.Equal(secondLease.Subnet) && firstLease.IPv6Subnet.Equal(secondLease.IPv6Subnet) {
return true
}
// etcd case
if !ipv4Enabled && !ipv6Enabled && firstLease.Subnet.Equal(secondLease.Subnet) {
return true
}

return false
}
2 changes: 2 additions & 0 deletions pkg/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ type Manager interface {
// of handling "fall-behind" logic where the history window has advanced too far
// and it needs to diff the latest snapshot with its saved state and generate events
func WatchLeases(ctx context.Context, sm Manager, ownLease *lease.Lease, receiver chan []lease.Event) {

// LeaseWatcher is initiated with the Lease of the local node
lw := &lease.LeaseWatcher{
OwnLease: ownLease,
}
Expand Down