<p>If anyone know solutions please help me how I can do this.<br />
I have “statefulset” which has following “volumeClaimTemplates” inside:<br />
When I scale my replica count:<br />
<strong>“kubectl scale statefulset --replicas=2 my-statefulset”</strong><br />
new “PVC” create from “volumesnapshot” object which name <strong>= “MySnapshot”</strong></p>
<pre class="lang-yaml prettyprint-override"><code>  volumeClaimTemplates:
  - apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: data
    spec:
      accessModes:
      - ReadWriteOnce
      dataSource:
        apiGroup: snapshot.storage.k8s.io
        kind: VolumeSnapshot
        name: MySnapshot
      resources:
        requests:
          storage: 800Gi
      storageClassName: ebs-sc
      volumeMode: Filesystem
</code></pre>
<p><strong>My question:</strong><br />
Is it possible to use dynamic name in field</p>
<pre class="lang-yaml prettyprint-override"><code>volumeClaimTemplates:
  dataSource:
    apiGroup: snapshot.storage.k8s.io
    kind: VolumeSnapshot
    name: ?  
</code></pre>
<p><strong>Clarify:</strong><br />
When new snapshot created, modify statefulset and set volumeClaimTemplates.dataSource.name = new-name<br />
<strong>Why I need this:</strong><br />
I have cronjob which automatically create snapshot with new name ex: MySnapshot_1, MySnapshot_2 …
And I need latest data into my “PVC” when new replica is created .</p>

Nariman Maad
<p><a href="https://kyverno.io/" rel="nofollow noreferrer">https://kyverno.io/</a>
Can do this Job</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: mutate-pvc
spec:
  rules:
    - name: set-name-latest-snapshot
      match:
        any:
        - resources:
            kinds:
            - PersistentVolumeClaim
      context:
      - name: latestSnapshotTime
        apiCall:
          urlPath: &quot;/apis/snapshot.storage.k8s.io/v1/namespaces/{{request.namespace}}/volumesnapshots&quot;
          jmesPath: &quot;items[?status.readyToUse].status.creationTime | sort(@)[-1]&quot; 
      - name: latestSnapshotName
        apiCall:
          urlPath: &quot;/apis/snapshot.storage.k8s.io/v1/namespaces/{{request.namespace}}/volumesnapshots&quot;
          jmesPath: &quot;items[?status.creationTime == '{{latestSnapshotTime}}'][].metadata.name | [0]&quot;
      mutate:
        patchStrategicMerge: 
          spec:
            dataSource:
              name: &quot;{{latestSnapshotName}}&quot;
</code></pre>
