File: container.go

package info (click to toggle)
witr 0.2.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 736 kB
  • sloc: sh: 79; makefile: 10
file content (53 lines) | stat: -rw-r--r-- 1,167 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package source

import (
	"os"
	"strconv"
	"strings"

	"github.com/pranshuparmar/witr/pkg/model"
)

func detectContainer(ancestry []model.Process) *model.Source {
	for _, p := range ancestry {
		data, err := os.ReadFile("/proc/" + itoa(p.PID) + "/cgroup")
		if err != nil {
			continue
		}
		content := string(data)

		switch {
		case strings.Contains(content, "docker"):
			return &model.Source{
				Type: model.SourceContainer,
				Name: "docker",
			}
		case strings.Contains(content, "podman"), strings.Contains(content, "libpod"):
			return &model.Source{
				Type: model.SourceContainer,
				Name: "podman",
			}
		case strings.Contains(content, "kubepods"):
			return &model.Source{
				Type: model.SourceContainer,
				Name: "kubernetes",
			}
		case strings.Contains(content, "colima"):
			return &model.Source{
				Type: model.SourceContainer,
				Name: "colima",
			}
		case strings.Contains(content, "containerd"):
			// Only match containerd if not already matched by docker/kubernetes/colima
			return &model.Source{
				Type: model.SourceContainer,
				Name: "containerd",
			}
		}
	}
	return nil
}

func itoa(n int) string {
	return strconv.Itoa(n)
}