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 27, 2022
1 parent 57cb956 commit c997d0b
Show file tree
Hide file tree
Showing 5 changed files with 51 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
27 changes: 27 additions & 0 deletions docs/k8s.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,30 @@ 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_sources:

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

Plugins can customize any Kubernetes resource in Tutor by overriding the definition of the resource with a :patch:`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

18 changes: 18 additions & 0 deletions docs/reference/patches.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ File: ``k8s/deployments.yml``

File: ``k8s/jobs.yml``

.. patch:: k8s-override

``k8s-override``
================

File: ``k8s/override.yml``

Any Kubernetes resource definition in this patch will override the resource defined by Tutor, provided that their names match. See :ref:`Customizing Kubernetes resources <customizing_kubernetes_sources>` for an example.

.. patch:: k8s-services

``k8s-services``
Expand Down Expand Up @@ -125,6 +134,15 @@ File: ``kustomization.yml``

File: ``kustomization.yml``

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

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

File: ``kustomization.yml``

This can be used to add more Kustomization patches that make use of the `strategic merge mechanism <https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/#customizing>`__.

.. 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 c997d0b

Please sign in to comment.