Skip to content

Commit

Permalink
fix: database on field dump and add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Apr 19, 2024
1 parent fb5929c commit 1c9a6a4
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ env:
XPKG: xpkg.upbound.io/${{ github.repository}}

# The package version to push. The default is 0.0.0-gitsha.
XPKG_VERSION: v0.5.3
XPKG_VERSION: v0.5.4

jobs:
unit-test:
Expand Down
2 changes: 2 additions & 0 deletions examples/resources/database/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run:
crossplane beta render xr.yaml composition.yaml functions.yaml -r
42 changes: 42 additions & 0 deletions examples/resources/database/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Example Manifests

You can run your function locally and test it using `crossplane beta render`
with these example manifests.

```shell
# Run the function locally
$ go run . --insecure --debug
```

```shell
# Then, in another terminal, call it with these example manifests
$ crossplane beta render xr.yaml composition.yaml functions.yaml -r
---
apiVersion: fn-demo.crossplane.io/v1alpha1
kind: Database
metadata:
name: database-test-functions
---
apiVersion: sql.gcp.upbound.io/v1beta1
kind: DatabaseInstance
metadata:
annotations:
crossplane.io/composition-resource-name: ""
generateName: database-test-functions-
labels:
crossplane.io/composite: database-test-functions
ownerReferences:
- apiVersion: fn-demo.crossplane.io/v1alpha1
blockOwnerDeletion: true
controller: true
kind: Database
name: database-test-functions
uid: ""
spec:
forProvider:
project: test-project
settings:
- databaseFlags:
- name: log_checkpoints
value: "on"
```
13 changes: 13 additions & 0 deletions examples/resources/database/composition.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
items = [{
apiVersion: "sql.gcp.upbound.io/v1beta1"
kind: "DatabaseInstance"
spec: {
forProvider: {
project: "test-project"
settings: [{databaseFlags: [{
name: "log_checkpoints"
value: "on"
}]}]
}
}
}]
37 changes: 37 additions & 0 deletions examples/resources/database/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: xlabels.fn-demo.crossplane.io
labels:
provider: aws
spec:
writeConnectionSecretsToNamespace: crossplane-system
compositeTypeRef:
apiVersion: fn-demo.crossplane.io/v1alpha1
kind: XNetwork
mode: Pipeline
pipeline:
- step: normal
functionRef:
name: kcl-function
input:
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLRun
metadata:
name: basic
spec:
target: Resources
source: |
items = [{
apiVersion: "sql.gcp.upbound.io/v1beta1"
kind: "DatabaseInstance"
spec: {
forProvider: {
project: "test-project"
settings: [{databaseFlags: [{
name: "log_checkpoints"
value: "on"
}]}]
}
}
}]
9 changes: 9 additions & 0 deletions examples/resources/database/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: kcl-function
annotations:
# This tells crossplane beta render to connect to the function locally.
render.crossplane.io/runtime: Development
spec:
package: xpkg.upbound.io/crossplane-contrib/function-kcl:latest
7 changes: 7 additions & 0 deletions examples/resources/database/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: fn-demo.crossplane.io/v1alpha1
kind: Database
metadata:
name: database-test-functions
namespace: default
spec:
id: database-test-functions
38 changes: 38 additions & 0 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ func TestRunFunction(t *testing.T) {
},
},
},
"DatabaseInstance": {
reason: "The Function should return a fatal result if no input was specified",
args: args{
req: &fnv1beta1.RunFunctionRequest{
Meta: &fnv1beta1.RequestMeta{Tag: "database-instance"},
Input: resource.MustStructJSON(`{
"apiVersion": "krm.kcl.dev/v1alpha1",
"kind": "KCLRun",
"metadata": {
"name": "basic"
},
"spec": {
"source": "items = [{ \n apiVersion: \"sql.gcp.upbound.io/v1beta1\"\n kind: \"DatabaseInstance\"\n spec: {\n forProvider: {\n project: \"test-project\"\n settings: [{databaseFlags: [{\n name: \"log_checkpoints\"\n value: \"on\"\n }]}]\n }\n }\n}]\n"
}
}`),
Observed: &fnv1beta1.State{
Composite: &fnv1beta1.Resource{
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`),
},
},
},
},
want: want{
rsp: &fnv1beta1.RunFunctionResponse{
Meta: &fnv1beta1.ResponseMeta{Tag: "database-instance", Ttl: durationpb.New(response.DefaultTTL)},
Desired: &fnv1beta1.State{
Composite: &fnv1beta1.Resource{
Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`),
},
Resources: map[string]*fnv1beta1.Resource{
"": {
Resource: resource.MustStructJSON(`{"apiVersion": "sql.gcp.upbound.io/v1beta1", "kind": "DatabaseInstance", "spec": {"forProvider": {"project": "test-project", "settings": [{"databaseFlags": [{"name": "log_checkpoints", "value": "on"}]}]}}}`),
},
},
},
},
},
},
}

for name, tc := range cases {
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
google.golang.org/protobuf v1.33.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.30.0
kcl-lang.io/krm-kcl v0.8.3-0.20240415150603-203d07fcebf6
kcl-lang.io/krm-kcl v0.8.5
sigs.k8s.io/controller-tools v0.14.0
sigs.k8s.io/yaml v1.4.0
)
Expand All @@ -25,7 +25,7 @@ require (
cloud.google.com/go/storage v1.36.0 // indirect
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20220720212527-133180134b93 // indirect
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20230427202446-3255accc518d // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
Expand Down Expand Up @@ -179,7 +179,7 @@ require (
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.63.0 // indirect
google.golang.org/grpc v1.63.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand All @@ -191,11 +191,11 @@ require (
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
kcl-lang.io/cli v0.8.5 // indirect
kcl-lang.io/kcl-go v0.8.4 // indirect
kcl-lang.io/cli v0.8.6 // indirect
kcl-lang.io/kcl-go v0.8.5 // indirect
kcl-lang.io/kcl-openapi v0.6.1 // indirect
kcl-lang.io/kpm v0.8.5-0.20240401071819-7a69be511c95 // indirect
kcl-lang.io/lib v0.8.4 // indirect
kcl-lang.io/kpm v0.8.5 // indirect
kcl-lang.io/lib v0.8.5 // indirect
oras.land/oras-go v1.2.3 // indirect
oras.land/oras-go/v2 v2.3.0 // indirect
sigs.k8s.io/controller-runtime v0.17.0 // indirect
Expand Down
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20220720212527-133180134b93 h1:c1GhPzYzU2a3E+/2WB9OxoljK2pNYfsBF5Fz9GkdYXs=
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20220720212527-133180134b93/go.mod h1:gkK43tTaPXFNASpbIbQImzhmt1hdcdin++kvzTblykc=
Expand Down Expand Up @@ -1482,8 +1482,8 @@ google.golang.org/grpc v1.48.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACu
google.golang.org/grpc v1.49.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.50.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.63.0 h1:WjKe+dnvABXyPJMD7KDNLxtoGk5tgk+YFWN6cBWjZE8=
google.golang.org/grpc v1.63.0/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
Expand Down Expand Up @@ -1558,18 +1558,18 @@ k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 h1:BZqlfIlq5YbRMFko6/PM7F
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340/go.mod h1:yD4MZYeKMBwQKVht279WycxKyM84kkAx2DPrTXaeb98=
k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCfRziVtos3ofG/sQ=
k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
kcl-lang.io/cli v0.8.5 h1:6xo1Zb+c34n9qYIEsAAnWafuNEOqo+QnNyFUQLZfgtk=
kcl-lang.io/cli v0.8.5/go.mod h1:Q/yQbud6IMaWjSiKMnB2sQdB9HWTPQrMWzvCWNVAWW4=
kcl-lang.io/kcl-go v0.8.4 h1:DIzrZTopuqQaBXM6+y100Bi0m+oH7rt1BpDwdHQBng4=
kcl-lang.io/kcl-go v0.8.4/go.mod h1:vmZ/g247eOcvlZhIWgXWMVkIpyDi1BCsLPDkIdMWglY=
kcl-lang.io/cli v0.8.6 h1:m0VIbLqiUULM6qGhCr1dvV+lOJfK/TVyeuOq7XcEFTo=
kcl-lang.io/cli v0.8.6/go.mod h1:shs86nJcmHs1m9ucWZksAiX5j6d9EGl9DHsOxlwtmVo=
kcl-lang.io/kcl-go v0.8.5 h1:YuaZju34cclGVB8Z1O1hhxZx6lYF4cW3x6yDqK6l3iI=
kcl-lang.io/kcl-go v0.8.5/go.mod h1:CkXBerH9YchN2mP7fTfq5DXdmhXHH2lrbg5TFVT4KL8=
kcl-lang.io/kcl-openapi v0.6.1 h1:iPH0EvPgDGZS5Lk00/Su5Av6AQP5IBG8f7gAUyevkHE=
kcl-lang.io/kcl-openapi v0.6.1/go.mod h1:Ai9mFztCVKkRSFabczO/r5hCNdqaNtAc2ZIRxTeV0Mk=
kcl-lang.io/kpm v0.8.5-0.20240401071819-7a69be511c95 h1:grwv/MI/n+kQUMxrvyM5qGTGooa3LBxBKq3lNr9wdFg=
kcl-lang.io/kpm v0.8.5-0.20240401071819-7a69be511c95/go.mod h1:3atE1tEbsSPaAuKslkADH1HTDi7SMWlDWllmuk2XsBA=
kcl-lang.io/krm-kcl v0.8.3-0.20240415150603-203d07fcebf6 h1:YK1+W3xFiOyNnJ31Hl0ASOCnXjiCqNFdmQYOh5pOD4Y=
kcl-lang.io/krm-kcl v0.8.3-0.20240415150603-203d07fcebf6/go.mod h1:0RUVOL78Ngy6o3frRcvVIWL45wJdxtbSHPZOBVmv3Uc=
kcl-lang.io/lib v0.8.4 h1:uQPNLqZe3abaW++90kYhCSEmTEi2DE0yf/xLM/Q9S2c=
kcl-lang.io/lib v0.8.4/go.mod h1:ubsalGXxJaa5II/EsHmsI/tL2EluYHIcW+BwzQPt+uY=
kcl-lang.io/kpm v0.8.5 h1:I5P64YHwzLoBKppoC//nVF6rR6zbXd1iAIU8pH4tJ3A=
kcl-lang.io/kpm v0.8.5/go.mod h1:3atE1tEbsSPaAuKslkADH1HTDi7SMWlDWllmuk2XsBA=
kcl-lang.io/krm-kcl v0.8.5 h1:eJDFPgabdJSqbeqUtYY8baqNjRLmK706SLfhpnrm+nM=
kcl-lang.io/krm-kcl v0.8.5/go.mod h1:RayZA+1UUhLVCN4e3Ia4m1fk73b7Q7dRL7e+0oAopAQ=
kcl-lang.io/lib v0.8.5 h1:9GsGcJlanBKw/B6jxe6imdv4lbfdiDsNW4bByA1TGUY=
kcl-lang.io/lib v0.8.5/go.mod h1:ubsalGXxJaa5II/EsHmsI/tL2EluYHIcW+BwzQPt+uY=
oras.land/oras-go v1.2.5 h1:XpYuAwAb0DfQsunIyMfeET92emK8km3W4yEzZvUbsTo=
oras.land/oras-go v1.2.5/go.mod h1:PuAwRShRZCsZb7g8Ar3jKKQR/2A/qN+pkYxIOd/FAoo=
oras.land/oras-go/v2 v2.5.0 h1:o8Me9kLY74Vp5uw07QXPiitjsw7qNXi8Twd+19Zf02c=
Expand Down

0 comments on commit 1c9a6a4

Please sign in to comment.