github.com/m3db/m3@v1.5.0/src/cmd/services/r2ctl/config/config.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package config
    22  
    23  import (
    24  	"errors"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/cluster/client/etcd"
    28  	clusterkv "github.com/m3db/m3/src/cluster/kv"
    29  	"github.com/m3db/m3/src/ctl/auth"
    30  	r2store "github.com/m3db/m3/src/ctl/service/r2/store"
    31  	r2kv "github.com/m3db/m3/src/ctl/service/r2/store/kv"
    32  	"github.com/m3db/m3/src/ctl/service/r2/store/stub"
    33  	"github.com/m3db/m3/src/metrics/rules"
    34  	ruleskv "github.com/m3db/m3/src/metrics/rules/store/kv"
    35  	"github.com/m3db/m3/src/metrics/rules/validator"
    36  	"github.com/m3db/m3/src/x/instrument"
    37  	"github.com/m3db/m3/src/x/log"
    38  )
    39  
    40  var (
    41  	errKVConfigRequired = errors.New("must provide kv configuration if not using stub store")
    42  )
    43  
    44  // Configuration is the global configuration for r2ctl.
    45  type Configuration struct {
    46  	// Logging configuration.
    47  	Logging log.Configuration `yaml:"logging"`
    48  
    49  	// HTTP server configuration.
    50  	HTTP serverConfig `yaml:"http"`
    51  
    52  	// Metrics configuration.
    53  	Metrics instrument.MetricsConfiguration `yaml:"metrics"`
    54  
    55  	// Store configuration.
    56  	Store r2StoreConfiguration `yaml:"store"`
    57  
    58  	// Simple Auth Config.
    59  	Auth *auth.SimpleAuthConfig `yaml:"auth"`
    60  }
    61  
    62  // r2StoreConfiguration has all the fields necessary for an R2 store.
    63  type r2StoreConfiguration struct {
    64  	// Stub means use the stub store.
    65  	Stub bool `yaml:"stub"`
    66  
    67  	// KV is the configuration for the etcd backed implementation of the kv store.
    68  	KV *kvStoreConfig `yaml:"kv,omitempty"`
    69  }
    70  
    71  // NewR2Store creates a new R2 store.
    72  func (c r2StoreConfiguration) NewR2Store(instrumentOpts instrument.Options) (r2store.Store, error) {
    73  	if c.Stub {
    74  		return stub.NewStore(instrumentOpts)
    75  	}
    76  
    77  	if c.KV == nil {
    78  		return nil, errKVConfigRequired
    79  	}
    80  
    81  	return c.KV.NewStore(instrumentOpts)
    82  }
    83  
    84  // kvStoreConfig is the configuration for the KV backed implementation of the R2 store.
    85  type kvStoreConfig struct {
    86  	// KVClient configures the client for key value store.
    87  	KVClient *etcd.Configuration `yaml:"kvClient" validate:"nonzero"`
    88  
    89  	// KV configuration for the rules store.
    90  	KVConfig clusterkv.OverrideConfiguration `yaml:"kvConfig"`
    91  
    92  	// NamespacesKey is KV key associated with namespaces..
    93  	NamespacesKey string `yaml:"namespacesKey" validate:"nonzero"`
    94  
    95  	// RuleSet key format.
    96  	RuleSetKeyFmt string `yaml:"ruleSetKeyFmt" validate:"nonzero"`
    97  
    98  	// Propagation delay for rule updates.
    99  	PropagationDelay time.Duration `yaml:"propagationDelay" validate:"nonzero"`
   100  
   101  	// Validation configuration.
   102  	Validation *validator.Configuration `yaml:"validation"`
   103  }
   104  
   105  // NewStore creates a new KV backed R2 store.
   106  func (c kvStoreConfig) NewStore(instrumentOpts instrument.Options) (r2store.Store, error) {
   107  	// Create rules store.
   108  	kvClient, err := c.KVClient.NewClient(instrumentOpts)
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  	kvOpts, err := c.KVConfig.NewOverrideOptions()
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	kvStore, err := kvClient.TxnStore(kvOpts)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	var validator rules.Validator
   121  	if c.Validation != nil {
   122  		validator, err = c.Validation.NewValidator(kvClient)
   123  		if err != nil {
   124  			return nil, err
   125  		}
   126  	}
   127  	rulesStoreOpts := ruleskv.NewStoreOptions(c.NamespacesKey, c.RuleSetKeyFmt, validator)
   128  	rulesStore := ruleskv.NewStore(kvStore, rulesStoreOpts)
   129  
   130  	// Create kv store.
   131  	r2StoreOpts := r2kv.NewStoreOptions().
   132  		SetInstrumentOptions(instrumentOpts).
   133  		SetRuleUpdatePropagationDelay(c.PropagationDelay).
   134  		SetValidator(validator)
   135  	return r2kv.NewStore(rulesStore, r2StoreOpts), nil
   136  }