| 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 |
DefaultEventsHistoryWindow = 75 * time.Second |
| 41 |
DefaultHealthcheckTimeout = 2 * time.Second |
| 42 |
DefaultReadinessTimeout = 2 * time.Second |
| 43 |
) |
| 44 |
|
| 45 |
// TransportConfig holds all connection related info, i.e. equal TransportConfig means equal servers we talk to. |
| 46 |
type TransportConfig struct { |
| 47 |
// ServerList is the list of storage servers to connect with. |
| 48 |
ServerList []string |
| 49 |
// TLS credentials |
| 50 |
KeyFile string |
| 51 |
CertFile string |
| 52 |
TrustedCAFile string |
| 53 |
// function to determine the egress dialer. (i.e. konnectivity server dialer) |
| 54 |
EgressLookup egressselector.Lookup |
| 55 |
// The TracerProvider can add tracing the connection |
| 56 |
TracerProvider oteltrace.TracerProvider |
| 57 |
} |
| 58 |
|
| 59 |
// Config is configuration for creating a storage backend. |
| 60 |
type Config struct { |
| 61 |
// Type defines the type of storage backend. Default ("") is "etcd3". |
| 62 |
Type string |
| 63 |
// Prefix is the prefix to all keys passed to storage.Interface methods. |
| 64 |
Prefix string |
| 65 |
// Transport holds all connection related info, i.e. equal TransportConfig means equal servers we talk to. |
| 66 |
Transport TransportConfig |
| 67 |
|
| 68 |
Codec runtime.Codec |
| 69 |
// EncodeVersioner is the same groupVersioner used to build the |
| 70 |
// storage encoder. Given a list of kinds the input object might belong |
| 71 |
// to, the EncodeVersioner outputs the gvk the object will be |
| 72 |
// converted to before persisted in etcd. |
| 73 |
EncodeVersioner runtime.GroupVersioner |
| 74 |
// Transformer allows the value to be transformed prior to persisting into etcd. |
| 75 |
Transformer value.Transformer |
| 76 |
|
| 77 |
// CompactionInterval is an interval of requesting compaction from apiserver. |
| 78 |
// If the value is 0, no compaction will be issued. |
| 79 |
CompactionInterval time.Duration |
| 80 |
// CountMetricPollPeriod specifies how often should count metric be updated |
| 81 |
CountMetricPollPeriod time.Duration |
| 82 |
// DBMetricPollInterval specifies how often should storage backend metric be updated. |
| 83 |
DBMetricPollInterval time.Duration |
| 84 |
// EventsHistoryWindow specifies minimum history duration that storage is keeping. |
| 85 |
EventsHistoryWindow time.Duration |
| 86 |
// HealthcheckTimeout specifies the timeout used when checking health |
| 87 |
HealthcheckTimeout time.Duration |
| 88 |
// ReadycheckTimeout specifies the timeout used when checking readiness |
| 89 |
ReadycheckTimeout time.Duration |
| 90 |
|
| 91 |
LeaseManagerConfig etcd3.LeaseManagerConfig |
| 92 |
|
| 93 |
// StorageObjectCountTracker is used to keep track of the total |
| 94 |
// number of objects in the storage per resource. |
| 95 |
StorageObjectCountTracker flowcontrolrequest.StorageObjectCountTracker |
| 96 |
} |
| 97 |
|
| 98 |
// ConfigForResource is a Config specialized to a particular `schema.GroupResource` |
| 99 |
type ConfigForResource struct { |
| 100 |
// Config is the resource-independent configuration |
| 101 |
Config |
| 102 |
|
| 103 |
// GroupResource is the relevant one |
| 104 |
GroupResource schema.GroupResource |
| 105 |
} |
| 106 |
|
| 107 |
// ForResource specializes to the given resource |
| 108 |
func (config *Config) ForResource(resource schema.GroupResource) *ConfigForResource { |
| 109 |
return &ConfigForResource{ |
| 110 |
Config: *config, |
| 111 |
GroupResource: resource, |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func NewDefaultConfig(prefix string, codec runtime.Codec) *Config { |
| 116 |
return &Config{ |
| 117 |
Prefix: prefix, |
| 118 |
Codec: codec, |
| 119 |
CompactionInterval: DefaultCompactInterval, |
| 120 |
DBMetricPollInterval: DefaultDBMetricPollInterval, |
| 121 |
EventsHistoryWindow: DefaultEventsHistoryWindow, |
| 122 |
HealthcheckTimeout: DefaultHealthcheckTimeout, |
| 123 |
ReadycheckTimeout: DefaultReadinessTimeout, |
| 124 |
LeaseManagerConfig: etcd3.NewDefaultLeaseManagerConfig(), |
| 125 |
Transport: TransportConfig{TracerProvider: noopoteltrace.NewTracerProvider()}, |
| 126 |
} |
| 127 |
} |