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 的输出显示 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

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

相关内容

热门资讯

最新技巧!!wepoker有没... 很多人使用云端 AI 时,都担心wepoker有没有辅助、内部资料上传带来隐私风险。这款本地运行的 ...
一秒答解!wepoker怎么增... 您好,一秒答解!wepoker怎么增加运气,WPK有挂吗确实有挂下载(最初有辅助挂)这款游戏可以开挂...
一秒答解!wpk辅助器,德扑之... wpk辅助器AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包,断网...
分辨真假!德普之星透视辅助,w... 分辨真假!德普之星透视辅助,wepoker开挂透视教程脚本(总是是真的有辅助挂)AI 是一款可以让一...
每日必看推荐!wepoker怎... 您好:wepoker怎么提高运气这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
推荐一款!!wepoker黑侠... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
一秒答解!wpk俱乐部辅助器,... 一秒答解!wpk俱乐部辅助器,德扑之星开挂脚本助手(固有有辅助器)AI 是一款可以让一直输的玩家修复...
玩家攻略!wepoker好友房... 您好,玩家攻略!wepoker好友房开挂,WPK哪里有挂神器(一贯有开挂)这款游戏可以开挂的,确实是...
重大通报!!智星德州可以透视吗... 很多人使用云端 AI 时,都担心智星德州可以透视吗、内部资料上传带来隐私风险。这款本地运行的 AI ...
玩家爆料!智星菠萝有挂吗,WP... 智星菠萝有挂吗AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包,断...