k8s.io/apiserver@v0.31.1/pkg/storage/storagebackend/config.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package storagebackend
    18  
    19  import (
    20  	"time"
    21  
    22  	oteltrace "go.opentelemetry.io/otel/trace"
    23  	noopoteltrace "go.opentelemetry.io/otel/trace/noop"
    24  
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/apiserver/pkg/server/egressselector"
    28  	"k8s.io/apiserver/pkg/storage/etcd3"
    29  	"k8s.io/apiserver/pkg/storage/value"
    30  	flowcontrolrequest "k8s.io/apiserver/pkg/util/flowcontrol/request"
    31  )
    32  
    33  const (
    34  	StorageTypeUnset = ""
    35  	StorageTypeETCD2 = "etcd2"
    36  	StorageTypeETCD3 = "etcd3"
    37  
    38  	DefaultCompactInterval      = 5 * time.Minute
    39  	DefaultDBMetricPollInterval = 30 * time.Second
    40  	DefaultHealthcheckTimeout   = 2 * time.Second
    41  	DefaultReadinessTimeout     = 2 * time.Second
    42  )
    43  
    44  // TransportConfig holds all connection related info,  i.e. equal TransportConfig means equal servers we talk to.
    45  type TransportConfig struct {
    46  	// ServerList is the list of storage servers to connect with.
    47  	ServerList []string
    48  	// TLS credentials
    49  	KeyFile       string
    50  	CertFile      string
    51  	TrustedCAFile string
    52  	// function to determine the egress dialer. (i.e. konnectivity server dialer)
    53  	EgressLookup egressselector.Lookup
    54  	// The TracerProvider can add tracing the connection
    55  	TracerProvider oteltrace.TracerProvider
    56  }
    57  
    58  // Config is configuration for creating a storage backend.
    59  type Config struct {
    60  	// Type defines the type of storage backend. Default ("") is "etcd3".
    61  	Type string
    62  	// Prefix is the prefix to all keys passed to storage.Interface methods.
    63  	Prefix string
    64  	// Transport holds all connection related info, i.e. equal TransportConfig means equal servers we talk to.
    65  	Transport TransportConfig
    66  
    67  	Codec runtime.Codec
    68  	// EncodeVersioner is the same groupVersioner used to build the
    69  	// storage encoder. Given a list of kinds the input object might belong
    70  	// to, the EncodeVersioner outputs the gvk the object will be
    71  	// converted to before persisted in etcd.
    72  	EncodeVersioner runtime.GroupVersioner
    73  	// Transformer allows the value to be transformed prior to persisting into etcd.
    74  	Transformer value.Transformer
    75  
    76  	// CompactionInterval is an interval of requesting compaction from apiserver.
    77  	// If the value is 0, no compaction will be issued.
    78  	CompactionInterval time.Duration
    79  	// CountMetricPollPeriod specifies how often should count metric be updated
    80  	CountMetricPollPeriod time.Duration
    81  	// DBMetricPollInterval specifies how often should storage backend metric be updated.
    82  	DBMetricPollInterval time.Duration
    83  	// HealthcheckTimeout specifies the timeout used when checking health
    84  	HealthcheckTimeout time.Duration
    85  	// ReadycheckTimeout specifies the timeout used when checking readiness
    86  	ReadycheckTimeout time.Duration
    87  
    88  	LeaseManagerConfig etcd3.LeaseManagerConfig
    89  
    90  	// StorageObjectCountTracker is used to keep track of the total
    91  	// number of objects in the storage per resource.
    92  	StorageObjectCountTracker flowcontrolrequest.StorageObjectCountTracker
    93  }
    94  
    95  // ConfigForResource is a Config specialized to a particular `schema.GroupResource`
    96  type ConfigForResource struct {
    97  	// Config is the resource-independent configuration
    98  	Config
    99  
   100  	// GroupResource is the relevant one
   101  	GroupResource schema.GroupResource
   102  }
   103  
   104  // ForResource specializes to the given resource
   105  func (config *Config) ForResource(resource schema.GroupResource) *ConfigForResource {
   106  	return &ConfigForResource{
   107  		Config:        *config,
   108  		GroupResource: resource,
   109  	}
   110  }
   111  
   112  func NewDefaultConfig(prefix string, codec runtime.Codec) *Config {
   113  	return &Config{
   114  		Prefix:               prefix,
   115  		Codec:                codec,
   116  		CompactionInterval:   DefaultCompactInterval,
   117  		DBMetricPollInterval: DefaultDBMetricPollInterval,
   118  		HealthcheckTimeout:   DefaultHealthcheckTimeout,
   119  		ReadycheckTimeout:    DefaultReadinessTimeout,
   120  		LeaseManagerConfig:   etcd3.NewDefaultLeaseManagerConfig(),
   121  		Transport:            TransportConfig{TracerProvider: noopoteltrace.NewTracerProvider()},
   122  	}
   123  }