forked from aylei/kubectl-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhancement: support target-container procfs correct (aylei#83)
* enhancement: support target-container procfs correct * update Makefile remove some wrong adjustments for my own dev environment * Update cmd.go fix my typo error I made for travis CI * fix: agent_daemonset yaml support lxcfs * add:使用alpine linux,减小debug-agent镜像大小 * fix: 修复nsenter在alpine发行版中的一些error * fix: reinforce the compatibility of debug-agent for kubectl-debug && fix typo && go fmt
- Loading branch information
1 parent
63e1ecc
commit ae58ddf
Showing
11 changed files
with
308 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
* | ||
!debug-agent | ||
|
||
!./scripts/start.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
FROM gcr.io/distroless/static | ||
FROM ubuntu:xenial as build | ||
|
||
RUN apt-get update && apt-get install libcgmanager-dev libnih-dbus-dev libnih-dev libfuse-dev automake libtool libpam-dev wget gcc automake -y && apt-get clean && rm -rf /var/lib/apt/lists/* | ||
|
||
ENV LXCFS_VERSION 3.1.2 | ||
RUN wget https://linuxcontainers.org/downloads/lxcfs/lxcfs-$LXCFS_VERSION.tar.gz && \ | ||
mkdir /lxcfs && tar xzvf lxcfs-$LXCFS_VERSION.tar.gz -C /lxcfs --strip-components=1 && \ | ||
cd /lxcfs && ./configure && make | ||
|
||
FROM alpine:3.10 | ||
|
||
COPY --from=build /lxcfs/lxcfs /usr/local/bin/lxcfs | ||
COPY --from=build /lxcfs/.libs/liblxcfs.so /usr/local/lib/lxcfs/liblxcfs.so | ||
COPY --from=build /lxcfs/lxcfs /lxcfs/lxcfs | ||
COPY --from=build /lxcfs/.libs/liblxcfs.so /lxcfs/liblxcfs.so | ||
COPY ./scripts/start.sh / | ||
COPY ./debug-agent /bin/debug-agent | ||
|
||
EXPOSE 10027 | ||
|
||
ENTRYPOINT ["/bin/debug-agent"] | ||
CMD ["/start.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package agent | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// List of LXC filesystem files | ||
const ( | ||
MemFile string = "/proc/meminfo" | ||
CpuFile string = "/proc/cpuinfo" | ||
UpTimeFile string = "/proc/uptime" | ||
SwapsFile string = "/proc/swaps" | ||
StatFile string = "/proc/stat" | ||
DiskStatsFile string = "/proc/diskstats" | ||
LoadavgFile string = "/proc/loadavg" | ||
) | ||
|
||
var ( | ||
// IsLxcfsEnabled means whether to enable lxcfs | ||
LxcfsEnabled bool | ||
|
||
// LxcfsRootDir | ||
LxcfsRootDir = "/var/lib/lxc" | ||
|
||
// LxcfsHomeDir means /var/lib/lxc/lxcfs | ||
LxcfsHomeDir = "/var/lib/lxc/lxcfs" | ||
|
||
// LxcfsFiles is a list of LXC files | ||
LxcfsProcFiles = []string{MemFile, CpuFile, UpTimeFile, SwapsFile, StatFile, DiskStatsFile, LoadavgFile} | ||
) | ||
|
||
// CheckLxcfsMount check if the the mount point of lxcfs exists | ||
func CheckLxcfsMount() error { | ||
isMount := false | ||
f, err := os.Open("/proc/1/mountinfo") | ||
if err != nil { | ||
return fmt.Errorf("Check lxcfs mounts failed: %v", err) | ||
} | ||
fr := bufio.NewReader(f) | ||
for { | ||
line, err := fr.ReadBytes('\n') | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return fmt.Errorf("Check lxcfs mounts failed: %v", err) | ||
} | ||
|
||
if bytes.Contains(line, []byte(LxcfsHomeDir)) { | ||
isMount = true | ||
break | ||
} | ||
} | ||
if !isMount { | ||
return fmt.Errorf("%s is not a mount point, please run \" lxcfs %s \" before debug", LxcfsHomeDir, LxcfsHomeDir) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package nsenter | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"strconv" | ||
) | ||
|
||
// MountNSEnter is the client used to enter the mount namespace | ||
type MountNSEnter struct { | ||
Target int // target PID (required) | ||
MountLxcfs bool // enter mount namespace or not | ||
MountFile string // Mount namespace location, default to /proc/PID/ns/mnt | ||
} | ||
|
||
// Execute runs the given command with a default background context | ||
func (cli *MountNSEnter) Execute(command string, args ...string) (stdout, stderr string, err error) { | ||
return cli.ExecuteContext(context.Background(), command, args...) | ||
} | ||
|
||
// ExecuteContext the given command using the specific nsenter config | ||
func (cli *MountNSEnter) ExecuteContext(ctx context.Context, command string, args ...string) (string, string, error) { | ||
cmd, err := cli.setCommand(ctx) | ||
if err != nil { | ||
return "", "", fmt.Errorf("Error when set command: %v", err) | ||
} | ||
|
||
var stdout, stderr bytes.Buffer | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
cmd.Args = append(cmd.Args, command) | ||
cmd.Args = append(cmd.Args, args...) | ||
|
||
err = cmd.Run() | ||
if err != nil { | ||
return stdout.String(), stderr.String(), fmt.Errorf("Error while executing command: %v", err) | ||
} | ||
|
||
return stdout.String(), stderr.String(), nil | ||
} | ||
|
||
func (cli *MountNSEnter) setCommand(ctx context.Context) (*exec.Cmd, error) { | ||
if cli.Target == 0 { | ||
return nil, fmt.Errorf("Target must be specified") | ||
} | ||
var args []string | ||
args = append(args, "--target", strconv.Itoa(cli.Target)) | ||
|
||
if cli.MountLxcfs { | ||
if cli.MountFile != "" { | ||
args = append(args, fmt.Sprintf("--mount=%s", cli.MountFile)) | ||
} else { | ||
args = append(args, "--mount") | ||
} | ||
} | ||
|
||
cmd := exec.CommandContext(ctx, "/usr/bin/nsenter", args...) | ||
return cmd, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.