forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
376 lines (339 loc) · 10.4 KB
/
client.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mesos
import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"sync"
"time"
log "github.com/golang/glog"
"github.com/mesos/mesos-go/detector"
mesos "github.com/mesos/mesos-go/mesosproto"
"golang.org/x/net/context"
"k8s.io/apimachinery/pkg/api/resource"
utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/kubernetes/pkg/api/v1"
)
const defaultClusterName = "mesos"
var noLeadingMasterError = errors.New("there is no current leading master available to query")
type mesosClient struct {
masterLock sync.RWMutex
master string // host:port formatted address
httpClient *http.Client
tr *http.Transport
initialMaster <-chan struct{} // signal chan, closes once an initial, non-nil master is found
state *stateCache
}
type slaveNode struct {
hostname string
kubeletRunning bool
resources *v1.NodeResources
}
type mesosState struct {
clusterName string
nodes map[string]*slaveNode // by hostname
}
type stateCache struct {
sync.Mutex
expiresAt time.Time
cached *mesosState
err error
ttl time.Duration
refill func(context.Context) (*mesosState, error)
}
// reloadCache reloads the state cache if it has expired.
func (c *stateCache) reloadCache(ctx context.Context) {
now := time.Now()
c.Lock()
defer c.Unlock()
if c.expiresAt.Before(now) {
log.V(4).Infof("Reloading cached Mesos state")
c.cached, c.err = c.refill(ctx)
c.expiresAt = now.Add(c.ttl)
} else {
log.V(4).Infof("Using cached Mesos state")
}
}
// cachedState returns the cached Mesos state.
func (c *stateCache) cachedState(ctx context.Context) (*mesosState, error) {
c.reloadCache(ctx)
return c.cached, c.err
}
// clusterName returns the cached Mesos cluster name.
func (c *stateCache) clusterName(ctx context.Context) (string, error) {
cached, err := c.cachedState(ctx)
if err != nil {
return "", err
}
return cached.clusterName, nil
}
// nodes returns the cached list of slave nodes.
func (c *stateCache) nodes(ctx context.Context) (map[string]*slaveNode, error) {
cached, err := c.cachedState(ctx)
if err != nil {
return nil, err
}
return cached.nodes, nil
}
func newMesosClient(
md detector.Master,
mesosHttpClientTimeout, stateCacheTTL time.Duration) (*mesosClient, error) {
tr := utilnet.SetTransportDefaults(&http.Transport{})
httpClient := &http.Client{
Transport: tr,
Timeout: mesosHttpClientTimeout,
}
return createMesosClient(md, httpClient, tr, stateCacheTTL)
}
func createMesosClient(
md detector.Master,
httpClient *http.Client,
tr *http.Transport,
stateCacheTTL time.Duration) (*mesosClient, error) {
initialMaster := make(chan struct{})
client := &mesosClient{
httpClient: httpClient,
tr: tr,
initialMaster: initialMaster,
state: &stateCache{
ttl: stateCacheTTL,
},
}
client.state.refill = client.pollMasterForState
first := true
if err := md.Detect(detector.OnMasterChanged(func(info *mesos.MasterInfo) {
host, port := extractMasterAddress(info)
if len(host) > 0 {
client.masterLock.Lock()
defer client.masterLock.Unlock()
client.master = fmt.Sprintf("%s:%d", host, port)
if first {
first = false
close(initialMaster)
}
}
log.Infof("cloud master changed to '%v'", client.master)
})); err != nil {
log.V(1).Infof("detector initialization failed: %v", err)
return nil, err
}
return client, nil
}
func extractMasterAddress(info *mesos.MasterInfo) (host string, port int) {
if info != nil {
host = info.GetAddress().GetHostname()
if host == "" {
host = info.GetAddress().GetIp()
}
if host != "" {
// use port from Address
port = int(info.GetAddress().GetPort())
} else {
// deprecated: get host and port directly from MasterInfo (and not Address)
host = info.GetHostname()
if host == "" {
host = unpackIPv4(info.GetIp())
}
port = int(info.GetPort())
}
}
return
}
func unpackIPv4(ip uint32) string {
octets := make([]byte, 4, 4)
binary.BigEndian.PutUint32(octets, ip)
ipv4 := net.IP(octets)
return ipv4.String()
}
// listSlaves returns a (possibly cached) map of slave nodes by hostname.
// Callers must not mutate the contents of the returned slice.
func (c *mesosClient) listSlaves(ctx context.Context) (map[string]*slaveNode, error) {
return c.state.nodes(ctx)
}
// clusterName returns a (possibly cached) cluster name.
func (c *mesosClient) clusterName(ctx context.Context) (string, error) {
return c.state.clusterName(ctx)
}
// pollMasterForState returns an array of slave nodes
func (c *mesosClient) pollMasterForState(ctx context.Context) (*mesosState, error) {
// wait for initial master detection
select {
case <-c.initialMaster: // noop
case <-ctx.Done():
return nil, ctx.Err()
}
master := func() string {
c.masterLock.RLock()
defer c.masterLock.RUnlock()
return c.master
}()
if master == "" {
return nil, noLeadingMasterError
}
//TODO(jdef) should not assume master uses http (what about https?)
var state *mesosState
successHandler := func(res *http.Response) error {
blob, err1 := ioutil.ReadAll(res.Body)
if err1 != nil {
return err1
}
log.V(3).Infof("Got mesos state, content length %v", len(blob))
state, err1 = parseMesosState(blob)
return err1
}
// thinking here is that we may get some other status codes from mesos at some point:
// - authentication
// - redirection (possibly from http to https)
// ...
for _, tt := range []struct {
uri string
handlers map[int]func(*http.Response) error
}{
{
uri: fmt.Sprintf("http://%s/state", master),
handlers: map[int]func(*http.Response) error{
200: successHandler,
},
},
{
uri: fmt.Sprintf("http://%s/state.json", master),
handlers: map[int]func(*http.Response) error{
200: successHandler,
},
},
} {
req, err := http.NewRequest("GET", tt.uri, nil)
if err != nil {
return nil, err
}
err = c.httpDo(ctx, req, func(res *http.Response, err error) error {
if err != nil {
return err
}
defer res.Body.Close()
if handler, ok := tt.handlers[res.StatusCode]; ok {
err1 := handler(res)
if err1 != nil {
return err1
}
}
// no handler for this error code, proceed to the next connection type
return nil
})
if state != nil || err != nil {
return state, err
}
}
return nil, errors.New("failed to sync with Mesos master")
}
func parseMesosState(blob []byte) (*mesosState, error) {
type State struct {
ClusterName string `json:"cluster"`
Slaves []*struct {
Id string `json:"id"` // ex: 20150106-162714-3815890698-5050-2453-S2
Pid string `json:"pid"` // ex: slave(1)@10.22.211.18:5051
Hostname string `json:"hostname"` // ex: 10.22.211.18, or slave-123.nowhere.com
Resources map[string]interface{} `json:"resources"` // ex: {"mem": 123, "ports": "[31000-3200]"}
} `json:"slaves"`
Frameworks []*struct {
Id string `json:"id"` // ex: 20151105-093752-3745622208-5050-1-0000
Pid string `json:"pid"` // ex: scheduler(1)@192.168.65.228:57124
Executors []*struct {
SlaveId string `json:"slave_id"` // ex: 20151105-093752-3745622208-5050-1-S1
ExecutorId string `json:"executor_id"` // ex: 6704d375c68fee1e_k8sm-executor
Name string `json:"name"` // ex: Kubelet-Executor
} `json:"executors"`
} `json:"frameworks"`
}
state := &State{ClusterName: defaultClusterName}
if err := json.Unmarshal(blob, state); err != nil {
return nil, err
}
executorSlaveIds := map[string]struct{}{}
for _, f := range state.Frameworks {
for _, e := range f.Executors {
// Note that this simple comparison breaks when we support more than one
// k8s instance in a cluster. At the moment this is not possible for
// a number of reasons.
// TODO(sttts): find way to detect executors of this k8s instance
if e.Name == KubernetesExecutorName {
executorSlaveIds[e.SlaveId] = struct{}{}
}
}
}
nodes := map[string]*slaveNode{} // by hostname
for _, slave := range state.Slaves {
if slave.Hostname == "" {
continue
}
node := &slaveNode{hostname: slave.Hostname}
cap := v1.ResourceList{}
if slave.Resources != nil && len(slave.Resources) > 0 {
// attempt to translate CPU (cores) and memory (MB) resources
if cpu, found := slave.Resources["cpus"]; found {
if cpuNum, ok := cpu.(float64); ok {
cap[v1.ResourceCPU] = *resource.NewQuantity(int64(cpuNum), resource.DecimalSI)
} else {
log.Warningf("unexpected slave cpu resource type %T: %v", cpu, cpu)
}
} else {
log.Warningf("slave failed to report cpu resource")
}
if mem, found := slave.Resources["mem"]; found {
if memNum, ok := mem.(float64); ok {
cap[v1.ResourceMemory] = *resource.NewQuantity(int64(memNum), resource.BinarySI)
} else {
log.Warningf("unexpected slave mem resource type %T: %v", mem, mem)
}
} else {
log.Warningf("slave failed to report mem resource")
}
}
if len(cap) > 0 {
node.resources = &v1.NodeResources{
Capacity: cap,
}
log.V(4).Infof("node %q reporting capacity %v", node.hostname, cap)
}
if _, ok := executorSlaveIds[slave.Id]; ok {
node.kubeletRunning = true
}
nodes[node.hostname] = node
}
result := &mesosState{
clusterName: state.ClusterName,
nodes: nodes,
}
return result, nil
}
type responseHandler func(*http.Response, error) error
// httpDo executes an HTTP request in the given context, canceling an ongoing request if the context
// is canceled prior to completion of the request. hacked from https://blog.golang.org/context
func (c *mesosClient) httpDo(ctx context.Context, req *http.Request, f responseHandler) error {
// Run the HTTP request in a goroutine and pass the response to f.
ch := make(chan error, 1)
go func() { ch <- f(c.httpClient.Do(req)) }()
select {
case <-ctx.Done():
c.tr.CancelRequest(req)
<-ch // Wait for f to return.
return ctx.Err()
case err := <-ch:
return err
}
}