Skip to content

✨ clusterctl: Allow user to suppress API warnings #11149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func newClusterClient(kubeconfig Kubeconfig, configClient config.Client, options

// if there is an injected proxy, use it, otherwise use a default one
if client.proxy == nil {
client.proxy = newProxy(client.kubeconfig)
client.proxy = NewProxy(client.kubeconfig)
}

// if there is an injected repositoryClientFactory, use it, otherwise use the default one
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/client/cluster/ownergraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func FilterClusterObjectsWithNameFilter(s string) func(u unstructured.Unstructur
// own owner references; there is no guarantee about the stability of this API. Using this test with providers may require
// a custom implementation of this function, or the OwnerGraph it returns.
func GetOwnerGraph(ctx context.Context, namespace, kubeconfigPath string, filterFn GetOwnerGraphFilterFunction) (OwnerGraph, error) {
p := newProxy(Kubeconfig{Path: kubeconfigPath, Context: ""})
p := NewProxy(Kubeconfig{Path: kubeconfigPath, Context: ""})
invClient := newInventoryClient(p, nil)

graph := newObjectGraph(p, invClient)
Expand Down
13 changes: 12 additions & 1 deletion cmd/clusterctl/client/cluster/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type proxy struct {
kubeconfig Kubeconfig
timeout time.Duration
configLoadingRules *clientcmd.ClientConfigLoadingRules
warningHandler rest.WarningHandler
}

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

restConfig.WarningHandler = k.warningHandler

return restConfig, nil
}

Expand Down Expand Up @@ -376,7 +379,15 @@ func InjectKubeconfigPaths(paths []string) ProxyOption {
}
}

func newProxy(kubeconfig Kubeconfig, opts ...ProxyOption) Proxy {
// InjectWarningHandler sets the handler for warnings returned by the Kubernetes API server.
func InjectWarningHandler(handler rest.WarningHandler) ProxyOption {
return func(p *proxy) {
p.warningHandler = handler
}
}

// NewProxy returns a proxy used for operating objects in a cluster.
func NewProxy(kubeconfig Kubeconfig, opts ...ProxyOption) Proxy {
// If a kubeconfig file isn't provided, find one in the standard locations.
rules := clientcmd.NewDefaultClientConfigLoadingRules()
if kubeconfig.Path != "" {
Expand Down
60 changes: 45 additions & 15 deletions cmd/clusterctl/client/cluster/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

. "github.com/onsi/gomega"
"k8s.io/client-go/rest"

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

proxy := newProxy(Kubeconfig{Path: configFile, Context: tt.context})
proxy := NewProxy(Kubeconfig{Path: configFile, Context: tt.context})
conf, err := proxy.GetConfig()
if tt.expectErr {
g.Expect(err).To(HaveOccurred())
Expand All @@ -88,18 +89,47 @@ func TestProxyGetConfig(t *testing.T) {
}
})

t.Run("configure timeout", func(t *testing.T) {
g := NewWithT(t)
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())
t.Run("configure option", func(t *testing.T) {
tests := []struct {
name string
option ProxyOption
optionTester func(t *testing.T, r *rest.Config, e error)
}{
{
name: "timeout",
option: InjectProxyTimeout(23 * time.Second),
optionTester: func(t *testing.T, r *rest.Config, e error) {
t.Helper()
g := NewWithT(t)
g.Expect(e).ToNot(HaveOccurred())
g.Expect(r.Timeout.String()).To(Equal("23s"))
},
},
{
name: "warning handler",
option: InjectWarningHandler(rest.NoWarnings{}),
optionTester: func(t *testing.T, r *rest.Config, e error) {
t.Helper()
g := NewWithT(t)
g.Expect(e).ToNot(HaveOccurred())
g.Expect(r.WarningHandler).To(Equal(rest.NoWarnings{}))
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())

proxy := newProxy(Kubeconfig{Path: configFile, Context: "management"}, InjectProxyTimeout(23*time.Second))
conf, err := proxy.GetConfig()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(conf.Timeout.String()).To(Equal("23s"))
proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, tt.option)
conf, err := proxy.GetConfig()
tt.optionTester(t, conf, err)
})
}
})
}

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

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

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

proxy := newProxy(Kubeconfig{Path: configFile, Context: tt.kubeconfigContext})
proxy := NewProxy(Kubeconfig{Path: configFile, Context: tt.kubeconfigContext})
ns, err := proxy.CurrentNamespace()
if tt.expectErr {
g.Expect(err).To(HaveOccurred())
Expand Down
Loading