Skip to content

Commit

Permalink
feat(exporter): add sidecar mode
Browse files Browse the repository at this point in the history
Signed-off-by: xiayu.lyt <xiayu.lyt@alibaba-inc.com>
  • Loading branch information
Lyt99 committed Aug 4, 2023
1 parent a63af93 commit 0a56cf9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
5 changes: 5 additions & 0 deletions pkg/exporter/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io"
"os"

"github.com/alibaba/kubeskoop/pkg/exporter/nettop"

"github.com/spf13/cobra"
"golang.org/x/exp/slog"
)
Expand All @@ -14,6 +16,7 @@ var (
Use: "inspector",
Short: "network inspection tool",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
nettop.Init(sidecar)
if debug {
opts := slog.HandlerOptions{
AddSource: true,
Expand All @@ -29,6 +32,7 @@ var (

debug bool
verbose bool
sidecar bool
)

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -45,4 +49,5 @@ func init() {

rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable debug log information")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable detail information")
rootCmd.PersistentFlags().BoolVarP(&sidecar, "sidecar", "", false, "use inspector for sidecar mode")
}
44 changes: 28 additions & 16 deletions pkg/exporter/nettop/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nettop
import (
"context"
"fmt"
"os"
"time"

"github.com/patrickmn/go-cache"
Expand Down Expand Up @@ -70,6 +71,10 @@ func (e *Entity) GetLabel(labelkey string) (string, bool) {
}

func (e *Entity) GetPodName() string {
if env := os.Getenv("INSPECTOR_PODNAME"); env != "" {
return env
}

if e.netnsMeta.isHostNetwork {
return hostNetwork
}
Expand All @@ -82,6 +87,10 @@ func (e *Entity) GetPodName() string {
}

func (e *Entity) GetPodNamespace() string {
if env := os.Getenv("INSPECTOR_PODNAMESPACE"); env != "" {
return env
}

if e.netnsMeta.isHostNetwork {
return hostNetwork
}
Expand Down Expand Up @@ -267,25 +276,28 @@ func cacheNetTopology() error {

logger.Debug("finished get all nsfs mount point")

// get pod meta info
podMap, err := getPodMetas(rcrisvc)
if err != nil {
logger.Warn("get pod meta failed %s", err.Error())
return err
}
var podMap map[string]podMeta
if !sidecarEnabled {
// get pod meta info
podMap, err := getPodMetas(rcrisvc)
if err != nil {
logger.Warn("get pod meta failed %s", err.Error())
return err
}

// if use docker, get docker sandbox
if top.Crimeta.RuntimeName == "docker" {
for sandbox, pm := range podMap {
if pm.nspath == "" && pm.pid == 0 {
pid, err := getPidForContainerBySock(sandbox)
if err != nil {
logger.Warn("get docker container", "sandbox", sandbox, "err", err.Error())
continue
// if use docker, get docker sandbox
if top.Crimeta.RuntimeName == "docker" {
for sandbox, pm := range podMap {
if pm.nspath == "" && pm.pid == 0 {
pid, err := getPidForContainerBySock(sandbox)
if err != nil {
logger.Warn("get docker container", "sandbox", sandbox, "err", err.Error())
continue
}
pm.pid = pid
}
pm.pid = pid
podMap[sandbox] = pm
}
podMap[sandbox] = pm
}
}

Expand Down
20 changes: 13 additions & 7 deletions pkg/exporter/nettop/nodemeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ var (

top = metacache{}
runtimeEndpoints = []string{"/var/run/dockershim.sock", "/run/containerd/containerd.sock"}

sidecarEnabled bool
)

type metacache struct {
NodeMeta
}

func init() {
func Init(sidecar bool) {
logger = slog.New(slog.NewJSONHandler(io.Discard))
top.NodeName = getNodeName()
kr, err := getKernelRelease()
Expand All @@ -33,14 +35,18 @@ func init() {
} else {
top.Kernel = kr
}
// use empty cri meta, fulfillize it when updated
c := &CriMeta{}
err = c.Update()
if err != nil {
logger.Warn("update cri meta failed %s", err.Error())
if !sidecar {
// use empty cri meta, fulfillize it when updated
c := &CriMeta{}
err = c.Update()
if err != nil {
logger.Warn("update cri meta failed %s", err.Error())
}

top.Crimeta = c
}

top.Crimeta = c
sidecarEnabled = sidecar
}

func getNodeName() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/exporter/nettop/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func getPodMetas(client internalapi.RuntimeService) (map[string]podMeta, error)
for _, sandbox := range listresponse {
status, err := client.PodSandboxStatus(sandbox.GetId(), true)
if err != nil {
logger.Debug("get pod sanbox %s status failed with %s", sandbox.GetId(), err)
logger.Debug("get pod sandbox %s status failed with %s", sandbox.GetId(), err)
continue
}
pm := podMeta{
Expand Down

0 comments on commit 0a56cf9

Please sign in to comment.