Skip to content

Commit

Permalink
wrapped ServerLeaseCounter into an interface and added a unit test fo…
Browse files Browse the repository at this point in the history
…r the ServerCount() method in clientset
  • Loading branch information
konryd committed Nov 15, 2024
1 parent db2cd97 commit 1eec50b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/agent/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
75 changes: 75 additions & 0 deletions pkg/agent/clientset_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}

}
4 changes: 4 additions & 0 deletions pkg/agent/lease_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1eec50b

Please sign in to comment.