add noop persister to plugin loader

Kubernetes-commit: 2dd86fe8c2cc7b655085b773bd1a06bc2ab54bbd
This commit is contained in:
Andrey Viktorov 2021-01-12 00:08:42 +02:00 committed by Kubernetes Publisher
parent f80da32721
commit b804f9f657
2 changed files with 32 additions and 0 deletions

View File

@ -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)
}

View File

@ -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{}