kubectl: added preemptionPolicy field when using kubectl get PriorityClass

This commit is contained in:
googs1025 2024-08-03 23:16:43 +08:00
parent dbc2b0a5c7
commit d28ee829f6
2 changed files with 49 additions and 6 deletions

View File

@ -494,6 +494,7 @@ func AddHandlers(h printers.PrintHandler) {
{Name: "Value", Type: "integer", Description: schedulingv1.PriorityClass{}.SwaggerDoc()["value"]},
{Name: "Global-Default", Type: "boolean", Description: schedulingv1.PriorityClass{}.SwaggerDoc()["globalDefault"]},
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
{Name: "PreemptionPolicy", Type: "string", Description: schedulingv1.PriorityClass{}.SwaggerDoc()["preemptionPolicy"]},
}
_ = h.TableHandler(priorityClassColumnDefinitions, printPriorityClass)
_ = h.TableHandler(priorityClassColumnDefinitions, printPriorityClassList)
@ -2747,7 +2748,13 @@ func printPriorityClass(obj *scheduling.PriorityClass, options printers.Generate
value := obj.Value
globalDefault := obj.GlobalDefault
row.Cells = append(row.Cells, name, int64(value), globalDefault, translateTimestampSince(obj.CreationTimestamp))
if options.Wide {
var preemptionPolicy string
if obj.PreemptionPolicy != nil {
preemptionPolicy = string(*obj.PreemptionPolicy)
}
row.Cells = append(row.Cells, preemptionPolicy)
}
return []metav1.TableRow{row}, nil
}

View File

@ -5757,35 +5757,71 @@ func TestPrintLease(t *testing.T) {
}
func TestPrintPriorityClass(t *testing.T) {
preemptNever := api.PreemptNever
preemptLowerPriority := api.PreemptLowerPriority
tests := []struct {
name string
options printers.GenerateOptions
pc scheduling.PriorityClass
expected []metav1.TableRow
}{
{
name: "Test case with PreemptNever policy",
pc: scheduling.PriorityClass{
ObjectMeta: metav1.ObjectMeta{
Name: "pc1",
CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
},
Value: 1,
Value: 1,
PreemptionPolicy: &preemptNever,
},
expected: []metav1.TableRow{{Cells: []interface{}{"pc1", int64(1), bool(false), "0s"}}},
},
{
name: "Test case with PreemptLowerPriority policy",
pc: scheduling.PriorityClass{
ObjectMeta: metav1.ObjectMeta{
Name: "pc2",
CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
},
Value: 1000000000,
GlobalDefault: true,
Value: 1000000000,
GlobalDefault: true,
PreemptionPolicy: &preemptLowerPriority,
},
expected: []metav1.TableRow{{Cells: []interface{}{"pc2", int64(1000000000), bool(true), "5m"}}},
},
{
name: "Test case with PreemptLowerPriority policy and wide output",
options: printers.GenerateOptions{Wide: true},
pc: scheduling.PriorityClass{
ObjectMeta: metav1.ObjectMeta{
Name: "pc2",
CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
},
Value: 1000000000,
GlobalDefault: true,
PreemptionPolicy: &preemptLowerPriority,
},
expected: []metav1.TableRow{{Cells: []interface{}{"pc2", int64(1000000000), bool(true), "5m", string(api.PreemptLowerPriority)}}},
},
{
name: "Test case without set PreemptLowerPriority policy",
options: printers.GenerateOptions{Wide: true},
pc: scheduling.PriorityClass{
ObjectMeta: metav1.ObjectMeta{
Name: "pc2",
CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)},
},
Value: 1000000000,
GlobalDefault: true,
PreemptionPolicy: nil,
},
expected: []metav1.TableRow{{Cells: []interface{}{"pc2", int64(1000000000), bool(true), "5m", string("")}}},
},
}
for i, test := range tests {
rows, err := printPriorityClass(&test.pc, printers.GenerateOptions{})
rows, err := printPriorityClass(&test.pc, test.options)
if err != nil {
t.Fatal(err)
}
@ -5793,7 +5829,7 @@ func TestPrintPriorityClass(t *testing.T) {
rows[i].Object.Object = nil
}
if !reflect.DeepEqual(test.expected, rows) {
t.Errorf("%d mismatch: %s", i, cmp.Diff(test.expected, rows))
t.Errorf("test case %s: %d mismatch: %s", test.name, i, cmp.Diff(test.expected, rows))
}
}
}