Modular Kubernetes Programming — Part III

Sidhartha Mani
Koki
Published in
3 min readFeb 1, 2018

This is a follow up post to the one published yesterday titled “Modular Kubernetes Programming-Part II”. If you aren’t familiar with modular Kubernetes resources, please read the previous article in this series before continuing.

The previous blog post talks about using params keyword to parameterize modules when importing them. This blog post explores the various mechanisms in Koki Short for using an imported array. Namely,

  • Substitution
  • Array Expansion
  • Array Indexing
Indexing into an array to select particular values is a powerful language feature (https://commons.wikimedia.org/wiki/File:CPT-programming-array.svg)

Substitution

In the previous two blog posts, I’ve only used one of the mechanisms available in Koki Short for module usage — substitution.

This operation is performed by simply replacing a variable enclosed in curly braces by the value represented by the variable.

Here’s an example of simple substitution:

params:
- role: "role of the rethinkdb service (admin|user)"
default: admin
labels:
db: rethinkdb
role: ${role}

In the example above, the value of label.role is substituted by the short tool to be admin.

Array Expansion

The second operator provided by the Koki Short tool is Array Expansion operation. Using this operator, it is possible to expand an imported array and append the values into another list. Let’s understand this with an example.

expose:
- admin-port: 8080
- driver-port: 28015
- cluster-port: 29015

The above yaml document represents an array of exposed ports.

Let’s use this in a pod definition.

imports:
- ports: ./rethink-expose-module.yaml

pod:
containers:
- env:
- from: metadata.namespace
key: POD_NAMESPACE
expose:
- user-port: 9090
- ${ports...} # Expands and Appends list of ports into expose
image: gcr.io/google_containers/rethinkdb:1.16.0_1
name: rethinkdb
volume:
- mount: /data/rethinkdb_data
store: rethinkdb-storage
labels:
db: rethinkdb
role: admin
name: rethinkdb-admin
version: v1
volumes:
rethinkdb-storage: empty_dir

The above syntax of referring the variable in curly braces followed by three dots — ${ports...} represents the list expansion operator. It expands the array that is represented by the variable ports, and adds it to the expose array.

Let’s look at the final result of this array expansion

$ short -k -f rethink-list-expansion.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
db: rethinkdb
name: rethinkdb-admin
role: admin
spec:
containers:
- env:
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
image: gcr.io/google_containers/rethinkdb:1.16.0_1
name: rethinkdb
ports:
- containerPort: 9090
name: user-port
protocol: TCP
- containerPort: 8080
name: admin-port
protocol: TCP
- containerPort: 28015
name: driver-port
protocol: TCP
- containerPort: 29015
name: cluster-port
protocol: TCP

resources: {}
volumeMounts:
- mountPath: /data/rethinkdb_data
mountPropagation: ""
name: rethinkdb-storage
volumes:
- emptyDir: {}
name: rethinkdb-storage

As you can see, the entire list of ports have been expanded and appended to the expose array in the Kubernetes resource.

Array Indexing

The third operator is the other array operator — Array Indexing operator. This operator can be used to select a particular entry from an imported array. Let’s understand this with an example.

expose:
- admin-port: 8080
- driver-port: 28015
- cluster-port: 29015

Let’s start with the exposed port yaml document again.

Instead of using the entire array in another module as shown in the section above, the indexing operator can be used to select particular entries of the array. Let’s select the admin-port (which is at index 0) in a pod definition

imports:
- ports: ./rethink-expose-module.yaml

pod:
containers:
- env:
- from: metadata.namespace
key: POD_NAMESPACE
expose:
- user-port: 9090
- ${ports.0} # Appends ports[0] into expose
image: gcr.io/google_containers/rethinkdb:1.16.0_1
name: rethinkdb
volume:
- mount: /data/rethinkdb_data
store: rethinkdb-storage
labels:
db: rethinkdb
role: admin
name: rethinkdb-admin
version: v1
volumes:
rethinkdb-storage: empty_dir

Here’s the final result of array indexing

apiVersion: v1
kind: Pod
metadata:
labels:
db: rethinkdb
name: rethinkdb-admin
role: admin
spec:
containers:
- env:
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
image: gcr.io/google_containers/rethinkdb:1.16.0_1
name: rethinkdb
ports:
- containerPort: 9090
name: user-port
protocol: TCP
- containerPort: 8080
name: admin-port
protocol: TCP

resources: {}
volumeMounts:
- mountPath: /data/rethinkdb_data
mountPropagation: ""
name: rethinkdb-storage
volumes:
- emptyDir: {}
name: rethinkdb-storage

As you can see, admin-port has been indexed and substituted in the Kubernetes array.

Conclusion

This blog post sheds light onto the array specific operators in the Koki Short module system. This powerful feature provide users with the flexibility needed for selecting particular entries from an imported array or expanding an entire imported list into a resource.

You can learn more about Short or download examples to play with. You can also try out the Chrome extension to translate live on your browser!

Stay tuned for more deep dives into Modular Kubernetes programming using Koki Short!

--

--