Skip to content

Commit

Permalink
Trim duplicate code (related to default route backend storage).
Browse files Browse the repository at this point in the history
  • Loading branch information
jkh52 committed Mar 31, 2024
1 parent f97c7af commit 7442126
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 57 deletions.
15 changes: 1 addition & 14 deletions pkg/server/backend_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,7 @@ type DefaultBackendStorage struct {
// randomly pick a key from a map (in this case, the backends) in
// Golang.
agentIDs []string
// defaultRouteAgentIDs tracks the agents that have claimed the default route.
defaultRouteAgentIDs []string
random *rand.Rand
random *rand.Rand
// idTypes contains the valid identifier types for this
// DefaultBackendStorage. The DefaultBackendStorage may only tolerate certain
// types of identifiers when associating to a specific BackendManager,
Expand Down Expand Up @@ -309,9 +307,6 @@ func (s *DefaultBackendStorage) addBackend(identifier string, idType header.Iden
s.backends[identifier] = []Backend{backend}
metrics.Metrics.SetBackendCount(len(s.backends))
s.agentIDs = append(s.agentIDs, identifier)
if idType == header.DefaultRoute {
s.defaultRouteAgentIDs = append(s.defaultRouteAgentIDs, identifier)
}
}

// removeBackend removes a backend.
Expand Down Expand Up @@ -347,14 +342,6 @@ func (s *DefaultBackendStorage) removeBackend(identifier string, idType header.I
break
}
}
if idType == header.DefaultRoute {
for i := range s.defaultRouteAgentIDs {
if s.defaultRouteAgentIDs[i] == identifier {
s.defaultRouteAgentIDs = append(s.defaultRouteAgentIDs[:i], s.defaultRouteAgentIDs[i+1:]...)
break
}
}
}
}
if !found {
klog.V(1).InfoS("Could not find connection matching identifier to remove", "agentID", identifier, "idType", idType)
Expand Down
43 changes: 0 additions & 43 deletions pkg/server/backend_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,12 @@ func TestDefaultBackendManager_AddRemoveBackends(t *testing.T) {
p.RemoveBackend(backend1)
expectedBackends := make(map[string][]Backend)
expectedAgentIDs := []string{}
expectedDefaultRouteAgentIDs := []string(nil)
if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}

p = NewDefaultBackendManager()
p.AddBackend(backend1)
Expand All @@ -148,16 +144,12 @@ func TestDefaultBackendManager_AddRemoveBackends(t *testing.T) {
"agent3": {backend3},
}
expectedAgentIDs = []string{"agent1", "agent3"}
expectedDefaultRouteAgentIDs = []string(nil)
if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
}

func TestDefaultRouteBackendManager_AddRemoveBackends(t *testing.T) {
Expand All @@ -176,16 +168,12 @@ func TestDefaultRouteBackendManager_AddRemoveBackends(t *testing.T) {
p.RemoveBackend(backend1)
expectedBackends := make(map[string][]Backend)
expectedAgentIDs := []string{}
expectedDefaultRouteAgentIDs := []string{}
if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}

p = NewDefaultRouteBackendManager()
p.AddBackend(backend1)
Expand All @@ -204,17 +192,13 @@ func TestDefaultRouteBackendManager_AddRemoveBackends(t *testing.T) {
"agent3": {backend3},
}
expectedAgentIDs = []string{"agent1", "agent3"}
expectedDefaultRouteAgentIDs = []string{"agent1", "agent3"}

if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
}

func TestDestHostBackendManager_AddRemoveBackends(t *testing.T) {
Expand All @@ -233,16 +217,12 @@ func TestDestHostBackendManager_AddRemoveBackends(t *testing.T) {
p.RemoveBackend(backend1)
expectedBackends := make(map[string][]Backend)
expectedAgentIDs := []string{}
expectedDefaultRouteAgentIDs := []string(nil)
if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}

p = NewDestHostBackendManager()
p.AddBackend(backend1)
Expand All @@ -266,9 +246,6 @@ func TestDestHostBackendManager_AddRemoveBackends(t *testing.T) {
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}

p.AddBackend(backend2)
p.AddBackend(backend3)
Expand All @@ -291,17 +268,13 @@ func TestDestHostBackendManager_AddRemoveBackends(t *testing.T) {
"::",
"node2.mydomain.com",
}
expectedDefaultRouteAgentIDs = []string(nil)

if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}

p.RemoveBackend(backend2)
p.RemoveBackend(backend1)
Expand All @@ -323,9 +296,6 @@ func TestDestHostBackendManager_AddRemoveBackends(t *testing.T) {
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}

p.RemoveBackend(backend3)
expectedBackends = map[string][]Backend{}
Expand All @@ -337,9 +307,6 @@ func TestDestHostBackendManager_AddRemoveBackends(t *testing.T) {
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
}

func TestDestHostBackendManager_WithDuplicateIdents(t *testing.T) {
Expand Down Expand Up @@ -374,17 +341,13 @@ func TestDestHostBackendManager_WithDuplicateIdents(t *testing.T) {
"::",
"node2.mydomain.com",
}
expectedDefaultRouteAgentIDs := []string(nil)

if e, a := expectedBackends, p.backends; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}

p.RemoveBackend(backend1)
p.RemoveBackend(backend3)
Expand All @@ -408,9 +371,6 @@ func TestDestHostBackendManager_WithDuplicateIdents(t *testing.T) {
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}

p.RemoveBackend(backend2)
expectedBackends = map[string][]Backend{}
Expand All @@ -422,7 +382,4 @@ func TestDestHostBackendManager_WithDuplicateIdents(t *testing.T) {
if e, a := expectedAgentIDs, p.agentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := expectedDefaultRouteAgentIDs, p.defaultRouteAgentIDs; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
}

0 comments on commit 7442126

Please sign in to comment.