Skip to content

Commit

Permalink
feat: support check mode for node to node, node to pod, pod to pod, p…
Browse files Browse the repository at this point in the history
…od to node

Signed-off-by: OrangeBao <[email protected]>
  • Loading branch information
OrangeBao committed Feb 21, 2024
1 parent 0130f51 commit 1a981f8
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cluster/images/buildx.floater.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ ARG TARGETPLATFORM

RUN apk add --no-cache ca-certificates
RUN apk update && apk upgrade
RUN apk add ip6tables iptables curl
RUN apk add ip6tables iptables curl netcat-openbsd

COPY ${TARGETPLATFORM}/${BINARY} /bin/${BINARY}
2 changes: 1 addition & 1 deletion cluster/images/floater.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ ARG BINARY

RUN apk add --no-cache ca-certificates
RUN apk update && apk upgrade
RUN apk add ip6tables iptables curl
RUN apk add ip6tables iptables curl netcat-openbsd

COPY ${BINARY} /bin/${BINARY}
7 changes: 4 additions & 3 deletions pkg/command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func NewInitCmd() *cobra.Command {

func (o *InitOptions) Run() error {
doOptions := share.DoOptions{
Namespace: utils.DefaultNamespace,
Port: "8889",
Namespace: utils.DefaultNamespace,
// Port: "8889",
PodWaitTime: 30,
Protocol: string(utils.TCP),
Protocol: string(utils.ICMP),
MaxNum: 3,
AutoClean: false,
CmdTimeout: 10,
Expand All @@ -61,6 +61,7 @@ func (o *InitOptions) Run() error {
// dst
DstImageRepository: "",
DstKubeConfig: "",
// Mode: "pod",
}

if err := utils.WriteOpt(doOptions); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/command/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ func (o *ResumeOptions) Complete() error {

var resumeData []*share.PrintCheckData

utils.ReadResume(&resumeData)
err := utils.ReadResume(&resumeData)
if err != nil {
klog.Fatal(err)
}

o.DoOption.ResumeRecord = resumeData

Expand Down
66 changes: 58 additions & 8 deletions pkg/command/share/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (
"k8s.io/klog/v2"
)

type ModeType string

const (
Node ModeType = "node"
Pod ModeType = "pod"
NodeToPod ModeType = "node-to-pod"
PodToNode ModeType = "pod-to-node"
)

type DoOptions struct {
Namespace string `json:"namespace,omitempty"`
Version string `json:"version,omitempty"`
Expand All @@ -29,36 +38,64 @@ type DoOptions struct {
SrcFloater *Floater `json:"-"`
DstFloater *Floater `json:"-"`

Mode ModeType `json:"mode,omitempty"`

ResumeRecord []*PrintCheckData `json:"-"`
}

func (o *DoOptions) GetInfo(floater *Floater, isSrc bool) ([]*FloatInfo, error) {
if isSrc {
switch o.Mode {
case Node, NodeToPod:
return floater.GetNodesInfo()
case Pod, PodToNode:
return floater.GetPodInfo()
}
} else {
switch o.Mode {
case Node, PodToNode:
return floater.GetNodesInfo()
case NodeToPod, Pod:
return floater.GetPodInfo()
}
}
return floater.GetPodInfo()
}

func (o *DoOptions) Run() error {
if err := o.SrcFloater.CreateFloater(); err != nil {
return err
}

if o.DstKubeConfig != "" {
srcPodInfos, err := o.SrcFloater.GetPodInfo()
srcInfos, err := o.GetInfo(o.SrcFloater, true)
if err != nil {
return fmt.Errorf("get src cluster podInfos failed: %s", err)
}

if err = o.DstFloater.CreateFloater(); err != nil {
return err
}
var dstPodInfos []*FloatInfo
dstPodInfos, err = o.DstFloater.GetPodInfo()
var dstInfos []*FloatInfo
dstInfos, err = o.GetInfo(o.DstFloater, false)
if err != nil {
return fmt.Errorf("get dist cluster podInfos failed: %s", err)
}

PrintResult(o.RunRange(srcPodInfos, dstPodInfos))
PrintResult(o.RunRange(srcInfos, dstInfos))
} else {
srcPodInfos, err := o.SrcFloater.GetPodInfo()
srcInfos, err := o.GetInfo(o.SrcFloater, true)
if err != nil {
return fmt.Errorf("get src cluster podInfos failed: %s", err)
}
PrintResult(o.RunRange(srcPodInfos, srcPodInfos))

var dstInfos []*FloatInfo
dstInfos, err = o.GetInfo(o.SrcFloater, false)
if err != nil {
return fmt.Errorf("get dist cluster podInfos failed: %s", err)
}

PrintResult(o.RunRange(srcInfos, dstInfos))
}

if o.AutoClean {
Expand Down Expand Up @@ -116,7 +153,13 @@ func (o *DoOptions) RunRange(iPodInfos []*FloatInfo, jPodInfos []*FloatInfo) []*

worker := func(iPodInfo *FloatInfo) {
for _, jPodInfo := range jPodInfos {
for _, ip := range jPodInfo.PodIPs {
targetIPs := jPodInfo.PodIPs
needWrapper := false
if len(jPodInfo.NodeIPs) != 0 {
targetIPs = jPodInfo.NodeIPs
needWrapper = true
}
for _, ip := range targetIPs {
var targetIP string
var err error
var cmdResult *command.Result
Expand All @@ -130,6 +173,11 @@ func (o *DoOptions) RunRange(iPodInfos []*FloatInfo, jPodInfos []*FloatInfo) []*
}
// ToDo RunRange && RunNative func support multiple commands, and the code needs to be optimized
cmdObj := command.NewCmd(o.Protocol, targetIP, o.Port)
if needWrapper {
cmdObj = command.Wrapper{
Cmd: cmdObj,
}
}
cmdResult = o.SrcFloater.CommandExec(iPodInfo, cmdObj)
}
mutex.Lock()
Expand All @@ -139,7 +187,9 @@ func (o *DoOptions) RunRange(iPodInfos []*FloatInfo, jPodInfos []*FloatInfo) []*
})
mutex.Unlock()
}
barctl.Add(1)
if err := barctl.Add(1); err != nil {
klog.Warning(err)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/command/share/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
command "github.com/kosmos.io/netdoctor/pkg/command/share/remote-command"
"github.com/kosmos.io/netdoctor/pkg/utils"
"github.com/olekukonko/tablewriter"
"k8s.io/klog/v2"
)

type PrintCheckData struct {
Expand Down Expand Up @@ -66,5 +67,8 @@ func PrintResult(resultData []*PrintCheckData) {
fmt.Println("")
tableException.Render()

utils.WriteResume(resumeData)
err := utils.WriteResume(resumeData)
if err != nil {
klog.Fatal(err)
}
}
10 changes: 8 additions & 2 deletions pkg/command/share/remote-command/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ package command
import (
"fmt"
"regexp"

"github.com/kosmos.io/netdoctor/pkg/utils"
)

var pingReg, _ = regexp.Compile(`PING[\s\S]*1\spackets\stransmitted,\s1\spackets\sreceived,\s0[%]\spacket\sloss[\s\S]*$`)
var pingReg, _ = regexp.Compile(`PING[\s\S]*\s0[%]\spacket\sloss[\s\S]*$`)

type Ping struct {
TargetIP string
}

func (c *Ping) GetCommandStr() string {
// execute once
return fmt.Sprintf("ping -c 1 %s", c.TargetIP)
if utils.IsIPv6(c.TargetIP) {
return fmt.Sprintf("ping6 -c 1 %s", c.TargetIP)
} else {
return fmt.Sprintf("ping -c 1 %s", c.TargetIP)
}
}

func (c *Ping) ParseResult(result string) *Result {
Expand Down
15 changes: 15 additions & 0 deletions pkg/command/share/remote-command/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package command

import "fmt"

type Wrapper struct {
Cmd Command
}

func (w Wrapper) GetCommandStr() string {
return fmt.Sprintf(`nsenter --target "1" --mount --uts --ipc --net --pid -- %s`, w.Cmd.GetCommandStr())
}

func (w Wrapper) ParseResult(str string) *Result {
return w.Cmd.ParseResult(str)
}
1 change: 1 addition & 0 deletions pkg/manifest/manifest_daemonsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ spec:
value: "{{ .Port }}"
- name: "ENABLE_ANALYSIS"
value: "{{ .EnableAnalysis }}"
hostPID: true
tolerations:
- effect: NoSchedule
operator: Exists
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ type Protocol string
const (
TCP Protocol = "tcp"
UDP Protocol = "udp"
IPv4 Protocol = "ipv4"
ICMP Protocol = "icmp"
)

0 comments on commit 1a981f8

Please sign in to comment.