github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/kvstore/config.go (about)

     1  // Copyright 2016-2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package kvstore
    16  
    17  import (
    18  	"fmt"
    19  	"sync"
    20  
    21  	"github.com/cilium/cilium/pkg/logging/logfields"
    22  )
    23  
    24  var (
    25  	// selectedModule is the name of the selected backend module
    26  	selectedModule string
    27  )
    28  
    29  // setOpts validates the specified options against the selected backend and
    30  // then modifies the configuration
    31  func setOpts(opts map[string]string, supportedOpts backendOptions) error {
    32  	errors := 0
    33  
    34  	for key, val := range opts {
    35  		opt, ok := supportedOpts[key]
    36  		if !ok {
    37  			errors++
    38  			log.WithField(logfields.Key, key).Error("unknown kvstore configuration key")
    39  			continue
    40  		}
    41  
    42  		if opt.validate != nil {
    43  			if err := opt.validate(val); err != nil {
    44  				log.Errorf("invalid value for key %s: %s", key, err)
    45  				errors++
    46  			}
    47  		}
    48  
    49  	}
    50  
    51  	// if errors have occurred, print the supported configuration keys to
    52  	// the log
    53  	if errors > 0 {
    54  		log.Error("Supported configuration keys:")
    55  		for key, val := range supportedOpts {
    56  			log.Errorf("  %-12s %s", key, val.description)
    57  		}
    58  
    59  		return fmt.Errorf("invalid kvstore configuration, see log for details")
    60  	}
    61  
    62  	// modify the configuration atomically after verification
    63  	for key, val := range opts {
    64  		supportedOpts[key].value = val
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func getOpts(opts backendOptions) map[string]string {
    71  	result := map[string]string{}
    72  
    73  	for key, opt := range opts {
    74  		result[key] = opt.value
    75  	}
    76  
    77  	return result
    78  }
    79  
    80  var (
    81  	setupOnce sync.Once
    82  )
    83  
    84  func setup(selectedBackend string, opts map[string]string, goOpts *ExtraOptions) error {
    85  	module := getBackend(selectedBackend)
    86  	if module == nil {
    87  		return fmt.Errorf("unknown key-value store type %q. See cilium.link/err-kvstore for details", selectedBackend)
    88  	}
    89  
    90  	if err := module.setConfig(opts); err != nil {
    91  		return err
    92  	}
    93  
    94  	if err := module.setExtraConfig(goOpts); err != nil {
    95  		return err
    96  	}
    97  
    98  	selectedModule = module.getName()
    99  
   100  	return initClient(module, goOpts)
   101  }
   102  
   103  // Setup sets up the key-value store specified in kvStore and configures it
   104  // with the options provided in opts
   105  func Setup(selectedBackend string, opts map[string]string, goOpts *ExtraOptions) error {
   106  	var err error
   107  
   108  	setupOnce.Do(func() {
   109  		err = setup(selectedBackend, opts, goOpts)
   110  	})
   111  
   112  	return err
   113  }