JSON Patch

The json-patch-component permits to apply a Kubernetes patch to a given component. The patch can modify any of the spec fields in the rendered entities. For more information, check the official documentation about patching entities.

The following snippet show an application with a single component that is patched before being launched. Changes include, replacing an existing label, deleting an existing label, adding a new container, and modifying the number of replicas.

apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  name: json-patch-app
  annotations: 
    version: "v0.0.1"
    description: "My json-patch application"
spec:
  components:
    - name: json-patch-component
      type: webservice
      properties:
        image: my-app-image:v1.0.0
        cmd: ["server", "run"]
        cpu: "0.25"
        memory: "200Mi"
        labels:
          label1: value1
          label2: value2
      traits:
        - type: json-patch
          properties:
            operations: # (Required) patch operations 
              # https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/
              # https://erosb.github.io/post/json-patch-vs-merge-patch/
              - op: operation # (Required) operation ("add", "remove", "replace", "move", "copy")
                path: path # (Required) path
                value: spec values # (Optional) new value
              
              # EXAMPLES
              # Example 1: Update a label
              - op: replace 
                path: "/spec/template/metadata/labels/label1" 
                value: updated 
              # Example 2: Remove a label  
              - op: remove 
                path: "/spec/template/metadata/labels/label2" 
              # Example 3: Add a new container
              - op: add
                path: "/spec/template/spec/containers/1"
                value:
                  name: added
                  image: busybox:1.35.0
                  command: ["sleep", "864000"]
                  resources:
                    requests:
                      cpu: 200m
                      memory: 100Mi
              # Example 4: Add  replicas
              - op: add
                path: "/spec/replicas"
                value: 2