Skip to content

Commit efbb0c9

Browse files
committed
Kubeconfig: Disable clientside ratelimtier if flowcontrol is supported
I noticed client-side ratelimiting messages from the kustomization controller for my kustomization that has a kubeconfig configured. Looking at the code, I saw that this already gets disabled in `GetConfigOrDie` if the server supports APF but not in the codepath where we construct a rest config from a custom kubeconfig, this change fixes that. Signed-off-by: Alvaro Aleman <alvaroaleman@users.noreply.github.com>
1 parent 0ce88c6 commit efbb0c9

4 files changed

Lines changed: 38 additions & 12 deletions

File tree

runtime/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func GetConfigOrDie(opts Options) *rest.Config {
7575
config := ctrl.GetConfigOrDie()
7676
enabled, err := flowcontrol.IsEnabled(context.Background(), config)
7777
if err == nil && enabled {
78-
// A negative QPS and Burst indicates that the client should not have a rate limiter.
78+
// A negative QPS indicates that the client should not have a rate limiter.
7979
// Ref: https://github.com/kubernetes/kubernetes/blob/v1.24.0/staging/src/k8s.io/client-go/rest/config.go#L354-L364
8080
config.QPS = -1
8181
config.Burst = -1

runtime/client/impersonator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (i *Impersonator) clientForKubeConfig(ctx context.Context) (rc.Client, *pol
169169
return nil, nil, err
170170
}
171171

172-
restConfig = KubeConfig(restConfig, i.kubeConfigOpts)
172+
restConfig = KubeConfig(ctx, restConfig, i.kubeConfigOpts)
173173
i.setImpersonationConfig(restConfig)
174174

175175
restMapper, err := NewDynamicRESTMapper(restConfig)

runtime/client/kubeconfig.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ limitations under the License.
1717
package client
1818

1919
import (
20+
"context"
2021
"time"
2122

23+
"github.com/fluxcd/cli-utils/pkg/flowcontrol"
2224
"github.com/spf13/pflag"
2325
"k8s.io/client-go/rest"
2426
)
@@ -67,7 +69,11 @@ func (o *KubeConfigOptions) BindFlags(fs *pflag.FlagSet) {
6769

6870
// KubeConfig sanitises a kubeconfig represented as *rest.Config using
6971
// KubeConfigOptions to inform the transformation decisions.
70-
func KubeConfig(in *rest.Config, opts KubeConfigOptions) *rest.Config {
72+
func KubeConfig(ctx context.Context, in *rest.Config, opts KubeConfigOptions) *rest.Config {
73+
return kubeconfig(ctx, in, opts, flowcontrol.IsEnabled)
74+
}
75+
76+
func kubeconfig(ctx context.Context, in *rest.Config, opts KubeConfigOptions, flowcontrolChecker func(context.Context, *rest.Config) (bool, error)) *rest.Config {
7177
var out *rest.Config
7278

7379
if in != nil {
@@ -108,5 +114,12 @@ func KubeConfig(in *rest.Config, opts KubeConfigOptions) *rest.Config {
108114
}
109115
}
110116

117+
enabled, err := flowcontrolChecker(ctx, out)
118+
if err == nil && enabled {
119+
// A negative QPS indicates that the client should not have a rate limiter.
120+
// Ref: https://github.com/kubernetes/kubernetes/blob/v1.24.0/staging/src/k8s.io/client-go/rest/config.go#L354-L364
121+
out.QPS = -1
122+
}
123+
111124
return out
112125
}

runtime/client/kubeconfig_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@ limitations under the License.
1717
package client
1818

1919
import (
20-
"reflect"
20+
"context"
2121
"testing"
2222
"time"
2323

24+
"github.com/stretchr/testify/assert"
2425
"k8s.io/client-go/rest"
2526
"k8s.io/client-go/tools/clientcmd/api"
2627
)
2728

2829
func TestKubeConfig(t *testing.T) {
2930
tests := []struct {
30-
description string
31-
in *rest.Config
32-
opts KubeConfigOptions
33-
expected *rest.Config
31+
description string
32+
in *rest.Config
33+
opts KubeConfigOptions
34+
flowControlEnabled bool
35+
expected *rest.Config
3436
}{
3537
{
3638
description: "ignore nil configs",
@@ -140,13 +142,24 @@ func TestKubeConfig(t *testing.T) {
140142
Timeout: 30 * time.Second,
141143
},
142144
},
145+
{
146+
description: "Flowcontrol enabled disables ratelimiting",
147+
in: &rest.Config{},
148+
opts: KubeConfigOptions{},
149+
flowControlEnabled: true,
150+
expected: &rest.Config{
151+
QPS: -1,
152+
Timeout: 30 * time.Second,
153+
},
154+
},
143155
}
144156

145157
for _, tt := range tests {
146-
got := KubeConfig(tt.in, tt.opts)
147-
if !reflect.DeepEqual(tt.expected, got) {
148-
t.Error(tt.description)
149-
}
158+
t.Run(tt.description, func(t *testing.T) {
159+
flowControlChecker := func(context.Context, *rest.Config) (bool, error) { return tt.flowControlEnabled, nil }
160+
got := kubeconfig(t.Context(), tt.in, tt.opts, flowControlChecker)
161+
assert.Equal(t, tt.expected, got)
162+
})
150163
}
151164
}
152165

0 commit comments

Comments
 (0)