provider-gcp
provider-gcp copied to clipboard
Add referencer and selector option for masterInstanceName in Cloud SQL
What problem are you facing?
Building compositions which creates master and replica databases instances can be challenging because masterInstanceName
is only supported by passing the master name directly.
This could be solved by just patching the name to the field. But we have 2 problems with this approach:
-
masterInstanceName
needs to be in the format<project-id>:<instance-name>
. For example:mycoolproject123:example-db
- Without the reference, there isn't a
depends_on
that will wait the master creation then create the replica, it will try to create both at the same time, which will lead to errors.
How could Crossplane help solve your problem?
Adding reference to other CloudSQLInstance in the field masterInstanceName
Workaround for this problem
I defined a field in XR status named masterName
, and this field is populated by CombineToComposite
in the master database patches. You can see this example below:
- type: CombineToComposite
combine:
variables:
- fromFieldPath: "status.atProvider.project"
- fromFieldPath: "metadata.labels[crossplane.io/claim-name]"
strategy: string
string:
fmt: "%s:%s"
toFieldPath: status.masterName
And then you can use FromCompositeFieldPath
to populate the masterInstanceName
field:
- type: FromCompositeFieldPath
fromFieldPath: "status.masterName"
toFieldPath: "spec.forProvider.masterInstanceName"
policy:
fromFieldPath: Required
Note that you need to use
policy: Required
because if you don't, the replica instance will skip this patch whilestatus.masterName
isn't fullfilled, which will lead to two separate database instances (as if you've created two masters). By defining this policy, the replica creation will raise errors until thestatus.masterName
is populated.