-
Notifications
You must be signed in to change notification settings - Fork 204
make lease label and lease namespace configurable #670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,6 +108,10 @@ type ProxyRunOptions struct { | |
|
|
||
| // Lease controller configuration | ||
| EnableLeaseController bool | ||
| // Lease Namespace | ||
| LeaseNamespace string | ||
| // Lease Labels | ||
| LeaseLabel string | ||
| } | ||
|
|
||
| func (o *ProxyRunOptions) Flags() *pflag.FlagSet { | ||
|
|
@@ -146,6 +150,8 @@ func (o *ProxyRunOptions) Flags() *pflag.FlagSet { | |
| 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.") | ||
| 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.") | ||
| flags.BoolVar(&o.EnableLeaseController, "enable-lease-controller", o.EnableLeaseController, "Enable lease controller to publish and garbage collect proxy server leases.") | ||
| flags.StringVar(&o.LeaseNamespace, "lease-namespace", o.LeaseNamespace, "The namespace where lease objects are managed by the controller.") | ||
| flags.StringVar(&o.LeaseLabel, "lease-label", o.LeaseLabel, "The labels on which the lease objects are managed.") | ||
| 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.") | ||
| flags.MarkDeprecated("warn-on-channel-limit", "This behavior is now thread safe and always on. This flag will be removed in a future release.") | ||
|
|
||
|
|
@@ -184,6 +190,9 @@ func (o *ProxyRunOptions) Print() { | |
| klog.V(1).Infof("KubeconfigBurst set to %d.\n", o.KubeconfigBurst) | ||
| klog.V(1).Infof("APIContentType set to %v.\n", o.APIContentType) | ||
| klog.V(1).Infof("ProxyStrategies set to %q.\n", o.ProxyStrategies) | ||
| klog.V(1).Infof("EnableLeaseController set to %v.\n", o.EnableLeaseController) | ||
| klog.V(1).Infof("LeaseNamespace set to %s.\n", o.LeaseNamespace) | ||
| klog.V(1).Infof("LeaseLabel set to %s.\n", o.LeaseLabel) | ||
| klog.V(1).Infof("CipherSuites set to %q.\n", o.CipherSuites) | ||
| klog.V(1).Infof("XfrChannelSize set to %d.\n", o.XfrChannelSize) | ||
| } | ||
|
|
@@ -321,6 +330,13 @@ func (o *ProxyRunOptions) Validate() error { | |
| } | ||
| } | ||
| } | ||
| // Validate labels provided. | ||
| if o.EnableLeaseController { | ||
| _, err := util.ParseLabels(o.LeaseLabel) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rather than parsing the labels more than once, would it make sense to store the result of the parsed label at this point?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't want to pollute the |
||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
@@ -361,6 +377,8 @@ func NewProxyRunOptions() *ProxyRunOptions { | |
| CipherSuites: make([]string, 0), | ||
| XfrChannelSize: 10, | ||
| EnableLeaseController: false, | ||
| LeaseNamespace: "kube-system", | ||
| LeaseLabel: "k8s-app=konnectivity-server", | ||
| } | ||
| return &o | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ParseLabels takes a comma-separated string of key-value pairs and returns a map of labels. | ||
| func ParseLabels(labelStr string) (map[string]string, error) { | ||
| labels := make(map[string]string) | ||
|
|
||
| if len(labelStr) == 0 { | ||
| return labels, fmt.Errorf("empty string provided") | ||
| } | ||
| pairs := strings.Split(labelStr, ",") | ||
|
|
||
| for _, pair := range pairs { | ||
| keyValue := strings.Split(pair, "=") | ||
| if len(keyValue) != 2 { | ||
ipochi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil, fmt.Errorf("invalid label format: %s", pair) | ||
| } | ||
| key := strings.TrimSpace(keyValue[0]) | ||
| value := strings.TrimSpace(keyValue[1]) | ||
| labels[key] = value | ||
| } | ||
| return labels, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseLabels(t *testing.T) { | ||
| testCases := []struct { | ||
| input string | ||
| expectedOutput map[string]string | ||
| shouldError bool | ||
| }{ | ||
| { | ||
| input: "app=myapp,env=prod,version=1.0", | ||
| expectedOutput: map[string]string{ | ||
| "app": "myapp", | ||
| "env": "prod", | ||
| "version": "1.0", | ||
| }, | ||
| shouldError: false, | ||
| }, | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to have a test case like "foo=var=baz" |
||
| input: "app=myapp,env=prod,invalid", | ||
| expectedOutput: nil, | ||
| shouldError: true, | ||
| }, | ||
| { | ||
| input: "app=myapp", | ||
| expectedOutput: map[string]string{ | ||
| "app": "myapp", | ||
| }, | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| input: "", | ||
| expectedOutput: map[string]string{}, | ||
| shouldError: true, | ||
| }, | ||
| { | ||
| input: " key = value , another = test ", | ||
| expectedOutput: map[string]string{ | ||
| "key": "value", | ||
| "another": "test", | ||
| }, | ||
| shouldError: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| output, err := ParseLabels(tc.input) | ||
|
|
||
| // Check for unexpected errors or missing errors | ||
| if tc.shouldError && err == nil { | ||
| t.Errorf("expected error for input %q but got none", tc.input) | ||
| continue | ||
| } | ||
| if !tc.shouldError && err != nil { | ||
| t.Errorf("did not expect error for input %q but got: %v", tc.input, err) | ||
| continue | ||
| } | ||
|
|
||
| // Compare maps if there was no error | ||
| if !tc.shouldError { | ||
| if len(output) != len(tc.expectedOutput) { | ||
| t.Errorf("for input %q, expected map length %d but got %d", tc.input, len(tc.expectedOutput), len(output)) | ||
| } | ||
| for key, expectedValue := range tc.expectedOutput { | ||
| if output[key] != expectedValue { | ||
| t.Errorf("for input %q, expected %q=%q but got %q=%q", tc.input, key, expectedValue, key, output[key]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.