Skip to content

Commit 841092c

Browse files
committed
✨ clusterctl: Allow user to suppress API warnings
1 parent a6109b7 commit 841092c

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

cmd/clusterctl/client/cluster/client.go

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 1 deletion
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

Lines changed: 20 additions & 5 deletions
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())
@@ -96,11 +97,25 @@ func TestProxyGetConfig(t *testing.T) {
9697
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
9798
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())
9899

99-
proxy := newProxy(Kubeconfig{Path: configFile, Context: "management"}, InjectProxyTimeout(23*time.Second))
100+
proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, InjectProxyTimeout(23*time.Second))
100101
conf, err := proxy.GetConfig()
101102
g.Expect(err).ToNot(HaveOccurred())
102103
g.Expect(conf.Timeout.String()).To(Equal("23s"))
103104
})
105+
106+
t.Run("configure warning handler", func(t *testing.T) {
107+
g := NewWithT(t)
108+
dir, err := os.MkdirTemp("", "clusterctl")
109+
g.Expect(err).ToNot(HaveOccurred())
110+
defer os.RemoveAll(dir)
111+
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
112+
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0o600)).To(Succeed())
113+
114+
proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, InjectWarningHandler(rest.NoWarnings{}))
115+
conf, err := proxy.GetConfig()
116+
g.Expect(err).ToNot(HaveOccurred())
117+
g.Expect(conf.WarningHandler).To(Equal(rest.NoWarnings{}))
118+
})
104119
}
105120

106121
// These tests are emulating the files passed in via KUBECONFIG env var by
@@ -123,7 +138,7 @@ func TestKUBECONFIGEnvVar(t *testing.T) {
123138
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
124139
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())
125140

126-
proxy := newProxy(
141+
proxy := NewProxy(
127142
// dont't give an explicit path but rather define the file in the
128143
// configLoadingRules precedence chain.
129144
Kubeconfig{Path: "", Context: context},
@@ -151,7 +166,7 @@ func TestKUBECONFIGEnvVar(t *testing.T) {
151166
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
152167
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())
153168

154-
proxy := newProxy(
169+
proxy := NewProxy(
155170
// dont't give an explicit path but rather define the file in the
156171
// configLoadingRules precedence chain.
157172
Kubeconfig{Path: "", Context: context},
@@ -229,7 +244,7 @@ func TestProxyCurrentNamespace(t *testing.T) {
229244
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0600)).To(Succeed())
230245
}
231246

232-
proxy := newProxy(Kubeconfig{Path: configFile, Context: tt.kubeconfigContext})
247+
proxy := NewProxy(Kubeconfig{Path: configFile, Context: tt.kubeconfigContext})
233248
ns, err := proxy.CurrentNamespace()
234249
if tt.expectErr {
235250
g.Expect(err).To(HaveOccurred())

0 commit comments

Comments
 (0)