<p>I have a project that need to update cloneset yaml</p>
<p>YAML document something like:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps.kruise.io/v1alpha1
kind: CloneSet
metadata:
  generation: 1
  ...
spec:
  ...
  ...
  volumeClaimTemplates:
  - metadata:
      creationTimestamp: null
      name: data1
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 60G
  - metadata:
      creationTimestamp: null
      name: data2
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 60G
status:
  availableReplicas: 1
</code></pre>
<p>I want to patch volumeClaimTemplates based on metadata.name</p>
<p>If name is not specified, storages for <strong>all volumeClaimTemplates</strong> are updated, and if name is specified and is matched, storages for a specific name are updated, if name is specified, but name  don't match, return error</p>
<pre class="lang-sh prettyprint-override"><code>matchVolumeTemplateName=${1-all}
storage=&quot;$2&quot;
if [[ ${matchVolumeTemplateName} == &quot;all&quot; ]]; then
  ./bin/yq e -i &quot;.spec.volumeClaimTemplates[].spec.resources.requests.storage = \&quot;${storage}\&quot;&quot; &quot;cloneset_modifystorage.yml&quot;
else
  ./bin/yq e -i &quot;.spec.volumeClaimTemplates[] | select(.metadata.name == &quot;\&quot;${matchVolumeTemplateName}\&quot;&quot;).spec.resources.requests.storage |= \&quot;${storage}\&quot;&quot; &quot;cloneset_modifystorage.yml&quot;
fi
</code></pre>
<p>However, with the above code, <strong>only part of the YAML data</strong> will be output if <code>a match is found</code> for the specified name, and the file will be <strong>empty</strong> if name don't match, for example <code>matchVolumeTemplateName=data3</code></p>
<pre class="lang-yaml prettyprint-override"><code># cloneset_modifystorage.yml
# matchVolumeTemplateName = data1   storage = 90G

# Other data of K8S YAML is lost
metadata:
  creationTimestamp: null
  name: data1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 90G
</code></pre>
<p>As a result, other data information about K8S is <strong>missing</strong>, and an empty file is printed <strong>instead of an error</strong> when a name match fails.</p>
<p>I really appreciate any help with this.</p>

moluzhui
<p>You're just missing brackets - so it's first filtering the yaml and then updating (instead of updating a specific portion of the yaml):</p>
<pre class="lang-sh prettyprint-override"><code>./bin/yq e -i &quot;(.spec.volumeClaimTemplates[] | select(.metadata.name == &quot;\&quot;${matchVolumeTemplateName}\&quot;&quot;).spec.resources.requests.storage) |= \&quot;${storage}\&quot;&quot; &quot;cloneset_modifystorage.yml&quot;
</code></pre>
<p>Disclosure: I wrote yq</p>
