From 1eec50b5b6f46e8d1e61d16eb939e6c2f2d101d6 Mon Sep 17 00:00:00 2001 From: Konrad Delong Date: Fri, 15 Nov 2024 15:21:43 +0100 Subject: [PATCH] wrapped ServerLeaseCounter into an interface and added a unit test for the ServerCount() method in clientset --- pkg/agent/clientset.go | 4 +- pkg/agent/clientset_test.go | 75 +++++++++++++++++++++++++++++++++++++ pkg/agent/lease_counter.go | 4 ++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 pkg/agent/clientset_test.go diff --git a/pkg/agent/clientset.go b/pkg/agent/clientset.go index 71a5b07f4..324dd12d4 100644 --- a/pkg/agent/clientset.go +++ b/pkg/agent/clientset.go @@ -39,7 +39,7 @@ type ClientSet struct { agentID string // ID of this agent address string // proxy server address. Assuming HA proxy server - leaseCounter *ServerLeaseCounter // counts number of proxy server leases + leaseCounter ServerCounter // counts number of proxy server leases lastReceivedServerCount int // last server count received from a proxy server lastServerCount int // last server count value from either lease system or proxy server, former takes priority @@ -147,7 +147,7 @@ type ClientSetConfig struct { WarnOnChannelLimit bool SyncForever bool XfrChannelSize int - ServerLeaseCounter *ServerLeaseCounter + ServerLeaseCounter ServerCounter } func (cc *ClientSetConfig) NewAgentClientSet(drainCh, stopCh <-chan struct{}) *ClientSet { diff --git a/pkg/agent/clientset_test.go b/pkg/agent/clientset_test.go new file mode 100644 index 000000000..be9860daa --- /dev/null +++ b/pkg/agent/clientset_test.go @@ -0,0 +1,75 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package agent + +import ( + "testing" +) + +type FakeServerCounter struct { + count int +} + +func (f *FakeServerCounter) Count() int { + return f.count +} + +func TestServerCount(t *testing.T) { + testCases := []struct{ + name string + responseCount int + leaseCount int + want int + } { + { + name: "higher from response", + responseCount: 42, + leaseCount: 24, + want: 42, + }, + { + name: "higher from leases", + responseCount: 3, + leaseCount: 6, + want: 6, + }, + { + name: "both zero", + responseCount: 0, + leaseCount: 0, + want: 1, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + lc := &FakeServerCounter{ + count: tc.leaseCount, + } + + cs := &ClientSet{ + clients: make(map[string]*Client), + leaseCounter: lc, + } + cs.lastReceivedServerCount = tc.responseCount + if got := cs.ServerCount(); got != tc.want { + t.Errorf("cs.ServerCount() = %v, want: %v", got, tc.want) + } + }) + } + +} diff --git a/pkg/agent/lease_counter.go b/pkg/agent/lease_counter.go index 4175d4ffe..cdc74ce70 100644 --- a/pkg/agent/lease_counter.go +++ b/pkg/agent/lease_counter.go @@ -36,6 +36,10 @@ import ( coordinationv1lister "k8s.io/client-go/listers/coordination/v1" ) +type ServerCounter interface { + Count() int +} + // A ServerLeaseCounter counts leases in the k8s apiserver to determine the // current proxy server count. type ServerLeaseCounter struct {