Skip to content

Commit 3896496

Browse files
committed
fix: allow specifying only environment patches
Signed-off-by: Philippe Scorsolini <p.scorsolini@gmail.com>
1 parent 46d3347 commit 3896496

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

fn_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestRunFunction(t *testing.T) {
5151
Results: []*fnv1beta1.Result{
5252
{
5353
Severity: fnv1beta1.Severity_SEVERITY_FATAL,
54-
Message: "invalid Function input: resources: Required value: resources is required",
54+
Message: "invalid Function input: resources: Required value: resources or environment patches are required",
5555
},
5656
},
5757
},
@@ -1197,6 +1197,44 @@ func TestRunFunction(t *testing.T) {
11971197
Context: contextWithEnvironment(map[string]interface{}{
11981198
"widgets": "10",
11991199
})}}},
1200+
"OnlyEnvironmentPatchesIsAllowed": {
1201+
reason: "Having only environment patches should be allowed and work as expected.",
1202+
args: args{
1203+
req: &fnv1beta1.RunFunctionRequest{
1204+
Input: resource.MustStructObject(&v1beta1.Resources{
1205+
Environment: &v1beta1.Environment{
1206+
Patches: []v1beta1.EnvironmentPatch{
1207+
{
1208+
Type: v1beta1.PatchTypeFromCompositeFieldPath,
1209+
Patch: v1beta1.Patch{
1210+
FromFieldPath: ptr.To[string]("spec.widgets"),
1211+
ToFieldPath: ptr.To[string]("envKey"),
1212+
},
1213+
},
1214+
},
1215+
},
1216+
}),
1217+
Observed: &fnv1beta1.State{
1218+
Composite: &fnv1beta1.Resource{
1219+
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR","spec":{"widgets":"10"}}`),
1220+
},
1221+
},
1222+
},
1223+
},
1224+
want: want{
1225+
rsp: &fnv1beta1.RunFunctionResponse{
1226+
Meta: &fnv1beta1.ResponseMeta{Ttl: durationpb.New(response.DefaultTTL)},
1227+
Desired: &fnv1beta1.State{
1228+
Composite: &fnv1beta1.Resource{
1229+
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`),
1230+
},
1231+
},
1232+
Context: contextWithEnvironment(map[string]interface{}{
1233+
"envKey": "10",
1234+
}),
1235+
},
1236+
},
1237+
},
12001238
}
12011239

12021240
for name, tc := range cases {

validate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func ValidateResources(r *v1beta1.Resources) *field.Error {
3939
return err
4040
}
4141
}
42-
if len(r.Resources) == 0 {
43-
return field.Required(field.NewPath("resources"), "resources is required")
42+
if len(r.Resources) == 0 && (r.Environment == nil || len(r.Environment.Patches) == 0) {
43+
return field.Required(field.NewPath("resources"), "resources or environment patches are required")
4444
}
4545
for i, r := range r.Resources {
4646
if err := ValidateComposedTemplate(r); err != nil {

0 commit comments

Comments
 (0)