@@ -55,6 +55,18 @@ func (ip InitParams) OptionsThroughYAML(dest interface{}) error {
55
55
return nil
56
56
}
57
57
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
+
58
70
// backends is the internal backend registry
59
71
var (
60
72
mu sync.Mutex
@@ -70,33 +82,15 @@ func RegisterBackend(typeName string, initFunc InitFunc) {
70
82
71
83
// GetBackend creates a new backend instance of given typeName. This type must
72
84
// 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.
91
85
//
92
86
// The options map contains backend dependant key-value options. Some backends
93
87
// take no options, others require some specific options.
94
88
//
89
+ // Additional parameters can be passed with extra arguments, like WithLogger.
90
+ //
95
91
// The lifetime of the context passed in must span the lifetime of the whole
96
92
// 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 ) {
100
94
if typeName == "" {
101
95
return nil , fmt .Errorf ("no storage.type configured" )
102
96
}
@@ -106,8 +100,12 @@ func GetBackendWithParams(ctx context.Context, typeName string, params InitParam
106
100
if ! exists {
107
101
return nil , fmt .Errorf ("storage.type %q not found or registered" , typeName )
108
102
}
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 ()
111
109
}
112
- return initFunc (ctx , params )
110
+ return initFunc (ctx , p )
113
111
}
0 commit comments