remote_linux.go 11 KB

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