Skip to content

Commit

Permalink
Merge pull request #809 from tomdee/remove-reservations
Browse files Browse the repository at this point in the history
subnet/*: Remove unused reservations code
  • Loading branch information
gunjan5 authored Sep 15, 2017
2 parents 6aab545 + a4a51d5 commit 166e5ef
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 262 deletions.
124 changes: 0 additions & 124 deletions subnet/etcdv2/local_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,130 +377,6 @@ func isSubnetConfigCompat(config *Config, sn ip.IP4Net) bool {
return sn.PrefixLen == config.SubnetLen
}

func (m *LocalManager) tryAddReservation(ctx context.Context, r *Reservation) error {
attrs := &LeaseAttrs{
PublicIP: r.PublicIP,
}

_, err := m.registry.createSubnet(ctx, r.Subnet, attrs, 0)
switch {
case err == nil:
return nil

case !isErrEtcdNodeExist(err):
return err
}

// This subnet or its reservation already exists.
// Get what's there and
// - if PublicIP matches, remove the TTL make it a reservation
// - otherwise, error out
sub, asof, err := m.registry.getSubnet(ctx, r.Subnet)
switch {
case err == nil:
case isErrEtcdKeyNotFound(err):
// Subnet just got expired or was deleted
return errTryAgain
default:
return err
}

if sub.Attrs.PublicIP != r.PublicIP {
// Subnet already taken
return ErrLeaseTaken
}

// remove TTL
_, err = m.registry.updateSubnet(ctx, r.Subnet, &sub.Attrs, 0, asof)
if isErrEtcdTestFailed(err) {
return errTryAgain
}
return err
}

func (m *LocalManager) AddReservation(ctx context.Context, r *Reservation) error {
config, err := m.GetNetworkConfig(ctx)
if err != nil {
return err
}

if config.SubnetLen != r.Subnet.PrefixLen {
return fmt.Errorf("reservation subnet has mask incompatible with network config")
}

if !config.Network.Overlaps(r.Subnet) {
return fmt.Errorf("reservation subnet is outside of flannel network")
}

for i := 0; i < raceRetries; i++ {
err := m.tryAddReservation(ctx, r)
switch {
case err == nil:
return nil
case err == errTryAgain:
continue
default:
return err
}
}

return ErrNoMoreTries
}

func (m *LocalManager) tryRemoveReservation(ctx context.Context, subnet ip.IP4Net) error {
sub, asof, err := m.registry.getSubnet(ctx, subnet)
if err != nil {
return err
}

// add back the TTL
_, err = m.registry.updateSubnet(ctx, subnet, &sub.Attrs, subnetTTL, asof)
if isErrEtcdTestFailed(err) {
return errTryAgain
}
return err
}

//RemoveReservation removes the subnet by setting TTL back to subnetTTL (24hours)
func (m *LocalManager) RemoveReservation(ctx context.Context, subnet ip.IP4Net) error {
for i := 0; i < raceRetries; i++ {
err := m.tryRemoveReservation(ctx, subnet)
switch {
case err == nil:
return nil
case err == errTryAgain:
continue
default:
return err
}
}

return ErrNoMoreTries
}

func (m *LocalManager) ListReservations(ctx context.Context) ([]Reservation, error) {
subnets, _, err := m.registry.getSubnets(ctx)
if err != nil {
return nil, err
}

rsvs := []Reservation{}
for _, sub := range subnets {
// Reservations don't have TTL and so no expiration
if !sub.Expiration.IsZero() {
continue
}

r := Reservation{
Subnet: sub.Subnet,
PublicIP: sub.Attrs.PublicIP,
}
rsvs = append(rsvs, r)
}

return rsvs, nil
}

func (m *LocalManager) Name() string {
return fmt.Sprintf("Etcd Local Manager with Previous Subnet: %s", m.previousSubnet.String())
}
118 changes: 0 additions & 118 deletions subnet/etcdv2/subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,120 +322,6 @@ func TestRenewLease(t *testing.T) {
t.Fatal("Failed to find acquired lease")
}

func TestAddReservation(t *testing.T) {
msr := newDummyRegistry()
sm := NewMockManager(msr)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

r := Reservation{
Subnet: newIP4Net("10.4.3.0", 24),
PublicIP: ip.MustParseIP4("52.195.12.13"),
}
if err := sm.AddReservation(ctx, &r); err == nil {
t.Fatalf("unexpectedly added a reservation outside of configured network")
}

r.Subnet = newIP4Net("10.3.10.0", 24)
if err := sm.AddReservation(ctx, &r); err != nil {
t.Fatalf("failed to add reservation: %v", err)
}

// Add the same reservation -- should succeed
if err := sm.AddReservation(ctx, &r); err != nil {
t.Fatalf("failed to add reservation: %v", err)
}

// Add a reservation with a different public IP -- should fail
r2 := r
r2.PublicIP = ip.MustParseIP4("52.195.12.17")
if err := sm.AddReservation(ctx, &r2); err != ErrLeaseTaken {
t.Fatalf("taken add reservation returned: %v", err)
}

attrs := &LeaseAttrs{
PublicIP: r.PublicIP,
}
l, err := sm.AcquireLease(ctx, attrs)
if err != nil {
t.Fatalf("failed to acquire subnet: %v", err)
}
if !l.Subnet.Equal(r.Subnet) {
t.Fatalf("acquired subnet is not the reserved one: expected %v, got %v", r.Subnet, l.Subnet)
}
if !l.Expiration.IsZero() {
t.Fatalf("acquired lease (prev reserved) has expiration set")
}
}

func TestRemoveReservation(t *testing.T) {
msr := newDummyRegistry()
sm := NewMockManager(msr)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

r := Reservation{
Subnet: newIP4Net("10.3.10.0", 24),
PublicIP: ip.MustParseIP4("52.195.12.13"),
}
if err := sm.AddReservation(ctx, &r); err != nil {
t.Fatalf("failed to add reservation: %v", err)
}

if err := sm.RemoveReservation(ctx, r.Subnet); err != nil {
t.Fatalf("failed to remove reservation: %v", err)
}

// The node should have a TTL
sub, _, err := msr.getSubnet(ctx, r.Subnet)
if err != nil {
t.Fatalf("getSubnet failed: %v", err)
}

if sub.Expiration.IsZero() {
t.Fatalf("removed reservation resulted in no TTL")
}
}

func TestListReservations(t *testing.T) {
msr := newDummyRegistry()
sm := NewMockManager(msr)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

r1 := Reservation{
Subnet: newIP4Net("10.3.10.0", 24),
PublicIP: ip.MustParseIP4("52.195.12.13"),
}
if err := sm.AddReservation(ctx, &r1); err != nil {
t.Fatalf("failed to add reservation: %v", err)
}

r2 := Reservation{
Subnet: newIP4Net("10.3.20.0", 24),
PublicIP: ip.MustParseIP4("52.195.12.14"),
}
if err := sm.AddReservation(ctx, &r2); err != nil {
t.Fatalf("failed to add reservation: %v", err)
}

rs, err := sm.ListReservations(ctx)
if err != nil {
if len(rs) != 2 {
t.Fatalf("unexpected number of reservations, expected 2, got %v", len(rs))
}
if !resvEqual(rs[0], r1) && !resvEqual(rs[1], r1) {
t.Fatalf("reservation not found")
}
if !resvEqual(rs[0], r2) && !resvEqual(rs[1], r2) {
t.Fatalf("reservation not found")
}
}
}

func inAllocatableRange(ctx context.Context, sm Manager, ipn ip.IP4Net) bool {
cfg, err := sm.GetNetworkConfig(ctx)
if err != nil {
Expand All @@ -444,7 +330,3 @@ func inAllocatableRange(ctx context.Context, sm Manager, ipn ip.IP4Net) bool {

return ipn.IP >= cfg.SubnetMin || ipn.IP <= cfg.SubnetMax
}

func resvEqual(r1, r2 Reservation) bool {
return r1.Subnet.Equal(r2.Subnet) && r1.PublicIP == r2.PublicIP
}
12 changes: 0 additions & 12 deletions subnet/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,6 @@ func (ksm *kubeSubnetManager) WatchLease(ctx context.Context, sn ip.IP4Net, curs
return subnet.LeaseWatchResult{}, ErrUnimplemented
}

func (ksm *kubeSubnetManager) AddReservation(ctx context.Context, r *subnet.Reservation) error {
return ErrUnimplemented
}

func (ksm *kubeSubnetManager) RemoveReservation(ctx context.Context, subnet ip.IP4Net) error {
return ErrUnimplemented
}

func (ksm *kubeSubnetManager) ListReservations(ctx context.Context) ([]subnet.Reservation, error) {
return nil, ErrUnimplemented
}

func (ksm *kubeSubnetManager) Name() string {
return fmt.Sprintf("Kubernetes Subnet Manager - %s", ksm.nodeName)
}
8 changes: 0 additions & 8 deletions subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ func (l *Lease) Key() string {
return MakeSubnetKey(l.Subnet)
}

type Reservation struct {
Subnet ip.IP4Net
PublicIP ip.IP4
}

type (
EventType int

Expand Down Expand Up @@ -130,8 +125,5 @@ type Manager interface {
WatchLease(ctx context.Context, sn ip.IP4Net, cursor interface{}) (LeaseWatchResult, error)
WatchLeases(ctx context.Context, cursor interface{}) (LeaseWatchResult, error)

AddReservation(ctx context.Context, r *Reservation) error
RemoveReservation(ctx context.Context, subnet ip.IP4Net) error
ListReservations(ctx context.Context) ([]Reservation, error)
Name() string
}

0 comments on commit 166e5ef

Please sign in to comment.