Skip to content

Instantly share code, notes, and snippets.

@muvaf
Last active January 12, 2022 14:24
Show Gist options
  • Save muvaf/391aee0f6f47acb397fff03b65c3b7be to your computer and use it in GitHub Desktop.
Save muvaf/391aee0f6f47acb397fff03b65c3b7be to your computer and use it in GitHub Desktop.
jetlag-is-so-nice
# Represented with nice boxes in the UI.
apiVersion: java.application.acme.org/v1alpha1
kind: SpringBoot
metadata:
name: dope-app
namespace: purple-squad
spec:
location: us-central1
# * "repo" creates a Github repo for the application.
# * "code" commits the boilerplate code to given repo.
# * "image-repository" creates an image repo in DockerHub to store image.
# * "image-push-user" creates a DockerHub user to be used in Github Actions of repo to push the image.
# * "bucket" creates a bucket because app depends on it.
# * "service" runs the container with the bucket info mounted as env var.
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: springbootgcp.acme.org
spec:
writeConnectionSecretsToNamespace: crossplane-system
compositeTypeRef:
apiVersion: java.application.acme.org/v1alpha1
kind: CompositeSpringBoot
resources:
# Creates a git repository in Github for application code to live.
- name: repo
base:
apiVersion: github.crossplane.io/v1alpha1
kind: Repository
spec:
forProvider:
public: true
organization: myorg
secrets:
- name: DOCKER_USR
secretContentRef:
name: my-docker-user
namespace: crossplane-system
key: user
- name: DOCKER_PSW
secretContentRef:
name: my-docker-user
namespace: crossplane-system
key: password
patches:
- fromFieldPath: "spec.claimRef.name"
toFieldPath: "metadata.annotations[crossplane.io/external-name]"
transforms:
- type: string
string:
type: Convert
convert: ToLower
- fromFieldPath: "spec.claimRef.namespace"
toFieldPath: "spec.forProvider.buildPipeline.githubActions.imageRepository.organization"
# Fetching the values from connection secret of Dockerhub User.
- fromFieldPath: metadata.uid
toFieldPath: spec.forProvider.secrets[0].secretContentRef.name
policy:
fromFieldPath: Required
transforms:
- type: string
string:
fmt: "%s-cicd-user"
- fromFieldPath: metadata.uid
toFieldPath: spec.forProvider.secrets[1].secretContentRef.name
policy:
fromFieldPath: Required
transforms:
- type: string
string:
fmt: "%s-cicd-user"
# This will commit the boilerplate Spring Boot code to
# given repository.
- name: code
base:
apiVersion: springboot.java.crossplane.io/v1alpha1
kind: Boilerplate
spec:
forProvider:
targetRepositoryRef:
matchControllerRef: true
buildPipeline:
type: GithubActions
githubActions:
imageRepository:
organization: myorg
credentials:
loginEnvName: DOCKER_USR
passwordEnvName: DOCKER_PSW
patches:
- fromFieldPath: "spec.claimRef.name"
toFieldPath: "metadata.annotations[crossplane.io/external-name]"
- fromFieldPath: "spec.claimRef.namespace"
toFieldPath: "spec.forProvider.buildPipeline.githubActions.imageRepository.organization"
# Creates an image repository in DockerHub to store the container image.
- name: image-repository
base:
apiVersion: dockerhub.crossplane.io/v1alpha1
kind: ImageRepository
spec:
forProvider:
organization: myorg
public: true
patches:
- fromFieldPath: "spec.claimRef.name"
toFieldPath: "metadata.annotations[crossplane.io/external-name]"
transforms:
- type: string
string:
type: Convert
convert: ToLower
- fromFieldPath: status.atProvider.url
toFieldPath: status.imageUrl
# Creates a DockerHub user for Github Actions to push the container image
- name: image-push-user
base:
apiVersion: dockerhub.crossplane.io/v1alpha1
kind: User
spec:
forProvider:
permissions:
- action: push
repositories:
- nameRef:
matchControllerRef: true
writeConnectionSecretsToRef:
namespace: crossplane-system
patches:
- fromFieldPath: "spec.claimRef.name"
toFieldPath: "metadata.annotations[crossplane.io/external-name]"
transforms:
- type: string
string:
fmt: "%s-cicd-user"
- fromFieldPath: "metadata.uid"
toFieldPath: spec.writeConnectionSecretsToRef.name
transforms:
- type: string
string:
fmt: "%s-cicd-user"
# We assume that application uses a bucket.
- name: bucket
base:
apiVersion: storage.gcp.jet.crossplane.io/v1alpha2
kind: Bucket
spec:
forProvider:
location: us-central1
patches:
- fromFieldPath: spec.parameters.location
toFieldPath: spec.forProvider.location
- type: ToCompositeFieldPath
fromFieldPath: metadata.annotations[crossplane.io/external-name]
toFieldPath: status.bucketName
# We run the container in Google CloudRun once image in DockerHub and bucket
# in GCP are ready and patch back the application URL.
- name: service
base:
apiVersion: cloudrun.gcp.jet.crossplane.io/v1alpha1
kind: Service
spec:
forProvider:
location: us-central1
template:
spec:
containers:
- image: toBePatched
env:
- name: BUCKET_NAME
value: toBePatched
traffic:
- latestRevision: true
percent: 100
patches:
- fromFieldPath: spec.parameters.location
toFieldPath: spec.forProvider.location
- fromFieldPath: status.imageUrl
toFieldPath: spec.forProvider.template.spec.containers[0].image
# To make sure we call GCP only once the image url appears in status.
policy:
fromFieldPath: Required
- fromFieldPath: status.bucketName
toFieldPath: spec.forProvider.template.spec.containers[0].env[0].value
# To make sure we call GCP only once the bucket name appears in status.
policy:
fromFieldPath: Required
- type: ToCompositeFieldPath
fromFieldPath: status.atProvider.status[0].url
toFieldPath: status.url
apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
name: compositespringboots.java.application.acme.org
spec:
group: java.application.acme.org
names:
kind: CompositeSpringBoot
plural: compositespringboots
claimNames:
kind: SpringBoot
plural: springboots
versions:
- name: v1alpha1
served: true
referenceable: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
parameters:
type: object
properties:
location:
type: string
required:
- location
required:
- parameters
status:
type: object
properties:
url:
type: string
bucketName:
type: string
imageUrl:
type: string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment