remote_linux.go 11 KB

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