首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何打印Kubebuilder中定义的状态字段以便在使用Kubectl时显示

如何打印Kubebuilder中定义的状态字段以便在使用Kubectl时显示
EN

Stack Overflow用户
提问于 2021-12-29 09:59:54
回答 1查看 1K关注 0票数 1

如何指定像+kubebuilder:printcolumn这样的注释来将列添加到命令kubectl get my-crd.my-group.my-domain.com的输出中

我有一个CRD (自定义资源定义),其中包含用于规范和状态的通常的structs (类似于Kubebuilder教程中解释的https://book.kubebuilder.io/cronjob-tutorial/new-api.html#adding-a-new-api)。

我有这样一个Status struct

代码语言:javascript
运行
复制
type ScheduleSetStatus struct {
    // When was the last time the Schedule Set
    // was successfully deployed.
    LastDeployTime string `json:"lastDeployTime"` // metav1.Time
    // The CronJobs that have been successfully deployed
    DeployedCronJobs []string `json:"deployedCronJobs"`
    // The CronJobs that had errors when the deployment
    // has been attempted.
    ErroredCronJobs map[string]string `json:"erroredCronJobs"` // TODO `error` JSON serialisable
}

它有几个问题:

时间场

我曾尝试过将该类型设置为metav1.Time (在kubectl.

  • So的输出中显示为空,我将类型更改为string (然后在执行oess.Status.LastDeployTime = fmt.Sprintf("%s", metav1.Time{Time: time.Now().UTC()})的控制器中),然后添加注释+kubebuilder:printcolumn:name="Last Deploy",type=string,JSONPath=.status.lastDeployTime,但该字段在kubectl.

的输出中仍然显示为空。

切片字段[]string 和映射字段 map[string]string

  • 如何配置这些?这里没有提到(当单击“显示详细参数帮助”时):https://book.kubebuilder.io/reference/markers/crd.html
  • In大小写--在使用kubectl时,它们不是带有格式问题的“简单类型”,这是否意味着我唯一的选择是使用某种fmt.Sprintf(...)
  • Any其他选项使它们成为string
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-08 13:07:39

解决方案是添加代码以更新控制器-- Reconcile(ctx context.Context, req ctrl.Request)中的资源状态,如下所示:

代码语言:javascript
运行
复制
    // Update the status for "last deploy time" of a ScheduleSet
    myStruct.Status.LastDeployTime = metav1.Time{Time: time.Now().UTC()} // https://book.kubebuilder.io/cronjob-tutorial/api-design.html?highlight=metav1.Time#designing-an-api
    if err := r.Status().Update(ctx, &myStruct); err != nil {
        log.Error(err, "unable to update status xyz")
        return ctrl.Result{}, err
    }

Kubebuilder的特殊注释没有问题:

代码语言:javascript
运行
复制
//+kubebuilder:printcolumn:name="Last Deploy",type="date",JSONPath=`.status.lastDeployTime`

此外,Go切片和Go地图都是从框中提取的,注释如下:

代码语言:javascript
运行
复制
...

    DeployedCronJobs []string `json:"deployedCronJobs"`

...

    ErroredCronJobs map[string]string `json:"erroredCronJobs"`
...

//+kubebuilder:printcolumn:name="Deployed CJs",type=string,JSONPath=`.status.deployedCronJobs`
//+kubebuilder:printcolumn:name="Errored CJs",type=string,JSONPath=`.status.erroredCronJobs`
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70517823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档