Skip to content

Commit 67be2b8

Browse files
authored
Fix e2e QueryRef from XR status (#13)
* Fix e2e QueryRef from XR status * Do not fatal fail but worn on empty Query * It will unblock the situation when XR status is getting updated by the Function pipeline and the referenced `status.queryField` is available only on the consequent reconciliation loop * Update queryref-from-xr-status example to include function-go-templating constructig the query for illustrative e2e tetsing and example usage Signed-off-by: Yury Tsarev <[email protected]> * Fix example for e2e test Signed-off-by: Yury Tsarev <[email protected]> --------- Signed-off-by: Yury Tsarev <[email protected]>
1 parent ccbf5d4 commit 67be2b8

File tree

7 files changed

+59
-21
lines changed

7 files changed

+59
-21
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.PHONY: render
22
render:
3-
crossplane render ./xr.yaml composition.yaml ../functions.yaml --function-credentials=../secrets/azure-creds.yaml -rc
3+
crossplane render ./xr.yaml composition.yaml ./functions.yaml --function-credentials=../secrets/azure-creds.yaml -rc

example/queryref-from-xr-status/composition.yaml

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,30 @@ spec:
88
kind: XR
99
mode: Pipeline
1010
pipeline:
11-
- step: query-azresourcegraph
12-
functionRef:
13-
name: function-azresourcegraph
14-
input:
15-
apiVersion: azresourcegraph.fn.crossplane.io/v1alpha1
16-
kind: Input
17-
queryRef: "status.azResourceGraphQuery"
18-
target: "context.azResourceGraphQueryResult"
19-
credentials:
20-
- name: azure-creds
21-
source: Secret
22-
secretRef:
23-
namespace: upbound-system
24-
name: azure-account-creds
11+
- step: query-to-status
12+
functionRef:
13+
name: function-go-templating
14+
input:
15+
apiVersion: gotemplating.fn.crossplane.io/v1beta1
16+
kind: GoTemplate
17+
source: Inline
18+
inline:
19+
template: |
20+
apiVersion: example.crossplane.io/v1
21+
kind: XR
22+
status:
23+
azResourceGraphQuery: Resources | where type == "{{ .observed.composite.resource.spec.queryResourceType }}" | count
24+
- step: query-azresourcegraph
25+
functionRef:
26+
name: function-azresourcegraph
27+
input:
28+
apiVersion: azresourcegraph.fn.crossplane.io/v1alpha1
29+
kind: Input
30+
queryRef: "status.azResourceGraphQuery"
31+
target: "status.azResourceGraphQueryResult"
32+
credentials:
33+
- name: azure-creds
34+
source: Secret
35+
secretRef:
36+
namespace: upbound-system
37+
name: azure-account-creds

example/queryref-from-xr-status/definition.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ spec:
1919
spec:
2020
description: XRSpec defines the desired state of XR.
2121
type: object
22+
properties:
23+
queryResourceType:
24+
description: resource type for az resource query construction
25+
type: string
2226
status:
2327
description: XRStatus defines the observed state of XR.
2428
type: object
@@ -29,6 +33,9 @@ spec:
2933
items:
3034
type: object
3135
x-kubernetes-preserve-unknown-fields: true
36+
azResourceGraphQuery:
37+
description: Freeform field containing query results from function-azresourcegraph
38+
type: string
3239
required:
3340
- spec
3441
type: object
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
apiVersion: pkg.crossplane.io/v1beta1
3+
kind: Function
4+
metadata:
5+
name: function-azresourcegraph
6+
annotations:
7+
# This tells crossplane beta render to connect to the function locally.
8+
# render.crossplane.io/runtime: Development
9+
spec:
10+
package: xpkg.upbound.io/upbound/function-azresourcegraph:v0.4.0-rc-test
11+
---
12+
apiVersion: pkg.crossplane.io/v1beta1
13+
kind: Function
14+
metadata:
15+
name: function-go-templating
16+
spec:
17+
package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.9.0

example/queryref-from-xr-status/xr.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ apiVersion: example.crossplane.io/v1
33
kind: XR
44
metadata:
55
name: example-xr
6-
spec: {}
6+
spec:
7+
queryResourceType: microsoft.compute/virtualmachines
78
status:
89
azResourceGraphQuery: Resources| count

fn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1.RunFunctionRequest
9090
}
9191

9292
if in.Query == "" {
93-
response.Fatal(rsp, errors.New("Query is empty"))
94-
f.log.Info("FAILURE: ", "query is empty", in.Query)
93+
response.Warning(rsp, errors.New("Query is empty"))
94+
f.log.Info("WARNING: ", "query is empty", in.Query)
9595
return rsp, nil
9696
}
9797

fn_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,8 @@ func TestRunFunction(t *testing.T) {
873873
},
874874
},
875875
},
876-
"FailIfQueryIsEmpty": {
877-
reason: "The Function should fail if Query is empty",
876+
"WarningIfQueryIsEmpty": {
877+
reason: "The Function should warn if Query is empty",
878878
args: args{
879879
ctx: context.Background(),
880880
req: &fnv1.RunFunctionRequest{
@@ -909,7 +909,7 @@ func TestRunFunction(t *testing.T) {
909909
Meta: &fnv1.ResponseMeta{Tag: "hello", Ttl: durationpb.New(response.DefaultTTL)},
910910
Results: []*fnv1.Result{
911911
{
912-
Severity: fnv1.Severity_SEVERITY_FATAL,
912+
Severity: fnv1.Severity_SEVERITY_WARNING,
913913
Message: `Query is empty`,
914914
Target: fnv1.Target_TARGET_COMPOSITE.Enum(),
915915
},

0 commit comments

Comments
 (0)