diff --git a/subnet/etcdv2/local_manager.go b/subnet/etcdv2/local_manager.go index 2026aedff2..6b3b75962f 100644 --- a/subnet/etcdv2/local_manager.go +++ b/subnet/etcdv2/local_manager.go @@ -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()) } diff --git a/subnet/etcdv2/subnet_test.go b/subnet/etcdv2/subnet_test.go index 19412e0c0b..4a21a14b04 100644 --- a/subnet/etcdv2/subnet_test.go +++ b/subnet/etcdv2/subnet_test.go @@ -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 { @@ -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 -} diff --git a/subnet/kube/kube.go b/subnet/kube/kube.go index 04ea009940..f35b427300 100644 --- a/subnet/kube/kube.go +++ b/subnet/kube/kube.go @@ -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) } diff --git a/subnet/subnet.go b/subnet/subnet.go index 4324093762..9df072fb47 100644 --- a/subnet/subnet.go +++ b/subnet/subnet.go @@ -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 @@ -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 }