gpt4 book ai didi

kubernetes - 如何使用分数格式(即 X/Y)为自定义资源显示 kubectl 列

转载 作者:行者123 更新时间:2023-12-05 02:01:07 27 4
gpt4 key购买 nike

在 Kubernetes 中,是否可以使用 CRD 的“additionalPrinterColumns”字段使用分数格式(即 X/Y)显示列?

更准确地说,我希望 kubectl 使用与下面的 READY 字段相同的格式显示 CR 字段的描述:

kubectl get statefulsets.apps <my-statefulset>
NAME READY AGE
<my-statefulset> 2/2 18m

能否提供“additionalPrinterColumns”部分的内容?

最佳答案

不幸的是,additionalPrinterColumns 仅支持 Simple JsonPath,因此我们不能在 JsonPath 中使用 .status.readyReplicas/.status.replicas,并且 GET 操作由 kube-apiserver 处理,因此 Operator 不参与 GET 操作。因此,我建议的最简单方法是在 .status 中创建一个名为 ready 的新字段,其值为“readyReplicas/replicas”,并在每次更新 readyReplicas 和副本时更新它。

然后放在additionalPrinterColumns下面

   additionalPrinterColumns:
- jsonPath: .status.ready
name: Ready
type: string

对于 StatefulSet,他们在 https://github.com/kubernetes/kubernetes/blob/master/pkg/printers/internalversion/printers.go 中使用以下代码打印 READY 列的值

    statefulSetColumnDefinitions := []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
{Name: "Ready", Type: "string", Description: "Number of the pod with ready state"},
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
{Name: "Containers", Type: "string", Priority: 1, Description: "Names of each container in the template."},
{Name: "Images", Type: "string", Priority: 1, Description: "Images referenced by each container in the template."},
}
h.TableHandler(statefulSetColumnDefinitions, printStatefulSet)
h.TableHandler(statefulSetColumnDefinitions, printStatefulSetList)


func printStatefulSet(obj *apps.StatefulSet, options printers.GenerateOptions) ([]metav1.TableRow, error) {
row := metav1.TableRow{
Object: runtime.RawExtension{Object: obj},
}
desiredReplicas := obj.Spec.Replicas
readyReplicas := obj.Status.ReadyReplicas
createTime := translateTimestampSince(obj.CreationTimestamp)
row.Cells = append(row.Cells, obj.Name, fmt.Sprintf("%d/%d", int64(readyReplicas), int64(desiredReplicas)), createTime)
if options.Wide {
names, images := layoutContainerCells(obj.Spec.Template.Spec.Containers)
row.Cells = append(row.Cells, names, images)
}
return []metav1.TableRow{row}, nil
}

func printStatefulSetList(list *apps.StatefulSetList, options printers.GenerateOptions) ([]metav1.TableRow, error) {
rows := make([]metav1.TableRow, 0, len(list.Items))
for i := range list.Items {
r, err := printStatefulSet(&list.Items[i], options)
if err != nil {
return nil, err
}
rows = append(rows, r...)
}
return rows, nil
}

关于kubernetes - 如何使用分数格式(即 X/Y)为自定义资源显示 kubectl 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66754354/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com