Skip to content

Commit a08e4b4

Browse files
authored
Merge pull request #11149 from dlipovetsky/option-suppress-api-warnings
✨ clusterctl: Allow user to suppress API warnings
2 parents d4a8f10 + 142cbb2 commit a08e4b4

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

cmd/clusterctl/client/cluster/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func newClusterClient(kubeconfig Kubeconfig, configClient config.Client, options
200200

201201
// if there is an injected proxy, use it, otherwise use a default one
202202
if client.proxy == nil {
203-
client.proxy = newProxy(client.kubeconfig)
203+
client.proxy = NewProxy(client.kubeconfig)
204204
}
205205

206206
// if there is an injected repositoryClientFactory, use it, otherwise use the default one

cmd/clusterctl/client/cluster/ownergraph.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func FilterClusterObjectsWithNameFilter(s string) func(u unstructured.Unstructur
6868
// own owner references; there is no guarantee about the stability of this API. Using this test with providers may require
6969
// a custom implementation of this function, or the OwnerGraph it returns.
7070
func GetOwnerGraph(ctx context.Context, namespace, kubeconfigPath string, filterFn GetOwnerGraphFilterFunction) (OwnerGraph, error) {
71-
p := newProxy(Kubeconfig{Path: kubeconfigPath, Context: ""})
71+
p := NewProxy(Kubeconfig{Path: kubeconfigPath, Context: ""})
7272
invClient := newInventoryClient(p, nil)
7373

7474
graph := newObjectGraph(p, invClient)

cmd/clusterctl/client/cluster/proxy.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type proxy struct {
8383
kubeconfig Kubeconfig
8484
timeout time.Duration
8585
configLoadingRules *clientcmd.ClientConfigLoadingRules
86+
warningHandler rest.WarningHandler
8687
}
8788

8889
var _ Proxy = &proxy{}
@@ -155,6 +156,8 @@ func (k *proxy) GetConfig() (*rest.Config, error) {
155156
restConfig.QPS = 20
156157
restConfig.Burst = 100
157158

159+
restConfig.WarningHandler = k.warningHandler
160+
158161
return restConfig, nil
159162
}
160163

@@ -376,7 +379,15 @@ func InjectKubeconfigPaths(paths []string) ProxyOption {
376379
}
377380
}
378381

379-
func newProxy(kubeconfig Kubeconfig, opts ...ProxyOption) Proxy {
382+
// InjectWarningHandler sets the handler for warnings returned by the Kubernetes API server.
383+
func InjectWarningHandler(handler rest.WarningHandler) ProxyOption {
384+
return func(p *proxy) {
385+
p.warningHandler = handler
386+
}
387+
}
388+
389+
// NewProxy returns a proxy used for operating objects in a cluster.
390+
func NewProxy(kubeconfig Kubeconfig, opts ...ProxyOption) Proxy {
380391
// If a kubeconfig file isn't provided, find one in the standard locations.
381392
rules := clientcmd.NewDefaultClientConfigLoadingRules()
382393
if kubeconfig.Path != "" {

cmd/clusterctl/client/cluster/proxy_test.go

+45-15
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
. "github.com/onsi/gomega"
27+
"k8s.io/client-go/rest"
2728

2829
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
2930
"sigs.k8s.io/cluster-api/version"
@@ -69,7 +70,7 @@ func TestProxyGetConfig(t *testing.T) {
6970
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
7071
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0600)).To(Succeed())
7172

72-
proxy := newProxy(Kubeconfig{Path: configFile, Context: tt.context})
73+
proxy := NewProxy(Kubeconfig{Path: configFile, Context: tt.context})
7374
conf, err := proxy.GetConfig()
7475
if tt.expectErr {
7576
g.Expect(err).To(HaveOccurred())
@@ -88,18 +89,47 @@ func TestProxyGetConfig(t *testing.T) {
8889
}
8990
})
9091

91-
t.Run("configure timeout", func(t *testing.T) {
92-
g := NewWithT(t)
93-
dir, err := os.MkdirTemp("", "clusterctl")
94-
g.Expect(err).ToNot(HaveOccurred())
95-
defer os.RemoveAll(dir)
96-
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
97-
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())
92+
t.Run("configure option", func(t *testing.T) {
93+
tests := []struct {
94+
name string
95+
option ProxyOption
96+
optionTester func(t *testing.T, r *rest.Config, e error)
97+
}{
98+
{
99+
name: "timeout",
100+
option: InjectProxyTimeout(23 * time.Second),
101+
optionTester: func(t *testing.T, r *rest.Config, e error) {
102+
t.Helper()
103+
g := NewWithT(t)
104+
g.Expect(e).ToNot(HaveOccurred())
105+
g.Expect(r.Timeout.String()).To(Equal("23s"))
106+
},
107+
},
108+
{
109+
name: "warning handler",
110+
option: InjectWarningHandler(rest.NoWarnings{}),
111+
optionTester: func(t *testing.T, r *rest.Config, e error) {
112+
t.Helper()
113+
g := NewWithT(t)
114+
g.Expect(e).ToNot(HaveOccurred())
115+
g.Expect(r.WarningHandler).To(Equal(rest.NoWarnings{}))
116+
},
117+
},
118+
}
119+
for _, tt := range tests {
120+
t.Run(tt.name, func(t *testing.T) {
121+
g := NewWithT(t)
122+
dir, err := os.MkdirTemp("", "clusterctl")
123+
g.Expect(err).ToNot(HaveOccurred())
124+
defer os.RemoveAll(dir)
125+
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
126+
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())
98127

99-
proxy := newProxy(Kubeconfig{Path: configFile, Context: "management"}, InjectProxyTimeout(23*time.Second))
100-
conf, err := proxy.GetConfig()
101-
g.Expect(err).ToNot(HaveOccurred())
102-
g.Expect(conf.Timeout.String()).To(Equal("23s"))
128+
proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, tt.option)
129+
conf, err := proxy.GetConfig()
130+
tt.optionTester(t, conf, err)
131+
})
132+
}
103133
})
104134
}
105135

@@ -123,7 +153,7 @@ func TestKUBECONFIGEnvVar(t *testing.T) {
123153
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
124154
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())
125155

126-
proxy := newProxy(
156+
proxy := NewProxy(
127157
// dont't give an explicit path but rather define the file in the
128158
// configLoadingRules precedence chain.
129159
Kubeconfig{Path: "", Context: context},
@@ -151,7 +181,7 @@ func TestKUBECONFIGEnvVar(t *testing.T) {
151181
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
152182
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())
153183

154-
proxy := newProxy(
184+
proxy := NewProxy(
155185
// dont't give an explicit path but rather define the file in the
156186
// configLoadingRules precedence chain.
157187
Kubeconfig{Path: "", Context: context},
@@ -229,7 +259,7 @@ func TestProxyCurrentNamespace(t *testing.T) {
229259
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0600)).To(Succeed())
230260
}
231261

232-
proxy := newProxy(Kubeconfig{Path: configFile, Context: tt.kubeconfigContext})
262+
proxy := NewProxy(Kubeconfig{Path: configFile, Context: tt.kubeconfigContext})
233263
ns, err := proxy.CurrentNamespace()
234264
if tt.expectErr {
235265
g.Expect(err).To(HaveOccurred())

0 commit comments

Comments
 (0)