Skip to content

Commit

Permalink
fix: Fix panic and provide better error message on watch endpoint (ar…
Browse files Browse the repository at this point in the history
  • Loading branch information
simster7 authored Jul 27, 2020
1 parent 491f4f7 commit f7be20c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion server/workflow/workflow_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (s *workflowServer) WatchWorkflows(req *workflowpkg.WatchWorkflowsRequest,
opts = req.ListOptions
wfName, err := argoutil.RecoverWorkflowNameFromSelectorString(opts.FieldSelector)
if err != nil {
return err
return fmt.Errorf("malformed request: workflows must be specified with a 'metadata.name=...' field selector; unable to parse: %w", err)
}
if len(wfName) > 0 {
wf, err := s.getWorkflow(wfClient, req.Namespace, wfName, metav1.GetOptions{})
Expand Down
12 changes: 6 additions & 6 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,21 @@ func RecoverIndexFromNodeName(name string) int {
func GenerateFieldSelectorFromWorkflowName(wfName string) string {
result := fields.ParseSelectorOrDie(fmt.Sprintf("metadata.name=%s", wfName)).String()
compare, err := RecoverWorkflowNameFromSelectorString(result)
if err != nil {
if err != nil || wfName != compare {
log.WithFields(log.Fields{"wfName": wfName}).Error(err)
}
if wfName != compare {
panic(fmt.Sprintf("Could not recover field selector from workflow name. Expected '%s' but got '%s'\n", wfName, compare))
}
return result
}

func RecoverWorkflowNameFromSelectorString(selector string) (string, error) {
nameIndex := strings.Index(selector, "=")
if nameIndex < 0 {
return "", fmt.Errorf("incorrect prefix: selector is not formatted correctly")
}
prefix := selector[:nameIndex]
if prefix != "metadata.name" {
return "", fmt.Errorf("Incorrect prefix. Expected 'metadata.name' but got '%s'", prefix)
return "", fmt.Errorf("incorrect prefix: expected 'metadata.name' but got '%s'", prefix)
}
name := selector[nameIndex+1:]
return name, nil
return selector[nameIndex+1:], nil
}
3 changes: 3 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ func TestRecoverWorkflowNameFromSelectorStringError(t *testing.T) {
name, err := RecoverWorkflowNameFromSelectorString("whatever=whalesay")
assert.NotNil(t, err)
assert.Equal(t, name, "")
assert.NotPanics(t, func() {
_, _ = RecoverWorkflowNameFromSelectorString("whatever")
})
}

0 comments on commit f7be20c

Please sign in to comment.