@@ -55,6 +55,18 @@ func (ip InitParams) OptionsThroughYAML(dest interface{}) error {
5555 return nil
5656}
5757
58+ // Param is the type of extra init parameters. It is returned by
59+ // calling functional params like WithLogger.
60+ type Param func (ip * InitParams )
61+
62+ // WithLogger is a GetBackend parameter that sets the logr.Logger to use in the
63+ // backends.
64+ func WithLogger (log logr.Logger ) Param {
65+ return func (ip * InitParams ) {
66+ ip .Logger = log
67+ }
68+ }
69+
5870// backends is the internal backend registry
5971var (
6072 mu sync.Mutex
@@ -70,33 +82,15 @@ func RegisterBackend(typeName string, initFunc InitFunc) {
7082
7183// GetBackend creates a new backend instance of given typeName. This type must
7284// have been previously registered with RegisterBackend.
73- // The options map contains backend dependant key-value options. Some backends
74- // take no options, others require some specific options.
75- // The lifetime of the context passed in must span the lifetime of the whole
76- // backend instance, not just the init time, so do not set any timeout on it!
77- //
78- // Deprecated: consider switching to GetBackendWithParams
79- func GetBackend (ctx context.Context , typeName string , options map [string ]interface {}) (Interface , error ) {
80- p := InitParams {OptionMap : options }
81- return GetBackendWithParams (ctx , typeName , p )
82- }
83-
84- // GetBackendWithParams creates a new backend instance of given typeName. This type must
85- // have been previously registered with RegisterBackend.
86- // Unlike the old GetBackend, this directly accepts an InitParams struct which allows
87- // us to add more options on the future.
88- //
89- // One notable addition is the InitParams.Logger field that passes a logr.Logger
90- // to the backend.
9185//
9286// The options map contains backend dependant key-value options. Some backends
9387// take no options, others require some specific options.
9488//
89+ // Additional parameters can be passed with extra arguments, like WithLogger.
90+ //
9591// The lifetime of the context passed in must span the lifetime of the whole
9692// backend instance, not just the init time, so do not set any timeout on it!
97- // TODO: the context lifetime requirement is perhaps error prone and this does
98- // not allow setting an init timeout. Not sure what would be a good solution.
99- func GetBackendWithParams (ctx context.Context , typeName string , params InitParams ) (Interface , error ) {
93+ func GetBackend (ctx context.Context , typeName string , options OptionMap , params ... Param ) (Interface , error ) {
10094 if typeName == "" {
10195 return nil , fmt .Errorf ("no storage.type configured" )
10296 }
@@ -106,8 +100,12 @@ func GetBackendWithParams(ctx context.Context, typeName string, params InitParam
106100 if ! exists {
107101 return nil , fmt .Errorf ("storage.type %q not found or registered" , typeName )
108102 }
109- if params .Logger .GetSink () == nil {
110- params .Logger = logr .Discard ()
103+ p := InitParams {OptionMap : options }
104+ for _ , param := range params {
105+ param (& p )
106+ }
107+ if p .Logger .GetSink () == nil {
108+ p .Logger = logr .Discard ()
111109 }
112- return initFunc (ctx , params )
110+ return initFunc (ctx , p )
113111}
0 commit comments