Skip to content

Commit

Permalink
Add kubernetes-nginx example (#219)
Browse files Browse the repository at this point in the history
* Add kubernetes-nginx example
* Fix test-examples in Justfile
* Fix test-templates in Justfile
  • Loading branch information
pawelprazak authored Oct 5, 2023
1 parent fec6fec commit 01b1cfd
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 17 deletions.
32 changes: 15 additions & 17 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -259,49 +259,47 @@ test-compiler-plugin: publish-local-sdk publish-local-compiler-plugin
# Runs a template test
test-template template-name:
echo "Testing template {{template-name}}"
@echo "Testing template {{template-name}}"
pulumi --color=never --emoji=false new -y --force --dir target/test/{{template-name}} -n templates-test-{{template-name}} --stack templates-test-{{template-name}} ../../../templates/{{template-name}}/
scala-cli compile target/test/{{template-name}}
echo "----------------------------------------"
@echo "----------------------------------------"
# Cleans after a template test
clean-test-template template-name:
echo "Cleaning template test for {{template-name}}"
@echo "Cleaning template test for {{template-name}}"
scala-cli clean target/test/{{template-name}} || echo "Could not clean"
pulumi --color=never --emoji=false stack rm --cwd target/test/{{template-name}} -y || echo "No stack to remove"
rm -rf ./templates/test/{{template-name}} || echo "No directory to remove"
rm -rf ./target/test/{{template-name}} || echo "No directory to remove"
rm -rf $HOME/.pulumi/stacks/templates-test-{{template-name}} || echo "No directory to remove"
echo "----------------------------------------"
@echo "----------------------------------------"
# Runs all template tests
test-templates:
just test-template default
just test-template kubernetes
just test-template aws
for file in `ls -d templates/*/ | cut -f2 -d'/'`; do just test-template $file; done
# Cleans after template tests
clean-test-templates:
just clean-test-template default
just clean-test-template kubernetes
just clean-test-template aws
for file in `ls -d templates/*/ | cut -f2 -d'/'`; do just clean-test-template $file; done
# Runs an example test
test-example example-name:
echo "Testing example {{example-name}}"
@echo "Testing example {{example-name}}"
scala-cli compile examples/{{example-name}}
echo "----------------------------------------"
@echo "----------------------------------------"
# Cleans after an example test
clean-test-example example-name:
echo "Cleaning example test for {{example-name}}"
echo "----------------------------------------"
@echo "Cleaning example test for {{example-name}}"
scala-cli clean examples/{{example-name}}
@echo "----------------------------------------"
# Runs all template tests
test-examples:
just test-example aws-s3-folder
for file in `ls -d examples/*/ | cut -f2 -d'/'`; do just test-example $file; done
# Cleans after template tests
clean-test-examples:
just clean-test-example aws-s3-folder
for file in `ls -d examples/*/ | cut -f2 -d'/'`; do just clean-test-example $file; done
####################
# Demo
Expand Down
8 changes: 8 additions & 0 deletions examples/kubernetes-nginx/.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*
47 changes: 47 additions & 0 deletions examples/kubernetes-nginx/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import besom.*
import besom.api.kubernetes
import besom.api.kubernetes.core.v1.*
import besom.api.kubernetes.core.v1.inputs.*
import besom.api.kubernetes.apps.v1.*
import besom.api.kubernetes.apps.v1.inputs.*
import besom.api.kubernetes.meta.v1.*
import besom.api.kubernetes.meta.v1.inputs.*

@main def main = Pulumi.run {
val nginxLabels = Map("app" -> "nginx")
val nginxDeployment = kubernetes.apps.v1.Deployment(
"nginx",
kubernetes.apps.v1.DeploymentArgs(
spec = DeploymentSpecArgs(
replicas = config.getInt("replicas"),
selector = LabelSelectorArgs(
matchLabels = nginxLabels
),
template = PodTemplateSpecArgs(
metadata = ObjectMetaArgs(
labels = nginxLabels
),
spec = PodSpecArgs(
containers = List(
ContainerArgs(
name = "nginx",
image = "nginx:stable",
ports = List(
ContainerPortArgs(
containerPort = 80
)
)
)
)
)
)
)
)
)

for {
_ <- nginxDeployment
} yield exports(
nginx = nginxDeployment.metadata.name
)
}
4 changes: 4 additions & 0 deletions examples/kubernetes-nginx/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: kubernetes-nginx
runtime: scala
description: Example of a Kubernetes Stateless Application Deployment, using Nginx
template:
53 changes: 53 additions & 0 deletions examples/kubernetes-nginx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Stateless Application Using a Kubernetes Deployment

A version of the [Kubernetes Stateless Application Deployment](
https://kubernetes.io/docs/tasks/run-application/run-stateless-application-deployment/) example that uses Pulumi.
This example deploys a replicated Nginx server to a Kubernetes cluster, using Scala and no YAML.

This example is based on a [Pulumi Tutorial available here](https://www.pulumi.com/docs/tutorials/kubernetes/stateless-app/).

## Prerequisites

[Follow the instructions](https://www.pulumi.com/docs/clouds/kubernetes/get-started/begin/)
to get started with Pulumi & Kubernetes.

## Deploying and running the program

Note: some values in this example will be different from run to run.
These values are indicated with `***`.

1. Create a new stack, which is an isolated deployment target for this example:

```bash
pulumi stack init kubernetes-nginx-dev
```

2. Run `pulumi up` to preview and deploy changes. After the preview is shown
you will be prompted if you want to continue or not.

```bash
pulumi config set replicas 2
pulumi up
```

After a couple of minutes, your deployment will be ready, then you can run commands like `kubectl get pods`
to see the application's resources.
The stack's replica count is configurable. By default, it will scale up to three instances, but we can easily change
that to five, by running the `pulumi config` command followed by another `pulumi up`:

```bash
pulumi config set replicas 5
pulumi up
```

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

4. To clean up resources, destroy your stack and remove it:

```bash
pulumi destroy
```
```bash
pulumi stack rm kubernetes-nginx-dev
```
5 changes: 5 additions & 0 deletions examples/kubernetes-nginx/project.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//> using scala "3.3.1"
//> using plugin "org.virtuslab::besom-compiler-plugin:0.1.0"
//> using dep "org.virtuslab::besom-core:0.1.0"
//> using dep "org.virtuslab::besom-kubernetes:4.3.0-core.0.1.0"
//> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement

0 comments on commit 01b1cfd

Please sign in to comment.