We want to define a new resource AcmeDatabase
(XRC), XAcmeDatabase
(XR), to
create CloudSQL PG instances (DatabaseInstance
).
We have two environments, dev
and prod
, for both of them we have some
environment-specific configurations.
We want to let users specify wether they want to have a Database
created too,
but if specified, the Database
can only be created once the DatabaseInstance is
ready.
- We define the Provider we need.
- We define our XRD.
- We define our environment specific configurations as EnvironmentConfigs.
- We define our Composition and all the functions required.
In pseudocode the above Composition logic could look like this:
# function-environment-configs
environment := getEnvironmentConfig(xr.spec.environment)
# function-patch-and-transform
desiredState := getDesiredResources(xr, environment)
# function-sequencer
desiredState = filterDatabaseIfDatabaseInstanceNotReady(desiredState)
# function-cel-filter
desiredState = filterDatabaseIfNotNeeded(xr, desiredState)
return desiredState
We can write a few examples:
- xr-dev.yaml: a DatabaseInstance with the
dev
environment configuration. - xr-dev-with-db.yaml: a DatabaseInstance and a Database with the
dev
environment configuration. - xr-prod.yaml: a Database with the
prod
environment configuration. - xr-prod-bigger.yaml: a Database with a larger disk than the default, 100GB.
We can then render them all locally to see everything works as expected:
crossplane beta render examples/xr-dev.yaml setup/composition.yaml setup/functions.yaml --extra-resources=setup/environmentConfigs.yam
crossplane beta render examples/xr-dev-with-db.yaml setup/composition.yaml setup/functions.yaml --extra-resources=setup/environmentConfigs.yam
crossplane beta render examples/xr-prod.yaml setup/composition.yaml setup/functions.yaml --extra-resources=setup/environmentConfigs.yam
crossplane beta render examples/xr-prod-bigger.yaml setup/composition.yaml setup/functions.yaml --extra-resources=setup/environmentConfigs.yam
Have a look at crossplane beta validate to validate the rendered resources against their schema.
kind create cluster
helm install crossplane crossplane-stable/crossplane --namespace crossplane-system --create-namespace
We'll first need to create a Secret with the GCP credentials, see the docs for more details on how to get the token in the first place.
kubectl create secret generic gcp-secret -n crossplane-system --from-file=creds=../gcp-credentials.json
Now we can apply all the requirements:
kubectl apply -f setup
Once all the requirements are successfully installed we can apply one of our examples and check it works as intended.
kubectl apply -f examples/xr-dev-with-db.yaml
crossplane beta trace xacmedatabases.acme.com acme-db-dev -o wide