Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 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 factory |
| 18 | |
| 19 | import ( |
| 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. |
| 34 | const keepaliveTime = 30 * time.Second |
| 35 | const 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) |
| 40 | const dialTimeout = 20 * time.Second |
| 41 | |
| 42 | func 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 | } |