Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 8577f4a

Browse files
committed
working tk
1 parent 8563c5e commit 8577f4a

File tree

3 files changed

+14
-45
lines changed

3 files changed

+14
-45
lines changed

java_test_pod/test_pod_config.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
kind: Pod
22
apiVersion: v1
33
metadata:
4-
name: test_java_pod
4+
name: test-java-pod
55
labels:
6-
app: test_java_pod
6+
app: test-java-pod
77
spec:
88
containers:
9-
- name: test_java_pod
9+
- name: test-java-pod
1010
image: gcr.io/genuine-flight-317411/java-toolkit/test_java_pod
1111
ports:
1212
- containerPort: 1111
@@ -17,7 +17,7 @@ metadata:
1717
name: example-service
1818
spec:
1919
selector:
20-
app: test_java_pod
20+
app: test-java-pod
2121
ports:
2222
- port: 8222
2323
targetPort: 1111

src/java_toolkit/common/utils/process.py

+9-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/local/bin/python3
22
import re
33
from typing import List
4-
from .remote_ns_cmd import run_cmd_in_proc_namespace
4+
from .remote_ns_cmd import run_command
55

66
import psutil
77
import typer
@@ -15,8 +15,6 @@
1515
r"\d+:.+:/kubepods\.slice/kubepods-[^/]+\.slice/kubepods-[^/]+-pod([^/]+)\.slice/docker-([0-9a-f]{64})"
1616
)
1717

18-
ps_regex = re.compile(r"\s*(?P<pid>.*?)\s+(?P<cmd>.*?)\s+(?P<cmd_args>.*?)$")
19-
2018
app = typer.Typer()
2119

2220

@@ -68,26 +66,14 @@ def get_pid_info(pid: int) -> Process:
6866
proc = psutil.Process(pid)
6967
return Process(pid=pid, exe=proc.exe(), cmdline=proc.cmdline())
7068

71-
def get_ns_processes(pid_in_ns: int, verbose: bool):
72-
cmd = "ps -Ao pid,comm,args | grep -v PID"
73-
processes = run_cmd_in_proc_namespace(pid_in_ns, cmd, verbose)
74-
processes = processes.split('\n')
75-
return_proc_list=[]
76-
for proc_line in processes:
77-
if not proc_line:
78-
continue
79-
match = ps_regex.match(proc_line)
80-
if not match:
81-
continue
82-
pid = int(match.group("pid"))
83-
exe = match.group("cmd")
84-
cmdline = match.group("cmd_args")
85-
if cmdline is None:
86-
cmdline = []
87-
else:
88-
cmdline = cmdline.split(' ')
89-
return_proc_list.append(Process(pid=pid, exe=exe, cmdline=cmdline))
90-
return ProcessList(processes=return_proc_list)
69+
def get_nspid(pid_in_ns: int, verbose: bool):
70+
cmd = f"cat /proc/{pid_in_ns}/status"
71+
status_output = run_command(cmd, verbose)
72+
nspid_regex = re.compile(r".+NSpid:\s+(?P<pid>.*?)\s+(?P<nspid>.*?)\s+", re.DOTALL)
73+
match = nspid_regex.match(status_output)
74+
if not match:
75+
raise Exception(f"No match found for pid {pid_in_ns}")
76+
return int(match.group("nspid"))
9177

9278

9379

src/java_toolkit/main.py

+1-18
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,12 @@
88

99
app = typer.Typer()
1010

11-
def get_matching_local_pid(pid: int, verbose: bool):
12-
process_cmd = ' '.join(get_pid_info(pid).cmdline)
13-
ns_processes = get_ns_processes(pid, verbose)
14-
process_list = []
15-
for ns_process in ns_processes.processes:
16-
ns_process_cmd = ' '.join(ns_process.cmdline)
17-
if process_cmd == ns_process_cmd:
18-
process_list.append(ns_process)
19-
if len(process_list) != 1:
20-
raise Exception(f"{len(process_list)} match the pid {pid} args '{process_cmd}'")
21-
return process_list[0].pid
2211

2312
def run_jdk_cmd(pid: int, cmd_missing_jdk_path:str, add_local_pid: bool, verbose: bool):
2413
with TmpRemotePodMounter(pid, JDK_PATH, LOCAL_MOUNT_PATH, verbose) as jdk_mounter:
2514
cmd = cmd_missing_jdk_path
2615
if add_local_pid:
27-
local_pid = get_matching_local_pid(pid, verbose)
16+
local_pid = get_nspid(pid, verbose)
2817
cmd = cmd.format(jdk_path=jdk_mounter.get_mounted_jdk_dir(),pid=local_pid)
2918
else:
3019
cmd = cmd.format(jdk_path=jdk_mounter.get_mounted_jdk_dir())
@@ -37,17 +26,11 @@ def pod_ps(pod_uid: str):
3726
typer.echo(get_pod_processes(pod_uid).json())
3827

3928

40-
@app.command()
41-
def pod_ns_ps(pid: int, verbose: bool = False):
42-
typer.echo(get_ns_processes(pid, verbose).json())
43-
44-
4529
@app.command()
4630
def jmap(pid: int, verbose: bool = False):
4731
run_jdk_cmd(pid, JMAP_CMD, add_local_pid=True, verbose=verbose)
4832

4933

50-
5134
@app.command()
5235
def jstack(pid: int, verbose: bool = False):
5336
run_jdk_cmd(pid, JSTACK_CMD, add_local_pid=True, verbose=verbose)

0 commit comments

Comments
 (0)