Skip to content

Commit ea0a0d3

Browse files
committed
make lease label and lease namesapce configurable
Signed-off-by: Imran Pochi <imranpochi@microsoft.com>
1 parent e4d5a2d commit ea0a0d3

File tree

6 files changed

+146
-6
lines changed

6 files changed

+146
-6
lines changed

cmd/agent/app/options/options.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ type GrpcProxyAgentOptions struct {
8585
// Enables updating the server count by counting the number of valid leases
8686
// matching the selector.
8787
CountServerLeases bool
88+
// Namespace where lease objects are managed.
89+
LeaseNamespace string
90+
// Labels on which lease objects are managed.
91+
LeaseLabel string
8892
// Path to kubeconfig (used by kubernetes client for lease listing)
8993
KubeconfigPath string
9094
// Content type of requests sent to apiserver.
@@ -132,6 +136,8 @@ func (o *GrpcProxyAgentOptions) Flags() *pflag.FlagSet {
132136
flags.BoolVar(&o.SyncForever, "sync-forever", o.SyncForever, "If true, the agent continues syncing, in order to support server count changes.")
133137
flags.IntVar(&o.XfrChannelSize, "xfr-channel-size", 150, "Set the size of the channel for transferring data between the agent and the proxy server.")
134138
flags.BoolVar(&o.CountServerLeases, "count-server-leases", o.CountServerLeases, "Enables lease counting system to determine the number of proxy servers to connect to.")
139+
flags.StringVar(&o.LeaseNamespace, "lease-namespace", o.LeaseNamespace, "Namespace where lease objects are managed.")
140+
flags.StringVar(&o.LeaseLabel, "lease-label", o.LeaseLabel, "The labels on which the lease objects are managed.")
135141
flags.StringVar(&o.KubeconfigPath, "kubeconfig", o.KubeconfigPath, "Path to the kubeconfig file")
136142
flags.StringVar(&o.APIContentType, "kube-api-content-type", o.APIContentType, "Content type of requests sent to apiserver.")
137143
return flags
@@ -159,6 +165,9 @@ func (o *GrpcProxyAgentOptions) Print() {
159165
klog.V(1).Infof("AgentIdentifiers set to %s.\n", util.PrettyPrintURL(o.AgentIdentifiers))
160166
klog.V(1).Infof("WarnOnChannelLimit set to %t.\n", o.WarnOnChannelLimit)
161167
klog.V(1).Infof("SyncForever set to %v.\n", o.SyncForever)
168+
klog.V(1).Infof("CountServerLeases set to %v.\n", o.CountServerLeases)
169+
klog.V(1).Infof("LeaseNamespace set to %s.\n", o.LeaseNamespace)
170+
klog.V(1).Infof("LeaseLabel set to %s.\n", o.LeaseLabel)
162171
klog.V(1).Infof("ChannelSize set to %d.\n", o.XfrChannelSize)
163172
klog.V(1).Infof("APIContentType set to %v.\n", o.APIContentType)
164173
}
@@ -216,6 +225,13 @@ func (o *GrpcProxyAgentOptions) Validate() error {
216225
return fmt.Errorf("error checking KubeconfigPath %q, got %v", o.KubeconfigPath, err)
217226
}
218227
}
228+
// Validate labels provided.
229+
if o.CountServerLeases {
230+
_, err := util.ParseLabels(o.LeaseLabel)
231+
if err != nil {
232+
return err
233+
}
234+
}
219235

220236
return nil
221237
}
@@ -263,6 +279,8 @@ func NewGrpcProxyAgentOptions() *GrpcProxyAgentOptions {
263279
SyncForever: false,
264280
XfrChannelSize: 150,
265281
CountServerLeases: false,
282+
LeaseNamespace: "kube-system",
283+
LeaseLabel: "k8s-app=konnectivity-server",
266284
KubeconfigPath: "",
267285
APIContentType: runtime.ContentTypeProtobuf,
268286
}

cmd/agent/app/server.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import (
5353

5454
const (
5555
ReadHeaderTimeout = 60 * time.Second
56-
LeaseNamespace = "kube-system"
5756
LeaseInformerResync = time.Second * 10
5857
)
5958

@@ -163,11 +162,11 @@ func (a *Agent) runProxyConnection(o *options.GrpcProxyAgentOptions, drainCh, st
163162
if err != nil {
164163
return nil, fmt.Errorf("failed to create kubernetes clientset: %v", err)
165164
}
166-
leaseInformer := agent.NewLeaseInformerWithMetrics(k8sClient, LeaseNamespace, LeaseInformerResync)
165+
leaseInformer := agent.NewLeaseInformerWithMetrics(k8sClient, o.LeaseNamespace, LeaseInformerResync)
167166
go leaseInformer.Run(stopCh)
168167
cache.WaitForCacheSync(stopCh, leaseInformer.HasSynced)
169168
leaseLister := coordinationv1lister.NewLeaseLister(leaseInformer.GetIndexer())
170-
serverLeaseSelector, _ := labels.Parse("k8s-app=konnectivity-server")
169+
serverLeaseSelector, _ := labels.Parse(o.LeaseLabel)
171170
serverLeaseCounter := agent.NewServerLeaseCounter(
172171
clock.RealClock{},
173172
leaseLister,

cmd/server/app/options/options.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ type ProxyRunOptions struct {
108108

109109
// Lease controller configuration
110110
EnableLeaseController bool
111+
// Lease Namespace
112+
LeaseNamespace string
113+
// Lease Labels
114+
LeaseLabel string
111115
}
112116

113117
func (o *ProxyRunOptions) Flags() *pflag.FlagSet {
@@ -146,6 +150,8 @@ func (o *ProxyRunOptions) Flags() *pflag.FlagSet {
146150
flags.StringSliceVar(&o.CipherSuites, "cipher-suites", o.CipherSuites, "The comma separated list of allowed cipher suites. Has no effect on TLS1.3. Empty means allow default list.")
147151
flags.IntVar(&o.XfrChannelSize, "xfr-channel-size", o.XfrChannelSize, "The size of the two KNP server channels used in server for transferring data. One channel is for data coming from the Kubernetes API Server, and the other one is for data coming from the KNP agent.")
148152
flags.BoolVar(&o.EnableLeaseController, "enable-lease-controller", o.EnableLeaseController, "Enable lease controller to publish and garbage collect proxy server leases.")
153+
flags.StringVar(&o.LeaseNamespace, "lease-namespace", o.LeaseNamespace, "The namespace where lease objects are managed by the controller.")
154+
flags.StringVar(&o.LeaseLabel, "lease-label", o.LeaseLabel, "The labels on which the lease objects are managed.")
149155
flags.Bool("warn-on-channel-limit", true, "This behavior is now thread safe and always on. This flag will be removed in a future release.")
150156
flags.MarkDeprecated("warn-on-channel-limit", "This behavior is now thread safe and always on. This flag will be removed in a future release.")
151157

@@ -184,6 +190,9 @@ func (o *ProxyRunOptions) Print() {
184190
klog.V(1).Infof("KubeconfigBurst set to %d.\n", o.KubeconfigBurst)
185191
klog.V(1).Infof("APIContentType set to %v.\n", o.APIContentType)
186192
klog.V(1).Infof("ProxyStrategies set to %q.\n", o.ProxyStrategies)
193+
klog.V(1).Infof("EnableLeaseController set to %v.\n", o.EnableLeaseController)
194+
klog.V(1).Infof("LeaseNamespace set to %s.\n", o.LeaseNamespace)
195+
klog.V(1).Infof("LeaseLabel set to %s.\n", o.LeaseLabel)
187196
klog.V(1).Infof("CipherSuites set to %q.\n", o.CipherSuites)
188197
klog.V(1).Infof("XfrChannelSize set to %d.\n", o.XfrChannelSize)
189198
}
@@ -321,6 +330,13 @@ func (o *ProxyRunOptions) Validate() error {
321330
}
322331
}
323332
}
333+
// Validate labels provided.
334+
if o.EnableLeaseController {
335+
_, err := util.ParseLabels(o.LeaseLabel)
336+
if err != nil {
337+
return err
338+
}
339+
}
324340

325341
return nil
326342
}
@@ -361,6 +377,8 @@ func NewProxyRunOptions() *ProxyRunOptions {
361377
CipherSuites: make([]string, 0),
362378
XfrChannelSize: 10,
363379
EnableLeaseController: false,
380+
LeaseNamespace: "kube-system",
381+
LeaseLabel: "k8s-app=konnectivity-server",
364382
}
365383
return &o
366384
}

cmd/server/app/server.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ const (
5757
LeaseDuration = 30 * time.Second
5858
LeaseRenewalInterval = 15 * time.Second
5959
LeaseGCInterval = 15 * time.Second
60-
LeaseNamespace = "kube-system"
6160
)
6261

6362
func NewProxyCommand(p *Proxy, o *options.ProxyRunOptions) *cobra.Command {
@@ -156,6 +155,11 @@ func (p *Proxy) Run(o *options.ProxyRunOptions, stopCh <-chan struct{}) error {
156155
}
157156
defer p.agentServer.Stop()
158157

158+
labels, err := util.ParseLabels(o.LeaseLabel)
159+
if err != nil {
160+
return err
161+
}
162+
159163
if o.EnableLeaseController {
160164
leaseController := leases.NewController(
161165
k8sClient,
@@ -164,8 +168,8 @@ func (p *Proxy) Run(o *options.ProxyRunOptions, stopCh <-chan struct{}) error {
164168
LeaseRenewalInterval,
165169
LeaseGCInterval,
166170
fmt.Sprintf("konnectivity-proxy-server-%v", o.ServerID),
167-
LeaseNamespace,
168-
map[string]string{"k8s-app": "konnectivity-server"},
171+
o.LeaseNamespace,
172+
labels,
169173
)
170174
klog.V(1).Infoln("Starting lease acquisition and garbage collection controller.")
171175
leaseController.Run(ctx)

pkg/util/util.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// ParseLabels takes a comma-separated string of key-value pairs and returns a map of labels.
9+
func ParseLabels(labelStr string) (map[string]string, error) {
10+
labels := make(map[string]string)
11+
12+
if len(labelStr) == 0 {
13+
return labels, fmt.Errorf("empty string provided")
14+
}
15+
pairs := strings.Split(labelStr, ",")
16+
17+
for _, pair := range pairs {
18+
keyValue := strings.Split(pair, "=")
19+
if len(keyValue) != 2 {
20+
return nil, fmt.Errorf("invalid label format: %s", pair)
21+
}
22+
key := strings.TrimSpace(keyValue[0])
23+
value := strings.TrimSpace(keyValue[1])
24+
labels[key] = value
25+
}
26+
return labels, nil
27+
}

pkg/util/util_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package util
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestParseLabels(t *testing.T) {
8+
testCases := []struct {
9+
input string
10+
expectedOutput map[string]string
11+
shouldError bool
12+
}{
13+
{
14+
input: "app=myapp,env=prod,version=1.0",
15+
expectedOutput: map[string]string{
16+
"app": "myapp",
17+
"env": "prod",
18+
"version": "1.0",
19+
},
20+
shouldError: false,
21+
},
22+
{
23+
input: "app=myapp,env=prod,invalid",
24+
expectedOutput: nil,
25+
shouldError: true,
26+
},
27+
{
28+
input: "app=myapp",
29+
expectedOutput: map[string]string{
30+
"app": "myapp",
31+
},
32+
shouldError: false,
33+
},
34+
{
35+
input: "",
36+
expectedOutput: map[string]string{},
37+
shouldError: true,
38+
},
39+
{
40+
input: " key = value , another = test ",
41+
expectedOutput: map[string]string{
42+
"key": "value",
43+
"another": "test",
44+
},
45+
shouldError: false,
46+
},
47+
}
48+
49+
for _, tc := range testCases {
50+
output, err := ParseLabels(tc.input)
51+
52+
// Check for unexpected errors or missing errors
53+
if tc.shouldError && err == nil {
54+
t.Errorf("expected error for input %q but got none", tc.input)
55+
continue
56+
}
57+
if !tc.shouldError && err != nil {
58+
t.Errorf("did not expect error for input %q but got: %v", tc.input, err)
59+
continue
60+
}
61+
62+
// Compare maps if there was no error
63+
if !tc.shouldError {
64+
if len(output) != len(tc.expectedOutput) {
65+
t.Errorf("for input %q, expected map length %d but got %d", tc.input, len(tc.expectedOutput), len(output))
66+
}
67+
for key, expectedValue := range tc.expectedOutput {
68+
if output[key] != expectedValue {
69+
t.Errorf("for input %q, expected %q=%q but got %q=%q", tc.input, key, expectedValue, key, output[key])
70+
}
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)