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实例。

相关内容

热门资讯

第1分钟了解!浙江同城游辅助器... 第1分钟了解!浙江同城游辅助器!原来一直总是有辅助教程(证实有挂)-哔哩哔哩1、第1分钟了解!浙江同...
迎来新发展!智星菠萝透视(透视... 迎来新发展!智星菠萝透视(透视)辅助修改器(确实有挂)-哔哩哔哩1、智星菠萝透视破解器简单,智星菠萝...
第5分钟了解!都莱大菠萝辅助器... 第5分钟了解!都莱大菠萝辅助器,闲娱江西辅助器,指引教程(有挂透视)-哔哩哔哩运都莱大菠萝辅助器辅助...
十分钟了解!湖北卡五星技能!其... 十分钟了解!湖北卡五星技能!其实存在有辅助插件(真是有挂)-哔哩哔哩湖北卡五星技能破解侠是真的助透视...
透视玄学!佛手大菠萝有挂吗(透... 透视玄学!佛手大菠萝有挂吗(透视)辅助软件(有挂存在)-哔哩哔哩1、佛手大菠萝有挂吗免费辅助多个强度...
九分钟了解!乐酷辅助,同乡游有... 您好,同乡游有辅助软件码这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多...
9分钟了解!途游休闲铺破解版!... 9分钟了解!途游休闲铺破解版!切实有辅助方法(新版有挂)-哔哩哔哩1、每一步都需要思考,不同水平的挑...
事发当天!wepoker透视脚... 事发当天!wepoker透视脚本免费app(透视)辅助安装(揭秘有挂)-哔哩哔哩1、在wepoker...
2分钟了解!天天福建十三兵修改... 2分钟了解!天天福建十三兵修改器,新老夫子免费辅助,总结教程(真是有挂)-哔哩哔哩1、很好的工具软件...
日前!wepoker养号规律(... 日前!wepoker养号规律(透视)辅助工具(竟然有挂)-哔哩哔哩1、很好的工具软件,可以解锁游戏的...