/
agent.go
530 lines (478 loc) · 16.8 KB
/
agent.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Copyright 2014 The Serviced 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 serviced - agent implements a service that runs on a serviced node.
// It is responsible for ensuring that a particular node is running the correct
// services and reporting the state and health of those services back to the
// master serviced.
package node
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/zenoss/glog"
"github.com/control-center/serviced/commons/docker"
"github.com/control-center/serviced/commons/iptables"
coordclient "github.com/control-center/serviced/coordinator/client"
coordzk "github.com/control-center/serviced/coordinator/client/zookeeper"
"github.com/control-center/serviced/dfs/registry"
"github.com/control-center/serviced/domain/addressassignment"
"github.com/control-center/serviced/domain/service"
"github.com/control-center/serviced/domain/servicedefinition"
"github.com/control-center/serviced/proxy"
"github.com/control-center/serviced/utils"
"github.com/control-center/serviced/volume"
"github.com/control-center/serviced/zzk"
zkdocker "github.com/control-center/serviced/zzk/docker"
zkservice "github.com/control-center/serviced/zzk/service"
)
/*
glog levels:
0: important info that should always be shown
1: info that might be important for debugging
2: very verbose debug info
3: trace level info
*/
const (
dockerEndpoint = "unix:///var/run/docker.sock"
circularBufferSize = 1000
)
// HostAgent is an instance of the control center Agent.
type HostAgent struct {
ipaddress string
poolID string
master string // the connection string to the master agent
uiport string // the port to the ui (legacy was port 8787, now default 443)
rpcport string // the rpc port to serviced (default is 4979)
rpcDisableTLS bool // true of TLS should be disabled for RPC
hostID string // the hostID of the current host
dockerDNS []string // docker dns addresses
storage volume.Driver // driver supporting the application data
storageTenants []string // tenants we have mounted
mount []string // each element is in the form: dockerImage,hostPath,containerPath
currentServices map[string]*exec.Cmd // the current running services
mux *proxy.TCPMux
muxport string // the mux port to serviced (default is 22250)
useTLS bool // true if TLS should be enabled for MUX
proxyRegistry proxy.ProxyRegistry
zkClient *coordclient.Client
maxContainerAge time.Duration // maximum age for a stopped container before it is removed
virtualAddressSubnet string // subnet for virtual addresses
servicedChain *iptables.Chain // Assigned IP rule chain
controllerBinary string // Path to the controller binary
logstashURL string
dockerLogDriver string
dockerLogConfig map[string]string
pullreg registry.Registry
zkSessionTimeout int
delegateKeyFile string
tokenFile string
conntrackFlush bool
serviceCache *ServiceCache
vip VIP
}
func getZkDSN(zookeepers []string,
sessionTimeout int,
connectTimeout int,
perHostConnectDelay int,
reconnectStartDelay int,
reconnectMaxDelay int) string {
if len(zookeepers) == 0 {
zookeepers = []string{"127.0.0.1:2181"}
}
dsn := coordzk.DSN{
Servers: zookeepers,
SessionTimeout: time.Duration(sessionTimeout) * time.Second,
ConnectTimeout: time.Duration(connectTimeout) * time.Second,
PerHostConnectDelay: time.Duration(perHostConnectDelay) * time.Second,
ReconnectStartDelay: time.Duration(reconnectStartDelay) * time.Second,
ReconnectMaxDelay: time.Duration(reconnectMaxDelay) * time.Second,
}
return dsn.String()
}
type AgentOptions struct {
IPAddress string
PoolID string
Master string
UIPort string
RPCPort string
RPCDisableTLS bool // true if TLS should be disabled for RPC
DockerDNS []string
VolumesPath string
Mount []string
FSType volume.DriverType
Zookeepers []string
Mux *proxy.TCPMux
MuxPort string
UseTLS bool
DockerRegistry string
MaxContainerAge time.Duration // Maximum container age for a stopped container before being removed
VirtualAddressSubnet string
ControllerBinary string
LogstashURL string
DockerLogDriver string
DockerLogConfig map[string]string
ZKSessionTimeout int
ZKConnectTimeout int
ZKPerHostConnectDelay int
ZKReconnectStartDelay int
ZKReconnectMaxDelay int
DelegateKeyFile string
TokenFile string
ConntrackFlush bool
}
// NewHostAgent creates a new HostAgent given a connection string
func NewHostAgent(options AgentOptions, reg registry.Registry) (*HostAgent, error) {
// save off the arguments
agent := &HostAgent{}
agent.ipaddress = options.IPAddress
agent.poolID = options.PoolID
agent.master = options.Master
agent.uiport = options.UIPort
agent.rpcport = options.RPCPort
agent.rpcDisableTLS = options.RPCDisableTLS
agent.dockerDNS = options.DockerDNS
agent.mount = options.Mount
agent.mux = options.Mux
agent.muxport = options.MuxPort
agent.useTLS = options.UseTLS
agent.maxContainerAge = options.MaxContainerAge
agent.virtualAddressSubnet = options.VirtualAddressSubnet
agent.servicedChain = iptables.NewChain("SERVICED")
agent.controllerBinary = options.ControllerBinary
agent.logstashURL = options.LogstashURL
agent.dockerLogDriver = options.DockerLogDriver
agent.dockerLogConfig = options.DockerLogConfig
agent.zkSessionTimeout = options.ZKSessionTimeout
agent.delegateKeyFile = options.DelegateKeyFile
agent.tokenFile = options.TokenFile
agent.conntrackFlush = options.ConntrackFlush
agent.serviceCache = NewServiceCache(options.Master)
var err error
dsn := getZkDSN(options.Zookeepers,
agent.zkSessionTimeout,
options.ZKConnectTimeout,
options.ZKPerHostConnectDelay,
options.ZKReconnectStartDelay,
options.ZKReconnectMaxDelay)
if agent.zkClient, err = coordclient.New("zookeeper", dsn, "", nil); err != nil {
return nil, err
}
if agent.storage, err = volume.GetDriver(options.VolumesPath); err != nil {
glog.Errorf("Could not load storage driver at %s: %s", options.VolumesPath, err)
return nil, err
}
if agent.hostID, err = utils.HostID(); err != nil {
panic("Could not get hostid")
}
agent.currentServices = make(map[string]*exec.Cmd)
agent.proxyRegistry = proxy.NewDefaultProxyRegistry()
agent.pullreg = reg
agent.vip = NewVirtualIPManager("cc")
return agent, err
}
// SetVIP allows you to update the default vip (primarily for testing)
func (a *HostAgent) SetVIP(v VIP) {
a.vip = v
}
func attachAndRun(dockerID, command string) ([]byte, error) {
if dockerID == "" {
return nil, errors.New("missing docker ID")
} else if command == "" {
return nil, nil
}
output, err := utils.AttachAndRun(dockerID, []string{command})
if err != nil {
err = fmt.Errorf("%s (%s)", string(output), err)
glog.Errorf("Could not pause container %s: %s", dockerID, err)
return nil, err
}
return output, nil
}
/*
writeConfFile is responsible for writing contents out to a file
Input string prefix : cp_cd67c62b-e462-5137-2cd8-38732db4abd9_zenmodeler_logstash_forwarder_conf_
Input string id : Service ID (example cd67c62b-e462-5137-2cd8-38732db4abd9)
Input string filename: zenmodeler_logstash_forwarder_conf
Input string content : the content that you wish to write to a file
Output *os.File f : file handler to the file that you've just opened and written the content to
Example name of file that is written: /tmp/cp_cd67c62b-e462-5137-2cd8-38732db4abd9_zenmodeler_logstash_forwarder_conf_592084261
*/
func writeConfFile(prefix string, id string, filename string, content string) (*os.File, error) {
f, err := ioutil.TempFile("", prefix)
if err != nil {
glog.Errorf("Could not generate tempfile for config %s %s", id, filename)
return f, err
}
_, err = f.WriteString(content)
if err != nil {
glog.Errorf("Could not write out config file %s %s", id, filename)
return f, err
}
return f, nil
}
// chownConfFile() runs 'chown $owner $filename && chmod $permissions $filename'
// using the given dockerImage. An error is returned if owner is not specified,
// the owner is not in user:group format, or if there was a problem setting
// the permissions.
func chownConfFile(filename, owner, permissions string, dockerImage string) error {
// TODO: reach in to the dockerImage and get the effective UID, GID so we can do this without a bind mount
if !IsValidOwnerSpec(owner) {
return fmt.Errorf("unsupported owner specification: %s", owner)
}
uid, gid, err := getInternalImageIDs(owner, dockerImage)
if err != nil {
return err
}
// this will fail if we are not running as root
if err := os.Chown(filename, uid, gid); err != nil {
return err
}
octal, err := strconv.ParseInt(permissions, 8, 32)
if err != nil {
return err
}
if err := os.Chmod(filename, os.FileMode(octal)); err != nil {
return err
}
return nil
}
func manageTransparentProxy(endpoint *service.ServiceEndpoint, addressConfig *addressassignment.AddressAssignment, ctr *docker.Container, isDelete bool) error {
var appendOrDeleteFlag string
if isDelete {
appendOrDeleteFlag = "-D"
} else {
appendOrDeleteFlag = "-A"
}
return exec.Command(
"iptables",
"-t", "nat",
appendOrDeleteFlag, "PREROUTING",
"-d", fmt.Sprintf("%s", addressConfig.IPAddr),
"-p", endpoint.Protocol,
"--dport", fmt.Sprintf("%d", addressConfig.Port),
"-j", "DNAT",
"--to-destination", fmt.Sprintf("%s:%d", ctr.NetworkSettings.IPAddress, endpoint.PortNumber),
).Run()
}
// setupVolume
func (a *HostAgent) setupVolume(tenantID string, service *service.Service, volume servicedefinition.Volume) (string, error) {
glog.V(4).Infof("setupVolume for service Name:%s ID:%s", service.Name, service.ID)
vol, err := a.storage.Get(tenantID)
if err != nil {
return "", fmt.Errorf("could not get subvolume %s: %s", tenantID, err)
}
a.addStorageTenant(tenantID)
resourcePath := filepath.Join(vol.Path(), volume.ResourcePath)
if err = os.MkdirAll(resourcePath, 0770); err != nil && !os.IsExist(err) {
return "", fmt.Errorf("Could not create resource path: %s, %s", resourcePath, err)
}
conn, err := zzk.GetLocalConnection("/")
if err != nil {
return "", fmt.Errorf("Could not get zk connection for resource path: %s, %s", resourcePath, err)
}
containerPath := volume.InitContainerPath
if len(strings.TrimSpace(containerPath)) == 0 {
containerPath = volume.ContainerPath
}
image, err := a.pullreg.ImagePath(service.ImageID)
if err != nil {
glog.Errorf("Could not get registry image for %s: %s", service.ImageID, err)
return "", err
}
if err := CreateVolumeDir(conn, resourcePath, containerPath, image, volume.Owner, volume.Permission); err != nil {
glog.Errorf("Error populating resource path: %s with container path: %s, %v", resourcePath, containerPath, err)
return "", err
}
glog.V(4).Infof("resourcePath: %s containerPath: %s", resourcePath, containerPath)
return resourcePath, nil
}
// main loop of the HostAgent
func (a *HostAgent) Start(shutdown <-chan interface{}) {
glog.Info("Starting HostAgent")
// CC-1991: Unmount NFS on agent shutdown
if a.storage.DriverType() == volume.DriverTypeNFS {
defer a.releaseStorageTenants()
}
var wg sync.WaitGroup
defer func() {
glog.Info("Waiting for agent routines...")
wg.Wait()
glog.Info("Agent routines ended")
}()
wg.Add(1)
go func() {
glog.Infof("Starting TTL for old Docker containers")
docker.RunTTL(shutdown, time.Minute, a.maxContainerAge)
glog.Info("Docker TTL done")
wg.Done()
}()
// run the virtual ip listener
wg.Add(1)
go func() {
glog.Infof("Starting virtual ip listener")
var addrs []string
for _, ip := range a.vip.GetAll() {
addrs = append(addrs, ip.Addr)
}
l := zkservice.NewHostIPListener(a.hostID, a, addrs)
zzk.Manage(shutdown, zzk.GeneratePoolPath(a.poolID), l)
glog.Infof("Virtual ip listener done")
wg.Done()
}()
// Increase the number of maximal tracked connections for iptables
maxConnections := "655360"
if cnxns := strings.TrimSpace(os.Getenv("SERVICED_IPTABLES_MAX_CONNECTIONS")); cnxns != "" {
maxConnections = cnxns
}
glog.Infof("Set sysctl maximum tracked connections for iptables to %s", maxConnections)
utils.SetSysctl("net.netfilter.nf_conntrack_max", maxConnections)
// Clean up any extant iptables chain, just in case
a.servicedChain.Remove()
// Add our chain for assigned IP rules
if err := a.servicedChain.Inject(); err != nil {
glog.Errorf("Error creating SERVICED iptables chain (%v)", err)
}
// Clean up when we're done
defer a.servicedChain.Remove()
unregister := make(chan interface{})
stop := make(chan interface{})
// Create the host state listener here, so it keeps its state
hsListener := zkservice.NewHostStateListener(a, a.hostID, shutdown)
for {
// handle shutdown if we are waiting for a zk connection
var conn coordclient.Connection
select {
case conn = <-zzk.Connect(zzk.GeneratePoolPath(a.poolID), zzk.GetLocalConnection):
case <-shutdown:
return
}
if conn == nil {
continue
}
glog.Info("Got a connected client")
rwg := &sync.WaitGroup{}
rwg.Add(1)
go func() {
defer rwg.Done()
t := time.NewTimer(time.Second)
defer t.Stop()
for {
err := zkservice.RegisterHost(unregister, conn, a.hostID)
if err != nil {
t.Stop()
t = time.NewTimer(time.Second)
select {
case <-t.C:
case <-unregister:
return
}
} else {
return
}
}
}()
// watch docker action nodes
actionListener := zkdocker.NewActionListener(a, a.hostID)
// watch the host state nodes
// this blocks until
// 1) has a connection
// 2) its node is registered
// 3) receives signal to shutdown or breaks
startExit := make(chan struct{})
go func() {
defer close(startExit)
glog.Infof("Host Agent successfully started")
zzk.Start(stop, conn, hsListener, actionListener)
}()
select {
case <-startExit:
glog.Infof("Host Agent restarting")
close(unregister)
rwg.Wait()
// The unregister var is used directly in the goroutine that calls
// RegisterHost above. Always wait for rwg before re-assigning it.
unregister = make(chan interface{})
case <-shutdown:
glog.Infof("Host Agent shutting down")
lockpth := path.Join("/hosts", a.hostID, "locked")
err := conn.CreateIfExists(lockpth, &coordclient.Dir{})
if err == nil || err == coordclient.ErrNodeExists {
mu, _ := conn.NewLock(lockpth)
if mu != nil {
mu.Lock()
defer mu.Unlock()
}
}
close(stop)
<-startExit
close(unregister)
rwg.Wait()
conn.Delete(path.Join("/hosts", a.hostID, "online"))
return
}
}
}
// AttachAndRun implements zkdocker.ActionHandler; it attaches to a running
// container and performs a command as specified by the container's service
// definition
func (a *HostAgent) AttachAndRun(dockerID string, command []string) ([]byte, error) {
return utils.AttachAndRun(dockerID, command)
}
// addStorageTenant remembers a storage tenant we have used
func (a *HostAgent) addStorageTenant(tenantID string) {
for _, tid := range a.storageTenants {
if tid == tenantID {
return
}
}
a.storageTenants = append(a.storageTenants, tenantID)
}
// releaseStorageTenants releases the resources for each tenant we have used
func (a *HostAgent) releaseStorageTenants() {
for _, tenantID := range a.storageTenants {
if err := a.storage.Release(tenantID); err != nil {
glog.Warningf("Could not release tenant %s: %s", tenantID, err)
}
}
}
type stateResult struct {
id string
err error
}
func waitForSsNodes(processing map[string]chan int, ssResultChan chan stateResult) (err error) {
for key, shutdown := range processing {
glog.V(1).Infof("Agent signaling for %s to shutdown.", key)
shutdown <- 1
}
// Wait for goroutines to shutdown
for len(processing) > 0 {
select {
case ssResult := <-ssResultChan:
glog.V(1).Infof("Goroutine finished %s", ssResult.id)
if err == nil && ssResult.err != nil {
err = ssResult.err
}
delete(processing, ssResult.id)
}
}
glog.V(0).Info("All service state nodes are shut down")
return
}