Add timezone column in cronjob get command

This commit is contained in:
Arda Güçlü 2023-12-08 10:06:39 +03:00
parent 89dfbebe2e
commit 7897709ed0
2 changed files with 56 additions and 7 deletions

View File

@ -180,6 +180,7 @@ func AddHandlers(h printers.PrintHandler) {
cronJobColumnDefinitions := []metav1.TableColumnDefinition{ cronJobColumnDefinitions := []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]}, {Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
{Name: "Schedule", Type: "string", Description: batchv1beta1.CronJobSpec{}.SwaggerDoc()["schedule"]}, {Name: "Schedule", Type: "string", Description: batchv1beta1.CronJobSpec{}.SwaggerDoc()["schedule"]},
{Name: "Timezone", Type: "string", Description: batchv1beta1.CronJobSpec{}.SwaggerDoc()["timeZone"]},
{Name: "Suspend", Type: "boolean", Description: batchv1beta1.CronJobSpec{}.SwaggerDoc()["suspend"]}, {Name: "Suspend", Type: "boolean", Description: batchv1beta1.CronJobSpec{}.SwaggerDoc()["suspend"]},
{Name: "Active", Type: "integer", Description: batchv1beta1.CronJobStatus{}.SwaggerDoc()["active"]}, {Name: "Active", Type: "integer", Description: batchv1beta1.CronJobStatus{}.SwaggerDoc()["active"]},
{Name: "Last Schedule", Type: "string", Description: batchv1beta1.CronJobStatus{}.SwaggerDoc()["lastScheduleTime"]}, {Name: "Last Schedule", Type: "string", Description: batchv1beta1.CronJobStatus{}.SwaggerDoc()["lastScheduleTime"]},
@ -1185,7 +1186,12 @@ func printCronJob(obj *batch.CronJob, options printers.GenerateOptions) ([]metav
lastScheduleTime = translateTimestampSince(*obj.Status.LastScheduleTime) lastScheduleTime = translateTimestampSince(*obj.Status.LastScheduleTime)
} }
row.Cells = append(row.Cells, obj.Name, obj.Spec.Schedule, printBoolPtr(obj.Spec.Suspend), int64(len(obj.Status.Active)), lastScheduleTime, translateTimestampSince(obj.CreationTimestamp)) timeZone := "<none>"
if obj.Spec.TimeZone != nil {
timeZone = *obj.Spec.TimeZone
}
row.Cells = append(row.Cells, obj.Name, obj.Spec.Schedule, timeZone, printBoolPtr(obj.Spec.Suspend), int64(len(obj.Status.Active)), lastScheduleTime, translateTimestampSince(obj.CreationTimestamp))
if options.Wide { if options.Wide {
names, images := layoutContainerCells(obj.Spec.JobTemplate.Spec.Template.Spec.Containers) names, images := layoutContainerCells(obj.Spec.JobTemplate.Spec.Template.Spec.Containers)
row.Cells = append(row.Cells, names, images, metav1.FormatLabelSelector(obj.Spec.JobTemplate.Spec.Selector)) row.Cells = append(row.Cells, names, images, metav1.FormatLabelSelector(obj.Spec.JobTemplate.Spec.Selector))

View File

@ -5089,6 +5089,7 @@ func TestPrintComponentStatus(t *testing.T) {
} }
func TestPrintCronJob(t *testing.T) { func TestPrintCronJob(t *testing.T) {
timeZone := "LOCAL"
completions := int32(2) completions := int32(2)
suspend := false suspend := false
tests := []struct { tests := []struct {
@ -5133,7 +5134,47 @@ func TestPrintCronJob(t *testing.T) {
}, },
options: printers.GenerateOptions{}, options: printers.GenerateOptions{},
// Columns: Name, Schedule, Suspend, Active, Last Schedule, Age // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
expected: []metav1.TableRow{{Cells: []interface{}{"cronjob1", "0/5 * * * ?", "False", int64(0), "0s", "0s"}}}, expected: []metav1.TableRow{{Cells: []interface{}{"cronjob1", "0/5 * * * ?", "<none>", "False", int64(0), "0s", "0s"}}},
},
// Basic cron job; does not print containers, images, or labels.
{
cronjob: batch.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: "cronjob1",
CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
},
Spec: batch.CronJobSpec{
Schedule: "0/5 * * * ?",
TimeZone: &timeZone,
Suspend: &suspend,
JobTemplate: batch.JobTemplateSpec{
Spec: batch.JobSpec{
Completions: &completions,
Template: api.PodTemplateSpec{
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "fake-job-container1",
Image: "fake-job-image1",
},
{
Name: "fake-job-container2",
Image: "fake-job-image2",
},
},
},
},
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
},
},
},
Status: batch.CronJobStatus{
LastScheduleTime: &metav1.Time{Time: time.Now().Add(1.9e9)},
},
},
options: printers.GenerateOptions{},
// Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
expected: []metav1.TableRow{{Cells: []interface{}{"cronjob1", "0/5 * * * ?", "LOCAL", "False", int64(0), "0s", "0s"}}},
}, },
// Generate options: Wide; prints containers, images, and labels. // Generate options: Wide; prints containers, images, and labels.
{ {
@ -5172,7 +5213,7 @@ func TestPrintCronJob(t *testing.T) {
}, },
options: printers.GenerateOptions{Wide: true}, options: printers.GenerateOptions{Wide: true},
// Columns: Name, Schedule, Suspend, Active, Last Schedule, Age // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
expected: []metav1.TableRow{{Cells: []interface{}{"cronjob1", "0/5 * * * ?", "False", int64(0), "0s", "0s", "fake-job-container1,fake-job-container2", "fake-job-image1,fake-job-image2", "a=b"}}}, expected: []metav1.TableRow{{Cells: []interface{}{"cronjob1", "0/5 * * * ?", "<none>", "False", int64(0), "0s", "0s", "fake-job-container1,fake-job-container2", "fake-job-image1,fake-job-image2", "a=b"}}},
}, },
// CronJob with Last Schedule and Age // CronJob with Last Schedule and Age
{ {
@ -5191,7 +5232,7 @@ func TestPrintCronJob(t *testing.T) {
}, },
options: printers.GenerateOptions{}, options: printers.GenerateOptions{},
// Columns: Name, Schedule, Suspend, Active, Last Schedule, Age // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
expected: []metav1.TableRow{{Cells: []interface{}{"cronjob2", "0/5 * * * ?", "False", int64(0), "30s", "5m"}}}, expected: []metav1.TableRow{{Cells: []interface{}{"cronjob2", "0/5 * * * ?", "<none>", "False", int64(0), "30s", "5m"}}},
}, },
// CronJob without Last Schedule // CronJob without Last Schedule
{ {
@ -5208,7 +5249,7 @@ func TestPrintCronJob(t *testing.T) {
}, },
options: printers.GenerateOptions{}, options: printers.GenerateOptions{},
// Columns: Name, Schedule, Suspend, Active, Last Schedule, Age // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
expected: []metav1.TableRow{{Cells: []interface{}{"cronjob3", "0/5 * * * ?", "False", int64(0), "<none>", "5m"}}}, expected: []metav1.TableRow{{Cells: []interface{}{"cronjob3", "0/5 * * * ?", "<none>", "False", int64(0), "<none>", "5m"}}},
}, },
} }
@ -5227,6 +5268,7 @@ func TestPrintCronJob(t *testing.T) {
} }
func TestPrintCronJobList(t *testing.T) { func TestPrintCronJobList(t *testing.T) {
timeZone := "LOCAL"
completions := int32(2) completions := int32(2)
suspend := false suspend := false
@ -5239,6 +5281,7 @@ func TestPrintCronJobList(t *testing.T) {
}, },
Spec: batch.CronJobSpec{ Spec: batch.CronJobSpec{
Schedule: "0/5 * * * ?", Schedule: "0/5 * * * ?",
TimeZone: &timeZone,
Suspend: &suspend, Suspend: &suspend,
JobTemplate: batch.JobTemplateSpec{ JobTemplate: batch.JobTemplateSpec{
Spec: batch.JobSpec{ Spec: batch.JobSpec{
@ -5275,8 +5318,8 @@ func TestPrintCronJobList(t *testing.T) {
// Columns: Name, Schedule, Suspend, Active, Last Schedule, Age // Columns: Name, Schedule, Suspend, Active, Last Schedule, Age
expectedRows := []metav1.TableRow{ expectedRows := []metav1.TableRow{
{Cells: []interface{}{"cronjob1", "0/5 * * * ?", "False", int64(0), "0s", "0s"}}, {Cells: []interface{}{"cronjob1", "0/5 * * * ?", "LOCAL", "False", int64(0), "0s", "0s"}},
{Cells: []interface{}{"cronjob2", "4/5 1 1 1 ?", "False", int64(0), "20m", "0s"}}, {Cells: []interface{}{"cronjob2", "4/5 1 1 1 ?", "<none>", "False", int64(0), "20m", "0s"}},
} }
rows, err := printCronJobList(&cronJobList, printers.GenerateOptions{}) rows, err := printCronJobList(&cronJobList, printers.GenerateOptions{})