-
Notifications
You must be signed in to change notification settings - Fork 15
/
infrastructure.go
344 lines (293 loc) · 7.21 KB
/
infrastructure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package cke
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/cybozu-go/etcdutil"
"github.com/cybozu-go/log"
"github.com/cybozu-go/well"
vault "github.com/hashicorp/vault/api"
clientv3 "go.etcd.io/etcd/client/v3"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
var httpClient = &well.HTTPClient{
Client: &http.Client{},
}
var vaultClient atomic.Value
type certCache struct {
cert []byte
key []byte
timestamp time.Time
lifetime time.Duration
}
func (c *certCache) get(issue func() (cert, key []byte, err error)) (cert, key []byte, err error) {
now := time.Now()
if c.cert != nil {
if now.Sub(c.timestamp) < c.lifetime {
return c.cert, c.key, nil
}
}
cert, key, err = issue()
if err == nil {
c.cert = cert
c.key = key
c.timestamp = now
}
return
}
// KubeHTTP provides TLS client certificate to access kube-apiserver.
// The certificate is cached in memory in order to avoid excessive certificate issuance.
type KubeHTTP struct {
once sync.Once
cache *certCache
err error
ca string
client *well.HTTPClient
}
// Init initializes KubeHTTP.
func (k *KubeHTTP) Init(ctx context.Context, inf Infrastructure) error {
k.once.Do(func() {
cache := &certCache{
lifetime: time.Hour * 24,
}
k.cache = cache
ca, err := inf.Storage().GetCACertificate(ctx, CAKubernetes)
if err != nil {
k.err = err
return
}
k.ca = ca
cp := x509.NewCertPool()
cp.AppendCertsFromPEM([]byte(k.ca))
k.client = &well.HTTPClient{
Client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: cp,
},
},
},
}
})
return k.err
}
// CACert returns the CA certificate of kube-apiserver.
func (k *KubeHTTP) CACert() string {
return k.ca
}
// GetCert retrieves cached TLS client certificate to access kube-apiserver.
func (k *KubeHTTP) GetCert(ctx context.Context, inf Infrastructure) (cert, key []byte, err error) {
issue := func() (cert, key []byte, err error) {
c, k, e := KubernetesCA{}.IssueUserCert(ctx, inf, RoleAdmin, AdminGroup, "25h")
if e != nil {
return nil, nil, e
}
return []byte(c), []byte(k), nil
}
return k.cache.get(issue)
}
// Client returns a HTTP client to acess kube-apiserver.
func (k *KubeHTTP) Client() *well.HTTPClient {
return k.client
}
func setVaultClient(client *vault.Client) {
vaultClient.Store(client)
}
func getVaultClient() (*vault.Client, error) {
v := vaultClient.Load()
if v == nil {
return nil, errors.New("vault is not connected")
}
return v.(*vault.Client), nil
}
// Infrastructure presents an interface for infrastructure on CKE
type Infrastructure interface {
Close()
// Agent returns the agent corresponding to addr and returns nil if addr is not connected.
Agent(addr string) Agent
Engine(addr string) ContainerEngine
Vault() (*vault.Client, error)
Storage() Storage
NewEtcdClient(ctx context.Context, endpoints []string) (*clientv3.Client, error)
K8sConfig(ctx context.Context, n *Node) (*rest.Config, error)
K8sClient(ctx context.Context, n *Node) (*kubernetes.Clientset, error)
HTTPClient() *well.HTTPClient
HTTPSClient(ctx context.Context) (*well.HTTPClient, error)
ReleaseAgent(addrs string)
}
var kubeHTTP KubeHTTP
type ckeInfrastructure struct {
agents map[string]Agent
storage Storage
etcdOnce sync.Once
etcdErr error
serverCA string
etcdCert string
etcdKey string
// following fields are accessed by multiple goroutines, hence
// they need to be guarded by sync.Once.
once sync.Once
initErr error
kubeCert []byte
kubeKey []byte
}
func (i *ckeInfrastructure) init(ctx context.Context) error {
if err := kubeHTTP.Init(ctx, i); err != nil {
return err
}
i.once.Do(func() {
cert, key, err := kubeHTTP.GetCert(ctx, i)
if err != nil {
i.initErr = err
return
}
i.kubeCert = cert
i.kubeKey = key
})
return i.initErr
}
// NewInfrastructure creates a new Infrastructure instance
func NewInfrastructure(ctx context.Context, c *Cluster, s Storage) (Infrastructure, error) {
vc, err := getVaultClient()
if err != nil {
return nil, err
}
secret, err := vc.Logical().Read(SSHSecret)
if err != nil {
return nil, err
}
privkeys := secret.Data
agents := make(map[string]Agent)
defer func() {
for _, a := range agents {
a.Close()
}
}()
mu := new(sync.Mutex)
env := well.NewEnvironment(ctx)
for _, n := range c.Nodes {
node := n
env.Go(func(ctx context.Context) error {
mykey, ok := privkeys[node.Address]
if !ok {
mykey = privkeys[""]
}
if mykey == nil {
return errors.New("no ssh private key for " + node.Address)
}
a, err := SSHAgent(node, mykey.(string))
if err != nil {
log.Warn("failed to create SSHAgent for "+node.Address, map[string]interface{}{
log.FnError: err,
})
// lint:ignore nilerr Just skip adding my agent to agents.
return nil
}
mu.Lock()
agents[node.Address] = a
mu.Unlock()
return nil
})
}
env.Stop()
err = env.Wait()
if err != nil {
return nil, err
}
// This assignment of the `agent` must be placed last.
inf := &ckeInfrastructure{agents: agents, storage: s}
agents = nil
return inf, nil
}
func (i *ckeInfrastructure) Agent(addr string) Agent {
return i.agents[addr]
}
func (i *ckeInfrastructure) Engine(addr string) ContainerEngine {
return Docker(i.agents[addr])
}
func (i *ckeInfrastructure) Vault() (*vault.Client, error) {
return getVaultClient()
}
func (i *ckeInfrastructure) Storage() Storage {
return i.storage
}
func (i *ckeInfrastructure) Close() {
for _, a := range i.agents {
a.Close()
}
i.agents = nil
}
func (i *ckeInfrastructure) NewEtcdClient(ctx context.Context, endpoints []string) (*clientv3.Client, error) {
i.etcdOnce.Do(func() {
serverCA, err := i.Storage().GetCACertificate(ctx, CAServer)
if err != nil {
i.etcdErr = err
return
}
etcdCert, etcdKey, err := EtcdCA{}.IssueRoot(ctx, i)
if err != nil {
i.etcdErr = err
return
}
i.serverCA = serverCA
i.etcdCert = etcdCert
i.etcdKey = etcdKey
})
if i.etcdErr != nil {
return nil, i.etcdErr
}
cfg := &etcdutil.Config{
Endpoints: endpoints,
Timeout: etcdutil.DefaultTimeout,
TLSCA: i.serverCA,
TLSCert: i.etcdCert,
TLSKey: i.etcdKey,
}
return etcdutil.NewClient(cfg)
}
func (i *ckeInfrastructure) K8sConfig(ctx context.Context, n *Node) (*rest.Config, error) {
if err := i.init(ctx); err != nil {
return nil, err
}
return &rest.Config{
Host: "https://" + n.Address + ":6443",
TLSClientConfig: rest.TLSClientConfig{
CertData: i.kubeCert,
KeyData: i.kubeKey,
CAData: []byte(kubeHTTP.CACert()),
},
Timeout: 5 * time.Second,
}, nil
}
func (i *ckeInfrastructure) K8sClient(ctx context.Context, n *Node) (*kubernetes.Clientset, error) {
cfg, err := i.K8sConfig(ctx, n)
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(cfg)
}
func (i *ckeInfrastructure) HTTPClient() *well.HTTPClient {
return httpClient
}
func (i *ckeInfrastructure) HTTPSClient(ctx context.Context) (*well.HTTPClient, error) {
err := i.init(ctx)
if err != nil {
return nil, err
}
return kubeHTTP.Client(), nil
}
func (i *ckeInfrastructure) ReleaseAgent(addr string) {
a := i.agents[addr]
if a != nil {
delete(i.agents, addr)
go func() {
a.Close()
}()
}
}