Skip to content

Commit e0b7baf

Browse files
authored
Merge pull request #57 from Colvin-Y/enhancement/kubeconfig-provider
✨ enhancement: kubeconfig provider support more params
2 parents eb0549c + 04b3442 commit e0b7baf

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

providers/kubeconfig/provider.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ import (
2929

3030
corev1 "k8s.io/api/core/v1"
3131
apierrors "k8s.io/apimachinery/pkg/api/errors"
32-
"k8s.io/apimachinery/pkg/runtime"
33-
"k8s.io/client-go/kubernetes/scheme"
32+
"k8s.io/client-go/rest"
3433
"k8s.io/client-go/tools/clientcmd"
3534

3635
ctrl "sigs.k8s.io/controller-runtime"
@@ -63,9 +62,6 @@ func New(opts Options) *Provider {
6362
if opts.KubeconfigSecretKey == "" {
6463
opts.KubeconfigSecretKey = DefaultKubeconfigSecretKey
6564
}
66-
if opts.Scheme == nil {
67-
opts.Scheme = scheme.Scheme
68-
}
6965

7066
return &Provider{
7167
opts: opts,
@@ -82,8 +78,10 @@ type Options struct {
8278
KubeconfigSecretLabel string
8379
// KubeconfigSecretKey is the key in the secret data that contains the kubeconfig.
8480
KubeconfigSecretKey string
85-
// Scheme is the scheme to use for the clusters.
86-
Scheme *runtime.Scheme
81+
// ClusterOptions is the list of options to pass to the cluster object.
82+
ClusterOptions []cluster.Option
83+
// RESTOptions is the list of options to pass to the rest client.
84+
RESTOptions []func(cfg *rest.Config) error
8785
}
8886

8987
type index struct {
@@ -259,11 +257,16 @@ func (p *Provider) createAndEngageCluster(ctx context.Context, clusterName strin
259257
return fmt.Errorf("failed to parse kubeconfig: %w", err)
260258
}
261259

260+
// Apply REST options
261+
for _, opt := range p.opts.RESTOptions {
262+
if err := opt(restConfig); err != nil {
263+
return fmt.Errorf("failed to apply REST option: %w", err)
264+
}
265+
}
266+
262267
// Create a new cluster
263268
log.Info("Creating new cluster from kubeconfig")
264-
cl, err := cluster.New(restConfig, func(o *cluster.Options) {
265-
o.Scheme = p.opts.Scheme
266-
})
269+
cl, err := cluster.New(restConfig, p.opts.ClusterOptions...)
267270
if err != nil {
268271
return fmt.Errorf("failed to create cluster: %w", err)
269272
}

providers/kubeconfig/provider_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
apierrors "k8s.io/apimachinery/pkg/api/errors"
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030
"k8s.io/apimachinery/pkg/util/runtime"
31+
"k8s.io/client-go/kubernetes/scheme"
3132
"k8s.io/client-go/rest"
3233
"k8s.io/client-go/tools/clientcmd"
3334
"k8s.io/client-go/tools/clientcmd/api"
@@ -42,6 +43,7 @@ import (
4243

4344
mcbuilder "sigs.k8s.io/multicluster-runtime/pkg/builder"
4445
mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager"
46+
mcluster "sigs.k8s.io/multicluster-runtime/pkg/multicluster"
4547
mcreconcile "sigs.k8s.io/multicluster-runtime/pkg/reconcile"
4648

4749
. "github.com/onsi/ginkgo/v2"
@@ -79,6 +81,18 @@ var _ = Describe("Provider Namespace", Ordered, func() {
7981
KubeconfigSecretLabel: kubeconfigSecretLabel,
8082
Namespace: kubeconfigSecretNamespace,
8183
KubeconfigSecretKey: kubeconfigSecretKey,
84+
RESTOptions: []func(cfg *rest.Config) error{
85+
func(cfg *rest.Config) error {
86+
cfg.QPS = 100
87+
cfg.Burst = 200
88+
return nil
89+
},
90+
},
91+
ClusterOptions: []cluster.Option{
92+
func(clusterOptions *cluster.Options) {
93+
clusterOptions.Scheme = scheme.Scheme
94+
},
95+
},
8296
})
8397

8498
By("Creating a namespace in the local cluster", func() {
@@ -432,7 +446,7 @@ var _ = Describe("Provider race condition", func() {
432446
case 1:
433447
// Concurrently get a cluster.
434448
_, err := p.Get(context.Background(), "cluster-1")
435-
Expect(err).To(Or(BeNil(), MatchError("cluster cluster-1 not found")))
449+
Expect(err).To(Or(BeNil(), MatchError(mcluster.ErrClusterNotFound)))
436450
case 2:
437451
// Concurrently list clusters.
438452
p.ListClusters()

0 commit comments

Comments
 (0)