From b8f773a5efde67a769301aecf8434b8c19bbea93 Mon Sep 17 00:00:00 2001 From: Foad Lind Date: Tue, 7 Jun 2022 15:59:25 +0200 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + docs/k8s.rst | 27 +++++++++++++++++++++++++++ docs/reference/patches.rst | 18 ++++++++++++++++++ tutor/templates/k8s/override.yml | 1 + tutor/templates/kustomization.yml | 4 ++++ 5 files changed, 51 insertions(+) create mode 100644 tutor/templates/k8s/override.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index bb47797..089719d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/docs/k8s.rst b/docs/k8s.rst index e8508e4..649c614 100644 --- a/docs/k8s.rst +++ b/docs/k8s.rst @@ -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 `__ 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 + diff --git a/docs/reference/patches.rst b/docs/reference/patches.rst index 9b76c5b..49bcf4f 100644 --- a/docs/reference/patches.rst +++ b/docs/reference/patches.rst @@ -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 ` 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 `__. + .. patch:: kustomization-resources ``kustomization-resources`` diff --git a/tutor/templates/k8s/override.yml b/tutor/templates/k8s/override.yml new file mode 100644 index 0000000..5c10515 --- /dev/null +++ b/tutor/templates/k8s/override.yml @@ -0,0 +1 @@ +{{ patch("k8s-override") }} diff --git a/tutor/templates/kustomization.yml b/tutor/templates/kustomization.yml index a5a2815..1bfb637 100644 --- a/tutor/templates/kustomization.yml +++ b/tutor/templates/kustomization.yml @@ -59,4 +59,8 @@ configMapGenerator: app.kubernetes.io/name: redis {{ patch("kustomization-configmapgenerator") }} +patchesStrategicMerge: +- k8s/override.yml +{{ patch("kustomization-patches-strategic-merge") }} + {{ patch("kustomization") }}