Modular Kubernetes Programming — Part IV

Sidhartha Mani
Koki
Published in
3 min readFeb 1, 2018

This is the final post in the Modular Kubernetes Programming series. If you aren’t familiar with modular Kubernetes resources, please read the previous article in this series before continuing.

The previous post talks about two array operators. Namely,

  • Array Indexing
  • Array Expansion

In this post, we’ll cover the rest of the operators provided by the Short module system

  • Dictionary Selection
  • Operator Chaining
Just as a piece of clothing can be tailored to fit your needs, Koki Short’s module system provides powerful primitives to fit any of your needs exactly

Dictionary Selection

The dictionary selection operator can be used to select a particular value in a dictionary by its key. Let’s understand this with an example.

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

The above labels module was also used in other blog posts in this series. If you want to re-use the role parameter in a pod definition, then the dictionary selection operator is the answer.

imports:
- labels: ./rethink-labels-module.yaml

pod:
containers:
- env:
- from: metadata.namespace
key: POD_NAMESPACE
expose:
- admin-port: 8080
- driver-port: 28015
- cluster-port: 29015
image: gcr.io/google_containers/rethinkdb:1.16.0_1
name: rethinkdb
volume:
- mount: /data/rethinkdb_data
store: rethinkdb-storage
labels:
db: rethinkdb
role: ${labels.role} # selects the value of the 'role' key
name: rethinkdb-admin
version: v1
volumes:
rethinkdb-storage: empty_dir

Let’s look at the final result of dictionary selection

$ short -k -f rethink-dictionary-selection.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
db: rethinkdb
role: admin
name: rethinkdb-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: 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 role value has been correctly selected and substituted into the label.

Operator Chaining

Koki Short provides a powerful primitive to promote reusability of module — “Operator Chaining”.

Operator chaining is where multiple operators can be used one after the other to achieve granular selection.

Consider this module:

pod:
containers:
- image: example/container1
name: container1
volume:
- mount: /data/vol1
store: vol1-storage
- mount: /data/vol2
store: vol2-storage
- image: example/container2
name: container2
volume:
- mount: /data/vol3
store: vol3-storage
- mount: /data/vol4
store: vol4-storage

Operator chaining can be used to index into any of the keys when reusing this module. Let’s see how it can be used to select the first volume store in the second container.

imports:
- example: ./rethink-chaining-module.yaml

pod:
containers:
- env:
- from: metadata.namespace
key: POD_NAMESPACE
expose:
- admin-port: 8080
- driver-port: 28015
- cluster-port: 29015
image: gcr.io/google_containers/rethinkdb:1.16.0_1
name: rethinkdb
volume:
- mount: /data/rethinkdb_data
store: ${example.containers.1.volume.0.store}
labels:
db: rethinkdb
role: admin
name: rethinkdb-admin
version: v1
volumes:
rethinkdb-storage: empty_dir

Let’s take a closer look at the syntax.

${example.containers.1.volume.0.store}

The first operator is a dictionary selection operator, selecting containers from example. The second operator is a array indexer. Since arrays are 0 indexed, we select the second container by the index 1. The third operator is another dictionary selector, which selects the volume sub-tree from the container dictionary. The next operator is another array indexer, which selects the first volume from the list of volumes. Then, we select the store using the dictionary selector operator. The final result of this should be vol3-store.

Let’s see the final result of operator chaining

$ short -k -f rethink-operator-chaining.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
db: rethinkdb
role: admin
name: rethinkdb-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: 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: vol3-storage
volumes:
- emptyDir: {}
name: rethinkdb-storage

As you can see, the store has been set correctly to vol3-storage.

Conclusion

This is the final post in the “Modular Kubernetes Programming” series. The powerful features discussed in the series combined, provide a powerful mechanism for users writing Kubernetes manifests.

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!

--

--