← All TILs · kubernetes

Lire en français traduction automatique Google Traduire sur votre ordinateur Firefox

Kustomize overlays: trimming an upstream release without editing it

kubernetes - 2026-11-08

The kube-prometheus entry installs a release of 131 objects as 62: no Grafana, no prometheus-adapter, one Prometheus instead of two. None of the release's files were edited. The changes are a Kustomize overlay, one file next to the unpacked release, and this entry goes through it.

Kustomize in one paragraph

Kustomize builds Kubernetes manifests from a kustomization.yaml: a list of resources (files, or directories with their own kustomization.yaml) plus changes to apply to them. It ships inside kubectl: kubectl kustomize <dir> prints the result, and kubectl apply -k <dir> applies it. kubectl v1.37.1 reports Kustomize Version: v5.8.1 in kubectl version --client. An overlay is a kustomization.yaml whose resource is another directory, the base, and which only adds changes on top.

kube-prometheus generates its manifests from jsonnet, and its documentation customizes them by compiling your own copy from its jsonnet library. It also publishes a kustomization.yaml at the root of the release, listing every file, so the release as unpacked is a valid base. The overlay uses that:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../upstream        # the release, unpacked here as published
patches:
  # ...

A new release is a new ../upstream. Every local change is in patches, and nothing else differs from what the project ships.

Deleting objects: $patch: delete

patches takes two kinds of patch. A strategic merge patch looks like a piece of the object, and Kubernetes' strategic merge patch adds directives to it: $patch: delete "indicates that the element that contains it should be deleted". At the top of a patch, the element is the whole object, so the object is removed from the output. Kustomize's own loadHttp example removes a RoleBinding that way.

A target makes one patch apply to every object it selects, by group, version, kind, name, namespace, label or annotation:

patches:
  - target:
      labelSelector: app.kubernetes.io/name=prometheus-adapter
    patch: &delete |-
      $patch: delete
      apiVersion: v1
      kind: Any
      metadata:
        name: any
  - target:
      labelSelector: app.kubernetes.io/name=blackbox-exporter
    patch: *delete
  - target:
      name: grafana(-config|-datasources|-dashboards|-rules)?
    patch: *delete
  - target:
      kind: ConfigMap
      name: grafana-dashboard-.*
    patch: *delete
  - target:
      kind: ServiceMonitor
      name: kube-scheduler|kube-controller-manager|coredns
    patch: *delete
  - target:
      kind: PodDisruptionBudget
    patch: *delete

Four things make this work:

Changing a custom resource: JSON patches

The other kind is a JSON patch (RFC 6902): a list of operations on paths. The patches reference notes that strategic merge patches "may require additional configuration" for custom resources, while "JSON6902 patch usage is the same for built-in and custom resources". Prometheus is a custom resource, so its changes are JSON operations:

  - target:
      kind: Prometheus
      name: k8s
    patch: |-
      - op: replace
        path: /spec/replicas
        value: 1
      - op: add
        path: /spec/retention
        value: 7d
      - op: add
        path: /spec/retentionSize
        value: 2GB
      - op: replace
        path: /spec/resources
        value:
          requests:
            memory: 400Mi
          limits:
            memory: 2Gi
      - op: add
        path: /spec/storage
        value:
          volumeClaimTemplate:
            spec:
              storageClassName: local-path
              accessModes: [ReadWriteOnce]
              resources:
                requests:
                  storage: 4Gi

replace needs the field to exist: upstream sets replicas: 2 and a 400Mi memory request. add creates it: upstream has no retention (the CRD's default is "24h" when no retention setting is given) and no storage. Alertmanager gets the same replace of its replicas, 3 upstream.

Removing list items by position, safely

JSON paths address list items by index. The overlay drops two groups of alert rules for the scheduler and controller manager, whose targets don't exist on RKE2:

  - target:
      kind: PrometheusRule
      name: kubernetes-monitoring-rules
    patch: |-
      - op: test
        path: /spec/groups/8/name
        value: kubernetes-system-controller-manager
      - op: remove
        path: /spec/groups/8
      - op: test
        path: /spec/groups/7/name
        value: kubernetes-system-scheduler
      - op: remove
        path: /spec/groups/7

Two details:

Error: testing value /spec/groups/8/name failed: test failed

Checking what the overlay did

kubectl kustomize doesn't need a cluster. Counting the objects and their kinds, before and after:

kubectl kustomize upstream | grep -c '^kind:'     # 131
kubectl kustomize overlay  | grep -c '^kind:'     # 62

diff <(kubectl kustomize upstream | grep '^kind:' | sort | uniq -c) \
     <(kubectl kustomize overlay  | grep '^kind:' | sort | uniq -c)

The diff shows, for example, all 36 ConfigMaps going away (the 33 dashboards, grafana-dashboards, and prometheus-adapter's and blackbox-exporter's configuration), the one APIService, the three PodDisruptionBudgets, and 13 ServiceMonitors becoming 7. For the names of what went away, this lists each object as Kind/name and keeps those only in the first render:

names() { kubectl kustomize "$1" |
  awk '/^---/{k="";m=0} /^kind:/{k=$2} /^metadata:/{m=1} m&&/^  name:/{print k"/"$2; m=0}' | sort; }
comm -23 <(names upstream) <(names overlay)

The second check needs the cluster: kubectl apply --server-side --dry-run=server -k overlay has the API server validate every object without storing anything, once the CRDs it uses exist.

Two clusters, two overlays

The homelab runs kube-prometheus on a second cluster, a single-node GitLab lab, which has its own Grafana in a grafana namespace. Its overlay is a second directory next to the first, with the same shape (resources: [../upstream] and a list of patches), pinned to its own release: v0.18.0 there, v0.19.0 on the three-node cluster. Most patches are the same text in both: prometheus-adapter, blackbox-exporter, the three ServiceMonitors, the PodDisruptionBudgets, the guarded rule groups (at the same positions in both releases, so both test pairs pass), and the Prometheus and Alertmanager changes. What differs follows from that Grafana:

yaml - target: kind: ConfigMap name: grafana-dashboard-(controller-manager|scheduler|proxy|grafana-overview|prometheus-remote-write|k8s-resources-multicluster|k8s-resources-windows-.*|k8s-windows-.*|nodes-aix|nodes-darwin) patch: *delete

yaml - target: kind: ConfigMap name: grafana-dashboard-.* patch: |- - op: add path: /metadata/labels/grafana_dashboard value: "1"

yaml - target: kind: NetworkPolicy name: prometheus-k8s patch: |- - op: add path: /spec/ingress/- value: from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: grafana podSelector: matchLabels: app.kubernetes.io/name: grafana ports: - port: 9090 protocol: TCP

Rendered, the policy keeps upstream's three rules and gains this fourth.

Against v0.18.0's 131 objects, the lab's overlay renders 82: the same deletions, minus the 20 dashboards it keeps. Each overlay reads only its own release, so one cluster can move to a new kube-prometheus without the other: the guarded test operations say at build time whether the new release still fits.

The renders and the failing test ran with kubectl v1.37.1 in a Linux cloud container, against v0.19.0 and, for the GitLab lab's overlay, v0.18.0; the same counts came out on the owner's workstation, and the overlays were applied to the two clusters.

Sources

Created 2026-11-08T22:58:12+01:00, updated 2026-11-08T23:23:21+01:00 · History · Edit