Skip to content

Commit 4f0a7af

Browse files
add WithConfig function to pass custom config for new client creation
1 parent 0acab73 commit 4f0a7af

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

go.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,4 +799,4 @@ sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h6
799799
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE=
800800
sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E=
801801
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
802-
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
802+
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=

support/client.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"k8s.io/client-go/dynamic"
2424
"k8s.io/client-go/kubernetes"
2525
_ "k8s.io/client-go/plugin/pkg/client/auth"
26+
"k8s.io/client-go/rest"
2627
"k8s.io/client-go/tools/clientcmd"
2728

2829
imagev1 "github.com/openshift/client-go/image/clientset/versioned"
@@ -81,13 +82,16 @@ func (t *testClient) Dynamic() dynamic.Interface {
8182
return t.dynamic
8283
}
8384

84-
func newTestClient() (Client, error) {
85-
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
86-
clientcmd.NewDefaultClientConfigLoadingRules(),
87-
&clientcmd.ConfigOverrides{},
88-
).ClientConfig()
89-
if err != nil {
90-
return nil, err
85+
func newTestClient(cfg *rest.Config) (Client, error) {
86+
var err error
87+
if cfg == nil {
88+
cfg, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
89+
clientcmd.NewDefaultClientConfigLoadingRules(),
90+
&clientcmd.ConfigOverrides{},
91+
).ClientConfig()
92+
if err != nil {
93+
return nil, err
94+
}
9195
}
9296

9397
kubeClient, err := kubernetes.NewForConfig(cfg)

support/test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/onsi/gomega"
2727

2828
corev1 "k8s.io/api/core/v1"
29+
"k8s.io/client-go/rest"
2930
)
3031

3132
type Test interface {
@@ -54,8 +55,13 @@ func (o errorOption[T]) applyTo(to T) error {
5455
var _ Option[any] = errorOption[any](nil)
5556

5657
func With(t *testing.T) Test {
58+
return WithConfig(t, nil)
59+
}
60+
61+
func WithConfig(t *testing.T, cfg *rest.Config) Test {
5762
t.Helper()
5863
ctx := context.Background()
64+
5965
if deadline, ok := t.Deadline(); ok {
6066
withDeadline, cancel := context.WithDeadline(ctx, deadline)
6167
t.Cleanup(cancel)
@@ -66,6 +72,7 @@ func With(t *testing.T) Test {
6672
WithT: gomega.NewWithT(t),
6773
t: t,
6874
ctx: ctx,
75+
cfg: cfg,
6976
}
7077
}
7178

@@ -75,6 +82,7 @@ type T struct {
7582
// nolint: containedctx
7683
ctx context.Context
7784
client Client
85+
cfg *rest.Config
7886
outputDir string
7987
once struct {
8088
client sync.Once
@@ -94,7 +102,7 @@ func (t *T) Client() Client {
94102
t.T().Helper()
95103
t.once.client.Do(func() {
96104
if t.client == nil {
97-
c, err := newTestClient()
105+
c, err := newTestClient(t.cfg)
98106
if err != nil {
99107
t.T().Fatalf("Error creating client: %v", err)
100108
}

0 commit comments

Comments
 (0)