Skip to content

Commit 9e53206

Browse files
gcp-cherry-pick-bot[bot]jumpe1MasonM
authored
fix: don't print help for non-validation errors. Fixes #14234 (cherry-pick #14249) (#14283)
Signed-off-by: Koichi Shimada <[email protected]> Signed-off-by: Mason Malone <[email protected]> Co-authored-by: koichi <[email protected]> Co-authored-by: Mason Malone <[email protected]>
1 parent 0a0ef61 commit 9e53206

File tree

5 files changed

+76
-52
lines changed

5 files changed

+76
-52
lines changed

cmd/argoexec/commands/root.go

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ func NewRootCommand() *cobra.Command {
5555
},
5656
PersistentPreRun: func(cmd *cobra.Command, args []string) {
5757
initConfig()
58+
59+
// Disable printing of usage string on errors, except for argument validation errors
60+
// (i.e. when the "Args" function returns an error).
61+
//
62+
// This is set here instead of directly in "command" because Cobra
63+
// executes PersistentPreRun after performing argument validation:
64+
// https://github.com/spf13/cobra/blob/3a5efaede9d389703a792e2f7bfe3a64bc82ced9/command.go#L939-L957
65+
cmd.SilenceUsage = true
5866
},
5967
}
6068

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: argoproj.io/v1alpha1
2+
kind: Workflow
3+
metadata:
4+
generateName: serviceaccount-insufficient-permissions
5+
spec:
6+
# This ServiceAccount is only intended for use in webhooks, and doesn't have
7+
# the permissions needed by the executor.
8+
serviceAccountName: github.com
9+
entrypoint: main
10+
templates:
11+
- name: main
12+
container:
13+
image: argoproj/argosay:v2

test/e2e/fixtures/then.go

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fixtures
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"reflect"
78
"testing"
89
"time"
@@ -264,6 +265,25 @@ func (t *Then) ExpectPods(f func(t *testing.T, pods []apiv1.Pod)) *Then {
264265
return t
265266
}
266267

268+
func (t *Then) ExpectContainerLogs(container string, f func(t *testing.T, logs string)) *Then {
269+
t.t.Helper()
270+
271+
stream, err := t.kubeClient.CoreV1().Pods(t.wf.Namespace).GetLogs(t.wf.Name, &apiv1.PodLogOptions{Container: container}).Stream(context.Background())
272+
if err != nil {
273+
t.t.Fatal(err)
274+
}
275+
276+
defer stream.Close()
277+
logBytes, err := io.ReadAll(stream)
278+
if err != nil {
279+
t.t.Fatal(err)
280+
}
281+
282+
f(t.t, string(logBytes))
283+
284+
return t
285+
}
286+
267287
func (t *Then) ExpectWorkflowTaskSet(block func(t *testing.T, wfts *wfv1.WorkflowTaskSet)) *Then {
268288
t.t.Helper()
269289
ctx := context.Background()

test/e2e/functional_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -1446,3 +1446,20 @@ func (s *FunctionalSuite) TestWorkflowParallelismDAGFailFast() {
14461446
assert.Equal(t, wfv1.NodeSucceeded, status.Nodes.FindByDisplayName("task2").Phase)
14471447
})
14481448
}
1449+
1450+
func (s *FunctionalSuite) TestWorkflowInvalidServiceAccountError() {
1451+
s.Given().
1452+
Workflow("@expectedfailures/serviceaccount-insufficient-permissions.yaml").
1453+
When().
1454+
SubmitWorkflow().
1455+
WaitForWorkflow(fixtures.ToBeErrored).
1456+
Then().
1457+
ExpectContainerLogs("main", func(t *testing.T, logs string) {
1458+
assert.Contains(t, logs, "hello argo")
1459+
}).
1460+
ExpectContainerLogs("wait", func(t *testing.T, logs string) {
1461+
assert.Contains(t, logs, "Error: workflowtaskresults.argoproj.io is forbidden: User \"system:serviceaccount:argo:github.com\" cannot create resource")
1462+
// Shouldn't have print help text
1463+
assert.NotContains(t, logs, "Usage:")
1464+
})
1465+
}

test/e2e/retry_test.go

+18-52
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
package e2e
44

55
import (
6-
"context"
7-
"io"
86
"strings"
97
"testing"
108
"time"
@@ -123,8 +121,6 @@ spec:
123121
}
124122

125123
func (s *RetryTestSuite) TestWorkflowTemplateWithRetryStrategyInContainerSet() {
126-
var name string
127-
var ns string
128124
s.Given().
129125
WorkflowTemplate("@testdata/workflow-template-with-containerset.yaml").
130126
Workflow(`
@@ -142,55 +138,25 @@ spec:
142138
ExpectWorkflow(func(t *testing.T, metadata *metav1.ObjectMeta, status *wfv1.WorkflowStatus) {
143139
assert.Equal(t, wfv1.WorkflowFailed, status.Phase)
144140
}).
145-
ExpectWorkflowNode(func(status v1alpha1.NodeStatus) bool {
146-
return status.Name == "workflow-template-containerset"
147-
}, func(t *testing.T, status *v1alpha1.NodeStatus, pod *apiv1.Pod) {
148-
name = pod.GetName()
149-
ns = pod.GetNamespace()
141+
// Success, no need retry
142+
ExpectContainerLogs("c1", func(t *testing.T, logs string) {
143+
count := strings.Count(logs, "capturing logs")
144+
assert.Equal(t, 1, count)
145+
assert.Contains(t, logs, "hi")
146+
}).
147+
// Command err. No retry logic is entered.
148+
ExpectContainerLogs("c2", func(t *testing.T, logs string) {
149+
count := strings.Count(logs, "capturing logs")
150+
assert.Equal(t, 0, count)
151+
assert.Contains(t, logs, "executable file not found in $PATH")
152+
}).
153+
// Retry when err.
154+
ExpectContainerLogs("c3", func(t *testing.T, logs string) {
155+
count := strings.Count(logs, "capturing logs")
156+
assert.Equal(t, 2, count)
157+
countFailureInfo := strings.Count(logs, "intentional failure")
158+
assert.Equal(t, 2, countFailureInfo)
150159
})
151-
// Success, no need retry
152-
s.Run("ContainerLogs", func() {
153-
ctx := context.Background()
154-
podLogOptions := &apiv1.PodLogOptions{Container: "c1"}
155-
stream, err := s.KubeClient.CoreV1().Pods(ns).GetLogs(name, podLogOptions).Stream(ctx)
156-
s.Require().NoError(err)
157-
defer stream.Close()
158-
logBytes, err := io.ReadAll(stream)
159-
s.Require().NoError(err)
160-
output := string(logBytes)
161-
count := strings.Count(output, "capturing logs")
162-
s.Equal(1, count)
163-
s.Contains(output, "hi")
164-
})
165-
// Command err. No retry logic is entered.
166-
s.Run("ContainerLogs", func() {
167-
ctx := context.Background()
168-
podLogOptions := &apiv1.PodLogOptions{Container: "c2"}
169-
stream, err := s.KubeClient.CoreV1().Pods(ns).GetLogs(name, podLogOptions).Stream(ctx)
170-
s.Require().NoError(err)
171-
defer stream.Close()
172-
logBytes, err := io.ReadAll(stream)
173-
s.Require().NoError(err)
174-
output := string(logBytes)
175-
count := strings.Count(output, "capturing logs")
176-
s.Equal(0, count)
177-
s.Contains(output, "executable file not found in $PATH")
178-
})
179-
// Retry when err.
180-
s.Run("ContainerLogs", func() {
181-
ctx := context.Background()
182-
podLogOptions := &apiv1.PodLogOptions{Container: "c3"}
183-
stream, err := s.KubeClient.CoreV1().Pods(ns).GetLogs(name, podLogOptions).Stream(ctx)
184-
s.Require().NoError(err)
185-
defer stream.Close()
186-
logBytes, err := io.ReadAll(stream)
187-
s.Require().NoError(err)
188-
output := string(logBytes)
189-
count := strings.Count(output, "capturing logs")
190-
s.Equal(2, count)
191-
countFailureInfo := strings.Count(output, "intentional failure")
192-
s.Equal(2, countFailureInfo)
193-
})
194160
}
195161

196162
func (s *RetryTestSuite) TestRetryNodeAntiAffinity() {

0 commit comments

Comments
 (0)