Home > Software engineering >  PersistemVolumeClaim Metadata can not be decoded correctly
PersistemVolumeClaim Metadata can not be decoded correctly

Time:12-27

Environment

Kubectl Version

Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.4", GitCommit:"c96aede7b5205121079932896c4ad89bb93260af", GitTreeState:"clean", BuildDate:"2020-06-18T02:59:13Z", GoVersion:"go1.14.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20 ", GitVersion:"v1.20.4-80 89e0897d2cb807", GitCommit:"89e0897d2cb8073fbb8f700258573f1478d4826a", GitTreeState:"clean", BuildDate:"2021-11-22T03:53:35Z", GoVersion:"go1.15.8", Compiler:"gc", Platform:"linux/amd64"}

Kubernetes Version (Kind Cluster)

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

Kubebuilder Version

Version: main.version{KubeBuilderVersion:"3.1.0", KubernetesVendor:"1.19.2", GitCommit:"92e0349ca7334a0a8e5e499da4fb077eb524e94a", BuildDate:"2021-05-27T17:54:28Z", GoOs:"darwin", GoArch:"amd64"}

Os

Macos Big Sur 11.6

I use kubebuilder to define my own CRD like below, and it contains VolumeClaimTemplates filed which the type is []coreV1.PersistentVolumeClaim

package v1alpha1
import (
    apps "k8s.io/api/apps/v1"
    coreV1 "k8s.io/api/core/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/apimachinery/pkg/util/intstr"
)

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

//  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:",inline"`
    metav1.ObjectMeta `json:"metadata,omitempty"`

    Spec   DatabaseSetSpec   `json:"spec,omitempty"`
    Status DatabaseSetStatus `json:"status,omitempty"`
}

// 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:",inline"`
    metav1.ListMeta `json:"metadata,omitempty"`
    Items           []DatabaseSet `json:"items"`
}

func init() {
    SchemeBuilder.Register(&DatabaseSet{}, &DatabaseSetList{})
}

But when I apply the CR like the below, I found that the metadata filed is empty

apiVersion: apps.analyticdb.aliyun.com/v1alpha1
kind: DatabaseSet
metadata:
  name: databaseset-sample
spec: 
  ...
  volumeClaimTemplates:
    - metadata:
        name: pvc-test
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: "manual"
        resources:
          requests:
            storage: 3Gi

Here is the yaml which get from the k8s ectd, it cound be found that the metadata of the volumeClaimTemplates is empty.

apiVersion: apps.analyticdb.aliyun.com/v1alpha1
kind: DatabaseSet
metadata:
  creationTimestamp: "2021-12-24T09:46:22Z"
  generation: 1
  name: databaseset-sample
  namespace: default
  resourceVersion: "98727469"
  uid: e64107f2-7a4b-473b-9275-39ab5e2e88dc
spec:
  ...
  volumeClaimTemplates:
  - metadata: {}
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 3Gi
      storageClassName: manual

Does anyone know why?

And when I mark the volumeclaimtemplate field with the below comment, metada can be decoed correctly

//  kubebuilder:pruning:PreserveUnknownFields
//  kubebuilder:validation:Schemaless

CodePudding user response:

see kubebuilder issue#2460, use the controller-gen option crd:generateEmbeddedObjectMeta=true will work

CodePudding user response:

I have posted community wiki answer to summarise the topic:

The same question was asked on the github. The solution is like OP mentioned:

have found the solution, use the controller-gen crd option crd:generateEmbeddedObjectMeta=true will work

It was mentioned on the github:

I found this option through controller-gen -h, and there is no mention of this option in the official kubebuilder controller-gen CLI documention .

Yes, there is no mention about that (official doc for this tool is slightly old - August 2019) But look at this problem and this answer:

hi @numbnut I haven't tested yet with v1.21.2 but I'll take a look.

Using [email protected] was needed before to be able to run the operator with k8s >1.18 but that has been addressed in this fork at least up until v1.20.x`

The newer0.6.1 introduces generateEmbeddedObjectMeta which is required specifically to add the metadata for the PersistentVolumeClaims without it claims will not be deleted when the cluster is deleted;

@clouddra check if your env is not using a previous version of controller-tools

You can also find appropriate lines from the source code.

  • Related