익명 12:37

replace backslash with forward slash in JSON string using kubectl patch command

replace backslash with forward slash in JSON string using kubectl patch command

I'm trying to replace a string in kubectl JSON but I can't get it working.

I get following error:

kubectl patch daemonset node-agent -n velero -p '{"spec":{"template":{"spec":{"volumes":[{"hostPath": {"path": "/var/lib/kubelet/pod"}}]}}}}'

Error from server: map: map[hostPath:map[path:/var/lib/kubelet/pods]] does not contain declared merge key: name

I'm trying to replace this:

  volumes:
  - hostPath:
      path: \var\lib\kubelet\pods
      type: ""
    name: host-pods
  - hostPath:
      path: \var\lib\kubelet\plugins
      type: ""

with this:

  volumes:
  - hostPath:
      path: /var/lib/kubelet/pods
      type: ""
    name: host-pods
  - hostPath:
      path: /var/lib/kubelet/plugins
      type: ""

daemonset YAML:

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: DaemonSet
metadata:
  annotations:
    deprecated.daemonset.template.generation: "1"
  creationTimestamp: "2026-07-10T08:06:46Z"
  generation: 1
  labels:
    component: velero
  name: node-agent
  namespace: velero
  resourceVersion: "531"
  uid: a0c6f9a2-2e6e-41eb-889c-088945379710
spec:
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      name: node-agent
  template:
    metadata:
      labels:
        component: velero
        name: node-agent
        role: node-agent
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/os
                operator: NotIn
                values:
                - windows
      containers:
      - args:
        - node-agent
        - server
        - --features=
        command:
        - /velero
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: spec.nodeName
        - name: VELERO_NAMESPACE
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
        - name: VELERO_SCRATCH_DIR
          value: /scratch
        - name: GOOGLE_APPLICATION_CREDENTIALS
          value: /credentials/cloud
        - name: AWS_SHARED_CREDENTIALS_FILE
          value: /credentials/cloud
        - name: AZURE_CREDENTIALS_FILE
          value: /credentials/cloud
        - name: ALIBABA_CLOUD_CREDENTIALS_FILE
          value: /credentials/cloud
        image: velero/velero:v1.18.2
        imagePullPolicy: IfNotPresent
        name: node-agent
        ports:
        - containerPort: 8085
          name: metrics
          protocol: TCP
        resources: {}
        securityContext:
          privileged: false
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /host_pods
          mountPropagation: HostToContainer
          name: host-pods
        - mountPath: /var/lib/kubelet/plugins
          mountPropagation: HostToContainer
          name: host-plugins
        - mountPath: /scratch
          name: scratch
        - mountPath: /credentials
          name: cloud-credentials
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        runAsUser: 0
      serviceAccount: velero
      serviceAccountName: velero
      terminationGracePeriodSeconds: 30
      volumes:
      - hostPath:
          path: \var\lib\kubelet\pods
          type: ""
        name: host-pods
      - hostPath:
          path: \var\lib\kubelet\plugins
          type: ""
        name: host-plugins
      - emptyDir: {}
        name: scratch
      - name: cloud-credentials
        secret:
          defaultMode: 292
          secretName: cloud-credentials
  updateStrategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
    type: RollingUpdate
status:
  currentNumberScheduled: 1
  desiredNumberScheduled: 1
  numberMisscheduled: 0
  numberReady: 0
  numberUnavailable: 1
  observedGeneration: 1
  updatedNumberScheduled: 1

I need to escape double quotes when using PowerShell:

kubectl patch daemonset node-agent -n velero -p '{\"spec\":{\"template\":{\"spec\":{\"volumes\":[{\"hostPath\": {\"path\": \"/var/lib/kubelet/pods\"}}]}}}}'



Top Answer/Comment:

Figured it out. I had to include "name":"host-pods" under volumes[0].

kubectl patch daemonset node-agent -n velero -p '{"spec":{"template":{"spec":{"volumes":[{"name":"host-pods", "hostPath": {"path": "/var/lib/kubelet/pods", "": "" }}]}}}}'

When using powershell, it sould be as follows (with escaped characters)

kubectl patch daemonset node-agent -n velero -p '{\"spec\":{\"template\":{\"spec\":{\"volumes\":[{\"name\":\"host-pods\", \"hostPath\": {\"path\": \"/var/lib/kubelet/pods\", \"\": \"\" }}]}}}}'

상단 광고의 [X] 버튼을 누르면 내용이 보입니다