Skip to content

Commit 8d8f954

Browse files
authored
fix(resource): catch fatal kubectl errors (argoproj#13321)
Signed-off-by: Anton Gilgur <agilgur5@gmail.com>
1 parent 1a81dfb commit 8d8f954

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

test/e2e/resource_template_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ func (s *ResourceTemplateSuite) TestResourceTemplateWithOutputs() {
165165
})
166166
}
167167

168+
func (s *ResourceTemplateSuite) TestResourceTemplateFailed() {
169+
s.Given().
170+
Workflow("@testdata/resource-templates/failed.yaml").
171+
When().
172+
SubmitWorkflow().
173+
WaitForWorkflow().
174+
Then().
175+
ExpectWorkflow(func(t *testing.T, _ *metav1.ObjectMeta, status *wfv1.WorkflowStatus) {
176+
assert.Equal(t, wfv1.WorkflowFailed, status.Phase)
177+
})
178+
}
179+
168180
func TestResourceTemplateSuite(t *testing.T) {
169181
suite.Run(t, new(ResourceTemplateSuite))
170182
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: argoproj.io/v1alpha1
2+
kind: Workflow
3+
metadata:
4+
generateName: failed-
5+
spec:
6+
entrypoint: main
7+
templates:
8+
- name: main
9+
resource:
10+
action: patch
11+
manifest: |
12+
apiVersion: v1
13+
# kind: Pod -- missing this causes an error
14+
metadata:
15+
name: {{pod.name}}
16+
annotations:
17+
foo: bar

workflow/executor/resource.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"k8s.io/gengo/namer"
2525
gengotypes "k8s.io/gengo/types"
2626
kubectlcmd "k8s.io/kubectl/pkg/cmd"
27+
kubectlutil "k8s.io/kubectl/pkg/cmd/util"
2728

2829
"github.com/argoproj/argo-workflows/v3/errors"
2930
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
@@ -370,6 +371,13 @@ func runKubectl(args ...string) ([]byte, error) {
370371
defer func() {
371372
os.Args = osArgs
372373
}()
374+
375+
var fatalErr error
376+
// catch `os.Exit(1)` from kubectl
377+
kubectlutil.BehaviorOnFatal(func(msg string, code int) {
378+
fatalErr = errors.New(fmt.Sprint(code), msg)
379+
})
380+
373381
var buf bytes.Buffer
374382
if err := kubectlcmd.NewKubectlCommand(kubectlcmd.KubectlOptions{
375383
Arguments: args,
@@ -383,5 +391,8 @@ func runKubectl(args ...string) ([]byte, error) {
383391
}).Execute(); err != nil {
384392
return nil, err
385393
}
394+
if fatalErr != nil {
395+
return nil, fatalErr
396+
}
386397
return buf.Bytes(), nil
387398
}

0 commit comments

Comments
 (0)