Skip to content

Commit c834ef5

Browse files
miltalexJoibel
andauthored
fix(cli): argo lint with strict should report case-sensitive errors. Fixes argoproj#13006 (argoproj#13250)
Signed-off-by: Miltiadis Alexis <alexmiltiadis@gmail.com> Co-authored-by: Alan Clucas <alan@clucas.org>
1 parent d7495b8 commit c834ef5

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

cmd/argo/commands/lint_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,45 @@ spec:
172172

173173
assert.False(t, fatal, "should not have exited")
174174
})
175+
176+
workflowCaseSensitivePath := filepath.Join(subdir, "workflowCaseSensitive.yaml")
177+
err = os.WriteFile(workflowCaseSensitivePath, []byte(`
178+
apiVersion: argoproj.io/v1alpha1
179+
kind: Workflow
180+
metadata:
181+
generateName: hello-world-
182+
spec:
183+
entrypoInt: whalesay
184+
templates:
185+
- name: whalesay
186+
container:
187+
image: docker/whalesay
188+
command: [ cowsay ]
189+
args: [ "hello world" ]
190+
resources:
191+
limits:
192+
memory: 32Mi
193+
cpu: 100m
194+
`), 0644)
195+
require.NoError(t, err)
196+
197+
t.Run("linting a workflow with case sensitive fields and strict enabled", func(t *testing.T) {
198+
defer func() { logrus.StandardLogger().ExitFunc = nil }()
199+
var fatal bool
200+
logrus.StandardLogger().ExitFunc = func(int) { fatal = true }
201+
202+
runLint(context.Background(), []string{workflowCaseSensitivePath}, true, nil, "pretty", true)
203+
204+
assert.True(t, fatal, "should have exited")
205+
})
206+
207+
t.Run("linting a workflow with case sensitive fields and strict disabled", func(t *testing.T) {
208+
defer func() { logrus.StandardLogger().ExitFunc = nil }()
209+
var fatal bool
210+
logrus.StandardLogger().ExitFunc = func(int) { fatal = true }
211+
212+
runLint(context.Background(), []string{workflowCaseSensitivePath}, true, nil, "pretty", false)
213+
214+
assert.False(t, fatal, "should not have exited")
215+
})
175216
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ require (
294294
k8s.io/component-helpers v0.26.15 // indirect
295295
k8s.io/metrics v0.26.15 // indirect
296296
moul.io/http2curl/v2 v2.3.0 // indirect
297-
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
297+
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2
298298
sigs.k8s.io/kustomize/api v0.12.1 // indirect
299299
sigs.k8s.io/kustomize/kustomize/v4 v4.5.7 // indirect
300300
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect

test/e2e/cli_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ func (s *CLISuite) TestTemplateCommands() {
11481148
s.Given().RunCli([]string{"template", "lint", "testdata/workflow-templates"}, func(t *testing.T, output string, err error) {
11491149
assert.Error(t, err)
11501150
assert.Contains(t, output, "invalid-workflowtemplate.yaml")
1151-
assert.Contains(t, output, `unknown field "entrypoints"`)
1151+
assert.Contains(t, output, `unknown field "spec.entrypoints"`)
11521152
assert.Contains(t, output, "linting errors found!")
11531153
})
11541154
})

workflow/common/parse.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
log "github.com/sirupsen/logrus"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11+
"k8s.io/apimachinery/pkg/runtime"
12+
kjson "sigs.k8s.io/json"
1113
"sigs.k8s.io/yaml"
1214

1315
wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
@@ -96,7 +98,19 @@ func toWorkflowTypeYAML(body []byte, kind string, strict bool) (metav1.Object, e
9698
func toWorkflowTypeJSON(body []byte, kind string, strict bool) (metav1.Object, error) {
9799
v := objectForKind(kind)
98100
if strict {
99-
return v, jsonpkg.UnmarshalStrict(body, v)
101+
var strictErrs []error
102+
strictJSONErrs, err := kjson.UnmarshalStrict(body, v)
103+
if err != nil {
104+
// fatal decoding error, not due to strictness
105+
return v, err
106+
}
107+
strictErrs = append(strictErrs, strictJSONErrs...)
108+
109+
if len(strictErrs) > 0 {
110+
// return the successfully decoded object along with the strict errors
111+
return v, runtime.NewStrictDecodingError(strictErrs)
112+
}
113+
return v, err
100114
}
101115

102116
return v, jsonpkg.Unmarshal(body, v)

0 commit comments

Comments
 (0)