remote_linux.go 13 KB

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