Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add parameter validate #74

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pkg/skoop/cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func NewSkoopCmd() *cobra.Command {
}
return nil
}

} else {
fmt.Printf("Packet path:\n%+v\n", packetPath.Paths())
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/skoop/context/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package context

import (
"fmt"
"net"

"golang.org/x/exp/slices"

"github.com/alibaba/kubeskoop/pkg/skoop/model"

Expand All @@ -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
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/skoop/context/ui.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package context

import (
"fmt"
"strings"

"github.com/spf13/pflag"
"golang.org/x/exp/slices"
)

type UIConfig struct {
Expand All @@ -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.")
Expand All @@ -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
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/skoop/plugin/flannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.")
Expand All @@ -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
}

Expand Down