Skip to content

Commit

Permalink
feat(crossplane): extend crossplane lib with various helper functions (
Browse files Browse the repository at this point in the history
  • Loading branch information
Duologic authored Apr 14, 2022
1 parent 175eea1 commit ad74662
Show file tree
Hide file tree
Showing 4 changed files with 514 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libs/crossplane/config.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ config.new(
prefix: '^io\\.crossplane\\.(pkg|apiextensions)\\..*',
crds: ['https://doc.crds.dev/raw/github.com/crossplane/[email protected]'],
localName: 'crossplane',
patchDir: 'custom/crossplane',
},
{
output: 'crossplane/1.6',
prefix: '^io\\.crossplane\\.(pkg|apiextensions)\\..*',
crds: ['https://doc.crds.dev/raw/github.com/crossplane/[email protected]'],
localName: 'crossplane',
patchDir: 'custom/crossplane',
},
{
output: 'crossplane/1.5',
prefix: '^io\\.crossplane\\.(pkg|apiextensions)\\..*',
crds: ['https://doc.crds.dev/raw/github.com/crossplane/[email protected]'],
localName: 'crossplane',
patchDir: 'custom/crossplane',
},
{
output: 'provider-aws/0.24',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
local d = import 'doc-util/main.libsonnet';

{
apiextensions+: {
v1+: {
xrd: self.compositeResourceDefinition,
compositeResourceDefinition+: {

'#new':: d.fn(help=|||
new returns an instance of CompositeResourceDefinition=
For example: xpostgresqlinstances.example.org
- <kind>: XPostgreSQLInstance
- <plural>: xpostgresqlinstances
- <group>: example.org
A common convention is that the XR (composite resource) are prefixed with 'X'
while claim names are not. This lets app team members think of creating a claim
as (e.g.) 'creating a PostgreSQLInstance'. Use `withClaimNames` to set this.
|||, args=[
d.arg('kind', d.T.string),
d.arg('plural', d.T.string),
d.arg('group', d.T.string),
]),
new(kind, plural, group):
super.new(plural + '.' + group)
+ super.metadata.withAnnotations({
// Tell Tanka to not set metadata.namespace.
'tanka.dev/namespaced': 'false',
})
+ super.spec.withGroup(group)
+ super.spec.names.withKind(kind)
+ super.spec.names.withPlural(plural)
,

'#withClaimNames':: d.fn(help=|||
Sets the ClaimNames attribute.
Example:
- <kind>: PostgreSQLInstance
- <plural>: postgresqlinstances
A common convention is that the XR (composite resource) are prefixed with 'X'
while claim names are not. This lets app team members think of creating a claim
as (e.g.) 'creating a PostgreSQLInstance'.
|||, args=[
d.arg('kind', d.T.string),
d.arg('plural', d.T.string),
]),
withClaimNames(kind, plural):
super.spec.claimNames.withKind(kind)
+ super.spec.claimNames.withPlural(plural),

'#mapVersions':: d.fn(help=|||
Sets the ClaimNames attribute.
Example:
- <kind>: PostgreSQLInstance
- <plural>: postgresqlinstances
A common convention is that the XR (composite resource) are prefixed with 'X'
while claim names are not. This lets app team members think of creating a claim
as (e.g.) 'creating a PostgreSQLInstance'.
|||, args=[
d.arg('kind', d.T.string),
d.arg('plural', d.T.string),
]),
mapVersions(f): {
local versions = super.spec.versions,
spec+: {
versions: std.map(f, versions),
},
},
},
},
},
}
51 changes: 51 additions & 0 deletions libs/crossplane/custom/crossplane/composition.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local d = import 'doc-util/main.libsonnet';

{
apiextensions+: {
v1+: {
composition+: {
'#fromXRD':: d.fn(help=|||
Create a Composition based on an XRD.
Attributes:
- <name> of the composition
- <namespace> where connectionDetails are propagated too, commonly the the
management namespace (ie. crossplane)
- <provider> of the resources in this composition
- <xrdRef> XRD object with which this composition is compatible
- <xrdVersion> Version of XRD object with which this composition is compatible
|||, args=[
d.arg('name', d.T.string),
d.arg('resource', d.T.object),
]),
fromXRD(name, namespace, provider, xrdRef, xrdVersion):
super.new(name)
+ super.metadata.withAnnotations({
// Tell Tanka to not set metadata.namespace.
'tanka.dev/namespaced': 'false',
})
+ super.metadata.withLabels({
// An optional convention is to include a label of the XRD. This allows easy
// discovery of compatible Compositions.
'crossplane.io/xrd': xrdRef.metadata.name,
// Another optional convention is to include a label of the (most common)
// provider for the resource(s) in this composition. This label can be used in
// 'compositionSelector' in an XR or Claim.
provider: provider,
})

// Each Composition must declare that it is compatible with a particular type of
// Composite Resource using its 'compositeTypeRef' field. The referenced
// version must be marked 'referenceable' in the XRD that defines the XR.
+ super.spec.compositeTypeRef.withApiVersion(xrdRef.spec.group + '/' + xrdVersion)
+ super.spec.compositeTypeRef.withKind(xrdRef.spec.names.kind)


// When an XR is created in response to a claim Crossplane needs to know where it
// should create the XR's connection secret. This is configured using the
// 'writeConnectionSecretsToNamespace' field.
+ super.spec.withWriteConnectionSecretsToNamespace(namespace),
},
},
},
}
Loading

0 comments on commit ad74662

Please sign in to comment.