registry.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package containers
  2. import (
  3. "fmt"
  4. "github.com/coroot/coroot-node-agent/cgroup"
  5. "github.com/coroot/coroot-node-agent/common"
  6. "github.com/coroot/coroot-node-agent/ebpftracer"
  7. "github.com/coroot/coroot-node-agent/proc"
  8. "github.com/prometheus/client_golang/prometheus"
  9. "github.com/vishvananda/netns"
  10. "k8s.io/klog/v2"
  11. "os"
  12. "time"
  13. )
  14. var (
  15. selfNetNs = netns.None()
  16. hostNetNsId = netns.None().UniqueId()
  17. agentPid = uint32(os.Getpid())
  18. )
  19. type Registry struct {
  20. reg prometheus.Registerer
  21. tracer *ebpftracer.Tracer
  22. events chan ebpftracer.Event
  23. containersById map[ContainerID]*Container
  24. containersByCgroupId map[string]*Container
  25. containersByPid map[uint32]*Container
  26. }
  27. func NewRegistry(reg prometheus.Registerer, kernelVersion string) (*Registry, error) {
  28. ns, err := proc.GetSelfNetNs()
  29. if err != nil {
  30. return nil, err
  31. }
  32. selfNetNs = ns
  33. hostNetNs, err := proc.GetHostNetNs()
  34. if err != nil {
  35. return nil, err
  36. }
  37. defer hostNetNs.Close()
  38. hostNetNsId = hostNetNs.UniqueId()
  39. err = proc.ExecuteInNetNs(hostNetNs, selfNetNs, func() error {
  40. if err := TaskstatsInit(); err != nil {
  41. return err
  42. }
  43. if err := ConntrackInit(); err != nil {
  44. return err
  45. }
  46. return nil
  47. })
  48. if err != nil {
  49. return nil, err
  50. }
  51. if err := cgroup.Init(); err != nil {
  52. return nil, err
  53. }
  54. if err := DockerdInit(); err != nil {
  55. klog.Warningln(err)
  56. }
  57. if err := ContainerdInit(); err != nil {
  58. klog.Warningln(err)
  59. }
  60. if err := JournaldInit(); err != nil {
  61. klog.Warningln(err)
  62. }
  63. cs := &Registry{
  64. reg: reg,
  65. events: make(chan ebpftracer.Event, 10000),
  66. containersById: map[ContainerID]*Container{},
  67. containersByCgroupId: map[string]*Container{},
  68. containersByPid: map[uint32]*Container{},
  69. }
  70. go cs.handleEvents(cs.events)
  71. t, err := ebpftracer.NewTracer(cs.events, kernelVersion)
  72. if err != nil {
  73. close(cs.events)
  74. return nil, err
  75. }
  76. cs.tracer = t
  77. return cs, nil
  78. }
  79. func (r *Registry) Close() {
  80. r.tracer.Close()
  81. close(r.events)
  82. }
  83. func (r *Registry) handleEvents(ch <-chan ebpftracer.Event) {
  84. gcTicker := time.NewTicker(gcInterval)
  85. defer gcTicker.Stop()
  86. for {
  87. select {
  88. case now := <-gcTicker.C:
  89. for id, c := range r.containersById {
  90. if !c.Dead(now) {
  91. continue
  92. }
  93. for cg, cc := range r.containersByCgroupId {
  94. if cc == c {
  95. delete(r.containersByCgroupId, cg)
  96. }
  97. }
  98. for pid, cc := range r.containersByPid {
  99. if cc == c {
  100. delete(r.containersByPid, pid)
  101. }
  102. }
  103. prometheus.WrapRegistererWith(prometheus.Labels{"container_id": string(id)}, r.reg).Unregister(c)
  104. delete(r.containersById, id)
  105. c.Close()
  106. }
  107. case e, more := <-ch:
  108. if !more {
  109. return
  110. }
  111. switch e.Type {
  112. case ebpftracer.EventTypeProcessStart:
  113. c, seen := r.containersByPid[e.Pid]
  114. switch { // possible pids wraparound + missed `process-exit` event
  115. case c == nil && seen: // ignored
  116. delete(r.containersByPid, e.Pid)
  117. continue
  118. case c != nil: // revalidating by cgroup
  119. cg, err := proc.ReadCgroup(e.Pid)
  120. if err != nil || cg.Id != c.cgroup.Id {
  121. delete(r.containersByPid, e.Pid)
  122. c.onProcessExit(e.Pid, false)
  123. }
  124. }
  125. if c := r.getOrCreateContainer(e.Pid); c != nil {
  126. c.onProcessStart(e.Pid)
  127. }
  128. case ebpftracer.EventTypeProcessExit:
  129. if c := r.containersByPid[e.Pid]; c != nil {
  130. c.onProcessExit(e.Pid, e.Reason == ebpftracer.EventReasonOOMKill)
  131. }
  132. delete(r.containersByPid, e.Pid)
  133. case ebpftracer.EventTypeFileOpen:
  134. if c := r.getOrCreateContainer(e.Pid); c != nil {
  135. c.onFileOpen(e.Pid, e.Fd)
  136. }
  137. case ebpftracer.EventTypeListenOpen:
  138. if c := r.getOrCreateContainer(e.Pid); c != nil {
  139. c.onListenOpen(e.Pid, e.SrcAddr, false)
  140. } else {
  141. klog.Infoln("TCP listen open from unknown container", e)
  142. }
  143. case ebpftracer.EventTypeListenClose:
  144. if c := r.containersByPid[e.Pid]; c != nil {
  145. c.onListenClose(e.Pid, e.SrcAddr)
  146. }
  147. case ebpftracer.EventTypeConnectionOpen:
  148. if c := r.getOrCreateContainer(e.Pid); c != nil {
  149. c.onConnectionOpen(e.Pid, e.SrcAddr, e.DstAddr, false)
  150. } else {
  151. klog.Infoln("TCP connection from unknown container", e)
  152. }
  153. case ebpftracer.EventTypeConnectionError:
  154. if c := r.getOrCreateContainer(e.Pid); c != nil {
  155. c.onConnectionOpen(e.Pid, e.SrcAddr, e.DstAddr, true)
  156. } else {
  157. klog.Infoln("TCP connection error from unknown container", e)
  158. }
  159. case ebpftracer.EventTypeConnectionClose:
  160. srcDst := AddrPair{src: e.SrcAddr, dst: e.DstAddr}
  161. for _, c := range r.containersById {
  162. if c.onConnectionClose(srcDst) {
  163. break
  164. }
  165. }
  166. case ebpftracer.EventTypeTCPRetransmit:
  167. srcDst := AddrPair{src: e.SrcAddr, dst: e.DstAddr}
  168. for _, c := range r.containersById {
  169. if c.onRetransmit(srcDst) {
  170. break
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. func (r *Registry) getOrCreateContainer(pid uint32) *Container {
  178. if c, seen := r.containersByPid[pid]; c != nil {
  179. return c
  180. } else if seen { // ignored
  181. return nil
  182. }
  183. cg, err := proc.ReadCgroup(pid)
  184. if err != nil {
  185. if !common.IsNotExist(err) {
  186. klog.Warningln("failed to read proc cgroup:", err)
  187. }
  188. return nil
  189. }
  190. klog.Infof("got cgroup by pid %d -> %s", pid, cg.Id)
  191. if c := r.containersByCgroupId[cg.Id]; c != nil {
  192. klog.Infof("found container by cgroup pid %d -> %s", pid, cg.Id)
  193. r.containersByPid[pid] = c
  194. return c
  195. }
  196. md, err := getContainerMetadata(cg)
  197. if err != nil {
  198. klog.Warningln(err)
  199. return nil
  200. }
  201. id := calcId(cg, md)
  202. klog.Infof("calculated container id %d -> %s -> %s", pid, cg.Id, id)
  203. if id == "" {
  204. if cg.Id == "/init.scope" && pid != 1 {
  205. klog.InfoS("ignoring without persisting", "cg", cg.Id, "pid", pid)
  206. } else {
  207. klog.InfoS("ignoring", "cg", cg.Id, "pid", pid)
  208. r.containersByPid[pid] = nil
  209. }
  210. return nil
  211. }
  212. if c := r.containersById[id]; c != nil {
  213. klog.Warningln("id conflict:", id)
  214. if cg.CreatedAt().After(c.cgroup.CreatedAt()) {
  215. c.cgroup = cg
  216. c.metadata = md
  217. c.runLogParser("")
  218. }
  219. r.containersByPid[pid] = c
  220. r.containersByCgroupId[cg.Id] = c
  221. return c
  222. }
  223. c := NewContainer(cg, md)
  224. klog.InfoS("detected container", "pid", pid, "cg", cg.Id, "id", id)
  225. if err := prometheus.WrapRegistererWith(prometheus.Labels{"container_id": string(id)}, r.reg).Register(c); err != nil {
  226. klog.Warningln(err)
  227. return nil
  228. }
  229. r.containersByPid[pid] = c
  230. r.containersByCgroupId[cg.Id] = c
  231. r.containersById[id] = c
  232. return c
  233. }
  234. func calcId(cg *cgroup.Cgroup, md *ContainerMetadata) ContainerID {
  235. if cg.ContainerType == cgroup.ContainerTypeSystemdService {
  236. return ContainerID(cg.ContainerId)
  237. }
  238. if cg.ContainerType != cgroup.ContainerTypeDocker && cg.ContainerType != cgroup.ContainerTypeContainerd {
  239. return ""
  240. }
  241. if md.labels["io.kubernetes.pod.name"] != "" {
  242. pod := md.labels["io.kubernetes.pod.name"]
  243. namespace := md.labels["io.kubernetes.pod.namespace"]
  244. name := md.labels["io.kubernetes.container.name"]
  245. if name == "" || name == "POD" { // skip pause|sandbox containers
  246. return ""
  247. }
  248. return ContainerID(fmt.Sprintf("/k8s/%s/%s/%s", namespace, pod, name))
  249. }
  250. if md.name == "" { // should be "pure" dockerd container here
  251. klog.Warningln("empty dockerd container name for:", cg.ContainerId)
  252. return ""
  253. }
  254. return ContainerID("/docker/" + md.name)
  255. }
  256. func getContainerMetadata(cg *cgroup.Cgroup) (*ContainerMetadata, error) {
  257. if cg.ContainerType != cgroup.ContainerTypeDocker && cg.ContainerType != cgroup.ContainerTypeContainerd {
  258. return &ContainerMetadata{}, nil
  259. }
  260. var dockerdErr error
  261. if dockerdClient != nil {
  262. md, err := DockerdInspect(cg.ContainerId)
  263. if err == nil {
  264. return md, nil
  265. }
  266. klog.Warningln("failed to inspect container %s: %s", cg.ContainerId, err)
  267. dockerdErr = err
  268. }
  269. var containerdErr error
  270. if containerdClient != nil {
  271. md, err := ContainerdInspect(cg.ContainerId)
  272. if err == nil {
  273. return md, nil
  274. }
  275. klog.Warningln("failed to inspect container %s: %s", cg.ContainerId, err)
  276. containerdErr = err
  277. }
  278. return nil, fmt.Errorf("failed to interact with dockerd (%s) or with containerd (%s)", dockerdErr, containerdErr)
  279. }