持久卷声明(PersistemVolumeClaim)的元数据无法正确解码。

huangapple go评论95阅读模式
英文:

PersistemVolumeClaim Metadata can not be decoded correctly

问题

环境

Kubectl版本

客户端版本: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”;}
服务器版本: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版本(Kind集群)

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版本

版本:main.version{KubeBuilderVersion:“3.1.0”,KubernetesVendor:“1.19.2”,GitCommit:“92e0349ca7334a0a8e5e499da4fb077eb524e94a”,BuildDate:“2021-05-27T17:54:28Z”,GoOs:“darwin”,GoArch:“amd64”;}

操作系统

Macos Big Sur 11.6

我使用kubebuilder定义了自己的CRD,如下所示,其中包含类型为[]coreV1.PersistentVolumeClaimVolumeClaimTemplates字段。

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是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定义了DatabaseSet的观察状态
type DatabaseSetStatus struct {
    ...
}

//+kubebuilder:object:root=true

// DatabaseSetList包含DatabaseSet的列表
type DatabaseSetList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []DatabaseSet `json:"items"`
}

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

但是当我应用以下CR时,我发现metadata字段为空。

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

这是从k8s etcd中获取的yaml,可以发现volumeClaimTemplates的元数据为空。

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

有人知道为什么吗?

当我使用以下注释标记volumeclaimtemplate字段时,metadata可以正确解码。

// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Schemaless
英文:

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

答案1

得分: 1

我已经发布了一个社区维基答案来总结这个主题:

在 GitHub 上也有人提出了同样的问题(https://github.com/kubernetes-sigs/kubebuilder/issues/2460)。解决方案如 OP 所提到的:

>我找到了解决方案,使用 controller-gen crd 选项 crd:generateEmbeddedObjectMeta=true 就可以工作了。

在 GitHub 上提到了这个问题:
>我通过 controller-gen -h 找到了这个选项,在官方的 kubebuilder controller-gen CLI 文档 中没有提到这个选项。

是的,官方文档中没有提到这个(这个工具的官方文档稍微有点旧了——2019 年 8 月)。但是看看这个问题这个答案

>嗨,@numbnut,我还没有测试过 v1.21.2,但我会看一下。
>
>在运行 k8s >1.18 的操作符之前,需要使用 controller-tools@0.2.4,但至少在这个分支中已经解决了这个问题,直到 v1.20.x
>
>新的 0.6.1 版本引入了 generateEmbeddedObjectMeta,这是专门用于为 PersistentVolumeClaims 添加元数据的,如果没有它,当集群被删除时,声明将不会被删除;
>
>@clouddra 检查一下你的环境是否没有使用之前的 controller-tools 版本。

你还可以从源代码中找到相应的代码行。

英文:

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 controller-tools@0.2.4 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 newer
0.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](https://github.com/clouddra) check if your env is not using a previous version of
controller-tools`

You can also find appropriate lines from the source code.

答案2

得分: 0

查看kubebuilder问题#2460,使用controller-gen选项crd:generateEmbeddedObjectMeta=true将起作用。

英文:

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

huangapple
  • 本文由 发表于 2021年12月26日 16:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/70485009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定