Skip to content

Commit

Permalink
Reduce file mount needs when deploy in the Kubernetes (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu authored Sep 25, 2024
1 parent 77a4205 commit 782bcd5
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 128 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Release Notes.
* Introduce `MonitorFilter` into access log module.
* Support monitoring ztunnel to adapt istio ambient mode.
* Enhance get connection address strategy in access log module.
* Reduce file mount needs when deploy in the Kubernetes, split env name `ROVER_HOST_MAPPING` to `ROVER_HOST_PROC_MAPPING` and `ROVER_HOST_ETC_MAPPING`.

#### Bug Fixes
* Fixed the issue where `conntrack` could not find the Reply IP in the access log module.
Expand Down
2 changes: 1 addition & 1 deletion pkg/accesslog/collector/ztunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (z *ZTunnelCollector) findZTunnelProcessAndCollect() error {
}

func (z *ZTunnelCollector) collectZTunnelProcess(p *process.Process) error {
pidExeFile := host.GetFileInHost(fmt.Sprintf("/proc/%d/exe", p.Pid))
pidExeFile := host.GetHostProcInHost(fmt.Sprintf("%d/exe", p.Pid))
elfFile, err := elf.NewFile(pidExeFile)
if err != nil {
return fmt.Errorf("read executable file error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/accesslog/common/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (c *ConnectionManager) Start(ctx context.Context, accessLogContext *AccessL
}

func (c *ConnectionManager) checkProcessFDExist(pid, fd uint32) bool {
return path.Exists(host.GetFileInHost(fmt.Sprintf("/proc/%d/fd/%d", pid, fd)))
return path.Exists(host.GetHostProcInHost(fmt.Sprintf("%d/fd/%d", pid, fd)))
}

func (c *ConnectionManager) Stop() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/process/finders/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func tryToFindFileExecutePath(ps *process.Process) string {
if path.Exists(exe) {
return exe
}
pathInNs := host.GetFileInHost(fmt.Sprintf("/proc/%d/root%s", ps.Pid, exe))
pathInNs := host.GetHostProcInHost(fmt.Sprintf("%d/root%s", ps.Pid, exe))
if path.Exists(pathInNs) {
return pathInNs
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/process/finders/kubernetes/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (f *ProcessFinder) buildEntity(err error, ps *process.Process, pc *PodConta
}

func (f *ProcessFinder) getProcessCGroup(pid int32) ([]string, error) {
processCgroupFilePath := host.GetFileInHost(fmt.Sprintf("/proc/%d/cgroup", pid))
processCgroupFilePath := host.GetHostProcInHost(fmt.Sprintf("%d/cgroup", pid))
cgroupFile, err := os.Open(processCgroupFilePath)
if err != nil {
return nil, err
Expand Down
58 changes: 0 additions & 58 deletions pkg/tools/btf/check.go

This file was deleted.

34 changes: 24 additions & 10 deletions pkg/tools/host/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,37 @@ package host

import (
"os"
"strings"
"path"
)

var hostMappingPath string
var (
hostProcMappingPath string
hostEtcMappingPath string
)

func init() {
hostMappingPath = os.Getenv("ROVER_HOST_MAPPING")
hostProcMappingPath = os.Getenv("ROVER_HOST_PROC_MAPPING")
// adapt with gopsutil framework to read the right process directory of host
if hostMappingPath != "" {
os.Setenv("HOST_PROC", hostMappingPath+"/proc")
if hostProcMappingPath != "" {
os.Setenv("HOST_PROC", hostProcMappingPath)
}
hostEtcMappingPath = os.Getenv("ROVER_HOST_ETC_MAPPING")
}

func GetHostProcInHost(procSubPath string) string {
if hostProcMappingPath != "" {
return cleanPath(hostProcMappingPath + "/" + procSubPath)
}
return cleanPath("/proc/" + procSubPath)
}

// GetFileInHost means add the host root mapping prefix, it's dependent when the rover is deploy in a container
func GetFileInHost(absPath string) string {
if hostMappingPath != "" && strings.HasPrefix(absPath, hostMappingPath) {
return absPath
func GetHostEtcInHost(etcSubPath string) string {
if hostEtcMappingPath != "" {
return cleanPath(hostEtcMappingPath + "/" + etcSubPath)
}
return hostMappingPath + absPath
return cleanPath("/etc/" + etcSubPath)
}

func cleanPath(p string) string {
return path.Clean(p)
}
8 changes: 4 additions & 4 deletions pkg/tools/operator/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type DistributionInfo struct {
// GetDistributionInfo of machine
func GetDistributionInfo() (*DistributionInfo, error) {
var result = &DistributionInfo{}
tryingToFindDistributionByReleaseFile(result, "/etc/lsb-release", "DISTRIB_ID", "DISTRIB_RELEASE", "")
tryingToFindDistributionByReleaseFile(result, "/etc/os-release", "ID", "VERSION_ID", "")
tryingToFindDistributionByReleaseFile(result, "lsb-release", "DISTRIB_ID", "DISTRIB_RELEASE", "")
tryingToFindDistributionByReleaseFile(result, "os-release", "ID", "VERSION_ID", "")

tryingToFindDistributionByCommand(result, "Distributor ID", "Release", "", "lsb_release", "-a")
tryingToFindDistributionByCommand(result, "", "", "Architecture", "hostnamectl")
Expand All @@ -56,11 +56,11 @@ func GetDistributionInfo() (*DistributionInfo, error) {
return result, nil
}

func tryingToFindDistributionByReleaseFile(data *DistributionInfo, filename, nameKey, versionKey, architectureKey string) {
func tryingToFindDistributionByReleaseFile(data *DistributionInfo, etcSubFilename, nameKey, versionKey, architectureKey string) {
if data.AllDataSuccess() {
return
}
file, err := os.Open(host.GetFileInHost(filename))
file, err := os.Open(host.GetHostEtcInHost(etcSubFilename))
if err != nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/tools/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ var (

// KernelFileProfilingStat is works for read the kernel and get is support for kernel symbol analyze
func KernelFileProfilingStat() (*profiling.Info, error) {
if !kernelFinder.IsSupport(profiling.KernelSymbolFilePath) {
if !kernelFinder.IsSupport(profiling.KernelProcSymbolFilePath) {
return nil, fmt.Errorf("not support kernel space profiling")
}
return kernelFinder.Analyze(profiling.KernelSymbolFilePath)
return kernelFinder.Analyze(profiling.KernelProcSymbolFilePath)
}

// ProfilingStat is validating the exe file could be profiling and get info
Expand Down Expand Up @@ -95,7 +95,7 @@ func Modules(pid int32) ([]*profiling.Module, error) {

func analyzeProfilingInfo(context *analyzeContext, pid int32) (*profiling.Info, error) {
// analyze process mapping
mapFile, _ := os.Open(host2.GetFileInHost(fmt.Sprintf("/proc/%d/maps", pid)))
mapFile, _ := os.Open(host2.GetHostProcInHost(fmt.Sprintf("%d/maps", pid)))
scanner := bufio.NewScanner(mapFile)
modules := make(map[string]*profiling.Module)
for scanner.Scan() {
Expand Down Expand Up @@ -126,7 +126,7 @@ func analyzeProfilingInfo(context *analyzeContext, pid int32) (*profiling.Info,
module.Ranges = append(module.Ranges, moduleRange)
continue
}
modulePath := host2.GetFileInHost(fmt.Sprintf("/proc/%d/root%s", pid, moduleName))
modulePath := host2.GetHostProcInHost(fmt.Sprintf("%d/root%s", pid, moduleName))
if !path.Exists(modulePath) {
log.Debugf("could not found the module, ignore. name: %s, path: %s", moduleName, modulePath)
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/profiling/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
type ModuleType int8

var (
KernelSymbolFilePath = "/proc/kallsyms"
KernelProcSymbolFilePath = "kallsyms" // after host.GetHostProcInHost, should be "/proc/kallsyms"

log = logger.GetLogger("tools", "profiling")
)
Expand Down
8 changes: 4 additions & 4 deletions pkg/tools/profiling/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ type KernelFinder struct {
}

func NewKernelFinder() *KernelFinder {
stat, _ := os.Stat(host.GetFileInHost(KernelSymbolFilePath))
stat, _ := os.Stat(host.GetHostProcInHost(KernelProcSymbolFilePath))
return &KernelFinder{kernelFileExists: stat != nil}
}

func (k *KernelFinder) IsSupport(filepath string) bool {
if filepath != KernelSymbolFilePath {
if filepath != KernelProcSymbolFilePath {
return false
}
stat, _ := os.Stat(filepath)
stat, _ := os.Stat(host.GetHostProcInHost(filepath))
return stat != nil
}

func (k *KernelFinder) Analyze(filepath string) (*Info, error) {
kernelPath, err := os.Open(filepath)
kernelPath, err := os.Open(host.GetHostProcInHost(filepath))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tools/ssl/gotls.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (r *Register) GoTLS(symbolAddrMap *ebpf.Map, write, writeRet, read, readRet
if buildVersionSymbol == nil {
return false, nil
}
pidExeFile := host.GetFileInHost(fmt.Sprintf("/proc/%d/exe", r.pid))
pidExeFile := host.GetHostProcInHost(fmt.Sprintf("%d/exe", r.pid))
elfFile, err := elf.NewFile(pidExeFile)
if err != nil {
return false, fmt.Errorf("read executable file error: %v", err)
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/cases/access_log/rover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ spec:
- SYS_ADMIN
privileged: true
volumeMounts:
- name: host
mountPath: /host
- name: host-proc
mountPath: /host-proc
readOnly: true
- name: host-sys
mountPath: /sys
Expand All @@ -90,8 +90,8 @@ spec:
value: "false"
- name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_ACTIVE
value: "false"
- name: ROVER_HOST_MAPPING
value: /host
- name: ROVER_HOST_PROC_MAPPING
value: /host-proc
- name: ROVER_ACCESS_LOG_ACTIVE
value: "true"
- name: ROVER_ACCESS_LOG_FLUSH_PERIOD
Expand All @@ -100,9 +100,9 @@ spec:
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
volumes:
- name: host
- name: host-proc
hostPath:
path: /host
path: /host/proc
type: Directory
- name: host-sys
hostPath:
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/cases/process/istio/rover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ spec:
- SYS_ADMIN
privileged: true
volumeMounts:
- name: host
mountPath: /host
- name: host-proc
mountPath: /host-proc
readOnly: true
env:
- name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ACTIVE
Expand All @@ -89,15 +89,15 @@ spec:
value: "true"
- name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_K8S_SERVICE_ACTIVE
value: "false"
- name: ROVER_HOST_MAPPING
value: /host
- name: ROVER_HOST_PROC_MAPPING
value: /host-proc
- name: ROVER_CORE_CLUSTER_NAME
value: e2e
hostPID: true
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
volumes:
- name: host
- name: host-proc
hostPath:
path: /host
path: /host/proc
type: Directory
12 changes: 6 additions & 6 deletions test/e2e/cases/profiling/continuous/rover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ spec:
- SYS_ADMIN
privileged: true
volumeMounts:
- name: host
mountPath: /host
- name: host-proc
mountPath: /host-proc
readOnly: true
env:
- name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ACTIVE
Expand All @@ -93,13 +93,13 @@ spec:
value: "test-continuous"
- name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_K8S_SERVICE_INSTANCE_NAME
value: "test-instance"
- name: ROVER_HOST_MAPPING
value: /host
- name: ROVER_HOST_PROC_MAPPING
value: /host-proc
hostPID: true
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
volumes:
- name: host
- name: host-proc
hostPath:
path: /host
path: /host/proc
type: Directory
12 changes: 6 additions & 6 deletions test/e2e/cases/profiling/task/network/envoy/rover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ spec:
- SYS_ADMIN
privileged: true
volumeMounts:
- name: host
mountPath: /host
- name: host-proc
mountPath: /host-proc
readOnly: true
env:
- name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ACTIVE
Expand All @@ -83,8 +83,8 @@ spec:
value: skywalking-oap.istio-system:11800
- name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_K8S_SERVICE_ACTIVE
value: "false"
- name: ROVER_HOST_MAPPING
value: /host
- name: ROVER_HOST_PROC_MAPPING
value: /host-proc
- name: ROVER_LOGGER_LEVEL
value: DEBUG
- name: ROVER_PROCESS_DISCOVERY_KUBERNETES_ANALYZER_ISTIO_APPLICATION_PROCESS_NAME
Expand All @@ -97,7 +97,7 @@ spec:
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
volumes:
- name: host
- name: host-proc
hostPath:
path: /host
path: /host/proc
type: Directory
Loading

0 comments on commit 782bcd5

Please sign in to comment.