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 := DockerdInit(); err != nil {
  52. klog.Warningln(err)
  53. }
  54. if err := ContainerdInit(); err != nil {
  55. klog.Warningln(err)
  56. }
  57. if err := JournaldInit(); err != nil {
  58. klog.Warningln(err)
  59. }
  60. cs := &Registry{
  61. reg: reg,
  62. events: make(chan ebpftracer.Event, 10000),
  63. containersById: map[ContainerID]*Container{},
  64. containersByCgroupId: map[string]*Container{},
  65. containersByPid: map[uint32]*Container{},
  66. }
  67. go cs.handleEvents(cs.events)
  68. t, err := ebpftracer.NewTracer(cs.events, kernelVersion)
  69. if err != nil {
  70. close(cs.events)
  71. return nil, err
  72. }
  73. cs.tracer = t
  74. return cs, nil
  75. }
  76. func (r *Registry) Close() {
  77. r.tracer.Close()
  78. close(r.events)
  79. }
  80. func (r *Registry) handleEvents(ch <-chan ebpftracer.Event) {
  81. gcTicker := time.NewTicker(gcInterval)
  82. defer gcTicker.Stop()
  83. for {
  84. select {
  85. case now := <-gcTicker.C:
  86. for id, c := range r.containersById {
  87. if !c.Dead(now) {
  88. continue
  89. }
  90. for cg, cc := range r.containersByCgroupId {
  91. if cc == c {
  92. delete(r.containersByCgroupId, cg)
  93. }
  94. }
  95. for pid, cc := range r.containersByPid {
  96. if cc == c {
  97. delete(r.containersByPid, pid)
  98. }
  99. }
  100. prometheus.WrapRegistererWith(prometheus.Labels{"container_id": string(id)}, r.reg).Unregister(c)
  101. delete(r.containersById, id)
  102. c.Close()
  103. }
  104. case e, more := <-ch:
  105. if !more {
  106. return
  107. }
  108. klog.Infoln(e)
  109. switch e.Type {
  110. case ebpftracer.EventTypeProcessStart:
  111. c, seen := r.containersByPid[e.Pid]
  112. switch { // possible pids wraparound + missed `process-exit` event
  113. case c == nil && seen: // ignored
  114. delete(r.containersByPid, e.Pid)
  115. continue
  116. case c != nil: // revalidating by cgroup
  117. cg, err := proc.ReadCgroup(e.Pid)
  118. if err != nil || cg.Id != c.cgroup.Id {
  119. delete(r.containersByPid, e.Pid)
  120. c.onProcessExit(e.Pid, false)
  121. }
  122. }
  123. if c := r.getOrCreateContainer(e.Pid); c != nil {
  124. c.onProcessStart(e.Pid)
  125. }
  126. case ebpftracer.EventTypeProcessExit:
  127. if c := r.containersByPid[e.Pid]; c != nil {
  128. c.onProcessExit(e.Pid, e.Reason == ebpftracer.EventReasonOOMKill)
  129. }
  130. delete(r.containersByPid, e.Pid)
  131. case ebpftracer.EventTypeFileOpen:
  132. if c := r.getOrCreateContainer(e.Pid); c != nil {
  133. c.onFileOpen(e.Pid, e.Fd)
  134. }
  135. case ebpftracer.EventTypeListenOpen:
  136. if c := r.getOrCreateContainer(e.Pid); c != nil {
  137. c.onListenOpen(e.Pid, e.SrcAddr)
  138. } else {
  139. klog.Infoln("TCP listen open from unknown container", e)
  140. }
  141. case ebpftracer.EventTypeListenClose:
  142. if c := r.containersByPid[e.Pid]; c != nil {
  143. c.onListenClose(e.Pid, e.SrcAddr)
  144. }
  145. case ebpftracer.EventTypeConnectionOpen:
  146. if c := r.getOrCreateContainer(e.Pid); c != nil {
  147. c.onConnectionOpen(e.Pid, e.SrcAddr, e.DstAddr, false)
  148. } else {
  149. klog.Infoln("TCP connection from unknown container", e)
  150. }
  151. case ebpftracer.EventTypeConnectionError:
  152. if c := r.getOrCreateContainer(e.Pid); c != nil {
  153. c.onConnectionOpen(e.Pid, e.SrcAddr, e.DstAddr, true)
  154. } else {
  155. klog.Infoln("TCP connection error from unknown container", e)
  156. }
  157. case ebpftracer.EventTypeConnectionClose:
  158. srcDst := AddrPair{src: e.SrcAddr, dst: e.DstAddr}
  159. for _, c := range r.containersById {
  160. if c.onConnectionClose(srcDst) {
  161. break
  162. }
  163. }
  164. case ebpftracer.EventTypeTCPRetransmit:
  165. srcDst := AddrPair{src: e.SrcAddr, dst: e.DstAddr}
  166. for _, c := range r.containersById {
  167. if c.onRetransmit(srcDst) {
  168. break
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. func (r *Registry) getOrCreateContainer(pid uint32) *Container {
  176. if c, seen := r.containersByPid[pid]; c != nil {
  177. klog.Infof("got container by pid %d -> %s", pid, c.cgroup.Id)
  178. return c
  179. } else if seen { // ignored
  180. klog.Infof("ignored container for pid %d", pid)
  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 {
  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 {
  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. }