@@ -12,7 +12,6 @@ import (
12
12
"google.golang.org/protobuf/types/known/structpb"
13
13
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14
14
"k8s.io/apimachinery/pkg/runtime"
15
- "k8s.io/apimachinery/pkg/runtime/schema"
16
15
"k8s.io/utils/ptr"
17
16
18
17
"github.com/crossplane/crossplane-runtime/pkg/logging"
@@ -52,7 +51,7 @@ func TestRunFunction(t *testing.T) {
52
51
Results : []* fnv1beta1.Result {
53
52
{
54
53
Severity : fnv1beta1 .Severity_SEVERITY_FATAL ,
55
- Message : "invalid Function input: resources: Required value: resources is required" ,
54
+ Message : "invalid Function input: resources: Required value: resources or environment patches are required" ,
56
55
},
57
56
},
58
57
},
@@ -95,7 +94,7 @@ func TestRunFunction(t *testing.T) {
95
94
},
96
95
},
97
96
},
98
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
97
+ Context : contextWithEnvironment (nil ),
99
98
},
100
99
},
101
100
},
@@ -126,7 +125,7 @@ func TestRunFunction(t *testing.T) {
126
125
},
127
126
},
128
127
},
129
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
128
+ Context : contextWithEnvironment (nil ),
130
129
},
131
130
},
132
131
want : want {
@@ -145,7 +144,7 @@ func TestRunFunction(t *testing.T) {
145
144
},
146
145
},
147
146
},
148
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
147
+ Context : contextWithEnvironment (nil ),
149
148
},
150
149
},
151
150
},
@@ -210,7 +209,7 @@ func TestRunFunction(t *testing.T) {
210
209
},
211
210
},
212
211
},
213
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
212
+ Context : contextWithEnvironment (nil ),
214
213
},
215
214
},
216
215
},
@@ -282,7 +281,7 @@ func TestRunFunction(t *testing.T) {
282
281
},
283
282
},
284
283
},
285
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
284
+ Context : contextWithEnvironment (nil ),
286
285
},
287
286
},
288
287
},
@@ -380,7 +379,7 @@ func TestRunFunction(t *testing.T) {
380
379
},
381
380
},
382
381
},
383
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
382
+ Context : contextWithEnvironment (nil ),
384
383
},
385
384
},
386
385
},
@@ -436,7 +435,7 @@ func TestRunFunction(t *testing.T) {
436
435
},
437
436
},
438
437
},
439
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
438
+ Context : contextWithEnvironment (nil ),
440
439
},
441
440
},
442
441
},
@@ -537,7 +536,7 @@ func TestRunFunction(t *testing.T) {
537
536
// Note "new-resource" doesn't appear here.
538
537
},
539
538
},
540
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
539
+ Context : contextWithEnvironment (nil ),
541
540
Results : []* fnv1beta1.Result {
542
541
{
543
542
Severity : fnv1beta1 .Severity_SEVERITY_WARNING ,
@@ -652,7 +651,7 @@ func TestRunFunction(t *testing.T) {
652
651
},
653
652
},
654
653
},
655
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
654
+ Context : contextWithEnvironment (nil ),
656
655
},
657
656
},
658
657
},
@@ -715,7 +714,7 @@ func TestRunFunction(t *testing.T) {
715
714
},
716
715
},
717
716
},
718
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
717
+ Context : contextWithEnvironment (nil ),
719
718
},
720
719
},
721
720
},
@@ -785,7 +784,7 @@ func TestRunFunction(t *testing.T) {
785
784
},
786
785
},
787
786
},
788
- Context : & structpb. Struct { Fields : map [ string ] * structpb. Value { fncontext . KeyEnvironment : structpb . NewStructValue (nil )}} ,
787
+ Context : contextWithEnvironment (nil ),
789
788
},
790
789
},
791
790
},
@@ -1198,6 +1197,44 @@ func TestRunFunction(t *testing.T) {
1198
1197
Context : contextWithEnvironment (map [string ]interface {}{
1199
1198
"widgets" : "10" ,
1200
1199
})}}},
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
+ },
1201
1238
}
1202
1239
1203
1240
for name , tc := range cases {
@@ -1227,7 +1264,7 @@ func contextWithEnvironment(data map[string]interface{}) *structpb.Struct {
1227
1264
data = map [string ]interface {}{}
1228
1265
}
1229
1266
u := unstructured.Unstructured {Object : data }
1230
- u .SetGroupVersionKind (schema. GroupVersionKind { Group : "internal.crossplane.io" , Version : "v1alpha1" , Kind : "Environment" } )
1267
+ u .SetGroupVersionKind (internalEnvironmentGVK )
1231
1268
d , err := structpb .NewStruct (u .UnstructuredContent ())
1232
1269
if err != nil {
1233
1270
panic (err )
0 commit comments