github.com/cilium/cilium@v1.16.2/pkg/kvstore/config.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package kvstore 5 6 import ( 7 "context" 8 "fmt" 9 "sync" 10 11 "github.com/cilium/cilium/pkg/logging/logfields" 12 ) 13 14 var ( 15 // selectedModule is the name of the selected backend module 16 selectedModule string 17 ) 18 19 // setOpts validates the specified options against the selected backend and 20 // then modifies the configuration 21 func setOpts(opts map[string]string, supportedOpts backendOptions) error { 22 errors := 0 23 24 for key, val := range opts { 25 opt, ok := supportedOpts[key] 26 if !ok { 27 errors++ 28 log.WithField(logfields.Key, key).Error("unknown kvstore configuration key") 29 continue 30 } 31 32 if opt.validate != nil { 33 if err := opt.validate(val); err != nil { 34 log.WithError(err).Errorf("invalid value for key %s", key) 35 errors++ 36 } 37 } 38 39 } 40 41 // if errors have occurred, print the supported configuration keys to 42 // the log 43 if errors > 0 { 44 log.Error("Supported configuration keys:") 45 for key, val := range supportedOpts { 46 log.Errorf(" %-12s %s", key, val.description) 47 } 48 49 return fmt.Errorf("invalid kvstore configuration, see log for details") 50 } 51 52 // modify the configuration atomically after verification 53 for key, val := range opts { 54 supportedOpts[key].value = val 55 } 56 57 return nil 58 } 59 60 func getOpts(opts backendOptions) map[string]string { 61 result := map[string]string{} 62 63 for key, opt := range opts { 64 result[key] = opt.value 65 } 66 67 return result 68 } 69 70 var ( 71 setupOnce sync.Once 72 ) 73 74 func setup(ctx context.Context, selectedBackend string, opts map[string]string, goOpts *ExtraOptions) error { 75 module := getBackend(selectedBackend) 76 if module == nil { 77 return fmt.Errorf("unknown key-value store type %q. See cilium.link/err-kvstore for details", selectedBackend) 78 } 79 80 if err := module.setConfig(opts); err != nil { 81 return err 82 } 83 84 if err := module.setExtraConfig(goOpts); err != nil { 85 return err 86 } 87 88 selectedModule = module.getName() 89 90 return initClient(ctx, module, goOpts) 91 } 92 93 // Setup sets up the key-value store specified in kvStore and configures it 94 // with the options provided in opts 95 func Setup(ctx context.Context, selectedBackend string, opts map[string]string, goOpts *ExtraOptions) error { 96 var err error 97 98 setupOnce.Do(func() { 99 err = setup(ctx, selectedBackend, opts, goOpts) 100 }) 101 102 return err 103 }