+kubebuilder:printcolumn 标签用于自定义 kubectl get 命令输出时显示的列。通过在自定义资源定义(CRD)中使用 +kubebuilder:printcolumn 标签,你可以定义在执行 kubectl get 时,哪些字段会以列的形式显示,以及如何格式化这些字段。
假设你正在定义一个自定义资源 MyResource,它有以下字段:
Name:资源的名称Age:资源的年龄Status:资源的当前状态CreationTimestamp:资源的创建时间你想要 kubectl get myresources 的输出显示 Name、Age、Status 以及 CreationTimestamp,并自定义输出的列。
以下是一个完整的例子,展示如何在 MyResource 的定义中使用 +kubebuilder:printcolumn 标签:
// api/v1/myresource_types.go package v1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // MyResourceSpec defines the desired state of MyResource type MyResourceSpec struct { // Age is the age of the resource Age int `json:"age,omitempty"` // Status represents the current state of the resource Status string `json:"status,omitempty"` } // MyResourceStatus defines the observed state of MyResource type MyResourceStatus struct { // Add additional status fields here } // +kubebuilder:object:root=true // +kubebuilder:subresource:status // +kubebuilder:resource:path=myresources,scope=Namespaced // +kubebuilder:printcolumn:name="Age",type="integer",JSONPath=".spec.age",description="Age of the resource" // +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".spec.status",description="Current status of the resource" // +kubebuilder:printcolumn:name="Created",type="date",JSONPath=".metadata.creationTimestamp",description="Creation time of the resource" // +kubebuilder:printcolumn:name="Namespaced",type="string",JSONPath=".metadata.namespace",priority=1,description="Namespace of the resource" // +kubebuilder:printcolumn:name="Name",type="string",JSONPath=".metadata.name",priority=1,description="Name of the resource" // MyResource is the Schema for the myresources API type MyResource struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` Spec MyResourceSpec `json:"spec,omitempty"` Status MyResourceStatus `json:"status,omitempty"` } // +kubebuilder:object:root=true // MyResourceList contains a list of MyResource type MyResourceList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` Items []MyResource `json:"items"` } func init() { SchemeBuilder.Register(&MyResource{}, &MyResourceList{}) } +kubebuilder:printcolumn:name="Age",type="integer",JSONPath=".spec.age",description="Age of the resource"
Age 的列,类型为 integer,显示 spec.age 字段的值,列描述为 "Age of the resource"。+kubebuilder:printcolumn:name="Status",type="string",JSONPath=".spec.status",description="Current status of the resource"
Status 的列,类型为 string,显示 spec.status 字段的值,列描述为 "Current status of the resource"。+kubebuilder:printcolumn:name="Created",type="date",JSONPath=".metadata.creationTimestamp",description="Creation time of the resource"
Created 的列,类型为 date,显示资源的创建时间 metadata.creationTimestamp。+kubebuilder:printcolumn:name="Namespaced",type="string",JSONPath=".metadata.namespace",priority=1,description="Namespace of the resource"
Namespaced 的列,类型为 string,显示资源的命名空间 metadata.namespace。+kubebuilder:printcolumn:name="Name",type="string",JSONPath=".metadata.name",priority=1,description="Name of the resource"
Name 的列,类型为 string,显示资源的名称 metadata.name。编辑完成后,生成CRD并应用:
make generate make manifests kubectl apply -f config/crd/bases 执行 kubectl get myresources 时,将显示自定义列:
kubectl get myresources 输出:
NAME AGE STATUS CREATED NAMESPACED foo 5 Ready 2024-08-01T12:00:00Z default bar 10 Pending 2024-08-01T12:05:00Z custom 通过这种方式,你可以自定义 kubectl get 输出的列,更直观地查看和管理你的CR实例。