Patch your Kubernetes Deployment (Advanced)

Patch or Add resources using Kustomize

TrueFoundry allows you to tweak the most common parameters of the deployment through the service spec. However, there might be situations in which you might want to override some fields that are not exposed in the TrueFoundry Service spec. You can then use Kustomize to add, patch or delete the Kubernetes resources that TrueFoundry deploys on the cluster.

Kustomize enables you to

  • Patch the rendered Kubernetes resources generated by the TrueFoundry Application. E.g. Adding extra annotations for Prometheus / Datadog
  • Add extra Kubernetes resources along with your TrueFoundry Application. E.g. Adding extra ConfigMap, Secret, Istio VirtualService, etc

📘

Supported Application Types

  • Service
  • Job
  • Helm

Using Kustomize

You can add patches and resources using the kustomize field of the spec

📘

Resources are patched/added in the same namespace

Patches and new resources are applied to the same namespace as the application

Here is an example that

  1. Adds Prometheus scrape annotations to the pod spec patches in Kubernetes Deployment resource that will be generated for the Service. Here we use the patch section of the kustomize because we only want to add extra annotations to an existing resource.
  2. Adds a new complete ConfigMap resource. Here we use additions as we define the complete spec of the new resource.

📘

Viewing Resources Generated by TrueFoundry

You can view all the resources rendered by the Application in the Application Spec Tab and then selecting Applied K8s Manifest

type: service
name: my-service
image:
  type: image
  image_uri: nginx:latest
  ...
ports:
  - port: 8000
    ...
kustomize:
  patch:
    patchesStrategicMerge:
      - |
        kind: Deployment
        apiVersion: apps/v1
        metadata:
          name: my-service
        spec:
          template:
            metadata:
              annotations:
                prometheus.io/port: "8000"
                prometheus.io/scrape: "true"
  additions:
    - apiVersion: v1
      data:
        test: data
      kind: ConfigMap
      metadata:
        name: configmap-1
# pip install PyYAML==6.0.1
from truefoundry.deploy import Service, Image, Port, Kustomize
import yaml

SERVICE_NAME = "my-service"
SERVICE_PORT = 8000

# Add Prometheus annotation to the Deployment
ADD_PROMETHEUS_ANNOTATIONS_TO_DEPLOYMENT = f"""\
kind: Deployment
apiVersion: apps/v1
metadata:
  name: {SERVICE_NAME}
spec:
  template:
    metadata:
      annotations:
        prometheus.io/port: "{SERVICE_PORT}"
        prometheus.io/scrape: "true"
"""

ADD_CONFIG_MAP = """\
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-1
data:
  test: data
"""

service = Service(
    name=SERVICE_NAME,
    image=Image(image_uri="nginx:latest", command=...),
    ports=[Port(port=SERVICE_PORT, ...)],
    ...,
    kustomize=Kustomize(
        patch={
            "patchesStrategicMerge": [
                ADD_PROMETHEUS_ANNOTATIONS_TO_DEPLOYMENT,
            ]
        },
        additions=[
            yaml.safe_load(ADD_CONFIG_MAP),
        ]
    ),
)

📘

Data Types

  • kustomize.patch is an object. The most commonly used key under it is patchesStrategicMerge which is a list of strings. Each string member is a patch in YAML format
  • kustomize.additions is a list of objects. Each object is a Kubernetes resource definition.

You can configure the same using the UI by enabling the Advanced Fields and then enabling Kustomize