forked from vmware-tanzu/velero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_mux.go
76 lines (62 loc) · 2.42 KB
/
server_mux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Copyright 2018 the Heptio Ark contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package plugin
import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
)
// HandlerInitializer is a function that initializes and returns a new instance of one of Ark's plugin interfaces
// (ObjectStore, BlockStore, BackupItemAction, RestoreItemAction).
type HandlerInitializer func(logger logrus.FieldLogger) (interface{}, error)
// serverMux manages multiple implementations of a single plugin kind, such as pod and pvc BackupItemActions.
type serverMux struct {
kind PluginKind
initializers map[string]HandlerInitializer
handlers map[string]interface{}
serverLog logrus.FieldLogger
}
// newServerMux returns a new serverMux.
func newServerMux(logger logrus.FieldLogger) *serverMux {
return &serverMux{
initializers: make(map[string]HandlerInitializer),
handlers: make(map[string]interface{}),
serverLog: logger,
}
}
// register registers the initializer for name.
func (m *serverMux) register(name string, f HandlerInitializer) {
// TODO(ncdc): return an error on duplicate registrations for the same name.
m.initializers[name] = f
}
// names returns a list of all registered implementations.
func (m *serverMux) names() []string {
return sets.StringKeySet(m.initializers).List()
}
// getHandler returns the instance for a plugin with the given name. If an instance has already been initialized,
// that is returned. Otherwise, the instance is initialized by calling its initialization function.
func (m *serverMux) getHandler(name string) (interface{}, error) {
if instance, found := m.handlers[name]; found {
return instance, nil
}
initializer, found := m.initializers[name]
if !found {
return nil, errors.Errorf("unknown %v plugin: %s", m.kind, name)
}
instance, err := initializer(m.serverLog)
if err != nil {
return nil, err
}
m.handlers[name] = instance
return m.handlers[name], nil
}