Skip to content

Commit

Permalink
feat: Make it possible to customize k8s resources
Browse files Browse the repository at this point in the history
Currently there is no way for plugins to customize Kubernetes resources
defined in Tutor deployment manifests.
This change makes that possible by taking advantage of the strategic
merge patching mechanism in `kustomization.yml`.
Any resource definition in a `k8s-override` patch in a plugin will
override the resource defined by Tutor, provided that their names match.

Reference: overhangio#675
  • Loading branch information
foadlind committed Jun 21, 2022
1 parent 57cb956 commit e95f666
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Every user-facing change should have an entry in this changelog. Please respect
-->

## Unreleased
- [Improvement] Make it possible to override k8s resources in plugins using `k8s-override` patch. (by @foadlind)

## v13.3.1 (2022-06-06)

Expand Down
24 changes: 24 additions & 0 deletions docs/k8s.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,27 @@ Updating docker images
Kubernetes does not provide a single command for updating docker images out of the box. A `commonly used trick <https://github.com/kubernetes/kubernetes/issues/33664>`__ is to modify an innocuous label on all resources::

kubectl patch -k "$(tutor config printroot)/env" --patch "{\"spec\": {\"template\": {\"metadata\": {\"labels\": {\"date\": \"`date +'%Y%m%d-%H%M%S'`\"}}}}}"

Customizing Kubernetes resources
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Plugins can customize any Kubernetes resource in Tutor by overriding the definition of the resource with a ``k8s-override`` patch. For example, to change the volume size for MongoDB from ``5Gi`` to ``10Gi``, add the following to the plugin:

::

# myplugin/tutormyplugin/patches/k8s-override
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongodb
labels:
app.kubernetes.io/component: volume
app.kubernetes.io/name: mongodb
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi

7 changes: 7 additions & 0 deletions docs/reference/patches.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ File: ``kustomization.yml``

File: ``kustomization.yml``

.. patch:: kustomization-patches-strategic-merge

``kustomization-patches-strategic-merge``
=========================================

File: ``kustomization.yml``

.. patch:: kustomization-resources

``kustomization-resources``
Expand Down
1 change: 1 addition & 0 deletions tutor/templates/k8s/override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ patch("k8s-override") }}
4 changes: 4 additions & 0 deletions tutor/templates/kustomization.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ configMapGenerator:
app.kubernetes.io/name: redis
{{ patch("kustomization-configmapgenerator") }}

patchesStrategicMerge:
- k8s/override.yml
{{ patch("kustomization-patches-strategic-merge") }}

{{ patch("kustomization") }}

0 comments on commit e95f666

Please sign in to comment.