定义API
在设计的CR spec中,包含size、image、storage属性,因此需要修改api/v1/hwfka_types.go中HwfkaSpec和HwfkaStatus部分,为应用定义参数和状态属性。
package v1
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// HwfkaSpec defines the desired state of Hwfka
type HwfkaSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Maximum=3
// +kubebuilder:validation:ExclusiveMaximum=false // 包含最大值
Size int32 `json:"size"` // Hwfka 应用包含的 broker 数量
Image string `json:"image"` // Hwfka 镜像地址
Storage *StorageSpec `json:"storage"` // Hwfka 数据存储相关配置
}
type StorageSpec struct {
Class string `json:"class"`
AccessModes corev1.PersistentVolumeAccessMode `json:"accessModes"`
Size resource.Quantity `json:"size"`
// 华为公有云 EVS 场景需额外指定 diskType, region, zone
DiskType string `json:"diskType,omitempty"` // omitempty 表示可以为空
Region string `json:"region,omitempty"`
Zone string `json:"zone,omitempty"`
}
// HwfkaStatus defines the observed state of Hwfka
type HwfkaStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Phase string `json:"phase,omitempty"` // Hwfka 实例安装状态
Server string `json:"server,omitempty"` // Hwfka 访问地址
}
使用Kubebuilder,可通过在API结构的属性上定义Markers,自动生成CRD中的spec.validation.openAPIV3Schema,即基于OpenAPI的校验规则,以便校验用户创建的CR中字段值的合法性。如上述设置size属性的最小值和最大值:
// +kubebuilder:validation:Minimum=1 // +kubebuilder:validation:Maximum=3
详细使用方法参见:CRD Validation。每次修改API定义后,需要执行命令自动重新生成代码和CRD:
$ make generate $ make manifests