blob: 06b935a8c1483c8ba452e4c996b4786a0838d881 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package factory
18
19import (
20 "context"
21 "time"
22
23 "github.com/coreos/etcd/clientv3"
24 "github.com/coreos/etcd/pkg/transport"
25
26 "k8s.io/apiserver/pkg/storage"
27 "k8s.io/apiserver/pkg/storage/etcd3"
28 "k8s.io/apiserver/pkg/storage/storagebackend"
29 "k8s.io/apiserver/pkg/storage/value"
30)
31
32// The short keepalive timeout and interval have been chosen to aggressively
33// detect a failed etcd server without introducing much overhead.
34const keepaliveTime = 30 * time.Second
35const keepaliveTimeout = 10 * time.Second
36
37// dialTimeout is the timeout for failing to establish a connection.
38// It is set to 20 seconds as times shorter than that will cause TLS connections to fail
39// on heavily loaded arm64 CPUs (issue #64649)
40const dialTimeout = 20 * time.Second
41
42func newETCD3Storage(c storagebackend.Config) (storage.Interface, DestroyFunc, error) {
43 tlsInfo := transport.TLSInfo{
44 CertFile: c.CertFile,
45 KeyFile: c.KeyFile,
46 CAFile: c.CAFile,
47 }
48 tlsConfig, err := tlsInfo.ClientConfig()
49 if err != nil {
50 return nil, nil, err
51 }
52 // NOTE: Client relies on nil tlsConfig
53 // for non-secure connections, update the implicit variable
54 if len(c.CertFile) == 0 && len(c.KeyFile) == 0 && len(c.CAFile) == 0 {
55 tlsConfig = nil
56 }
57 cfg := clientv3.Config{
58 DialTimeout: dialTimeout,
59 DialKeepAliveTime: keepaliveTime,
60 DialKeepAliveTimeout: keepaliveTimeout,
61 Endpoints: c.ServerList,
62 TLS: tlsConfig,
63 }
64 client, err := clientv3.New(cfg)
65 if err != nil {
66 return nil, nil, err
67 }
68 ctx, cancel := context.WithCancel(context.Background())
69 etcd3.StartCompactor(ctx, client, c.CompactionInterval)
70 destroyFunc := func() {
71 cancel()
72 client.Close()
73 }
74 transformer := c.Transformer
75 if transformer == nil {
76 transformer = value.IdentityTransformer
77 }
78 if c.Quorum {
79 return etcd3.New(client, c.Codec, c.Prefix, transformer, c.Paging), destroyFunc, nil
80 }
81 return etcd3.NewWithNoQuorumRead(client, c.Codec, c.Prefix, transformer, c.Paging), destroyFunc, nil
82}