K8s Operator读取CR内runtime.RawExtension字段返回空对象问题咨询
K8s Operator读取CR中第三方结构体RawExtension字段为空问题
问题现象
开发K8s Operator过程中,从自定义资源(CR)读取RAW类型数据时始终得到空对象,其余普通字段读取功能均正常。已构造最小复现样例,样例中目标读取字段为infrastructureConfig对象。
本次问题核心难点:自定义业务结构体引用了第三方项目的结构体,该第三方结构体包含runtime.RawExtension类型的原始数据属性,无法直接修改第三方源码补充配置。
相关资源链接
- 最小复现代码仓库:JennyMet/gardner_test
- 测试用CR示例位置:config/samples/mygroup_v1alpha1_rawtest.yaml 第11行
- 控制器字段读取逻辑位置:controllers/rawtest_controller.go 第70行
- 引用的第三方类型来源:Gardener项目 pkg/apis/core/v1beta1/types_shoot.go 第1184行
初步排查与已尝试方案
初步判断问题与CRD schema缺失x-kubernetes-preserve-unknown-fields: true配置有关,该配置规则可参考Kubebuilder官方CRD处理标记说明。
已尝试在自定义的System结构体的Provider字段上添加// +kubebuilder:pruning:PreserveUnknownFields标记,代码如下:
type System struct { Type system `json:"type,omitempty"` // +kubebuilder:pruning:PreserveUnknownFields Provider v1beta1.Provider `json:"provider,omitempty"` }
该方案未生效,原因是RAW类型的InfrastructureConfig字段位于直接引用的第三方Provider结构体内部,无法修改第三方结构体源码添加对应标记。第三方Provider结构体定义如下,其中InfrastructureConfig字段为*runtime.RawExtension类型:
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"` }
待解决问题
该场景下应当如何正确配置,才能正常读取到该RawExtension字段的原始数据?
内容的提问来源于stack exchange,提问作者JME
相关产品推荐
相关产品推荐

