remote_unix.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // +build linux solaris
  2. package libcontainerd
  3. import (
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net"
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. goruntime "runtime"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "syscall"
  17. "time"
  18. "github.com/Sirupsen/logrus"
  19. containerd "github.com/containerd/containerd/api/grpc/types"
  20. "github.com/docker/docker/pkg/locker"
  21. "github.com/docker/docker/pkg/system"
  22. "github.com/golang/protobuf/ptypes"
  23. "github.com/golang/protobuf/ptypes/timestamp"
  24. "golang.org/x/net/context"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/grpclog"
  27. "google.golang.org/grpc/health/grpc_health_v1"
  28. "google.golang.org/grpc/transport"
  29. )
  30. const (
  31. maxConnectionRetryCount = 3
  32. containerdHealthCheckTimeout = 3 * time.Second
  33. containerdShutdownTimeout = 15 * time.Second
  34. containerdBinary = "docker-containerd"
  35. containerdPidFilename = "docker-containerd.pid"
  36. containerdSockFilename = "docker-containerd.sock"
  37. containerdStateDir = "containerd"
  38. eventTimestampFilename = "event.ts"
  39. )
  40. type remote struct {
  41. sync.RWMutex
  42. apiClient containerd.APIClient
  43. daemonPid int
  44. stateDir string
  45. rpcAddr string
  46. startDaemon bool
  47. closedManually bool
  48. debugLog bool
  49. rpcConn *grpc.ClientConn
  50. clients []*client
  51. eventTsPath string
  52. runtime string
  53. runtimeArgs []string
  54. daemonWaitCh chan struct{}
  55. liveRestore bool
  56. oomScore int
  57. restoreFromTimestamp *timestamp.Timestamp
  58. }
  59. // New creates a fresh instance of libcontainerd remote.
  60. func New(stateDir string, options ...RemoteOption) (_ Remote, err error) {
  61. defer func() {
  62. if err != nil {
  63. err = fmt.Errorf("Failed to connect to containerd. Please make sure containerd is installed in your PATH or you have specified the correct address. Got error: %v", err)
  64. }
  65. }()
  66. r := &remote{
  67. stateDir: stateDir,
  68. daemonPid: -1,
  69. eventTsPath: filepath.Join(stateDir, eventTimestampFilename),
  70. }
  71. for _, option := range options {
  72. if err := option.Apply(r); err != nil {
  73. return nil, err
  74. }
  75. }
  76. if err := system.MkdirAll(stateDir, 0700, ""); err != nil {
  77. return nil, err
  78. }
  79. if r.rpcAddr == "" {
  80. r.rpcAddr = filepath.Join(stateDir, containerdSockFilename)
  81. }
  82. if r.startDaemon {
  83. if err := r.runContainerdDaemon(); err != nil {
  84. return nil, err
  85. }
  86. }
  87. // don't output the grpc reconnect logging
  88. grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
  89. dialOpts := []grpc.DialOption{
  90. grpc.WithInsecure(),
  91. grpc.WithBackoffMaxDelay(2 * time.Second),
  92. grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
  93. return net.DialTimeout("unix", addr, timeout)
  94. }),
  95. }
  96. conn, err := grpc.Dial(r.rpcAddr, dialOpts...)
  97. if err != nil {
  98. return nil, fmt.Errorf("error connecting to containerd: %v", err)
  99. }
  100. r.rpcConn = conn
  101. r.apiClient = containerd.NewAPIClient(conn)
  102. // Get the timestamp to restore from
  103. t := r.getLastEventTimestamp()
  104. tsp, err := ptypes.TimestampProto(t)
  105. if err != nil {
  106. logrus.Errorf("libcontainerd: failed to convert timestamp: %q", err)
  107. }
  108. r.restoreFromTimestamp = tsp
  109. go r.handleConnectionChange()
  110. if err := r.startEventsMonitor(); err != nil {
  111. return nil, err
  112. }
  113. return r, nil
  114. }
  115. func (r *remote) UpdateOptions(options ...RemoteOption) error {
  116. for _, option := range options {
  117. if err := option.Apply(r); err != nil {
  118. return err
  119. }
  120. }
  121. return nil
  122. }
  123. func (r *remote) handleConnectionChange() {
  124. var transientFailureCount = 0
  125. ticker := time.NewTicker(500 * time.Millisecond)
  126. defer ticker.Stop()
  127. healthClient := grpc_health_v1.NewHealthClient(r.rpcConn)
  128. for {
  129. <-ticker.C
  130. ctx, cancel := context.WithTimeout(context.Background(), containerdHealthCheckTimeout)
  131. _, err := healthClient.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
  132. cancel()
  133. if err == nil {
  134. continue
  135. }
  136. logrus.Debugf("libcontainerd: containerd health check returned error: %v", err)
  137. if r.daemonPid != -1 {
  138. if r.closedManually {
  139. // Well, we asked for it to stop, just return
  140. return
  141. }
  142. // all other errors are transient
  143. // Reset state to be notified of next failure
  144. transientFailureCount++
  145. if transientFailureCount >= maxConnectionRetryCount {
  146. transientFailureCount = 0
  147. if system.IsProcessAlive(r.daemonPid) {
  148. system.KillProcess(r.daemonPid)
  149. }
  150. <-r.daemonWaitCh
  151. if err := r.runContainerdDaemon(); err != nil { //FIXME: Handle error
  152. logrus.Errorf("libcontainerd: error restarting containerd: %v", err)
  153. }
  154. continue
  155. }
  156. }
  157. }
  158. }
  159. func (r *remote) Cleanup() {
  160. if r.daemonPid == -1 {
  161. return
  162. }
  163. r.closedManually = true
  164. r.rpcConn.Close()
  165. // Ask the daemon to quit
  166. syscall.Kill(r.daemonPid, syscall.SIGTERM)
  167. // Wait up to 15secs for it to stop
  168. for i := time.Duration(0); i < containerdShutdownTimeout; i += time.Second {
  169. if !system.IsProcessAlive(r.daemonPid) {
  170. break
  171. }
  172. time.Sleep(time.Second)
  173. }
  174. if system.IsProcessAlive(r.daemonPid) {
  175. logrus.Warnf("libcontainerd: containerd (%d) didn't stop within 15 secs, killing it\n", r.daemonPid)
  176. syscall.Kill(r.daemonPid, syscall.SIGKILL)
  177. }
  178. // cleanup some files
  179. os.Remove(filepath.Join(r.stateDir, containerdPidFilename))
  180. os.Remove(filepath.Join(r.stateDir, containerdSockFilename))
  181. }
  182. func (r *remote) Client(b Backend) (Client, error) {
  183. c := &client{
  184. clientCommon: clientCommon{
  185. backend: b,
  186. containers: make(map[string]*container),
  187. locker: locker.New(),
  188. },
  189. remote: r,
  190. exitNotifiers: make(map[string]*exitNotifier),
  191. liveRestore: r.liveRestore,
  192. }
  193. r.Lock()
  194. r.clients = append(r.clients, c)
  195. r.Unlock()
  196. return c, nil
  197. }
  198. func (r *remote) updateEventTimestamp(t time.Time) {
  199. f, err := os.OpenFile(r.eventTsPath, syscall.O_CREAT|syscall.O_WRONLY|syscall.O_TRUNC, 0600)
  200. if err != nil {
  201. logrus.Warnf("libcontainerd: failed to open event timestamp file: %v", err)
  202. return
  203. }
  204. defer f.Close()
  205. b, err := t.MarshalText()
  206. if err != nil {
  207. logrus.Warnf("libcontainerd: failed to encode timestamp: %v", err)
  208. return
  209. }
  210. n, err := f.Write(b)
  211. if err != nil || n != len(b) {
  212. logrus.Warnf("libcontainerd: failed to update event timestamp file: %v", err)
  213. f.Truncate(0)
  214. return
  215. }
  216. }
  217. func (r *remote) getLastEventTimestamp() time.Time {
  218. t := time.Now()
  219. fi, err := os.Stat(r.eventTsPath)
  220. if os.IsNotExist(err) || fi.Size() == 0 {
  221. return t
  222. }
  223. f, err := os.Open(r.eventTsPath)
  224. if err != nil {
  225. logrus.Warnf("libcontainerd: Unable to access last event ts: %v", err)
  226. return t
  227. }
  228. defer f.Close()
  229. b := make([]byte, fi.Size())
  230. n, err := f.Read(b)
  231. if err != nil || n != len(b) {
  232. logrus.Warnf("libcontainerd: Unable to read last event ts: %v", err)
  233. return t
  234. }
  235. t.UnmarshalText(b)
  236. return t
  237. }
  238. func (r *remote) startEventsMonitor() error {
  239. // First, get past events
  240. t := r.getLastEventTimestamp()
  241. tsp, err := ptypes.TimestampProto(t)
  242. if err != nil {
  243. logrus.Errorf("libcontainerd: failed to convert timestamp: %q", err)
  244. }
  245. er := &containerd.EventsRequest{
  246. Timestamp: tsp,
  247. }
  248. var events containerd.API_EventsClient
  249. for {
  250. events, err = r.apiClient.Events(context.Background(), er, grpc.FailFast(false))
  251. if err == nil {
  252. break
  253. }
  254. logrus.Warnf("libcontainerd: failed to get events from containerd: %q", err)
  255. if r.closedManually {
  256. // ignore error if grpc remote connection is closed manually
  257. return nil
  258. }
  259. <-time.After(100 * time.Millisecond)
  260. }
  261. go r.handleEventStream(events)
  262. return nil
  263. }
  264. func (r *remote) handleEventStream(events containerd.API_EventsClient) {
  265. for {
  266. e, err := events.Recv()
  267. if err != nil {
  268. if grpc.ErrorDesc(err) == transport.ErrConnClosing.Desc &&
  269. r.closedManually {
  270. // ignore error if grpc remote connection is closed manually
  271. return
  272. }
  273. logrus.Errorf("libcontainerd: failed to receive event from containerd: %v", err)
  274. go r.startEventsMonitor()
  275. return
  276. }
  277. logrus.Debugf("libcontainerd: received containerd event: %#v", e)
  278. var container *container
  279. var c *client
  280. r.RLock()
  281. for _, c = range r.clients {
  282. container, err = c.getContainer(e.Id)
  283. if err == nil {
  284. break
  285. }
  286. }
  287. r.RUnlock()
  288. if container == nil {
  289. logrus.Warnf("libcontainerd: unknown container %s", e.Id)
  290. continue
  291. }
  292. if err := container.handleEvent(e); err != nil {
  293. logrus.Errorf("libcontainerd: error processing state change for %s: %v", e.Id, err)
  294. }
  295. tsp, err := ptypes.Timestamp(e.Timestamp)
  296. if err != nil {
  297. logrus.Errorf("libcontainerd: failed to convert event timestamp: %q", err)
  298. continue
  299. }
  300. r.updateEventTimestamp(tsp)
  301. }
  302. }
  303. func (r *remote) runContainerdDaemon() error {
  304. pidFilename := filepath.Join(r.stateDir, containerdPidFilename)
  305. f, err := os.OpenFile(pidFilename, os.O_RDWR|os.O_CREATE, 0600)
  306. if err != nil {
  307. return err
  308. }
  309. defer f.Close()
  310. // File exist, check if the daemon is alive
  311. b := make([]byte, 8)
  312. n, err := f.Read(b)
  313. if err != nil && err != io.EOF {
  314. return err
  315. }
  316. if n > 0 {
  317. pid, err := strconv.ParseUint(string(b[:n]), 10, 64)
  318. if err != nil {
  319. return err
  320. }
  321. if system.IsProcessAlive(int(pid)) {
  322. logrus.Infof("libcontainerd: previous instance of containerd still alive (%d)", pid)
  323. r.daemonPid = int(pid)
  324. return nil
  325. }
  326. }
  327. // rewind the file
  328. _, err = f.Seek(0, os.SEEK_SET)
  329. if err != nil {
  330. return err
  331. }
  332. // Truncate it
  333. err = f.Truncate(0)
  334. if err != nil {
  335. return err
  336. }
  337. // Start a new instance
  338. args := []string{
  339. "-l", fmt.Sprintf("unix://%s", r.rpcAddr),
  340. "--metrics-interval=0",
  341. "--start-timeout", "2m",
  342. "--state-dir", filepath.Join(r.stateDir, containerdStateDir),
  343. }
  344. if goruntime.GOOS == "solaris" {
  345. args = append(args, "--shim", "containerd-shim", "--runtime", "runc")
  346. } else {
  347. args = append(args, "--shim", "docker-containerd-shim")
  348. if r.runtime != "" {
  349. args = append(args, "--runtime")
  350. args = append(args, r.runtime)
  351. }
  352. }
  353. if r.debugLog {
  354. args = append(args, "--debug")
  355. }
  356. if len(r.runtimeArgs) > 0 {
  357. for _, v := range r.runtimeArgs {
  358. args = append(args, "--runtime-args")
  359. args = append(args, v)
  360. }
  361. logrus.Debugf("libcontainerd: runContainerdDaemon: runtimeArgs: %s", args)
  362. }
  363. cmd := exec.Command(containerdBinary, args...)
  364. // redirect containerd logs to docker logs
  365. cmd.Stdout = os.Stdout
  366. cmd.Stderr = os.Stderr
  367. cmd.SysProcAttr = setSysProcAttr(true)
  368. cmd.Env = nil
  369. // clear the NOTIFY_SOCKET from the env when starting containerd
  370. for _, e := range os.Environ() {
  371. if !strings.HasPrefix(e, "NOTIFY_SOCKET") {
  372. cmd.Env = append(cmd.Env, e)
  373. }
  374. }
  375. if err := cmd.Start(); err != nil {
  376. return err
  377. }
  378. // unless strictly necessary, do not add anything in between here
  379. // as the reaper goroutine below needs to kick in as soon as possible
  380. // and any "return" from code paths added here will defeat the reaper
  381. // process.
  382. r.daemonWaitCh = make(chan struct{})
  383. go func() {
  384. cmd.Wait()
  385. close(r.daemonWaitCh)
  386. }() // Reap our child when needed
  387. logrus.Infof("libcontainerd: new containerd process, pid: %d", cmd.Process.Pid)
  388. if err := setOOMScore(cmd.Process.Pid, r.oomScore); err != nil {
  389. system.KillProcess(cmd.Process.Pid)
  390. return err
  391. }
  392. if _, err := f.WriteString(fmt.Sprintf("%d", cmd.Process.Pid)); err != nil {
  393. system.KillProcess(cmd.Process.Pid)
  394. return err
  395. }
  396. r.daemonPid = cmd.Process.Pid
  397. return nil
  398. }
  399. // WithRemoteAddr sets the external containerd socket to connect to.
  400. func WithRemoteAddr(addr string) RemoteOption {
  401. return rpcAddr(addr)
  402. }
  403. type rpcAddr string
  404. func (a rpcAddr) Apply(r Remote) error {
  405. if remote, ok := r.(*remote); ok {
  406. remote.rpcAddr = string(a)
  407. return nil
  408. }
  409. return fmt.Errorf("WithRemoteAddr option not supported for this remote")
  410. }
  411. // WithRuntimePath sets the path of the runtime to be used as the
  412. // default by containerd
  413. func WithRuntimePath(rt string) RemoteOption {
  414. return runtimePath(rt)
  415. }
  416. type runtimePath string
  417. func (rt runtimePath) Apply(r Remote) error {
  418. if remote, ok := r.(*remote); ok {
  419. remote.runtime = string(rt)
  420. return nil
  421. }
  422. return fmt.Errorf("WithRuntime option not supported for this remote")
  423. }
  424. // WithRuntimeArgs sets the list of runtime args passed to containerd
  425. func WithRuntimeArgs(args []string) RemoteOption {
  426. return runtimeArgs(args)
  427. }
  428. type runtimeArgs []string
  429. func (rt runtimeArgs) Apply(r Remote) error {
  430. if remote, ok := r.(*remote); ok {
  431. remote.runtimeArgs = rt
  432. return nil
  433. }
  434. return fmt.Errorf("WithRuntimeArgs option not supported for this remote")
  435. }
  436. // WithStartDaemon defines if libcontainerd should also run containerd daemon.
  437. func WithStartDaemon(start bool) RemoteOption {
  438. return startDaemon(start)
  439. }
  440. type startDaemon bool
  441. func (s startDaemon) Apply(r Remote) error {
  442. if remote, ok := r.(*remote); ok {
  443. remote.startDaemon = bool(s)
  444. return nil
  445. }
  446. return fmt.Errorf("WithStartDaemon option not supported for this remote")
  447. }
  448. // WithDebugLog defines if containerd debug logs will be enabled for daemon.
  449. func WithDebugLog(debug bool) RemoteOption {
  450. return debugLog(debug)
  451. }
  452. type debugLog bool
  453. func (d debugLog) Apply(r Remote) error {
  454. if remote, ok := r.(*remote); ok {
  455. remote.debugLog = bool(d)
  456. return nil
  457. }
  458. return fmt.Errorf("WithDebugLog option not supported for this remote")
  459. }
  460. // WithLiveRestore defines if containers are stopped on shutdown or restored.
  461. func WithLiveRestore(v bool) RemoteOption {
  462. return liveRestore(v)
  463. }
  464. type liveRestore bool
  465. func (l liveRestore) Apply(r Remote) error {
  466. if remote, ok := r.(*remote); ok {
  467. remote.liveRestore = bool(l)
  468. for _, c := range remote.clients {
  469. c.liveRestore = bool(l)
  470. }
  471. return nil
  472. }
  473. return fmt.Errorf("WithLiveRestore option not supported for this remote")
  474. }
  475. // WithOOMScore defines the oom_score_adj to set for the containerd process.
  476. func WithOOMScore(score int) RemoteOption {
  477. return oomScore(score)
  478. }
  479. type oomScore int
  480. func (o oomScore) Apply(r Remote) error {
  481. if remote, ok := r.(*remote); ok {
  482. remote.oomScore = int(o)
  483. return nil
  484. }
  485. return fmt.Errorf("WithOOMScore option not supported for this remote")
  486. }