Skip to main content

Patching with Kustomize

Kustomize allows for last-mile patches (such as modifications) to the application that are otherwise not configurable using the Config page. For more information, see the Kustomize website.

Directory Structure

Click View files and lets take a look at the directory structure.

Kustomize Dir Structure

Upstream

The upstream directory mirrors exactly the content pushed to a release. This includes the template functions, preflight checks, support-bundle, config options, license, and so on. In addition, it has a userdata directory which includes the license file, config file, and so on.

Note: With the exception of upstream/userdata, no changes should be made in the upstream directory as they are overwritten on each new release.

Base

After the Replicated app manager processes and renders the upstream, it puts those files in the base directory. Any non-deployable manifests such as template functions, preflight checks, config options, and so on are removed, and only the deployable application (such as, deployable with kubectl apply) will be placed here.

Note: No changes should be made in the base directory as they are overwritten on each new release.

Overlays

The overlays directory references the base directory, and this is where your local Kustomize patches should be placed. Unlike upstream and base, any changes made here will persist between releases.


Patching the Overlays

Since upstream and base are ephemeral, let's make a Kustomize patch in overlays/downstreams/this-cluster/ so it persists between releases.

Go to View Files and click Need to edit these files? Click here to learn how.

edit-patches-kots-app

To download the app manager locally:

export APP_NAMESPACE=app-namespace
export APP_SLUG=app-slug
kubectl kots download --namespace ${APP_NAMESPACE} --slug ${APP_SLUG} --dest ~/my-kots-app
โ€ข Connecting to cluster โœ“

Let's patch something simple like the replicas count: Create a file in ~/my-kots-app/overlays/downstreams/this-cluster/patch-deployment.yaml with the following:

cat <<EOF >>~/my-kots-app/overlays/downstreams/this-cluster/patch-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-nginx
spec:
replicas: 2
EOF

Don't forget to add this file under patches in ~/my-kots-app/overlays/downstreams/this-cluster/kustomization.yaml:

@@ -2,3 +2,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
bases:
- ../../midstream
kind: Kustomization
+patches:
+- ./patch-deployment.yaml

Upload the app manager to the cluster:

export APP_NAMESPACE=app-namespace
export APP_SLUG=app-slug
kubectl kots upload --namespace ${APP_NAMESPACE} --slug ${APP_SLUG} ~/my-kots-app
โ€ข Uploading local application to Admin Console โœ“

Under View History, you should see a new version ready to deploy along with the diff of the changes we pushed in the last few steps.

kustomize-view-history-diff

Before deploying, ensure there is only 1 NGINX pod running:

$ kubectl get po | grep example-nginx
example-nginx-f5c49fdf6-bf584 1/1 Running 0 1h

Click Deploy to apply the changes.

kustomize-view-history-deploy

You should now see 2 NGINX pods running:

$ kubectl get po | grep example-nginx
example-nginx-f5c49fdf6-bf584 1/1 Running 0 1h
example-nginx-t6ght74jr-58fhr 1/1 Running 0 1m