├── README.md ├── configmap-importfile ├── .gitignore ├── README.md ├── app.yaml ├── components │ ├── nginx-configmap.jsonnet │ ├── nginx.conf │ ├── params.libsonnet │ └── proxy.conf └── environments │ ├── base.libsonnet │ └── default │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet ├── configmap-parameterized ├── .gitignore ├── README.md ├── app.yaml ├── components │ ├── example-configmap.jsonnet │ └── params.libsonnet └── environments │ ├── base.libsonnet │ └── default │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet ├── configmap ├── .gitignore ├── README.md ├── app.yaml ├── components │ ├── example-configmap.jsonnet │ └── params.libsonnet └── environments │ ├── base.libsonnet │ └── default │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet ├── drop-in-yaml ├── .gitignore ├── README.md ├── app.yaml ├── components │ ├── nginx-deployment.yaml │ └── params.libsonnet └── environments │ ├── base.libsonnet │ └── default │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet ├── env-extra-resource ├── .gitignore ├── README.md ├── app.yaml ├── components │ ├── nginx-deployment.jsonnet │ └── params.libsonnet └── environments │ ├── base.libsonnet │ ├── default │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet │ └── extra-resource │ ├── extra-deployment.jsonnet │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet ├── env-metadata ├── .gitignore ├── README.md ├── app.yaml ├── components │ ├── nginx-deployment.jsonnet │ └── params.libsonnet └── environments │ ├── base.libsonnet │ ├── default │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet │ └── prod │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet ├── global-params ├── .gitignore ├── README.md ├── app.yaml ├── components │ ├── nginx-deployment.jsonnet │ └── params.libsonnet └── environments │ ├── base.libsonnet │ ├── default │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet │ └── prod │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet ├── parse-yaml ├── .gitignore ├── README.md ├── app.yaml ├── components │ ├── imports │ │ └── nginx-deployment.yaml │ ├── nginx-deployment.jsonnet │ └── params.libsonnet └── environments │ ├── base.libsonnet │ └── default │ ├── globals.libsonnet │ ├── main.jsonnet │ └── params.libsonnet └── sidecar-injection ├── .gitignore ├── README.md ├── app.yaml ├── components ├── nginx-deployment.jsonnet └── params.libsonnet └── environments ├── base.libsonnet ├── default ├── globals.libsonnet ├── main.jsonnet └── params.libsonnet └── no-sidecar ├── globals.libsonnet ├── main.jsonnet └── params.libsonnet /README.md: -------------------------------------------------------------------------------- 1 | # Practical Ksonnet Examples 2 | 3 | This repository contains practical, real-world examples of using ksonnet to manage kubernetes 4 | configurations in bite-size examples. Each directory is a separate ksonnet app which demonstrates 5 | a single feature of Jsonnet/Ksonnet to achieve a specific use case. To run an example, clone 6 | the repo and run `ks show default` inside the ksonnet app directory of an example. 7 | 8 | Contributions are welcome! 9 | 10 | ## Requirements 11 | * ksonnet 1.11+ 12 | -------------------------------------------------------------------------------- /configmap-importfile/.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /.ksonnet/registries 3 | /app.override.yaml 4 | /.ks_environment 5 | -------------------------------------------------------------------------------- /configmap-importfile/README.md: -------------------------------------------------------------------------------- 1 | ## ConfigMap with Imported Files 2 | This example builds on the [Parameterized ConfigMap](../configmap-parameterized/README.md) example 3 | and demonstrates how jsonnet can import a local file on disk as a string using the `importstr` instruction. The config data is defined in the files `components/nginx.conf` and `components/proxy.conf`. The configmap component, `components/nginx-configmap.jsonnet`, imports those files as strings and performs string formatting with parameter substitution. 4 | 5 | `nginx.conf:` 6 | ``` 7 | { 8 | ... 9 | server { # php/fastcgi 10 | listen %(port)s; 11 | server_name %(serverNames)s; 12 | access_log logs/domain1.access.log main; 13 | ... 14 | } 15 | } 16 | 17 | ``` 18 | 19 | `components/nginx-configmap.jsonnet:` 20 | ``` 21 | local params = std.extVar("__ksonnet/params").components["nginx-configmap"]; 22 | local nginxConf = importstr "./nginx.conf"; 23 | local proxyConf = importstr "./proxy.conf"; 24 | { 25 | "kind": "ConfigMap", 26 | "apiVersion": "v1", 27 | "metadata": { 28 | "name": "nginx-configmap" 29 | }, 30 | "data": { 31 | "nginx.conf": nginxConf % params, 32 | "proxy.conf": proxyConf % params, 33 | }, 34 | } 35 | ``` 36 | 37 | `ks show default`: 38 | ``` 39 | --- 40 | apiVersion: v1 41 | data: 42 | nginx.conf: |- 43 | ... 44 | server { # php/fastcgi 45 | listen 80; 46 | server_name domain1.com www.domain1.com; 47 | access_log logs/domain1.access.log main; 48 | ... 49 | kind: ConfigMap 50 | metadata: 51 | name: nginx-configmap 52 | ``` 53 | 54 | 55 | To run this example, run: 56 | ``` 57 | ks show default 58 | ``` 59 | -------------------------------------------------------------------------------- /configmap-importfile/app.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 0.1.0 2 | environments: 3 | default: 4 | destination: 5 | namespace: default 6 | server: https://1.2.3.4 7 | k8sVersion: v1.10.0 8 | path: default 9 | kind: ksonnet.io/app 10 | name: configmap-importfile 11 | version: 0.0.1 12 | -------------------------------------------------------------------------------- /configmap-importfile/components/nginx-configmap.jsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params").components["nginx-configmap"]; 2 | local nginxConf = importstr "./nginx.conf"; 3 | local proxyConf = importstr "./proxy.conf"; 4 | { 5 | "kind": "ConfigMap", 6 | "apiVersion": "v1", 7 | "metadata": { 8 | "name": "nginx-configmap" 9 | }, 10 | "data": { 11 | "nginx.conf": nginxConf % params, 12 | "proxy.conf": proxyConf % params, 13 | }, 14 | } -------------------------------------------------------------------------------- /configmap-importfile/components/nginx.conf: -------------------------------------------------------------------------------- 1 | user www www; ## Default: nobody 2 | worker_processes ; ## Default: 1 3 | error_log logs/error.log; 4 | pid logs/nginx.pid; 5 | worker_rlimit_nofile 8192; 6 | 7 | events { 8 | worker_connections 4096; ## Default: 1024 9 | } 10 | 11 | http { 12 | include conf/mime.types; 13 | include /etc/nginx/proxy.conf; 14 | include /etc/nginx/fastcgi.conf; 15 | index index.html index.htm index.php; 16 | 17 | default_type application/octet-stream; 18 | log_format main '$remote_addr - $remote_user [$time_local] $status ' 19 | '"$request" $body_bytes_sent "$http_referer" ' 20 | '"$http_user_agent" "$http_x_forwarded_for"'; 21 | access_log logs/access.log main; 22 | sendfile on; 23 | tcp_nopush on; 24 | server_names_hash_bucket_size 128; # this seems to be required for some vhosts 25 | 26 | server { # php/fastcgi 27 | listen %(port)s; 28 | server_name %(serverNames)s; 29 | access_log logs/domain1.access.log main; 30 | root html; 31 | 32 | location ~ \.php$ { 33 | fastcgi_pass 127.0.0.1:1025; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /configmap-importfile/components/params.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | global: { 3 | // User-defined global parameters; accessible to all component and environments, Ex: 4 | // replicas: 4, 5 | }, 6 | components: { 7 | "nginx-configmap": { 8 | "serverNames": "domain1.com www.domain1.com", 9 | "port": 80, 10 | }, 11 | }, 12 | } 13 | -------------------------------------------------------------------------------- /configmap-importfile/components/proxy.conf: -------------------------------------------------------------------------------- 1 | proxy_redirect off; 2 | proxy_set_header Host $host; 3 | proxy_set_header X-Real-IP $remote_addr; 4 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 5 | client_max_body_size 10m; 6 | client_body_buffer_size 128k; 7 | proxy_connect_timeout 90; 8 | proxy_send_timeout 90; 9 | proxy_read_timeout 90; 10 | proxy_buffers 32 4k; -------------------------------------------------------------------------------- /configmap-importfile/environments/base.libsonnet: -------------------------------------------------------------------------------- 1 | local components = std.extVar("__ksonnet/components"); 2 | components + { 3 | // Insert user-specified overrides here. 4 | } 5 | -------------------------------------------------------------------------------- /configmap-importfile/environments/default/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /configmap-importfile/environments/default/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | local k = import "k.libsonnet"; 3 | 4 | base + { 5 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 6 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 7 | } 8 | -------------------------------------------------------------------------------- /configmap-importfile/environments/default/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /configmap-parameterized/.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /.ksonnet/registries 3 | /app.override.yaml 4 | /.ks_environment 5 | -------------------------------------------------------------------------------- /configmap-parameterized/README.md: -------------------------------------------------------------------------------- 1 | ## Parameterized ConfigMap 2 | This example builds on the [ConfigMap](../configmap/README.md) example and demonstrates how a 3 | ConfigMap might be parameterized per environment. The configmap values are defined in 4 | `components/params.libsonnet`, and the `components/example-configmap.jsonnet` imports the parameters 5 | and references the value. Two styles of Jsonnet string formatting are demonstrated. The 6 | `game.properties` uses printf style of formatting of ordered variables, where `ui.properties` 7 | references of individual map keys in the params map. 8 | 9 | ``` 10 | local params = std.extVar("__ksonnet/params").components["example-configmap"]; 11 | { 12 | "kind": "ConfigMap", 13 | "apiVersion": "v1", 14 | "metadata": { 15 | "name": "game-config" 16 | }, 17 | "data": { 18 | "game.properties": ||| 19 | enemies=%s 20 | lives=%s 21 | enemies.cheat=%s 22 | enemies.cheat.level=%s 23 | secret.code.passphrase=%s 24 | secret.code.allowed=%s 25 | secret.code.lives=%s 26 | ||| % [params.enemies, params.lives, params.enemiesCheat, params.enemiesCheatLevel, 27 | params.secretCodePassphrase, params.secretCodeAllowed, params.secretCodeLives], 28 | 29 | "ui.properties": ||| 30 | color.good=%(colorGood)s 31 | color.bad=%(colorBad)s 32 | allow.textmode=%(allowTextMode)s 33 | how.nice.to.look=%(howNiceToLook)s 34 | ||| % params, 35 | }, 36 | } 37 | ``` 38 | 39 | To run this example, run: 40 | ``` 41 | ks show default 42 | ``` 43 | -------------------------------------------------------------------------------- /configmap-parameterized/app.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 0.1.0 2 | environments: 3 | default: 4 | destination: 5 | namespace: default 6 | server: https://1.2.3.4 7 | k8sVersion: v1.9.4 8 | path: default 9 | kind: ksonnet.io/app 10 | name: configmap 11 | registries: 12 | incubator: 13 | gitVersion: 14 | commitSha: 40285d8a14f1ac5787e405e1023cf0c07f6aa28c 15 | refSpec: master 16 | protocol: github 17 | uri: github.com/ksonnet/parts/tree/master/incubator 18 | version: 0.0.1 19 | -------------------------------------------------------------------------------- /configmap-parameterized/components/example-configmap.jsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params").components["example-configmap"]; 2 | { 3 | "kind": "ConfigMap", 4 | "apiVersion": "v1", 5 | "metadata": { 6 | "name": "game-config" 7 | }, 8 | "data": { 9 | "game.properties": ||| 10 | enemies=%s 11 | lives=%s 12 | enemies.cheat=%s 13 | enemies.cheat.level=%s 14 | secret.code.passphrase=%s 15 | secret.code.allowed=%s 16 | secret.code.lives=%s 17 | ||| % [params.enemies, params.lives, params.enemiesCheat, params.enemiesCheatLevel, 18 | params.secretCodePassphrase, params.secretCodeAllowed, params.secretCodeLives], 19 | "ui.properties": ||| 20 | color.good=%(colorGood)s 21 | color.bad=%(colorBad)s 22 | allow.textmode=%(allowTextMode)s 23 | how.nice.to.look=%(howNiceToLook)s 24 | ||| % params, 25 | }, 26 | } -------------------------------------------------------------------------------- /configmap-parameterized/components/params.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | global: { 3 | }, 4 | components: { 5 | "example-configmap": { 6 | // game.properties 7 | enemies: "aliens", 8 | lives: 3, 9 | enemiesCheat: true, 10 | enemiesCheatLevel: "noGoodRotten", 11 | secretCodePassphrase: "UUDDLRLRBABAS", 12 | secretCodeAllowed: true, 13 | secretCodeLives: 30, 14 | 15 | // ui.properties 16 | colorGood: "purple", 17 | colorBad: "yellow", 18 | allowTextMode: true, 19 | howNiceToLook: "fairlyNice", 20 | }, 21 | }, 22 | } 23 | -------------------------------------------------------------------------------- /configmap-parameterized/environments/base.libsonnet: -------------------------------------------------------------------------------- 1 | local components = std.extVar("__ksonnet/components"); 2 | components + { 3 | // Insert user-specified overrides here. 4 | } 5 | -------------------------------------------------------------------------------- /configmap-parameterized/environments/default/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /configmap-parameterized/environments/default/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | local k = import "k.libsonnet"; 3 | 4 | base + { 5 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 6 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 7 | } 8 | -------------------------------------------------------------------------------- /configmap-parameterized/environments/default/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /configmap/.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /.ksonnet/registries 3 | /app.override.yaml 4 | /.ks_environment 5 | -------------------------------------------------------------------------------- /configmap/README.md: -------------------------------------------------------------------------------- 1 | ## ConfigMap 2 | This example demonstrates a ConfigMap defined in jsonnet, whose data is maintainable in a human 3 | readable format. It makes use of Jsonnet's multi-line text blocks with triple pipe `|||` quoting. 4 | 5 | ``` 6 | { 7 | "kind": "ConfigMap", 8 | "apiVersion": "v1", 9 | "metadata": { 10 | "name": "game-config" 11 | }, 12 | "data": { 13 | "game.properties": ||| 14 | enemies=aliens 15 | lives=3 16 | enemies.cheat=true 17 | enemies.cheat.level=noGoodRotten 18 | secret.code.passphrase=UUDDLRLRBABAS 19 | secret.code.allowed=true 20 | secret.code.lives=30 21 | |||, 22 | "ui.properties": ||| 23 | color.good=purple 24 | color.bad=yellow 25 | allow.textmode=true 26 | how.nice.to.look=fairlyNice 27 | |||, 28 | }, 29 | } 30 | ``` 31 | 32 | To run this example, run: 33 | ``` 34 | ks show default 35 | ``` 36 | -------------------------------------------------------------------------------- /configmap/app.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 0.1.0 2 | environments: 3 | default: 4 | destination: 5 | namespace: default 6 | server: https://1.2.3.4 7 | k8sVersion: v1.9.4 8 | path: default 9 | kind: ksonnet.io/app 10 | name: configmap 11 | registries: 12 | incubator: 13 | gitVersion: 14 | commitSha: 40285d8a14f1ac5787e405e1023cf0c07f6aa28c 15 | refSpec: master 16 | protocol: github 17 | uri: github.com/ksonnet/parts/tree/master/incubator 18 | version: 0.0.1 19 | -------------------------------------------------------------------------------- /configmap/components/example-configmap.jsonnet: -------------------------------------------------------------------------------- 1 | { 2 | "kind": "ConfigMap", 3 | "apiVersion": "v1", 4 | "metadata": { 5 | "name": "game-config" 6 | }, 7 | "data": { 8 | "game.properties": ||| 9 | enemies=aliens 10 | lives=3 11 | enemies.cheat=true 12 | enemies.cheat.level=noGoodRotten 13 | secret.code.passphrase=UUDDLRLRBABAS 14 | secret.code.allowed=true 15 | secret.code.lives=30 16 | |||, 17 | "ui.properties": ||| 18 | color.good=purple 19 | color.bad=yellow 20 | allow.textmode=true 21 | how.nice.to.look=fairlyNice 22 | |||, 23 | }, 24 | } -------------------------------------------------------------------------------- /configmap/components/params.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | global: { 3 | }, 4 | components: { 5 | "example-configmap": { 6 | }, 7 | }, 8 | } 9 | -------------------------------------------------------------------------------- /configmap/environments/base.libsonnet: -------------------------------------------------------------------------------- 1 | local components = std.extVar("__ksonnet/components"); 2 | components + { 3 | // Insert user-specified overrides here. 4 | } 5 | -------------------------------------------------------------------------------- /configmap/environments/default/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /configmap/environments/default/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | local k = import "k.libsonnet"; 3 | 4 | base + { 5 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 6 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 7 | } 8 | -------------------------------------------------------------------------------- /configmap/environments/default/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /drop-in-yaml/.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /.ksonnet/registries 3 | /app.override.yaml 4 | /.ks_environment 5 | -------------------------------------------------------------------------------- /drop-in-yaml/README.md: -------------------------------------------------------------------------------- 1 | ## Drop-in YAML with parameterization 2 | 3 | Ksonnet supports native YAML components to be "dropped into" the components directory. This example 4 | demonstrates the ability to drop in a K8s YAML file into the components directory, and 5 | apply some parameterization. 6 | 7 | In this example we have a normal Kubernetes deployment yaml in the `components` directory at 8 | [`components/nginx-deployment.yaml`](components/nginx-deployment.yaml) 9 | 10 | ``` 11 | apiVersion: apps/v1 12 | kind: Deployment 13 | metadata: 14 | name: nginx-deployment 15 | labels: 16 | app: nginx 17 | spec: 18 | replicas: 3 19 | selector: 20 | matchLabels: 21 | app: nginx 22 | template: 23 | metadata: 24 | labels: 25 | app: nginx 26 | spec: 27 | containers: 28 | - name: nginx 29 | image: nginx:1.7.9 30 | ports: 31 | - containerPort: 80 32 | ``` 33 | 34 | Next, create a params entry for it in the [`components/params.libsonnet`](components/params.libsonnet): 35 | 36 | ``` 37 | { 38 | global: { 39 | }, 40 | components: { 41 | 'nginx-deployment': {}, 42 | }, 43 | } 44 | ``` 45 | 46 | Finally, in [`environments/default/params.libsonnet`](environments/default/params.libsonnet), we can 47 | override the value of the replicas by applying an overlay patch, as follows: 48 | 49 | ``` 50 | local envParams = params + { 51 | components +: { 52 | 'nginx-deployment' +: { 53 | spec +: { 54 | replicas: 10, 55 | }, 56 | }, 57 | }, 58 | }; 59 | 60 | ``` 61 | 62 | To run this example, run: 63 | ``` 64 | ks show default 65 | ``` 66 | -------------------------------------------------------------------------------- /drop-in-yaml/app.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 0.1.0 2 | environments: 3 | default: 4 | destination: 5 | namespace: default 6 | server: https://localhost:6443 7 | k8sVersion: v1.10.3 8 | path: default 9 | kind: ksonnet.io/app 10 | name: drop-in-yaml 11 | version: 0.0.1 12 | -------------------------------------------------------------------------------- /drop-in-yaml/components/nginx-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: nginx-deployment 5 | labels: 6 | app: nginx 7 | spec: 8 | replicas: 3 9 | selector: 10 | matchLabels: 11 | app: nginx 12 | template: 13 | metadata: 14 | labels: 15 | app: nginx 16 | spec: 17 | containers: 18 | - name: nginx 19 | image: nginx:1.7.9 20 | ports: 21 | - containerPort: 80 22 | -------------------------------------------------------------------------------- /drop-in-yaml/components/params.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | global: { 3 | }, 4 | components: { 5 | 'nginx-deployment': {}, 6 | }, 7 | } 8 | -------------------------------------------------------------------------------- /drop-in-yaml/environments/base.libsonnet: -------------------------------------------------------------------------------- 1 | local components = std.extVar("__ksonnet/components"); 2 | components + { 3 | // Insert user-specified overrides here. 4 | } 5 | -------------------------------------------------------------------------------- /drop-in-yaml/environments/default/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /drop-in-yaml/environments/default/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | // uncomment if you reference ksonnet-lib 3 | // local k = import "k.libsonnet"; 4 | 5 | base + { 6 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 7 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 8 | } 9 | -------------------------------------------------------------------------------- /drop-in-yaml/environments/default/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | 'nginx-deployment' +: { 6 | spec +: { 7 | replicas: 10, 8 | }, 9 | }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /env-extra-resource/.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /.ksonnet/registries 3 | /app.override.yaml 4 | /.ks_environment 5 | -------------------------------------------------------------------------------- /env-extra-resource/README.md: -------------------------------------------------------------------------------- 1 | ## Environment with an extra resource 2 | 3 | Example of an environment which has an added resource. -------------------------------------------------------------------------------- /env-extra-resource/app.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 0.1.0 2 | environments: 3 | default: 4 | destination: 5 | namespace: default 6 | server: http://1.2.3.4 7 | k8sVersion: v1.9.4 8 | path: default 9 | extra-resource: 10 | destination: 11 | namespace: extra-resource 12 | server: http://1.2.3.4 13 | k8sVersion: v1.9.4 14 | path: extra-resource 15 | kind: ksonnet.io/app 16 | name: env-extra-resource 17 | registries: 18 | incubator: 19 | gitVersion: 20 | commitSha: 40285d8a14f1ac5787e405e1023cf0c07f6aa28c 21 | refSpec: master 22 | protocol: github 23 | uri: github.com/ksonnet/parts/tree/master/incubator 24 | version: 0.0.1 25 | -------------------------------------------------------------------------------- /env-extra-resource/components/nginx-deployment.jsonnet: -------------------------------------------------------------------------------- 1 | { 2 | "apiVersion": "apps/v1beta2", 3 | "kind": "Deployment", 4 | "metadata": { 5 | "name": "nginx-deployment", 6 | "labels": { 7 | "app": "nginx" 8 | } 9 | }, 10 | "spec": { 11 | "selector": { 12 | "matchLabels": { 13 | "app": "nginx" 14 | } 15 | }, 16 | "template": { 17 | "metadata": { 18 | "labels": { 19 | "app": "nginx" 20 | } 21 | }, 22 | "spec": { 23 | "containers": [ 24 | { 25 | "name": "nginx", 26 | "image": "nginx:1.7.9", 27 | "ports": [ 28 | { 29 | "containerPort": 80 30 | } 31 | ] 32 | } 33 | ] 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /env-extra-resource/components/params.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | global: { 3 | // User-defined global parameters; accessible to all component and environments, Ex: 4 | // replicas: 4, 5 | }, 6 | components: { 7 | // Component-level parameters, defined initially from 'ks prototype use ...' 8 | // Each object below should correspond to a component in the components/ directory 9 | }, 10 | } 11 | -------------------------------------------------------------------------------- /env-extra-resource/environments/base.libsonnet: -------------------------------------------------------------------------------- 1 | local components = std.extVar("__ksonnet/components"); 2 | components + { 3 | // Insert user-specified overrides here. 4 | } 5 | -------------------------------------------------------------------------------- /env-extra-resource/environments/default/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /env-extra-resource/environments/default/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | local k = import "k.libsonnet"; 3 | 4 | base + { 5 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 6 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 7 | } -------------------------------------------------------------------------------- /env-extra-resource/environments/default/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /env-extra-resource/environments/extra-resource/extra-deployment.jsonnet: -------------------------------------------------------------------------------- 1 | { 2 | "apiVersion": "apps/v1beta2", 3 | "kind": "Deployment", 4 | "metadata": { 5 | "name": "nginx-deployment-2", 6 | "labels": { 7 | "app": "nginx" 8 | } 9 | }, 10 | "spec": { 11 | "selector": { 12 | "matchLabels": { 13 | "app": "nginx" 14 | } 15 | }, 16 | "template": { 17 | "metadata": { 18 | "labels": { 19 | "app": "nginx" 20 | } 21 | }, 22 | "spec": { 23 | "containers": [ 24 | { 25 | "name": "nginx", 26 | "image": "nginx:1.7.9", 27 | "ports": [ 28 | { 29 | "containerPort": 80 30 | } 31 | ] 32 | } 33 | ] 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /env-extra-resource/environments/extra-resource/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /env-extra-resource/environments/extra-resource/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | local k = import "k.libsonnet"; 3 | 4 | local extra = import "extra-deployment.jsonnet"; 5 | 6 | base + { 7 | "nginx-deployment-2" : extra, 8 | } 9 | -------------------------------------------------------------------------------- /env-extra-resource/environments/extra-resource/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /env-metadata/.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /.ksonnet/registries 3 | /app.override.yaml 4 | /.ks_environment 5 | -------------------------------------------------------------------------------- /env-metadata/README.md: -------------------------------------------------------------------------------- 1 | ## Environment Metadata 2 | 3 | Ksonnet exposes limited metadata about the environment which are made available in a jsonnet 4 | external variable `__ksonnet/environments`. Currently ksonnet exposes two variables: 5 | * `env.namespace` - the application's namespace 6 | * `env.server` - the Kubernetes API Server URL 7 | 8 | ``` 9 | local params = std.extVar("__ksonnet/params").components['nginx-deployment']; 10 | local env = std.extVar("__ksonnet/environments"); 11 | { 12 | "apiVersion": "apps/v1beta2", 13 | "kind": "Deployment", 14 | "metadata": { 15 | "name": "nginx-deployment", 16 | "labels": { 17 | "app": "nginx" 18 | }, 19 | "annotations": { 20 | "my-namespace": env.namespace, 21 | "my-cluster": env.server, 22 | }, 23 | ... 24 | ``` 25 | 26 | To run this example, compare the `default` environment to the `prod` environment, and notice that 27 | the annotations containing the namespace and server are different: 28 | ``` 29 | ks show default 30 | ks show prod 31 | ks diff local:default local:prod 32 | ``` -------------------------------------------------------------------------------- /env-metadata/app.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 0.1.0 2 | environments: 3 | default: 4 | destination: 5 | namespace: abc 6 | server: https://my-server-abc 7 | k8sVersion: v1.10.3 8 | path: default 9 | prod: 10 | destination: 11 | namespace: xyz 12 | server: https://my-server-xyz 13 | k8sVersion: v1.10.3 14 | path: prod 15 | kind: ksonnet.io/app 16 | name: env-metadata 17 | version: 0.0.1 18 | -------------------------------------------------------------------------------- /env-metadata/components/nginx-deployment.jsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params").components['nginx-deployment']; 2 | local env = std.extVar("__ksonnet/environments"); 3 | { 4 | "apiVersion": "apps/v1beta2", 5 | "kind": "Deployment", 6 | "metadata": { 7 | "name": "nginx-deployment", 8 | "labels": { 9 | "app": "nginx" 10 | }, 11 | "annotations": { 12 | "my-namespace": env.namespace, 13 | "my-cluster": env.server, 14 | }, 15 | }, 16 | "spec": { 17 | "selector": { 18 | "matchLabels": { 19 | "app": "nginx" 20 | } 21 | }, 22 | "template": { 23 | "metadata": { 24 | "labels": { 25 | "app": "nginx" 26 | } 27 | }, 28 | "spec": { 29 | "containers": [ 30 | { 31 | "name": "nginx", 32 | "image": "nginx:1.7.9", 33 | "ports": [ 34 | { 35 | "containerPort": 80 36 | } 37 | ] 38 | } 39 | ] 40 | } 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /env-metadata/components/params.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | global: { 3 | // User-defined global parameters; accessible to all component and environments, Ex: 4 | // replicas: 4, 5 | }, 6 | components: { 7 | // Component-level parameters, defined initially from 'ks prototype use ...' 8 | // Each object below should correspond to a component in the components/ directory 9 | "nginx-deployment": {}, 10 | }, 11 | } 12 | -------------------------------------------------------------------------------- /env-metadata/environments/base.libsonnet: -------------------------------------------------------------------------------- 1 | local components = std.extVar("__ksonnet/components"); 2 | components + { 3 | // Insert user-specified overrides here. 4 | } 5 | -------------------------------------------------------------------------------- /env-metadata/environments/default/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /env-metadata/environments/default/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | // uncomment if you reference ksonnet-lib 3 | // local k = import "k.libsonnet"; 4 | 5 | base + { 6 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 7 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 8 | } 9 | -------------------------------------------------------------------------------- /env-metadata/environments/default/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /env-metadata/environments/prod/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /env-metadata/environments/prod/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | // uncomment if you reference ksonnet-lib 3 | // local k = import "k.libsonnet"; 4 | 5 | base + { 6 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 7 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 8 | } 9 | -------------------------------------------------------------------------------- /env-metadata/environments/prod/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /global-params/.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /.ksonnet/registries 3 | /app.override.yaml 4 | /.ks_environment 5 | -------------------------------------------------------------------------------- /global-params/README.md: -------------------------------------------------------------------------------- 1 | ## Global Parameters 2 | 3 | Globals are a way to inject and make parameters available to be referenced in all components. To 4 | define a global, add the variable to the `global` section in 5 | [`components/params.libsonnet`](components/params.libsonnet). In the example below, `replicas` is a 6 | global with the default value of 1: 7 | 8 | ``` 9 | { 10 | global: { 11 | replicas: 1, 12 | }, 13 | components: { 14 | 'nginx-deployment': { 15 | image: "nginx:1.7.9", 16 | }, 17 | }, 18 | } 19 | ``` 20 | 21 | Referencing a global is exactly the same as referencing a component parameter. In the 22 | [`nginx-deployment`](components/nginx-deployment.jsonnet) component, the `replicas` global parameter, 23 | is referenced in the same manner as the `image` component parameter: 24 | 25 | ``` 26 | local params = std.extVar("__ksonnet/params").components['nginx-deployment']; 27 | { 28 | "apiVersion": "apps/v1beta2", 29 | "kind": "Deployment", 30 | "metadata": { 31 | "name": "nginx-deployment", 32 | "labels": { 33 | "app": "nginx" 34 | } 35 | }, 36 | "spec": { 37 | "replicas": params.replicas, 38 | "selector": { 39 | "matchLabels": { 40 | "app": "nginx" 41 | } 42 | }, 43 | "template": { 44 | "metadata": { 45 | "labels": { 46 | "app": "nginx" 47 | } 48 | }, 49 | "spec": { 50 | "containers": [ 51 | { 52 | "name": "nginx", 53 | "image": params.image, 54 | "ports": [ 55 | { 56 | "containerPort": 80 57 | } 58 | ] 59 | } 60 | ] 61 | } 62 | } 63 | } 64 | } 65 | ``` 66 | 67 | To override a global in a specific an environment, define the new value in the `globals.libsonnet` 68 | file for that environment. In this example, replicas is being overridden in the 69 | [`prod`](environments/prod/globals.libsonnet) environment: 70 | 71 | ``` 72 | { 73 | replicas: 10, 74 | } 75 | ``` 76 | 77 | To run this example, compare the `default` environment to the `prod` environment: 78 | ``` 79 | ks show default 80 | ks show prod 81 | ks diff local:default local:prod 82 | ``` -------------------------------------------------------------------------------- /global-params/app.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 0.1.0 2 | environments: 3 | default: 4 | destination: 5 | namespace: default 6 | server: https://localhost:6443 7 | k8sVersion: v1.10.3 8 | path: default 9 | prod: 10 | destination: 11 | namespace: default 12 | server: https://localhost:6443 13 | k8sVersion: v1.10.3 14 | path: prod 15 | kind: ksonnet.io/app 16 | name: global-params 17 | version: 0.0.1 18 | -------------------------------------------------------------------------------- /global-params/components/nginx-deployment.jsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params").components['nginx-deployment']; 2 | { 3 | "apiVersion": "apps/v1beta2", 4 | "kind": "Deployment", 5 | "metadata": { 6 | "name": "nginx-deployment", 7 | "labels": { 8 | "app": "nginx" 9 | } 10 | }, 11 | "spec": { 12 | "replicas": params.replicas, 13 | "selector": { 14 | "matchLabels": { 15 | "app": "nginx" 16 | } 17 | }, 18 | "template": { 19 | "metadata": { 20 | "labels": { 21 | "app": "nginx" 22 | } 23 | }, 24 | "spec": { 25 | "containers": [ 26 | { 27 | "name": "nginx", 28 | "image": params.image, 29 | "ports": [ 30 | { 31 | "containerPort": 80 32 | } 33 | ] 34 | } 35 | ] 36 | } 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /global-params/components/params.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | global: { 3 | replicas: 1, 4 | }, 5 | components: { 6 | 'nginx-deployment': { 7 | image: "nginx:1.7.9", 8 | }, 9 | }, 10 | } 11 | -------------------------------------------------------------------------------- /global-params/environments/base.libsonnet: -------------------------------------------------------------------------------- 1 | local components = std.extVar("__ksonnet/components"); 2 | components + { 3 | // Insert user-specified overrides here. 4 | } 5 | -------------------------------------------------------------------------------- /global-params/environments/default/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /global-params/environments/default/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | // uncomment if you reference ksonnet-lib 3 | // local k = import "k.libsonnet"; 4 | 5 | base + { 6 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 7 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 8 | } 9 | -------------------------------------------------------------------------------- /global-params/environments/default/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /global-params/environments/prod/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | replicas: 10, 3 | } 4 | -------------------------------------------------------------------------------- /global-params/environments/prod/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | // uncomment if you reference ksonnet-lib 3 | // local k = import "k.libsonnet"; 4 | 5 | base + { 6 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 7 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 8 | } 9 | -------------------------------------------------------------------------------- /global-params/environments/prod/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /parse-yaml/.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /.ksonnet/registries 3 | /app.override.yaml 4 | /.ks_environment 5 | -------------------------------------------------------------------------------- /parse-yaml/README.md: -------------------------------------------------------------------------------- 1 | ## Parse YAML with parameterization 2 | 3 | This example demonstrates the ability to parse a normal K8s YAML file into a jsonnet object for 4 | further customization using jsonnet. A `parseYaml` function is provided in ksonnet for conveniently 5 | parsing a file to a list of jsonnet objects. 6 | 7 | In this example we have a normal Kubernetes deployment yaml `nginx-deployment.yaml` file placed in 8 | the [`components/imports`](components/imports) directory. This location is arbitrary, but should be 9 | different than the components directory since otherwise ksonnet will treat it as a drop-in yaml. 10 | Notice that the image has a placeholder value. This will be replaced in the component jsonnet. 11 | 12 | ``` 13 | apiVersion: apps/v1 14 | kind: Deployment 15 | metadata: 16 | name: nginx-deployment 17 | labels: 18 | app: nginx 19 | spec: 20 | replicas: 3 21 | selector: 22 | matchLabels: 23 | app: nginx 24 | template: 25 | metadata: 26 | labels: 27 | app: nginx 28 | spec: 29 | containers: 30 | - name: nginx 31 | image: PLACEHOLDER_VALUE 32 | ports: 33 | - containerPort: 80 34 | ``` 35 | 36 | In [`params.libsonnet`](components/params.libsonnet), expose `image` as a parameter to the 37 | `nginx-deployment` component: 38 | ``` 39 | { 40 | global: { 41 | }, 42 | components: { 43 | 'nginx-deployment': { 44 | image: 'nginx:1.7.9', 45 | }, 46 | }, 47 | } 48 | ``` 49 | 50 | Finally, create the `nginx-deployment` jsonnet component for the deployment at 51 | [`components/nginx-deployment.jsonnet`](components/nginx-deployment.jsonnet). 52 | The code below will parse the YAML into a jsonnet object. Then replace the container list with 53 | a list containing our parameterized image: 54 | 55 | ``` 56 | local params = std.extVar("__ksonnet/params").components['nginx-deployment']; 57 | local parseYaml = std.native("parseYaml"); 58 | local nginxDeployment = parseYaml(importstr 'imports/nginx-deployment.yaml')[0]; 59 | 60 | nginxDeployment + { 61 | spec +: { 62 | template +: { 63 | spec +: { 64 | containers : [ 65 | nginxDeployment.spec.template.spec.containers[0] + { image: params.image }, 66 | ], 67 | }, 68 | }, 69 | }, 70 | } 71 | ``` 72 | 73 | To run this example, run: 74 | ``` 75 | ks show default 76 | ``` 77 | -------------------------------------------------------------------------------- /parse-yaml/app.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 0.1.0 2 | environments: 3 | default: 4 | destination: 5 | namespace: default 6 | server: https://localhost:6443 7 | k8sVersion: v1.10.3 8 | path: default 9 | kind: ksonnet.io/app 10 | name: parse-yaml 11 | version: 0.0.1 12 | -------------------------------------------------------------------------------- /parse-yaml/components/imports/nginx-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: nginx-deployment 5 | labels: 6 | app: nginx 7 | spec: 8 | replicas: 3 9 | selector: 10 | matchLabels: 11 | app: nginx 12 | template: 13 | metadata: 14 | labels: 15 | app: nginx 16 | spec: 17 | containers: 18 | - name: nginx 19 | image: PLACEHOLDER_VALUE 20 | ports: 21 | - containerPort: 80 22 | -------------------------------------------------------------------------------- /parse-yaml/components/nginx-deployment.jsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params").components['nginx-deployment']; 2 | local parseYaml = std.native("parseYaml"); 3 | local nginxDeployment = parseYaml(importstr 'imports/nginx-deployment.yaml')[0]; 4 | 5 | nginxDeployment + { 6 | spec +: { 7 | template +: { 8 | spec +: { 9 | containers : [ 10 | nginxDeployment.spec.template.spec.containers[0] + { image: params.image }, 11 | ], 12 | }, 13 | }, 14 | }, 15 | } 16 | -------------------------------------------------------------------------------- /parse-yaml/components/params.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | global: { 3 | }, 4 | components: { 5 | 'nginx-deployment': { 6 | image: 'nginx:1.7.9', 7 | }, 8 | }, 9 | } 10 | -------------------------------------------------------------------------------- /parse-yaml/environments/base.libsonnet: -------------------------------------------------------------------------------- 1 | local components = std.extVar("__ksonnet/components"); 2 | components + { 3 | // Insert user-specified overrides here. 4 | } 5 | -------------------------------------------------------------------------------- /parse-yaml/environments/default/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /parse-yaml/environments/default/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | // uncomment if you reference ksonnet-lib 3 | // local k = import "k.libsonnet"; 4 | 5 | base + { 6 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 7 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 8 | } 9 | -------------------------------------------------------------------------------- /parse-yaml/environments/default/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | 'nginx-deployment' +: { 11 | spec: { 12 | replicas: 10, 13 | }, 14 | }, 15 | }, 16 | }; 17 | 18 | { 19 | components: { 20 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 21 | }, 22 | } 23 | -------------------------------------------------------------------------------- /sidecar-injection/.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /.ksonnet/registries 3 | /app.override.yaml 4 | /.ks_environment 5 | -------------------------------------------------------------------------------- /sidecar-injection/README.md: -------------------------------------------------------------------------------- 1 | ## Environment specific sidecar injection 2 | This example demonstrates the ability to inject a sidecar in a specific environment. In `environments/default/main.jsonnet`, we define a sidecar, and and append it to the container list 3 | of the nginx-deployment deployment. Since the sidecar is defined only in 4 | `environments/default/main.jsonnet`, this sidecar will only be present when generating the manifests 5 | for the `default` environment. 6 | 7 | ``` 8 | local sidecar = { 9 | "name": "sidecar", 10 | "image": "mysidecar:latest", 11 | }; 12 | 13 | base + { 14 | "nginx-deployment" +: { 15 | "spec" +: { 16 | "template" +: { 17 | "spec" +: { 18 | "containers" +: [sidecar], 19 | } 20 | } 21 | } 22 | }, 23 | } 24 | ``` 25 | 26 | To run this example, show the `default` environment which contains the sidecar, and `no-sidecar` 27 | environment for the environment without the sidecar: 28 | ``` 29 | ks show default 30 | ks show no-sidecar 31 | ``` 32 | -------------------------------------------------------------------------------- /sidecar-injection/app.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 0.1.0 2 | environments: 3 | default: 4 | destination: 5 | namespace: default 6 | server: http://1.2.3.4 7 | k8sVersion: v1.9.4 8 | path: default 9 | no-sidecar: 10 | destination: 11 | namespace: no-sidecar 12 | server: http://1.2.3.4 13 | k8sVersion: v1.9.4 14 | path: no-sidecar 15 | kind: ksonnet.io/app 16 | name: sidecar-injection 17 | registries: 18 | incubator: 19 | gitVersion: 20 | commitSha: 40285d8a14f1ac5787e405e1023cf0c07f6aa28c 21 | refSpec: master 22 | protocol: github 23 | uri: github.com/ksonnet/parts/tree/master/incubator 24 | version: 0.0.1 25 | -------------------------------------------------------------------------------- /sidecar-injection/components/nginx-deployment.jsonnet: -------------------------------------------------------------------------------- 1 | { 2 | "apiVersion": "apps/v1beta2", 3 | "kind": "Deployment", 4 | "metadata": { 5 | "name": "nginx-deployment", 6 | "labels": { 7 | "app": "nginx" 8 | } 9 | }, 10 | "spec": { 11 | "selector": { 12 | "matchLabels": { 13 | "app": "nginx" 14 | } 15 | }, 16 | "template": { 17 | "metadata": { 18 | "labels": { 19 | "app": "nginx" 20 | } 21 | }, 22 | "spec": { 23 | "containers": [ 24 | { 25 | "name": "nginx", 26 | "image": "nginx:1.7.9", 27 | "ports": [ 28 | { 29 | "containerPort": 80 30 | } 31 | ] 32 | } 33 | ] 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /sidecar-injection/components/params.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | global: { 3 | // User-defined global parameters; accessible to all component and environments, Ex: 4 | // replicas: 4, 5 | }, 6 | components: { 7 | // Component-level parameters, defined initially from 'ks prototype use ...' 8 | // Each object below should correspond to a component in the components/ directory 9 | }, 10 | } 11 | -------------------------------------------------------------------------------- /sidecar-injection/environments/base.libsonnet: -------------------------------------------------------------------------------- 1 | local components = std.extVar("__ksonnet/components"); 2 | components + { 3 | // Insert user-specified overrides here. 4 | } 5 | -------------------------------------------------------------------------------- /sidecar-injection/environments/default/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /sidecar-injection/environments/default/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | local k = import "k.libsonnet"; 3 | 4 | local sidecar = { 5 | "name": "sidecar", 6 | "image": "mysidecar:latest", 7 | }; 8 | 9 | base + { 10 | "nginx-deployment" +: { 11 | "spec" +: { 12 | "template" +: { 13 | "spec" +: { 14 | "containers" +: [sidecar], 15 | } 16 | } 17 | } 18 | }, 19 | } 20 | -------------------------------------------------------------------------------- /sidecar-injection/environments/default/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /sidecar-injection/environments/no-sidecar/globals.libsonnet: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /sidecar-injection/environments/no-sidecar/main.jsonnet: -------------------------------------------------------------------------------- 1 | local base = import "base.libsonnet"; 2 | local k = import "k.libsonnet"; 3 | 4 | base + { 5 | // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n") 6 | // "nginx-deployment"+: k.deployment.mixin.metadata.labels({foo: "bar"}) 7 | } 8 | -------------------------------------------------------------------------------- /sidecar-injection/environments/no-sidecar/params.libsonnet: -------------------------------------------------------------------------------- 1 | local params = std.extVar("__ksonnet/params"); 2 | local globals = import "globals.libsonnet"; 3 | local envParams = params + { 4 | components +: { 5 | // Insert component parameter overrides here. Ex: 6 | // guestbook +: { 7 | // name: "guestbook-dev", 8 | // replicas: params.global.replicas, 9 | // }, 10 | }, 11 | }; 12 | 13 | { 14 | components: { 15 | [x]: envParams.components[x] + globals, for x in std.objectFields(envParams.components) 16 | }, 17 | } 18 | --------------------------------------------------------------------------------