feat: Make it possible to customize k8s resources

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: https://github.com/overhangio/tutor/pull/675
This commit is contained in:
Foad Lind 2022-06-07 15:59:25 +02:00 committed by Régis Behmo
parent 4a808fa87b
commit b8f773a5ef
5 changed files with 51 additions and 0 deletions

View File

@ -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)
## v14.0.2 (2022-06-27)

View File

@ -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

View File

@ -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``
@ -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``

View File

@ -0,0 +1 @@
{{ patch("k8s-override") }}

View File

@ -59,4 +59,8 @@ configMapGenerator:
app.kubernetes.io/name: redis
{{ patch("kustomization-configmapgenerator") }}
patchesStrategicMerge:
- k8s/override.yml
{{ patch("kustomization-patches-strategic-merge") }}
{{ patch("kustomization") }}