Search code examples
dockeropenshiftkubectl

oc/kubectl patch replaces whole line


I am using oc patch with op to replace one string in deployment, following is the command:-

oc patch dc abc --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "ab-repository/" },{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "bc-repository/" }]'

what it is doing is it changes below:-

Before:- ab-repository/ab:1.0.0
After:- bc-repository/

what I want is this:-

Before:- ab-repository/ab:1.0.0
After:- bc-repository/ab:1.0.0

Please let me know what i am doing wrong here.

Below is the YAML

apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: ruleengine
  namespace: apps
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    name: ruleengine
  strategy:
    activeDeadlineSeconds: 21600
    resources: {}
    rollingParams:
      intervalSeconds: 1
      updatePeriodSeconds: 1
    type: Rolling
  template:
    metadata:
      creationTimestamp: null
      labels:
        name: ruleengine
    spec:
      containers:
      - image: ab-repository/ab:1.0.0 ### containers should be provided in the form of an array

Solution

  • The 'replace' operation works like remove/add entire value:

    This operation is functionally identical to a "remove" operation for a value, followed immediately by an "add" operation at the same location with the replacement value.

    There's no such JSON patch operation as replace value partially (RFC6902, RFC7386)

    You can get image like:

    oc get dc ruleengine -o=jsonpath='{..image}'
    

    Then manipulate the value with sed and use it in 'oc patch'