Skip to content

Commit 1b41692

Browse files
committed
added a flag server-count-source to control the behavior when two sources are present
1 parent 1eec50b commit 1b41692

File tree

4 files changed

+81
-23
lines changed

4 files changed

+81
-23
lines changed

cmd/agent/app/options/options.go

+11
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ type GrpcProxyAgentOptions struct {
8989
LeaseNamespace string
9090
// Labels on which lease objects are managed.
9191
LeaseLabel string
92+
// ServerCountSource describes how server counts should be combined.
93+
ServerCountSource string
9294
// Path to kubeconfig (used by kubernetes client for lease listing)
9395
KubeconfigPath string
9496
// Content type of requests sent to apiserver.
@@ -108,6 +110,7 @@ func (o *GrpcProxyAgentOptions) ClientSetConfig(dialOptions ...grpc.DialOption)
108110
WarnOnChannelLimit: o.WarnOnChannelLimit,
109111
SyncForever: o.SyncForever,
110112
XfrChannelSize: o.XfrChannelSize,
113+
ServerCountSource: o.ServerCountSource,
111114
}
112115
}
113116

@@ -138,6 +141,7 @@ func (o *GrpcProxyAgentOptions) Flags() *pflag.FlagSet {
138141
flags.BoolVar(&o.CountServerLeases, "count-server-leases", o.CountServerLeases, "Enables lease counting system to determine the number of proxy servers to connect to.")
139142
flags.StringVar(&o.LeaseNamespace, "lease-namespace", o.LeaseNamespace, "Namespace where lease objects are managed.")
140143
flags.StringVar(&o.LeaseLabel, "lease-label", o.LeaseLabel, "The labels on which the lease objects are managed.")
144+
flags.StringVar(&o.ServerCountSource, "server-count-source", o.ServerCountSource, "Defines how the server counts from lease and from server responses are combined. Possible values: 'default' to use only one source (server or leases depending on other flags), 'max' to take the larger value.")
141145
flags.StringVar(&o.KubeconfigPath, "kubeconfig", o.KubeconfigPath, "Path to the kubeconfig file")
142146
flags.StringVar(&o.APIContentType, "kube-api-content-type", o.APIContentType, "Content type of requests sent to apiserver.")
143147
return flags
@@ -168,6 +172,7 @@ func (o *GrpcProxyAgentOptions) Print() {
168172
klog.V(1).Infof("CountServerLeases set to %v.\n", o.CountServerLeases)
169173
klog.V(1).Infof("LeaseNamespace set to %s.\n", o.LeaseNamespace)
170174
klog.V(1).Infof("LeaseLabel set to %s.\n", o.LeaseLabel)
175+
klog.V(1).Infof("ServerCountSource set to %s.\n", o.ServerCountSource)
171176
klog.V(1).Infof("ChannelSize set to %d.\n", o.XfrChannelSize)
172177
klog.V(1).Infof("APIContentType set to %v.\n", o.APIContentType)
173178
}
@@ -232,6 +237,11 @@ func (o *GrpcProxyAgentOptions) Validate() error {
232237
return err
233238
}
234239
}
240+
if o.ServerCountSource != "" {
241+
if o.ServerCountSource != "default" && o.ServerCountSource != "max" {
242+
return fmt.Errorf("--server-count-source must be one of '', 'default', 'max', got %v", o.ServerCountSource)
243+
}
244+
}
235245

236246
return nil
237247
}
@@ -281,6 +291,7 @@ func NewGrpcProxyAgentOptions() *GrpcProxyAgentOptions {
281291
CountServerLeases: false,
282292
LeaseNamespace: "kube-system",
283293
LeaseLabel: "k8s-app=konnectivity-server",
294+
ServerCountSource: "default",
284295
KubeconfigPath: "",
285296
APIContentType: runtime.ContentTypeProtobuf,
286297
}

cmd/agent/app/options/options_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ func TestValidate(t *testing.T) {
156156
fieldMap: map[string]interface{}{"XfrChannelSize": -10},
157157
expected: fmt.Errorf("channel size -10 must be greater than 0"),
158158
},
159+
"ServerCountSource": {
160+
fieldMap: map[string]interface{}{"ServerCountSource": "foobar"},
161+
expected: fmt.Errorf("--server-count-source must be one of '', 'default', 'max', got foobar"),
162+
},
159163
} {
160164
t.Run(desc, func(t *testing.T) {
161165
testAgentOptions := NewGrpcProxyAgentOptions()

pkg/agent/clientset.go

+40-15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ import (
3030
"sigs.k8s.io/apiserver-network-proxy/pkg/agent/metrics"
3131
)
3232

33+
const (
34+
fromResponses = "KNP server response headers"
35+
fromLeases = "KNP lease count"
36+
fromFallback = "fallback to 1"
37+
)
38+
3339
// ClientSet consists of clients connected to each instance of an HA proxy server.
3440
type ClientSet struct {
3541
mu sync.Mutex //protects the clients.
@@ -68,6 +74,7 @@ type ClientSet struct {
6874
xfrChannelSize int
6975

7076
syncForever bool // Continue syncing (support dynamic server count).
77+
serverCountSource string
7178
}
7279

7380
func (cs *ClientSet) ClientsCount() int {
@@ -148,6 +155,7 @@ type ClientSetConfig struct {
148155
SyncForever bool
149156
XfrChannelSize int
150157
ServerLeaseCounter ServerCounter
158+
ServerCountSource string
151159
}
152160

153161
func (cc *ClientSetConfig) NewAgentClientSet(drainCh, stopCh <-chan struct{}) *ClientSet {
@@ -167,6 +175,7 @@ func (cc *ClientSetConfig) NewAgentClientSet(drainCh, stopCh <-chan struct{}) *C
167175
xfrChannelSize: cc.XfrChannelSize,
168176
stopCh: stopCh,
169177
leaseCounter: cc.ServerLeaseCounter,
178+
serverCountSource: cc.ServerCountSource,
170179
}
171180
}
172181

@@ -218,25 +227,41 @@ func (cs *ClientSet) sync() {
218227
}
219228

220229
func (cs *ClientSet) ServerCount() int {
221-
countFromLeases := 0
222-
if cs.leaseCounter != nil {
223-
countFromLeases = cs.leaseCounter.Count()
224-
}
225-
countFromResponses := cs.lastReceivedServerCount
226230

227-
serverCount := countFromLeases
228-
countSource := "KNP server lease count"
229-
if countFromResponses > serverCount {
230-
serverCount = countFromResponses
231-
countSource = "KNP server response headers"
232-
}
233-
if serverCount == 0 {
234-
serverCount = 1
235-
countSource = "fallback to 1"
231+
var serverCount int
232+
var countSourceLabel string
233+
234+
switch cs.serverCountSource {
235+
case "", "default":
236+
if cs.leaseCounter != nil {
237+
serverCount = cs.leaseCounter.Count()
238+
countSourceLabel = fromLeases
239+
} else {
240+
serverCount = cs.lastReceivedServerCount
241+
countSourceLabel = fromResponses
242+
}
243+
case "max":
244+
countFromLeases := 0
245+
if cs.leaseCounter != nil {
246+
countFromLeases = cs.leaseCounter.Count()
247+
}
248+
countFromResponses := cs.lastReceivedServerCount
249+
250+
serverCount = countFromLeases
251+
countSourceLabel = fromLeases
252+
if countFromResponses > serverCount {
253+
serverCount = countFromResponses
254+
countSourceLabel = fromResponses
255+
}
256+
if serverCount == 0 {
257+
serverCount = 1
258+
countSourceLabel = fromFallback
259+
}
260+
236261
}
237262

238263
if serverCount != cs.lastServerCount {
239-
klog.Warningf("change detected in proxy server count (was: %d, now: %d, source: %q)", cs.lastServerCount, serverCount, countSource)
264+
klog.Warningf("change detected in proxy server count (was: %d, now: %d, source: %q)", cs.lastServerCount, serverCount, countSourceLabel)
240265
cs.lastServerCount = serverCount
241266
}
242267

pkg/agent/clientset_test.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,57 @@ func (f *FakeServerCounter) Count() int {
3131
func TestServerCount(t *testing.T) {
3232
testCases := []struct{
3333
name string
34+
serverCountSource string
35+
leaseCounter ServerCounter
3436
responseCount int
35-
leaseCount int
3637
want int
3738
} {
3839
{
3940
name: "higher from response",
41+
serverCountSource: "max",
4042
responseCount: 42,
41-
leaseCount: 24,
43+
leaseCounter: &FakeServerCounter{24},
4244
want: 42,
4345
},
4446
{
4547
name: "higher from leases",
48+
serverCountSource: "max",
4649
responseCount: 3,
47-
leaseCount: 6,
50+
leaseCounter: &FakeServerCounter{6},
4851
want: 6,
4952
},
5053
{
5154
name: "both zero",
55+
serverCountSource: "max",
5256
responseCount: 0,
53-
leaseCount: 0,
57+
leaseCounter: &FakeServerCounter{0},
5458
want: 1,
5559
},
60+
61+
{
62+
name: "response picked by default when no lease counter",
63+
serverCountSource: "default",
64+
responseCount: 3,
65+
leaseCounter: nil,
66+
want: 3,
67+
},
68+
{
69+
name: "lease counter always picked when present",
70+
serverCountSource: "default",
71+
responseCount: 6,
72+
leaseCounter: &FakeServerCounter{3},
73+
want: 3,
74+
},
5675
}
5776

5877
for _, tc := range testCases {
5978
t.Run(tc.name, func(t *testing.T) {
60-
lc := &FakeServerCounter{
61-
count: tc.leaseCount,
62-
}
6379

6480
cs := &ClientSet{
6581
clients: make(map[string]*Client),
66-
leaseCounter: lc,
82+
leaseCounter: tc.leaseCounter,
83+
serverCountSource: tc.serverCountSource,
84+
6785
}
6886
cs.lastReceivedServerCount = tc.responseCount
6987
if got := cs.ServerCount(); got != tc.want {

0 commit comments

Comments
 (0)