Can you patch an arbitrary resource with no base via kustomize?

1.8k Views Asked by At

I've been trying to patch a Deployment, declared and applied by a kops addon (Ebs drivers).

Unfortunately, after trying the variety of patching strategies, it seems that I am unable to patch a resource that doesn't have a base declared in my folder structure.

Note that I'm using FluxCD on top for reconciliation, which doesn't see any change for this resource when pushing the patch.

Here is an extract of the deployment automatically generated and applied by Kops that I want to change :

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    addon.kops.k8s.io/name: aws-ebs-csi-driver.addons.k8s.io
    addon.kops.k8s.io/version: 1.0.0-kops.1
    app.kubernetes.io/instance: aws-ebs-csi-driver
    app.kubernetes.io/managed-by: kops
    app.kubernetes.io/name: aws-ebs-csi-driver
    app.kubernetes.io/version: v1.0.0
    k8s-addon: aws-ebs-csi-driver.addons.k8s.io
    **super: unpatched**
  name: ebs-csi-controller
  namespace: kube-system

My kustomization file :

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: flux-system
patches:
- target:
    kind: Deployment
    name: ebs-csi-controller
    namespace: kube-system
  path: patch-ebs.yaml

and the actual patch-ebs.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ebs-csi-controller
  namespace: kube-system
  labels:
    super: patched

Also tried with a Json patch patch-ebs.json :

[
  {"op": "replace", "path": "/metadata/labels/super", "value": "Patched"}
]
  • Running a kustomize build doesn't generate any output ;
  • Creating a deployment file which is refered as a resource/base generate a proper patch that can be applied.

Is it a limitation of Kustomize, or am I missing something along the lines?

Thanks for your help !

2

There are 2 best solutions below

0
On

From what I can see you are trying to patch a kind of addon (managed by kops controller). Try this one from FluxCD docs

So, your case will be like patch-ebs.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kustomize.toolkit.fluxcd.io/prune: disabled
    kustomize.toolkit.fluxcd.io/ssa: merge
  name: ebs-csi-controller
  namespace: kube-system
  labels:
    super: patched

and kustomization file:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: kube-system
resources:
  - patch-ebs.yaml
0
On

Kustomize depends on the referenced resources to be included as resources, or to be generated using the built in generators for them to be patched, or mutated in any way.

One caveat would be that you could include a resource in your kustomization that matches your existing ebs-csi-controller Deployment and Kustomize will build a resource that can be applied on top of your existing deployment.