问题描述
我正在开发一个运算符,想知道是否有链接或文档在其中描述了 Spec 和 Status 字段的命名最佳实践?例如:
在哪里可以找到与每个字段或参数的注释相关的所有信息,以设置字段可选或设置默认值,如下所示:
// MyAppStatus defines the observed state of MyAppSPec
type MyAppStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
AppCount int32 `json:"appCount,omitempty"`
Apps map[string]string `json:"apps,omitempty"`
}
如何设置 AppCount 的默认值?另外,AppCount 的命名是否正确?我可以命名间接,即我可以将其设置为以下内容:
AppCount int32 `json:"count,omitempty"`
这将帮助我在 YAML 文件中公开不同的名称,但在代码内部,我将引用 AppCount。请让我知道是否有针对每个字段和字段标志(例如省略空或可选)的评论野兽做法的文档?
从评论的角度来看,我看到很多运营商都提到了kUbeBuilder,如下图:
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
在哪里可以找到上述内容的详细信息以及何时使用它们?
解决方法
您可以在 https://book.kubebuilder.io/reference/markers.html 处找到有关 controller-gen 注释的文档(阅读小节)。 omitempty
表示该字段是可选的。 JSON 名称和结构名称通常应相同(大小写除外),除非您有充分的理由不这样做。