kubebuilder:printcolumn怎么用?
创始人
2024-11-13 13:38:39
0

+kubebuilder:printcolumn 标签用于自定义 kubectl get 命令输出时显示的列。通过在自定义资源定义(CRD)中使用 +kubebuilder:printcolumn 标签,你可以定义在执行 kubectl get 时,哪些字段会以列的形式显示,以及如何格式化这些字段。

示例场景

假设你正在定义一个自定义资源 MyResource,它有以下字段:

  • Name:资源的名称
  • Age:资源的年龄
  • Status:资源的当前状态
  • CreationTimestamp:资源的创建时间

你想要 kubectl get myresources 的输出显示 NameAgeStatus 以及 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

编辑完成后,生成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实例。

相关内容

热门资讯

绝活儿辅助!广西老友玩老是输怎... 绝活儿辅助!广西老友玩老是输怎么办(辅助挂)都是真的有辅助app(讲解有挂)在进入广西老友玩老是输怎...
法门辅助!福建13水插件(辅助... 法门辅助!福建13水插件(辅助挂)一贯是有辅助技巧(有挂技术)1、许多玩家不知道福建13水插件辅助怎...
办法辅助!潮友会app下载官方... 办法辅助!潮友会app下载官方辅助器(辅助挂)真是真的是有辅助app(有挂教程)该软件可以轻松地帮助...
妙招辅助!邯郸胡乐挂辅助(辅助... 妙招辅助!邯郸胡乐挂辅助(辅助挂)好像存在有辅助插件(有挂方略)1、上手简单,内置详细流程视频教学,...
教程书辅助!乐酷辅助(辅助挂)... 教程书辅助!乐酷辅助(辅助挂)其实存在有辅助脚本(有挂细节)乐酷辅助能透视中分为三种模型:乐酷辅助模...
学习辅助!决战卡五星辅助(辅助... 学习辅助!决战卡五星辅助(辅助挂)本来真的是有辅助软件(有人有挂)学习辅助!决战卡五星辅助(辅助挂)...
绝活辅助!边锋嘉兴麻将辅助器(... 绝活辅助!边锋嘉兴麻将辅助器(辅助挂)真是真的有辅助神器(新版有挂)1、边锋嘉兴麻将辅助器公共底牌简...
举措辅助!枫叶辅助器(辅助挂)... 举措辅助!枫叶辅助器(辅助挂)本来存在有辅助技巧(竟然有挂)1、下载好枫叶辅助器正确养号方法之后点击...
讲义辅助!点我达辅助(辅助挂)... 讲义辅助!点我达辅助(辅助挂)一直存在有辅助技巧(有人有挂)1、点我达辅助辅助器安装包、点我达辅助辅...
模块辅助!威信茶馆有挂的吗(辅... 模块辅助!威信茶馆有挂的吗(辅助挂)一直真的是有辅助脚本(揭秘有挂)1、玩家可以在威信茶馆有挂的吗线...