Skip to content

Instantly share code, notes, and snippets.

@gotwarlost
Last active July 17, 2024 18:36
Show Gist options
  • Save gotwarlost/74791eb45f016188e0408b22da1e3677 to your computer and use it in GitHub Desktop.
Save gotwarlost/74791eb45f016188e0408b22da1e3677 to your computer and use it in GitHub Desktop.
Tooling that converts an API type in cue to a crossplane Composite Resource definition (XRD)
go install github.com/crossplane-contrib/function-cue/cmd/fn-cue-tools@v0.3.0
mkdir -p zz_generated/schemas
fn-cue-tools openapi --pkg schemas ./api/ >zz_generated/schemas/generated-schema-to-avoid.cue
cue eval ./xrd --out yaml
-- api/api.cue --
package api
// A config map that is replicated to multiple namespaces
#ReplicatedMapV1alpha1: {
// desired state of the replicated config map
spec: {
// input parameters
parameters: {
// data for the config map that will be created
data: [string]: string
// the namespaces to replicate the config map
namespaces: [...string]
// optional name for the config map. Default is the claim name
name?: string
}
}
}
-- cue.mod/module.cue --
module: "cue-functions.io/examples/simple"
language: version: "v0.9.0"
-- xrd/xrd.cue --
package xrd
import (
schemas "cue-functions.io/examples/simple/zz_generated/schemas"
)
let pluralName = "xreplicatedmaps"
let groupName = "simple.cuefn.example.com"
{
apiVersion: "apiextensions.crossplane.io/v1"
kind: "CompositeResourceDefinition"
metadata: {
name: "\(pluralName).\(groupName)"
}
spec: {
group: groupName
names: {
kind: "XReplicatedMap"
plural: pluralName
}
claimNames: {
kind: "ReplicatedMap"
plural: "replicatedmaps"
}
versions: [{
name: "v1alpha1"
served: true
referenceable: true
schema: openAPIV3Schema: schemas.components.schemas.ReplicatedMapV1alpha1
}]
}
}
package api
// A config map that is replicated to multiple namespaces
#ReplicatedMapV1alpha1: {
// desired state of the replicated config map
spec: {
// input parameters
parameters: {
// data for the config map that will be created
data: [string]: string
// the namespaces to replicate the config map
namespaces: [...string]
// optional name for the config map. Default is the claim name
name?: string
}
}
}
kind: CompositeResourceDefinition
apiVersion: apiextensions.crossplane.io/v1
metadata:
name: xreplicatedmaps.simple.cuefn.example.com
spec:
group: simple.cuefn.example.com
names:
plural: xreplicatedmaps
kind: XReplicatedMap
claimNames:
kind: ReplicatedMap
plural: replicatedmaps
versions:
- name: v1alpha1
served: true
referenceable: true
schema:
openAPIV3Schema:
description: A config map that is replicated to multiple namespaces
type: object
required:
- spec
properties:
spec:
description: desired state of the replicated config map
type: object
required:
- parameters
properties:
parameters:
description: input parameters
type: object
required:
- data
- namespaces
properties:
data:
description: data for the config map that will be created
type: object
additionalProperties:
type: string
namespaces:
description: the namespaces to replicate the config map
type: array
items:
type: string
name:
description: optional name for the config map. Default is the claim name
type: string
package schemas
{
openapi: "3.0.0"
info: {
description: "Generated by fn-cue-tools, DO NOT EDIT"
title: "XRD schemas"
version: "0.1.0"
}
paths: {}
components: schemas: {
ReplicatedMapV1alpha1: {
description: "A config map that is replicated to multiple namespaces"
type: "object"
required: [
"spec",
]
properties: spec: {
description: "desired state of the replicated config map"
type: "object"
required: [
"parameters",
]
properties: parameters: {
description: "input parameters"
type: "object"
required: [
"data",
"namespaces",
]
properties: {
data: {
description: "data for the config map that will be created"
type: "object"
additionalProperties: type: "string"
}
namespaces: {
description: "the namespaces to replicate the config map"
type: "array"
items: type: "string"
}
name: {
description: "optional name for the config map. Default is the claim name"
type: "string"
}
}
}
}
}
}
}
package xrd
import (
schemas "cue-functions.io/examples/simple/zz_generated/schemas"
)
let pluralName = "xreplicatedmaps"
let groupName = "simple.cuefn.example.com"
{
apiVersion: "apiextensions.crossplane.io/v1"
kind: "CompositeResourceDefinition"
metadata: {
name: "\(pluralName).\(groupName)"
}
spec: {
group: groupName
names: {
kind: "XReplicatedMap"
plural: pluralName
}
claimNames: {
kind: "ReplicatedMap"
plural: "replicatedmaps"
}
versions: [{
name: "v1alpha1"
served: true
referenceable: true
schema: openAPIV3Schema: schemas.components.schemas.ReplicatedMapV1alpha1
}]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment