如何指定像+kubebuilder:printcolumn
这样的注释来将列添加到命令kubectl get my-crd.my-group.my-domain.com
的输出中
我有一个CRD (自定义资源定义),其中包含用于规范和状态的通常的struct
s (类似于Kubebuilder教程中解释的https://book.kubebuilder.io/cronjob-tutorial/new-api.html#adding-a-new-api)。
我有这样一个Status struct
:
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
.
+kubebuilder:printcolumn:name="Last Deploy",type=string,JSONPath=
.status.lastDeployTime在kubectl
.
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
kubectl
时,它们不是带有格式问题的“简单类型”,这是否意味着我唯一的选择是使用某种fmt.Sprintf(...)
string
?发布于 2022-01-08 13:07:39
解决方案是添加代码以更新控制器-的- Reconcile(ctx context.Context, req ctrl.Request)
中的资源状态,如下所示:
// 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的特殊注释没有问题:
//+kubebuilder:printcolumn:name="Last Deploy",type="date",JSONPath=`.status.lastDeployTime`
此外,Go切片和Go地图都是从框中提取的,注释如下:
...
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`
https://stackoverflow.com/questions/70517823
复制相似问题