Skip to content

Commit

Permalink
Add Kubernetes YAML with CRD example (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
polkx authored Apr 24, 2024
1 parent f4618b5 commit 0b3b79e
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/kubernetes-yaml-crd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### Scala an JVM
*.class
*.log
.bsp
.scala-build

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
74 changes: 74 additions & 0 deletions examples/kubernetes-yaml-crd/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import besom.*
import besom.api.kubernetes as k8s

@main def main = Pulumi.run {

val crd =
k8s.yaml.v2.ConfigGroup(
name = "config-group",
k8s.yaml.v2.ConfigGroupArgs(
yaml = """apiVersion: apiextensions.k8s.io/v1
|kind: CustomResourceDefinition
|metadata:
| # name must match the spec fields below, and be in the form: <plural>.<group>
| name: crontabs.stable.example.com
|spec:
| # group name to use for REST API: /apis/<group>/<version>
| group: stable.example.com
| # list of versions supported by this CustomResourceDefinition
| versions:
| - name: v1
| # Each version can be enabled/disabled by Served flag.
| served: true
| # One and only one version must be marked as the storage version.
| storage: true
| schema:
| openAPIV3Schema:
| type: object
| properties:
| spec:
| type: object
| properties:
| cronSpec:
| type: string
| image:
| type: string
| replicas:
| type: integer
| # either Namespaced or Cluster
| scope: Namespaced
| names:
| # plural name to be used in the URL: /apis/<group>/<version>/<plural>
| plural: crontabs
| # singular name to be used as an alias on the CLI and for display
| singular: crontab
| # kind is normally the CamelCased singular type. Your resource manifests use this.
| kind: CronTab
| # shortNames allow shorter string to match your resource on the CLI
| shortNames:
| - ct
| """.stripMargin
)
)

val cronTab =
k8s.yaml.v2.ConfigGroup(
name = "cron-tab",
k8s.yaml.v2.ConfigGroupArgs(
yaml = """apiVersion: "stable.example.com/v1"
|kind: CronTab
|metadata:
| name: my-new-cron-object
|spec:
| cronSpec: "* * * * */5"
| image: my-awesome-cron-image
| """.stripMargin
),
opts = opts(dependsOn = crd)
)

Stack.exports(
crdResource = crd.resources,
cronTabResource = cronTab.resources
)
}
3 changes: 3 additions & 0 deletions examples/kubernetes-yaml-crd/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: kubernetes-yaml-crd
runtime: scala
description: Example of a Kubernetes Application using plain YAML files with custom CRD
54 changes: 54 additions & 0 deletions examples/kubernetes-yaml-crd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Kubernetes application using plain YAML files with custom CRD

Example based
on [Kubernetes custom resource definitions](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/).
Uses plain YAML files to create custom CronTab CRD and deploy it to Kubernetes cluster.

## Prerequisites

Follow the steps in [Pulumi Installation and
Setup](https://www.pulumi.com/docs/get-started/install/) and [Configuring Pulumi
Kubernetes](https://www.pulumi.com/docs/intro/cloud-providers/kubernetes/setup/) to get set up with
Pulumi and Kubernetes.

## Running the App

1. Create a new stack:

```sh
$ pulumi stack init
Enter a stack name: dev
```

2. Preview the deployment of the application and perform the deployment:

```sh
$ pulumi up
```

3. You can then manage your CronTab objects using kubectl:

```sh
$ kubectl get crontab
```
Should print a list like this:
```
NAME AGE
my-new-cron-object 6s
```

4. You can also view the raw YAML data:

```sh
$ kubectl get ct -o yaml
```

5. From there, feel free to experiment. Simply making edits and running pulumi up will incrementally update your
infrastructure.

6. Once you've finished experimenting, tear down your stack's resources by destroying and removing it:

```bash
$ pulumi destroy --yes
$ pulumi stack rm --yes
```
4 changes: 4 additions & 0 deletions examples/kubernetes-yaml-crd/project.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//> using scala "3.3.1"
//> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement
//> using dep "org.virtuslab::besom-core:0.3.1"
//> using dep "org.virtuslab::besom-kubernetes:4.11.0-core.0.3"

0 comments on commit 0b3b79e

Please sign in to comment.