Skip to content

Commit

Permalink
Add value check for sort-by flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Dyanngg committed Dec 2, 2020
1 parent c02b813 commit f585845
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
11 changes: 2 additions & 9 deletions pkg/agent/apiserver/handlers/networkpolicy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ import (
"github.com/vmware-tanzu/antrea/pkg/querier"
)

const sortByEffectivePriority = "effectivePriority"

// HandleFunc creates a http.HandlerFunc which uses an AgentNetworkPolicyInfoQuerier
// to query network policy rules in current agent.
func HandleFunc(aq agentquerier.AgentQuerier) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
npFilter, err := parseURLQuery(r.URL.Query())
npFilter, err := newFilterFromURLQuery(r.URL.Query())
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down Expand Up @@ -67,7 +65,7 @@ var mapToNetworkPolicyType = map[string]cpv1beta.NetworkPolicyType{
}

// Create a Network Policy Filter from URL Query
func parseURLQuery(query url.Values) (*querier.NetworkPolicyQueryFilter, error) {
func newFilterFromURLQuery(query url.Values) (*querier.NetworkPolicyQueryFilter, error) {
namespace := query.Get("namespace")
pod := query.Get("pod")
if pod != "" && namespace == "" {
Expand All @@ -86,11 +84,6 @@ func parseURLQuery(query url.Values) (*querier.NetworkPolicyQueryFilter, error)
return nil, fmt.Errorf("with a name, none of the other fields can be set")
}

sortBy := query.Get("sort-by")
if sortBy != "" && sortBy != sortByEffectivePriority {
return nil, fmt.Errorf("unsupported value for sort-by option. Current supported value is %s", sortByEffectivePriority)
}

return &querier.NetworkPolicyQueryFilter{
Name: name,
SourceName: source,
Expand Down
35 changes: 25 additions & 10 deletions pkg/antctl/command_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const (
)

const (
maxTableOutputColumnLength int = 50
maxTableOutputColumnLength int = 50
sortByEffectivePriority string = "effectivePriority"
)

// commandGroup is used to group commands, it could be specified in commandDefinition.
Expand Down Expand Up @@ -129,9 +130,10 @@ func (e *resourceEndpoint) flags() []flagInfo {
}
if e.groupVersionResource == &v1beta2.NetworkPolicyVersionResource {
flags = append(flags, flagInfo{
name: "sort-by",
defaultValue: "",
usage: "Get NetworkPolicies in specific order. Current supported value is effectivePriority.",
name: "sort-by",
defaultValue: "",
supportedValues: []string{sortByEffectivePriority},
usage: "Get NetworkPolicies in specific order. Current supported value is effectivePriority.",
})
}
return flags
Expand Down Expand Up @@ -163,11 +165,12 @@ type endpoint struct {

// flagInfo represents a command-line flag that can be provided when invoking an antctl command.
type flagInfo struct {
name string
shorthand string
defaultValue string
arg bool
usage string
name string
shorthand string
defaultValue string
supportedValues []string
arg bool
usage string
}

// rawCommand defines a full function cobra.Command which lets developers
Expand Down Expand Up @@ -725,7 +728,7 @@ func (cd *commandDefinition) output(resp io.Reader, writer io.Writer, ft formatt
return cd.tableOutput(obj, writer)
}
default:
return fmt.Errorf("unsupport format type: %v", ft)
return fmt.Errorf("unsupported format type: %v", ft)
}
return nil
}
Expand All @@ -741,6 +744,9 @@ func (cd *commandDefinition) collectFlags(cmd *cobra.Command, args []string) (ma
} else {
vs, err := cmd.Flags().GetString(f.name)
if err == nil && len(vs) != 0 {
if f.supportedValues != nil && !cd.validateFlagValue(vs, f.supportedValues) {
return nil, fmt.Errorf("unsupported value %s for flag %s", vs, f.name)
}
argMap[f.name] = vs
continue
}
Expand All @@ -753,6 +759,15 @@ func (cd *commandDefinition) collectFlags(cmd *cobra.Command, args []string) (ma
return argMap, nil
}

func (cd *commandDefinition) validateFlagValue(val string, supportedValues []string) bool {
for _, s := range supportedValues {
if s == val {
return true
}
}
return false
}

// newCommandRunE creates the RunE function for the command. The RunE function
// checks the args according to argOption and flags.
func (cd *commandDefinition) newCommandRunE(c *client) func(*cobra.Command, []string) error {
Expand Down

0 comments on commit f585845

Please sign in to comment.