Skip to content
This repository was archived by the owner on May 18, 2026. It is now read-only.

Commit 531cd8b

Browse files
fix:Adds validations to check multiple sources via flags on workload… (#568)
* fix: Adds validations to check multiple sources via flags on workload create or update * fix: review comments
1 parent 0a5bf47 commit 531cd8b

11 files changed

Lines changed: 148 additions & 8 deletions

File tree

pkg/apis/cartographer/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cli-runtime/validation/fielderrors.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
package validation
1818

1919
import (
20+
"fmt"
21+
"strings"
22+
2023
k8sfield "k8s.io/apimachinery/pkg/util/validation/field"
2124
)
2225

@@ -25,3 +28,9 @@ func ErrMissingFieldWithDetail(field string, detail string) FieldErrors {
2528
k8sfield.Required(k8sfield.NewPath(field), detail),
2629
}
2730
}
31+
32+
func ErrMultipleSources(names ...string) FieldErrors {
33+
return FieldErrors{
34+
k8sfield.Required(k8sfield.NewPath(fmt.Sprintf("[%s]", strings.Join(names, ", "))), "expected exactly one, got multiple"),
35+
}
36+
}

pkg/cli-runtime/validation/fielderrors_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package validation_test
1818

1919
import (
20+
"fmt"
21+
"strings"
2022
"testing"
2123

2224
"github.com/google/go-cmp/cmp"
@@ -56,3 +58,41 @@ func TestErrMissingFieldWithDetail(t *testing.T) {
5658
})
5759
}
5860
}
61+
62+
func TestErrMultipleSources(t *testing.T) {
63+
tests := []struct {
64+
testName string
65+
sources []string
66+
expected validation.FieldErrors
67+
}{
68+
{
69+
testName: "multiple sources",
70+
expected: validation.FieldErrors{
71+
k8sfield.Required(k8sfield.NewPath(fmt.Sprintf("[%s]", strings.Join([]string{flags.GitFlagWildcard, flags.ImageFlagName}, ", "))), "expected exactly one, got multiple"),
72+
},
73+
sources: append([]string{}, flags.GitFlagWildcard, flags.ImageFlagName),
74+
}, {
75+
testName: "single source",
76+
expected: validation.FieldErrors{
77+
k8sfield.Required(k8sfield.NewPath(fmt.Sprintf("[%s]", strings.Join([]string{flags.GitFlagWildcard}, ", "))), "expected exactly one, got multiple"),
78+
},
79+
sources: append([]string{}, flags.GitFlagWildcard),
80+
}, {
81+
testName: "Empty source",
82+
expected: validation.FieldErrors{
83+
k8sfield.Required(k8sfield.NewPath(fmt.Sprintf("[%s]", strings.Join([]string{}, ", "))), "expected exactly one, got multiple"),
84+
},
85+
sources: []string{},
86+
},
87+
}
88+
89+
for _, test := range tests {
90+
t.Run(test.testName, func(t *testing.T) {
91+
expected := test.expected
92+
actual := validation.ErrMultipleSources(test.sources...)
93+
if diff := cmp.Diff(expected, actual); diff != "" {
94+
t.Errorf("%s() = (-expected, +actual): %s", test.testName, diff)
95+
}
96+
})
97+
}
98+
}

pkg/commands/workload.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ const (
6464
AnnotationReservedKey = "annotations"
6565
MavenOverwrittenNoticeMsg = "Maven configuration flags have overwritten values provided by \"--params-yaml\"."
6666
WebTypeReservedKey = "web"
67+
MavenFlagWildcard = "--maven*"
68+
// --source-image can be a source for workload without local path and vice versa.
69+
LocalPathAndSource = "--local-path with/without --source-image"
6770
)
6871

6972
const (
@@ -153,6 +156,8 @@ type WorkloadOptions struct {
153156

154157
func (opts *WorkloadOptions) Validate(ctx context.Context) validation.FieldErrors {
155158
errs := validation.FieldErrors{}
159+
var mavenSource bool
160+
sources := []string{}
156161

157162
errs = errs.Also(validation.K8sName(opts.Namespace, flags.NamespaceFlagName))
158163
if opts.FilePath == "" {
@@ -200,6 +205,36 @@ func (opts *WorkloadOptions) Validate(ctx context.Context) validation.FieldError
200205
errs = errs.Also(validation.Enum(opts.Output, flags.OutputFlagName, []string{printer.OutputFormatJson, printer.OutputFormatYaml, printer.OutputFormatYml}))
201206
}
202207

208+
// validating sources as the source options are mutually exclusive
209+
if opts.MavenArtifact != "" || opts.MavenVersion != "" || opts.MavenGroup != "" || opts.MavenType != "" {
210+
mavenSource = true
211+
}
212+
if !mavenSource {
213+
for _, p := range opts.ParamsYaml {
214+
kv := parsers.DeletableKeyValue(p)
215+
if len(kv) != 1 {
216+
if kv[0] == cartov1alpha1.WorkloadMavenParam {
217+
mavenSource = true
218+
break
219+
}
220+
}
221+
}
222+
}
223+
if mavenSource {
224+
sources = append(sources, MavenFlagWildcard)
225+
}
226+
if opts.LocalPath != "" || opts.SourceImage != "" {
227+
sources = append(sources, LocalPathAndSource)
228+
}
229+
if opts.Image != "" {
230+
sources = append(sources, flags.ImageFlagName)
231+
}
232+
if opts.GitRepo != "" || opts.GitBranch != "" || opts.GitCommit != "" || opts.GitTag != "" {
233+
sources = append(sources, flags.GitFlagWildcard)
234+
}
235+
if len(sources) > 1 {
236+
errs = errs.Also(validation.ErrMultipleSources(sources...))
237+
}
203238
return errs
204239
}
205240

pkg/commands/workload_apply_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,23 @@ func TestWorkloadApplyOptionsValidate(t *testing.T) {
145145
},
146146
ExpectFieldErrors: validation.EnumInvalidValue("invalid", flags.UpdateStrategyFlagName, []string{"merge", "replace"}),
147147
},
148+
{
149+
Name: "apply with multiple sources",
150+
Validatable: &commands.WorkloadApplyOptions{
151+
WorkloadOptions: commands.WorkloadOptions{
152+
Namespace: "default",
153+
Name: "my-resource",
154+
GitRepo: "https://example.com/repo.git",
155+
GitBranch: "main",
156+
SourceImage: "repo.example/image:tag",
157+
Image: "repo.example/image:tag",
158+
MavenArtifact: "hello-world",
159+
MavenType: "jar",
160+
MavenVersion: "0.0.1",
161+
},
162+
},
163+
ExpectFieldErrors: validation.ErrMultipleSources(commands.MavenFlagWildcard, commands.LocalPathAndSource, flags.ImageFlagName, flags.GitFlagWildcard),
164+
},
148165
}
149166

150167
table.Run(t)

pkg/commands/workload_create_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ func TestWorkloadCreateOptionsValidate(t *testing.T) {
9292
},
9393
ExpectFieldErrors: validation.ErrInvalidArrayValue("FOO", flags.BuildEnvFlagName, 0),
9494
},
95+
{
96+
Name: "apply with multiple sources",
97+
Validatable: &commands.WorkloadCreateOptions{
98+
WorkloadOptions: commands.WorkloadOptions{
99+
Namespace: "default",
100+
Name: "my-resource",
101+
GitRepo: "https://example.com/repo.git",
102+
GitBranch: "main",
103+
SourceImage: "repo.example/image:tag",
104+
MavenArtifact: "hello-world",
105+
MavenType: "jar",
106+
MavenVersion: "0.0.1",
107+
},
108+
},
109+
ExpectFieldErrors: validation.ErrMultipleSources(commands.MavenFlagWildcard, commands.LocalPathAndSource, flags.GitFlagWildcard),
110+
},
95111
}
96112

97113
table.Run(t)

pkg/commands/workload_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,24 @@ func TestWorkloadOptionsValidate(t *testing.T) {
435435
SourceImage: "repo.example/image:tag",
436436
Image: "repo.example/image:tag",
437437
},
438-
ShouldValidate: true,
438+
ShouldValidate: false,
439+
ExpectFieldErrors: validation.ErrMultipleSources(commands.LocalPathAndSource, flags.ImageFlagName, flags.GitFlagWildcard),
440+
},
441+
{
442+
Name: "all sources including maven",
443+
Validatable: &commands.WorkloadOptions{
444+
Namespace: "default",
445+
Name: "my-resource",
446+
GitRepo: "https://example.com/repo.git",
447+
GitBranch: "main",
448+
SourceImage: "repo.example/image:tag",
449+
Image: "repo.example/image:tag",
450+
MavenArtifact: "hello-world",
451+
MavenType: "jar",
452+
MavenVersion: "0.0.1",
453+
},
454+
ShouldValidate: false,
455+
ExpectFieldErrors: validation.ErrMultipleSources(commands.MavenFlagWildcard, commands.LocalPathAndSource, flags.ImageFlagName, flags.GitFlagWildcard),
439456
},
440457
{
441458
Name: "wait",

pkg/dies/cartographer/v1alpha1/zz_generated.die.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/dies/cartographer/v1alpha1/zz_generated.die_test.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/dies/knative/serving/v1/zz_generated.die.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)