forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
113 lines (89 loc) · 2.7 KB
/
config.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2016-2018 Authors of Cilium
//
// 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 kvstore
import (
"fmt"
"sync"
"github.com/cilium/cilium/pkg/logging/logfields"
)
var (
// selectedModule is the name of the selected backend module
selectedModule string
)
// setOpts validates the specified options against the selected backend and
// then modifies the configuration
func setOpts(opts map[string]string, supportedOpts backendOptions) error {
errors := 0
for key, val := range opts {
opt, ok := supportedOpts[key]
if !ok {
errors++
log.WithField(logfields.Key, key).Error("unknown kvstore configuration key")
continue
}
if opt.validate != nil {
if err := opt.validate(val); err != nil {
log.Errorf("invalid value for key %s: %s", key, err)
errors++
}
}
}
// if errors have occurred, print the supported configuration keys to
// the log
if errors > 0 {
log.Error("Supported configuration keys:")
for key, val := range supportedOpts {
log.Errorf(" %-12s %s", key, val.description)
}
return fmt.Errorf("invalid kvstore configuration, see log for details")
}
// modify the configuration atomically after verification
for key, val := range opts {
supportedOpts[key].value = val
}
return nil
}
func getOpts(opts backendOptions) map[string]string {
result := map[string]string{}
for key, opt := range opts {
result[key] = opt.value
}
return result
}
var (
setupOnce sync.Once
)
func setup(selectedBackend string, opts map[string]string, goOpts *ExtraOptions) error {
module := getBackend(selectedBackend)
if module == nil {
return fmt.Errorf("unknown key-value store type %q. See cilium.link/err-kvstore for details", selectedBackend)
}
if err := module.setConfig(opts); err != nil {
return err
}
if err := module.setExtraConfig(goOpts); err != nil {
return err
}
selectedModule = module.getName()
return initClient(module, goOpts)
}
// Setup sets up the key-value store specified in kvStore and configures it
// with the options provided in opts
func Setup(selectedBackend string, opts map[string]string, goOpts *ExtraOptions) error {
var err error
setupOnce.Do(func() {
err = setup(selectedBackend, opts, goOpts)
})
return err
}