K8s操作员读取原始数据

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

K8s operator read raw data

问题

我正在尝试从运算符CR中获取RAW数据,但是得到了一个空对象(其他所有值都按预期工作)。
我已经为这个问题创建了一个最小示例,在这个示例中,我试图读取infrastructureConfig对象。

这里的棘手之处在于我的结构体是对另一个具有原始数据属性类型的结构体的引用

这是简单的CR:

https://github.com/JennyMet/gardner_test/blob/main/config/samples/mygroup_v1alpha1_rawtest.yaml#L11

在这里,我试图读取数据并得到一个空对象,有什么想法吗?

https://github.com/JennyMet/gardner_test/blob/main/controllers/rawtest_controller.go#L70

这是我正在使用的类型的引用
https://github.com/gardener/gardener/blob/5522be0e17ccf38aae36efb9fdb6463c66d6e4f1/pkg/apis/core/v1beta1/types_shoot.go#L1184

我认为这与字段有关

x-kubernetes-preserve-unknown-fields: true
https://book.kubebuilder.io/reference/markers/crd-processing.html
这个字段不存在
我如何在这里将其添加到模式中?

https://github.com/JennyMet/gardner_test/blob/main/api/v1alpha1/rawtest_types.go#L32,因为它在底层使用

https://github.com/gardener/gardener/blob/5522be0e17ccf38aae36efb9fdb6463c66d6e4f1/pkg/apis/core/v1beta1/types_shoot.go#L1184

我的意思是我尝试过了,但它不起作用,因为InfrastructureConfig是RAW的,而它在

type System struct {
	Type     system           `json:"type,omitempty"`
    // +kubebuilder:pruning:PreserveUnknownFields
	Provider v1beta1.Provider `json:"provider,omitempty"`
}

但是rawData在Provider下面,而Provider不是我的结构体,我只是在使用它

就像这样,看看InfrastructureConfig类型...

type Provider struct {

	Type string `json:"type" protobuf:"bytes,1,opt,name=type"`
	ControlPlaneConfig *runtime.RawExtension `json:"controlPlaneConfig,omitempty" protobuf:"bytes,2,opt,name=controlPlaneConfig"`
	InfrastructureConfig *runtime.RawExtension `json:"infrastructureConfig,omitempty" protobuf:"bytes,3,opt,name=infrastructureConfig"`

}
英文:

Im trying to get RAW data from operator CR and im getting an empty object . (all others value are working as expected )
I’ve created a minimal example for the issue, in the example im trying to read the infrastructureConfig object

The tricky part here that my struct is reference to another struct which have a property type rawdata

https://github.com/JennyMet/gardner_test

Here the simple CR

https://github.com/JennyMet/gardner_test/blob/main/config/samples/mygroup_v1alpha1_rawtest.yaml#L11

Here im tying to read the data and get an empty object, any idea?

https://github.com/JennyMet/gardner_test/blob/main/controllers/rawtest_controller.go#L70

this is the reference of the type which im using
https://github.com/gardener/gardener/blob/5522be0e17ccf38aae36efb9fdb6463c66d6e4f1/pkg/apis/core/v1beta1/types_shoot.go#L1184

I think its related to fields

x-kubernetes-preserve-unknown-fields: true
https://book.kubebuilder.io/reference/markers/crd-processing.html
which is not existing
How can I add it to the schema here ?

https://github.com/JennyMet/gardner_test/blob/main/api/v1alpha1/rawtest_types.go#L32 as under the hood it uses

https://github.com/gardener/gardener/blob/5522be0e17ccf38aae36efb9fdb6463c66d6e4f1/pkg/apis/core/v1beta1/types_shoot.go#L1184

I mean I tried and it doesnt work as the InfrastructureConfig which is RAW is under the

type System struct {
	Type     system           `json:"type,omitempty"`
    // +kubebuilder:pruning:PreserveUnknownFields
	Provider v1beta1.Provider `json:"provider,omitempty"`
}

But the rawData is under Provider which is not my struct, im just using it.

which is like this , see the InfrastructureConfig type...

type Provider struct {

	Type string `json:"type" protobuf:"bytes,1,opt,name=type"`
	ControlPlaneConfig *runtime.RawExtension `json:"controlPlaneConfig,omitempty" protobuf:"bytes,2,opt,name=controlPlaneConfig"`
	InfrastructureConfig *runtime.RawExtension `json:"infrastructureConfig,omitempty" protobuf:"bytes,3,opt,name=infrastructureConfig"`

}

答案1

得分: 2

目前,你只能将 // +kubebuilder:pruning:PreserveUnknownFields 放在 Provider v1beta1.Provider 上,这意味着其中的所有子字段都将允许包含额外的未知字段。

好消息是,在 https://github.com/kubernetes-sigs/controller-tools/pull/683 合并之后,你的问题将得到解决。在那之后,你就不需要使用 // +kubebuilder:pruning:PreserveUnknownFields,controller-tools 会自动为所有的 RawExtension 字段添加 x-kubernetes-preserve-unknown-fields: true

英文:

Currently, you can only put the // +kubebuilder:pruning:PreserveUnknownFields on the Provider v1beta1.Provider, which means all sub fields in it will be allowed with additional unknown fields.

The good news is, your problem will be solved after https://github.com/kubernetes-sigs/controller-tools/pull/683 merged. After that, you have not to use // +kubebuilder:pruning:PreserveUnknownFields and controller-tools would automatically add x-kubernetes-preserve-unknown-fields: true for all RawExtension fields.

huangapple
  • 本文由 发表于 2022年6月11日 00:38:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/72577439.html
匿名

发表评论

匿名网友

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

确定