Skip to content

Commit

Permalink
feat: support check mode for node to node, node to pod, pod to pod
Browse files Browse the repository at this point in the history
Signed-off-by: baoyinghai_yewu <[email protected]>
  • Loading branch information
OrangeBao committed Aug 22, 2024
1 parent a247b26 commit 64269b1
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 19 deletions.
7 changes: 5 additions & 2 deletions pkg/command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func NewCheckCmd() *cobra.Command {
func (o *CheckOptions) LoadConfig() {
fromConfig := &share.DoOptions{}
if err := utils.ReadOpt(fromConfig); err == nil {
if len(fromConfig.Mode) == 0 {
fromConfig.Mode = share.Pod
}
once.Do(func() {
klog.Infof("use config from file!!!!!!")
})
Expand All @@ -79,7 +82,7 @@ func (o *CheckOptions) Complete() error {
Version: o.DoOption.Version,
PodWaitTime: o.DoOption.PodWaitTime,
Port: o.DoOption.Port,
EnableHostNetwork: false,
EnableHostNetwork: o.DoOption.GetEnableHostNetwork(true),
EnableAnalysis: false,
}
if err := srcfloater.CompleteFromKubeConfigPath(o.DoOption.SrcKubeConfig, ""); err != nil {
Expand All @@ -97,7 +100,7 @@ func (o *CheckOptions) Complete() error {
Version: o.DoOption.Version,
PodWaitTime: o.DoOption.PodWaitTime,
Port: o.DoOption.Port,
EnableHostNetwork: false,
EnableHostNetwork: o.DoOption.GetEnableHostNetwork(false),
EnableAnalysis: false,
}
if err := dstfloater.CompleteFromKubeConfigPath(o.DoOption.DstKubeConfig, ""); err != nil {
Expand Down
7 changes: 5 additions & 2 deletions pkg/command/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func NewCleanCmd() *cobra.Command {
func (o *CleanOptions) LoadConfig() {
fromConfig := &share.DoOptions{}
if err := utils.ReadOpt(fromConfig); err == nil {
if len(fromConfig.Mode) == 0 {
fromConfig.Mode = share.Pod
}
once.Do(func() {
klog.Infof("use config from file!!!!!!")
})
Expand All @@ -68,7 +71,7 @@ func (o *CleanOptions) Complete() error {
Version: o.DoOption.Version,
PodWaitTime: o.DoOption.PodWaitTime,
Port: o.DoOption.Port,
EnableHostNetwork: false,
EnableHostNetwork: o.DoOption.GetEnableHostNetwork(true),
EnableAnalysis: false,
}
if err := srcfloater.CompleteFromKubeConfigPath(o.DoOption.SrcKubeConfig, ""); err != nil {
Expand All @@ -86,7 +89,7 @@ func (o *CleanOptions) Complete() error {
Version: o.DoOption.Version,
PodWaitTime: o.DoOption.PodWaitTime,
Port: o.DoOption.Port,
EnableHostNetwork: false,
EnableHostNetwork: o.DoOption.GetEnableHostNetwork(false),
EnableAnalysis: false,
}
if err := dstfloater.CompleteFromKubeConfigPath(o.DoOption.DstKubeConfig, ""); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (o *InitOptions) Run() error {
// dst
DstImageRepository: "",
DstKubeConfig: "",
Mode: share.Pod,
}

if err := utils.WriteOpt(doOptions); err != nil {
Expand Down
7 changes: 5 additions & 2 deletions pkg/command/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func NewResumeCmd() *cobra.Command {
func (o *ResumeOptions) LoadConfig() {
fromConfig := &share.DoOptions{}
if err := utils.ReadOpt(fromConfig); err == nil {
if len(fromConfig.Mode) == 0 {
fromConfig.Mode = share.Pod
}
once.Do(func() {
klog.Infof("use config from file!!!!!!")
})
Expand All @@ -72,7 +75,7 @@ func (o *ResumeOptions) Complete() error {
Version: o.DoOption.Version,
PodWaitTime: o.DoOption.PodWaitTime,
Port: o.DoOption.Port,
EnableHostNetwork: false,
EnableHostNetwork: o.DoOption.GetEnableHostNetwork(true),
EnableAnalysis: false,
}
if err := srcfloater.CompleteFromKubeConfigPath(o.DoOption.SrcKubeConfig, ""); err != nil {
Expand All @@ -90,7 +93,7 @@ func (o *ResumeOptions) Complete() error {
Version: o.DoOption.Version,
PodWaitTime: o.DoOption.PodWaitTime,
Port: o.DoOption.Port,
EnableHostNetwork: false,
EnableHostNetwork: o.DoOption.GetEnableHostNetwork(false),
EnableAnalysis: false,
}
if err := dstfloater.CompleteFromKubeConfigPath(o.DoOption.DstKubeConfig, ""); err != nil {
Expand Down
84 changes: 75 additions & 9 deletions pkg/command/share/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ 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"`
Namespace string `json:"namespace,omitempty"`
Version string `json:"version,omitempty"`
Mode ModeType `json:"mode,omitempty"`

Protocol string `json:"protocol,omitempty"`
PodWaitTime int `json:"podWaitTime,omitempty"`
Expand All @@ -38,33 +48,78 @@ type DoOptions struct {
ResumeRecord []*PrintCheckData `json:"-"`
}

func (o *DoOptions) GetEnableHostNetwork(isSrc bool) bool {
if isSrc {
switch o.Mode {
case Node, NodeToPod:
return true
case Pod, PodToNode:
return false
}
} else {
switch o.Mode {
case Node, PodToNode:
return true
case NodeToPod, Pod:
return false
}
}
return false
}

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 @@ -149,7 +204,13 @@ func (o *DoOptions) RunRange(iPodInfos []*FloatInfo, jPodInfos []*FloatInfo) []*
cmdObj = command.NewCmd(o.Protocol, o.TargetHostToLookup, o.TargetDNSServer)
} else {
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 @@ -163,6 +224,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 Down
16 changes: 15 additions & 1 deletion pkg/command/share/floater.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,21 @@ func (f *Floater) applyDaemonSet() error {
if err != nil {
return err
}
_, err = f.Client.AppsV1().DaemonSets(f.Namespace).Create(context.Background(), clusterlinkFloaterDaemonSet, metav1.CreateOptions{})

applyFunc := func() error {
_, err = f.Client.AppsV1().DaemonSets(f.Namespace).Create(context.Background(), clusterlinkFloaterDaemonSet, metav1.CreateOptions{})
return err
}

_, err = f.Client.AppsV1().DaemonSets(f.Namespace).Get(context.Background(), f.Name, metav1.GetOptions{})
if err == nil {
applyFunc = func() error {
_, err = f.Client.AppsV1().DaemonSets(f.Namespace).Update(context.Background(), clusterlinkFloaterDaemonSet, metav1.UpdateOptions{})
return err
}
}

err = applyFunc()
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("linkctl floater run error, daemonset options failed: %v", err)
Expand Down
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,9 +3,11 @@ 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
Expand All @@ -17,7 +19,11 @@ func (c *Ping) GetTargetStr() 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
19 changes: 19 additions & 0 deletions pkg/command/share/remote-command/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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)
}

func (w Wrapper) GetTargetStr() string {
return w.Cmd.GetTargetStr()
}
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,6 +11,6 @@ type Protocol string
const (
TCP Protocol = "tcp"
UDP Protocol = "udp"
IPv4 Protocol = "ipv4"
ICMP Protocol = "icmp"
DNS Protocol = "dns"
)

0 comments on commit 64269b1

Please sign in to comment.