Skip to content

Commit

Permalink
Resource import example (upbound#18)
Browse files Browse the repository at this point in the history
* Resource import example

* Get the data from az resource graph

* Drive the import with function-kcl

Signed-off-by: Yury Tsarev <[email protected]>

* Handle the case of empty query result

Signed-off-by: Yury Tsarev <[email protected]>

---------

Signed-off-by: Yury Tsarev <[email protected]>
  • Loading branch information
ytsarev authored Jan 15, 2025
1 parent a60bdc5 commit 126d996
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
3 changes: 3 additions & 0 deletions example/resource-import/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: render
render:
crossplane render ../xr.yaml composition.yaml ./functions.yaml --function-credentials=../secrets/azure-creds.yaml -rc
61 changes: 61 additions & 0 deletions example/resource-import/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-azresourcegraph
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
mode: Pipeline
pipeline:
- step: query-azresourcegraph
functionRef:
name: function-azresourcegraph
input:
apiVersion: azresourcegraph.fn.crossplane.io/v1alpha1
kind: Input
query: |
Resources
| where type == "microsoft.network/virtualnetworks"
| where tags["import"] == "me"
| project name, resourceGroup, location
target: "context.azResourceGraphQueryResult"
credentials:
- name: azure-creds
source: Secret
secretRef:
namespace: upbound-system
name: azure-account-creds

- step: import-as-observe-only
functionRef:
name: function-kcl
input:
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLInput
spec:
source: |
queryResult = option("params").ctx.azResourceGraphQueryResult
assert len(queryResult) > 0, "Azure Resource Graph query returned no results. Verify the query criteria."
importName = queryResult[0].name
importRgName = queryResult[0].resourceGroup
importLocation = queryResult[0].location
network = {
apiVersion = "network.azure.upbound.io/v1beta2"
kind = "VirtualNetwork"
metadata.annotations = {
"crossplane.io/external-name" = importName
}
metadata.name = importName
spec.managementPolicies = ["Observe"]
spec.forProvider = {
resourceGroupName = importRgName
location = importLocation
}
}
items = [network]
- step: automatically-detect-ready-composed-resources
functionRef:
name: function-auto-ready
43 changes: 43 additions & 0 deletions example/resource-import/definition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
name: xrs.example.crossplane.io
spec:
group: example.crossplane.io
names:
categories:
- crossplane
kind: XR
plural: xrs
versions:
- name: v1
referenceable: true
schema:
openAPIV3Schema:
description: XR is the Schema for the XR API.
properties:
spec:
description: XRSpec defines the desired state of XR.
type: object
status:
description: XRStatus defines the observed state of XR.
type: object
properties:
azResourceGraphQueryResult:
description: Freeform field containing query results from function-azresourcegraph
type: array
items:
type: object
x-kubernetes-preserve-unknown-fields: true
required:
- spec
type: object
served: true
status:
controllers:
compositeResourceClaimType:
apiVersion: ""
kind: ""
compositeResourceType:
apiVersion: ""
kind: ""
24 changes: 24 additions & 0 deletions example/resource-import/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-azresourcegraph
annotations:
# This tells crossplane beta render to connect to the function locally.
#render.crossplane.io/runtime: Development
spec:
package: xpkg.upbound.io/upbound/function-azresourcegraph:v0.4.0
---
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-kcl
spec:
package: xpkg.upbound.io/crossplane-contrib/function-kcl:v0.11.0
---
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-auto-ready
spec:
package: xpkg.upbound.io/crossplane-contrib/function-auto-ready:v0.4.0

0 comments on commit 126d996

Please sign in to comment.