From b804f9f657bf529e07ccc4d9c721b7b22f565cf1 Mon Sep 17 00:00:00 2001 From: Andrey Viktorov Date: Tue, 12 Jan 2021 00:08:42 +0200 Subject: [PATCH] add noop persister to plugin loader Kubernetes-commit: 2dd86fe8c2cc7b655085b773bd1a06bc2ab54bbd --- rest/plugin.go | 10 ++++++++++ rest/plugin_test.go | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/rest/plugin.go b/rest/plugin.go index 33d146cd..c2b3dfc0 100644 --- a/rest/plugin.go +++ b/rest/plugin.go @@ -47,6 +47,13 @@ type AuthProviderConfigPersister interface { Persist(map[string]string) error } +type noopPersister struct{} + +func (n *noopPersister) Persist(_ map[string]string) error { + // no operation persister + return nil +} + // All registered auth provider plugins. var pluginsLock sync.Mutex var plugins = make(map[string]Factory) @@ -69,5 +76,8 @@ func GetAuthProvider(clusterAddress string, apc *clientcmdapi.AuthProviderConfig if !ok { return nil, fmt.Errorf("no Auth Provider found for name %q", apc.Name) } + if persister == nil { + persister = &noopPersister{} + } return p(clusterAddress, apc.Config, persister) } diff --git a/rest/plugin_test.go b/rest/plugin_test.go index 070e5379..de51967f 100644 --- a/rest/plugin_test.go +++ b/rest/plugin_test.go @@ -171,6 +171,28 @@ func TestAuthPluginPersist(t *testing.T) { } +func Test_WhenNilPersister_NoOpPersisterIsAssigned(t *testing.T) { + + if err := RegisterAuthProviderPlugin("anyPlugin", pluginPersistProvider); err != nil { + t.Errorf("unexpected error: failed to register 'anyPlugin': %v", err) + } + cfg := &clientcmdapi.AuthProviderConfig{ + Name: "anyPlugin", + Config: nil, + } + plugin, err := GetAuthProvider("127.0.0.1", cfg, nil) + if err != nil { + t.Errorf("unexpected error: failed to get 'anyPlugin': %v", err) + } + + anyPlugin := plugin.(*pluginPersist) + + if _, ok := anyPlugin.persister.(*noopPersister); !ok { + t.Errorf("expected to be No Operation persister") + } + +} + // emptyTransport provides an empty http.Response with an initialized header // to allow wrapping RoundTrippers to set header values. type emptyTransport struct{}