<p><strong>Environment</strong></p>
<p><strong>Kubectl  Version</strong></p>
<pre class="lang-sh prettyprint-override"><code>Client Version: version.Info{Major:&quot;1&quot;, Minor:&quot;18&quot;, GitVersion:&quot;v1.18.4&quot;, GitCommit:&quot;c96aede7b5205121079932896c4ad89bb93260af&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2020-06-18T02:59:13Z&quot;, GoVersion:&quot;go1.14.3&quot;, Compiler:&quot;gc&quot;, Platform:&quot;darwin/amd64&quot;}
Server Version: version.Info{Major:&quot;1&quot;, Minor:&quot;20+&quot;, GitVersion:&quot;v1.20.4-80+89e0897d2cb807&quot;, GitCommit:&quot;89e0897d2cb8073fbb8f700258573f1478d4826a&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2021-11-22T03:53:35Z&quot;, GoVersion:&quot;go1.15.8&quot;, Compiler:&quot;gc&quot;, Platform:&quot;linux/amd64&quot;}
</code></pre>
<p><strong>Kubernetes Version (Kind Cluster)</strong></p>
<pre class="lang-yaml prettyprint-override"><code>kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
  - role: control-plane
    image: kindest/node:v1.20.7@sha256:cbeaf907fc78ac97ce7b625e4bf0de16e3ea725daf6b04f930bd14c67c671ff9
  - role: worker
    image: kindest/node:v1.20.7@sha256:cbeaf907fc78ac97ce7b625e4bf0de16e3ea725daf6b04f930bd14c67c671ff9
  - role: worker
    image: kindest/node:v1.20.7@sha256:cbeaf907fc78ac97ce7b625e4bf0de16e3ea725daf6b04f930bd14c67c671ff9
</code></pre>
<p><strong>Kubebuilder Version</strong></p>
<pre class="lang-sh prettyprint-override"><code>Version: main.version{KubeBuilderVersion:&quot;3.1.0&quot;, KubernetesVendor:&quot;1.19.2&quot;, GitCommit:&quot;92e0349ca7334a0a8e5e499da4fb077eb524e94a&quot;, BuildDate:&quot;2021-05-27T17:54:28Z&quot;, GoOs:&quot;darwin&quot;, GoArch:&quot;amd64&quot;}
</code></pre>
<p><strong>Os</strong></p>
<pre><code>Macos Big Sur 11.6
</code></pre>
<p>I use kubebuilder to define my own <code>CRD</code> like below, and it contains  <code>VolumeClaimTemplates</code> filed which the type is <code>[]coreV1.PersistentVolumeClaim</code></p>
<pre class="lang-golang prettyprint-override"><code>package v1alpha1
import (
    apps &quot;k8s.io/api/apps/v1&quot;
    coreV1 &quot;k8s.io/api/core/v1&quot;
    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
    &quot;k8s.io/apimachinery/pkg/util/intstr&quot;
)

type DatabaseSetSpec struct {
        ...
        // +optional
    VolumeClaimTemplates []coreV1.PersistentVolumeClaim `json:&quot;volumeClaimTemplates,omitempty&quot; protobuf:&quot;bytes,4,rep,name=volumeClaimTemplates&quot;`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=ami-dbs

// DatabaseSet is the Schema for the databasesets API
type DatabaseSet struct {
    metav1.TypeMeta   `json:&quot;,inline&quot;`
    metav1.ObjectMeta `json:&quot;metadata,omitempty&quot;`

    Spec   DatabaseSetSpec   `json:&quot;spec,omitempty&quot;`
    Status DatabaseSetStatus `json:&quot;status,omitempty&quot;`
}

// DatabaseSetStatus defines the observed state of DatabaseSet
type DatabaseSetStatus struct {
    ...
}

//+kubebuilder:object:root=true

// DatabaseSetList contains a list of DatabaseSet
type DatabaseSetList struct {
    metav1.TypeMeta `json:&quot;,inline&quot;`
    metav1.ListMeta `json:&quot;metadata,omitempty&quot;`
    Items           []DatabaseSet `json:&quot;items&quot;`
}

func init() {
    SchemeBuilder.Register(&amp;DatabaseSet{}, &amp;DatabaseSetList{})
}
</code></pre>
<p>But when I apply the CR like the below, I found that the  <code>metadata</code> filed is empty</p>
<pre><code>apiVersion: apps.analyticdb.aliyun.com/v1alpha1
kind: DatabaseSet
metadata:
  name: databaseset-sample
spec: 
  ...
  volumeClaimTemplates:
    - metadata:
        name: pvc-test
      spec:
        accessModes: [ &quot;ReadWriteOnce&quot; ]
        storageClassName: &quot;manual&quot;
        resources:
          requests:
            storage: 3Gi

</code></pre>
<p>Here is the yaml which get from the k8s ectd, it cound be found that the metadata of the <code>volumeClaimTemplates</code> is empty.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps.analyticdb.aliyun.com/v1alpha1
kind: DatabaseSet
metadata:
  creationTimestamp: &quot;2021-12-24T09:46:22Z&quot;
  generation: 1
  name: databaseset-sample
  namespace: default
  resourceVersion: &quot;98727469&quot;
  uid: e64107f2-7a4b-473b-9275-39ab5e2e88dc
spec:
  ...
  volumeClaimTemplates:
  - metadata: {}
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 3Gi
      storageClassName: manual
</code></pre>
<p>Does anyone know why？</p>
<p>And when I mark the volumeclaimtemplate field with the below comment, <code>metada</code> can be decoed correctly</p>
<pre class="lang-golang prettyprint-override"><code>// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Schemaless
</code></pre>

Huzhenyu
<p>I have posted community wiki answer to summarise the topic:</p>
<p>The same question was asked <a href="https://github.com/kubernetes-sigs/kubebuilder/issues/2460" rel="nofollow noreferrer">on the github</a>. The solution is like OP mentioned:</p>
<blockquote>
<p>have found the solution, use the controller-gen crd option <code>crd:generateEmbeddedObjectMeta=true</code> will work</p>
</blockquote>
<p>It was mentioned on the github:</p>
<blockquote>
<p>I found this option through <code>controller-gen -h</code>, and there is no mention of this option in the <a href="https://book.kubebuilder.io/reference/controller-gen.html" rel="nofollow noreferrer">official kubebuilder controller-gen CLI documention</a> .</p>
</blockquote>
<p>Yes, there is no mention about that (official doc for this tool is slightly old - August 2019) But look at <a href="https://issueexplorer.com/issue/spotify/flink-on-k8s-operator/83" rel="nofollow noreferrer">this problem</a> and <a href="https://issueexplorer.com/issue/spotify/flink-on-k8s-operator/83#6496747" rel="nofollow noreferrer">this answer</a>:</p>
<blockquote>
<p>hi  <a href="https://github.com/numbnut" rel="nofollow noreferrer">@numbnut</a>  I haven't tested yet with v1.21.2 but I'll take a look.</p>
<p>Using  <code>controller-tools@0.2.4</code>  was needed before to be able to run the operator with k8s  <code>&gt;1.18 but that has been addressed in this fork at least up until</code> v1.20.x`</p>
<p>The newer<code>0.6.1</code>  introduces  <code>generateEmbeddedObjectMeta</code>  which is required specifically to add the metadata for the  <code>PersistentVolumeClaims</code>  without it claims will not be deleted when the cluster is deleted;</p>
<p><a href="https://github.com/clouddra" rel="nofollow noreferrer">@clouddra</a>  check if your env is not using a previous version of  <code>controller-tools</code></p>
</blockquote>
<p>You can also find appropriate lines from the <a href="https://github.com/kubernetes-sigs/controller-tools/blob/c796d037386bc209fde0ca657df6b6face0ec5b7/pkg/crd/gen.go#L74" rel="nofollow noreferrer">source code</a>.</p>
