Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
chore: added example 02 - environment
Browse files Browse the repository at this point in the history
  • Loading branch information
ghen committed Nov 2, 2022
1 parent 1c32fda commit 48b53c0
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
115 changes: 115 additions & 0 deletions examples/02-environment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# 02 - Environment Variables

It is common to pass a dynamic environment-specific configuration to the container during the deployment.

In score specification a special `environment` resource type is used to support such cases:

```yaml
apiVersion: score.dev/v1b1

metadata:
name: hello-world

containers:
hello:
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo Hello $${FRIEND}!; sleep 5; done"]
variables:
FRIEND: ${resources.env.NAME}

resources:
env:
type: environment
properties:
NAME:
type: string
default: World
```
If there is a need to set the `NAME` variable value for the environment the workload would be deployed into, an `env.yaml` file can be used:

```yaml
env:
NAME: John
```

Now the source `score.yaml` file and the `env.yaml` file should be combined and converted into Helm values file with `score-helm` CLI tool:

```console
$ score-helm run -f ./score.yaml --values ./env.yaml -o ./values.yaml
```

Output `values.yaml` file would contain a workload configuration for the chart:

```yaml
containers:
hello:
args:
- -c
- while true; do echo Hello $${FRIEND}!; sleep 5; done
command:
- /bin/sh
env:
- name: FRIEND
value: John
image:
name: busybox
```

A generic chart template from `/examples/chart` can be used to prepare a Helm chart for this example:

```console
$ helm create -p ../examples/chart hello
```

Deploying the Helm chart:

```console
$ helm install --values ./values.yaml hello ./hello
NAME: hello
LAST DEPLOYED: Wed Nov 2 14:40:11 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
```

Resulting Kubernetes deployment object:

```yaml
# Source: hello/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello
labels:
helm.sh/chart: hello-0.1.0
app.kubernetes.io/name:
app.kubernetes.io/instance: hello
app.kubernetes.io/version: "0.1.0"
app.kubernetes.io/managed-by: Helm
spec:
selector:
matchLabels:
app.kubernetes.io/name:
app.kubernetes.io/instance: hello
template:
metadata:
labels:
app.kubernetes.io/name:
app.kubernetes.io/instance: hello
spec:
containers:
- name: hello
image: "busybox"
command:
- /bin/sh
args:
- -c
- while true; do echo Hello $${FRIEND}!; sleep 5; done
env:
- name: FRIEND
value: John
```
2 changes: 2 additions & 0 deletions examples/02-environment/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env:
NAME: John
17 changes: 17 additions & 0 deletions examples/02-environment/score.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: score.dev/v1b1
metadata:
name: hello-world
containers:
hello:
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo Hello $${FRIEND}!; sleep 5; done"]
variables:
FRIEND: ${resources.env.NAME}
resources:
env:
type: environment
properties:
NAME:
type: string
default: World
4 changes: 2 additions & 2 deletions examples/chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ spec:
args:
{{- toYaml $container.args | nindent 12 }}
{{- end }}
{{- with $container.variables }}
{{- with $container.env }}
env:
{{- toYaml $container.variables | nindent 12 }}
{{- toYaml $container.env | nindent 12 }}
{{- end }}
{{- with $container.volumeMounts }}
volumeMounts:
Expand Down

0 comments on commit 48b53c0

Please sign in to comment.