-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpid_lookup.go
203 lines (189 loc) · 6.71 KB
/
pid_lookup.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/kubelet/cri/remote"
)
func ListPods(pod *v1.Pod) ([]string, map[string]string) {
containerIdToName := make(map[string]string, 0)
containerIds := make([]string, 0)
if pod.Status.Phase == "Running" {
for i := 0; i < len(pod.Spec.Containers); i++ {
containerId := pod.Status.ContainerStatuses[i].ContainerID
containerName := pod.Status.ContainerStatuses[i].Name
containerIds = append(containerIds, containerId)
containerIdToName[containerId] = containerName
}
}
return containerIds, containerIdToName
}
func getPidFromJson(config string) (string, error) {
var pid string
var configMap map[string]interface{}
err := json.Unmarshal([]byte(config), &configMap)
if err != nil {
klog.Errorf("Failed to parse json")
return "", err
}
pid = strconv.FormatFloat(configMap["State"].(map[string]interface{})["Pid"].(float64), 'f', -1, 64)
return pid, nil
}
func removePid(localcache *Cache, podInfo string) map[string][]string {
deletePathItem, deletePidItem := localcache.DeletePodInfo(podInfo)
deletePsi(podInfo, deletePathItem)
return deletePidItem
}
// func findPid(localcache *Cache, pod *v1.Pod, procBaseDir string)
func findPidInContainerd(localCache *Cache, pod *v1.Pod, procBaseDir string, containerRuntimePath string) {
sockpath := "unix:///run/containerd/containerd.sock"
r, err := remote.NewRemoteRuntimeService(sockpath, time.Duration(10)*time.Second, nil)
if err != nil {
klog.Errorf("Failed to connect to containerd: %v", err)
return
}
containerIds, containerIdToName := ListPods(pod)
podInfo := pod.Namespace + "/" + pod.Name
containerPidPath := make(map[string]map[string]string, 0)
containerPid := make(map[string][]string, 0)
context := context.Background()
for _, containerId := range containerIds {
containerPid[containerIdToName[containerId]] = []string{}
containerPidPath[containerIdToName[containerId]] = map[string]string{}
status, err := r.ContainerStatus(context, containerId, true)
if err != nil {
klog.Errorf("Failed to get container info: %v", err)
}
info := status.GetInfo()["info"]
re := regexp.MustCompile(`"pid":(\d+)`)
match := re.FindStringSubmatch(info)
if len(match) >= 2 {
pid := match[1]
childrenPid, err := getChildrenPid(pid)
if err != nil {
klog.Errorf("Failed to get children PID for container %v", containerId)
}
for _, childPid := range childrenPid {
path := fmt.Sprintf("%s/%s/root/sys/fs/cgroup", procBaseDir, childPid)
containerPidPath[containerIdToName[containerId]][childPid] = path
containerPid[containerIdToName[containerId]] = append(containerPid[containerIdToName[containerId]], childPid)
}
} else {
fmt.Println("Pid not found in the string.")
}
}
localCache.AddNewPodInfo(podInfo, containerPid, containerPidPath)
}
func updatePids(localcache *Cache, podInfo string, procBaseDir string) {
containerPids := localcache.GetPodPidInfoFromPodInfo(podInfo)
newContainerPids := map[string][]string{}
newContainerPidPathes := map[string]map[string]string{}
for containerName, pids := range containerPids {
newContainerPids[containerName] = []string{}
newContainerPidPathes[containerName] = map[string]string{}
for _, pid := range pids {
newPids, err := getChildrenPid(pid)
if err != nil {
klog.Errorf("updatePids failed: %+v", err)
return
}
newContainerPids[containerName] = append(newContainerPids[containerName], newPids...)
}
encountered := make(map[string]bool)
result := []string{}
for _, pid := range newContainerPids[containerName] {
if !encountered[pid] {
encountered[pid] = true
result = append(result, pid)
path := fmt.Sprintf("%s/%s/root/sys/fs/cgroup", procBaseDir, pid)
newContainerPidPathes[containerName][pid] = path
}
}
klog.Infof("%s %s PIDs are : %s", podInfo, containerName, result)
newContainerPids[containerName] = result
}
localcache.AddNewPodInfo(podInfo, newContainerPids, nil)
}
func findPids(localcache *Cache, pod *v1.Pod, procBaseDir string, dockerBaseDir string) {
containerIds, containerIdToName := ListPods(pod)
// procBaseDir := "/root/proc"
containerBaseDir := dockerBaseDir + "/containers"
files, err := os.ReadDir(containerBaseDir)
if err != nil {
klog.Errorf("Fail to read dir %s", containerBaseDir)
return
}
podInfo := pod.Namespace + "/" + pod.Name
containerPidPath := make(map[string]map[string]string, 0)
containerPid := make(map[string][]string, 0)
for _, containerId := range containerIds {
containerPid[containerIdToName[containerId]] = []string{}
containerPidPath[containerIdToName[containerId]] = map[string]string{}
for _, file := range files {
if strings.Contains(containerId, file.Name()) {
klog.Infof("Investigating: %s", file.Name())
config, err := os.ReadFile(containerBaseDir + "/" + file.Name() + "/config.v2.json")
if err != nil {
klog.Errorf("Failed to open file for container %v", containerId)
continue
}
pid, err := getPidFromJson(string(config))
if err != nil {
klog.Infof("Failed to get PID from container config json file")
continue
}
klog.Infof("%s %s PID is : %s", podInfo, containerIdToName[containerId], pid)
childrenPid, err := getChildrenPid(pid)
klog.Infof("%s %s PIDs are : %v", podInfo, containerIdToName[containerId], childrenPid)
if err != nil {
klog.Errorf("Failed to get children PID for container %v", containerId)
klog.Errorf("Caused by: %+v", err)
continue
}
for _, childPid := range childrenPid {
path := fmt.Sprintf("%s/%s/root/sys/fs/cgroup", procBaseDir, childPid)
containerPidPath[containerIdToName[containerId]][childPid] = path
containerPid[containerIdToName[containerId]] = append(containerPid[containerIdToName[containerId]], childPid)
}
}
}
}
localcache.AddNewPodInfo(podInfo, containerPid, containerPidPath)
}
func getChildrenPid(pid string) ([]string, error) {
if pid == "0" {
// When deleting pythonpi driver, it will get PID == 0
return nil, errors.New("Container is completed")
}
allChildPids := []string{pid}
cmd := exec.Command("pgrep", "-P", pid)
output, err := cmd.Output()
if err != nil {
// If pgrep of a pid doesn't get children
// It will raise an error
// Simply ignore it
return []string{pid}, nil
}
childPids := strings.Fields(string(output))
for _, childPid := range childPids {
allChildPids = append(allChildPids, childPid)
grandChildPids, err := getChildrenPid(childPid)
if err != nil {
// If pgrep of a pid doesn't get children
// It will raise an error
// Simply ignore it
continue
}
allChildPids = append(allChildPids, grandChildPids...)
}
return allChildPids, nil
}