diff --git a/pkg/skoop/cmd/app.go b/pkg/skoop/cmd/app.go index 6c15ddab..0aa79eb6 100644 --- a/pkg/skoop/cmd/app.go +++ b/pkg/skoop/cmd/app.go @@ -70,7 +70,6 @@ func NewSkoopCmd() *cobra.Command { } return nil } - } else { fmt.Printf("Packet path:\n%+v\n", packetPath.Paths()) } diff --git a/pkg/skoop/context/task.go b/pkg/skoop/context/task.go index 18fe35c5..297fcf43 100644 --- a/pkg/skoop/context/task.go +++ b/pkg/skoop/context/task.go @@ -2,6 +2,9 @@ package context import ( "fmt" + "net" + + "golang.org/x/exp/slices" "github.com/alibaba/kubeskoop/pkg/skoop/model" @@ -27,6 +30,28 @@ func (tc *TaskConfig) BindFlags(fs *pflag.FlagSet) { } func (tc *TaskConfig) Validate() error { + if tc.Source == "" || tc.Destination.Address == "" { + return fmt.Errorf("source or destination address cannot be empty") + } + + if tc.Destination.Port <= 0 || tc.Destination.Port > 65535 { + return fmt.Errorf("a valid destination port should be provided") + } + + ip := net.ParseIP(tc.Source) + if ip == nil || ip.To4() == nil { + return fmt.Errorf("source address should be a valid IPv4 address") + } + + ip = net.ParseIP(tc.Destination.Address) + if ip == nil || ip.To4() == nil { + return fmt.Errorf("destination address should be a valid IPv4 address") + } + + if !slices.Contains([]string{"tcp", "udp"}, tc.Protocol) { + return fmt.Errorf("protocol should be tcp,udp") + } + return nil } diff --git a/pkg/skoop/context/ui.go b/pkg/skoop/context/ui.go index 26ca79cf..6749b7ad 100644 --- a/pkg/skoop/context/ui.go +++ b/pkg/skoop/context/ui.go @@ -1,7 +1,11 @@ package context import ( + "fmt" + "strings" + "github.com/spf13/pflag" + "golang.org/x/exp/slices" ) type UIConfig struct { @@ -12,6 +16,10 @@ type UIConfig struct { HTTPPort uint } +var ( + supportedFormat = []string{"d2", "svg", "json"} +) + func (c *UIConfig) BindFlags(fs *pflag.FlagSet) { fs.StringVarP(&c.Format, "format", "", "", "Output format of diagnose result, support d2/svg/json. If not set, only print simple path info on console.") fs.StringVarP(&c.Output, "output", "", "", "Output file name, default is output.d2/svg/json in current work directory.") @@ -20,6 +28,9 @@ func (c *UIConfig) BindFlags(fs *pflag.FlagSet) { } func (c *UIConfig) Validate() error { + if c.Format != "" && !slices.Contains(supportedFormat, c.Format) { + return fmt.Errorf("unsupported output format %q, should be %s", c.Format, strings.Join(supportedFormat, ",")) + } return nil } diff --git a/pkg/skoop/plugin/flannel.go b/pkg/skoop/plugin/flannel.go index 8ac88785..27224e1c 100644 --- a/pkg/skoop/plugin/flannel.go +++ b/pkg/skoop/plugin/flannel.go @@ -6,6 +6,8 @@ import ( "net" "strings" + "golang.org/x/exp/slices" + "github.com/spf13/pflag" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -32,6 +34,10 @@ type FlannelConfig struct { Interface string } +var ( + supportedFlannelBackendType = []string{"host-gw", "vxlan", "alloc"} +) + func (f *FlannelConfig) BindFlags(fs *pflag.FlagSet) { fs.StringVarP(&f.BackendType, "flannel-backend-type", "", "", "Backend type for flannel plugin, support host-gw,vxlan,alloc. If not set, it will auto detect from flannel config.") @@ -46,6 +52,10 @@ func (f *FlannelConfig) BindFlags(fs *pflag.FlagSet) { } func (f *FlannelConfig) Validate() error { + if f.BackendType != "" && !slices.Contains(supportedFlannelBackendType, f.BackendType) { + return fmt.Errorf("unsupported flannel backed type %q, should be %s", + f.BackendType, strings.Join(supportedFlannelBackendType, ",")) + } return nil }