Skip to content

Commit f97c7af

Browse files
authored
Merge pull request #599 from jkh52/backend-tests
Add test coverage around BackendManager and BackendStorage.
2 parents 80db82a + d1fd7dc commit f97c7af

File tree

7 files changed

+585
-109
lines changed

7 files changed

+585
-109
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ mock_gen:
6464
cat hack/go-license-header.txt proto/agent/mocks/agent_mock.go > proto/agent/mocks/agent_mock.licensed.go
6565
mv proto/agent/mocks/agent_mock.licensed.go proto/agent/mocks/agent_mock.go
6666

67+
# Unit tests with faster execution (nicer for development).
68+
.PHONY: fast-test
69+
fast-test:
70+
go test -mod=vendor -race ./...
71+
cd konnectivity-client && go test -race ./...
72+
73+
# Unit tests with fuller coverage, invoked by CI system.
6774
.PHONY: test
6875
test:
6976
go test -mod=vendor -race -covermode=atomic -coverprofile=konnectivity.out ./... && go tool cover -html=konnectivity.out -o=konnectivity.html

pkg/server/backend_manager.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ func NewBackend(conn agent.AgentService_ConnectServer) (Backend, error) {
182182
// BackendStorage is an interface to manage the storage of the backend
183183
// connections, i.e., get, add and remove
184184
type BackendStorage interface {
185-
// AddBackend adds a backend.
186-
AddBackend(identifier string, idType header.IdentifierType, backend Backend)
187-
// RemoveBackend removes a backend.
188-
RemoveBackend(identifier string, idType header.IdentifierType, backend Backend)
185+
// addBackend adds a backend.
186+
addBackend(identifier string, idType header.IdentifierType, backend Backend)
187+
// removeBackend removes a backend.
188+
removeBackend(identifier string, idType header.IdentifierType, backend Backend)
189189
// NumBackends returns the number of backends.
190190
NumBackends() int
191191
}
@@ -199,6 +199,10 @@ type BackendManager interface {
199199
// pick a backend for every tunnel session and each tunnel session may
200200
// contains multiple requests.
201201
Backend(ctx context.Context) (Backend, error)
202+
// AddBackend adds a backend.
203+
AddBackend(backend Backend)
204+
// RemoveBackend adds a backend.
205+
RemoveBackend(backend Backend)
202206
BackendStorage
203207
ReadinessManager
204208
}
@@ -215,13 +219,28 @@ func (dbm *DefaultBackendManager) Backend(_ context.Context) (Backend, error) {
215219
return dbm.DefaultBackendStorage.GetRandomBackend()
216220
}
217221

222+
func (dbm *DefaultBackendManager) AddBackend(backend Backend) {
223+
agentID := backend.GetAgentID()
224+
klog.V(5).InfoS("Add the agent to DefaultBackendManager", "agentID", agentID)
225+
dbm.addBackend(agentID, header.UID, backend)
226+
}
227+
228+
func (dbm *DefaultBackendManager) RemoveBackend(backend Backend) {
229+
agentID := backend.GetAgentID()
230+
klog.V(5).InfoS("Remove the agent from the DefaultBackendManager", "agentID", agentID)
231+
dbm.removeBackend(agentID, header.UID, backend)
232+
}
233+
218234
// DefaultBackendStorage is the default backend storage.
219235
type DefaultBackendStorage struct {
220236
mu sync.RWMutex //protects the following
221237
// A map between agentID and its grpc connections.
222238
// For a given agent, ProxyServer prefers backends[agentID][0] to send
223239
// traffic, because backends[agentID][1:] are more likely to be closed
224240
// by the agent to deduplicate connections to the same server.
241+
//
242+
// TODO: fix documentation. This is not always agentID, e.g. in
243+
// the case of DestHostBackendManager.
225244
backends map[string][]Backend
226245
// agentID is tracked in this slice to enable randomly picking an
227246
// agentID in the Backend() method. There is no reliable way to
@@ -267,8 +286,8 @@ func containIDType(idTypes []header.IdentifierType, idType header.IdentifierType
267286
return false
268287
}
269288

270-
// AddBackend adds a backend.
271-
func (s *DefaultBackendStorage) AddBackend(identifier string, idType header.IdentifierType, backend Backend) {
289+
// addBackend adds a backend.
290+
func (s *DefaultBackendStorage) addBackend(identifier string, idType header.IdentifierType, backend Backend) {
272291
if !containIDType(s.idTypes, idType) {
273292
klog.V(4).InfoS("fail to add backend", "backend", identifier, "error", &ErrWrongIDType{idType, s.idTypes})
274293
return
@@ -295,8 +314,8 @@ func (s *DefaultBackendStorage) AddBackend(identifier string, idType header.Iden
295314
}
296315
}
297316

298-
// RemoveBackend removes a backend.
299-
func (s *DefaultBackendStorage) RemoveBackend(identifier string, idType header.IdentifierType, backend Backend) {
317+
// removeBackend removes a backend.
318+
func (s *DefaultBackendStorage) removeBackend(identifier string, idType header.IdentifierType, backend Backend) {
300319
if !containIDType(s.idTypes, idType) {
301320
klog.ErrorS(&ErrWrongIDType{idType, s.idTypes}, "fail to remove backend")
302321
return

0 commit comments

Comments
 (0)