diff --git a/pkg/cli/job/resume.go b/pkg/cli/job/resume.go index 790e5e9dcf..9a0cb90286 100644 --- a/pkg/cli/job/resume.go +++ b/pkg/cli/job/resume.go @@ -54,7 +54,7 @@ func ResumeJob(ctx context.Context) error { return err } - return createJobCommand(ctx, config, + return util.CreateJobCommand(ctx, config, resumeJobFlags.Namespace, resumeJobFlags.JobName, v1alpha1.ResumeJobAction) } diff --git a/pkg/cli/job/run.go b/pkg/cli/job/run.go index 23d7084af5..2823479ad4 100644 --- a/pkg/cli/job/run.go +++ b/pkg/cli/job/run.go @@ -79,12 +79,12 @@ func RunJob(ctx context.Context) error { return err } - req, err := populateResourceListV1(launchJobFlags.Requests) + req, err := util.PopulateResourceListV1(launchJobFlags.Requests) if err != nil { return err } - limit, err := populateResourceListV1(launchJobFlags.Limits) + limit, err := util.PopulateResourceListV1(launchJobFlags.Limits) if err != nil { return err } diff --git a/pkg/cli/job/suspend.go b/pkg/cli/job/suspend.go index da1b11b63d..4d3daf2366 100644 --- a/pkg/cli/job/suspend.go +++ b/pkg/cli/job/suspend.go @@ -55,7 +55,7 @@ func SuspendJob(ctx context.Context) error { return err } - return createJobCommand(ctx, config, + return util.CreateJobCommand(ctx, config, suspendJobFlags.Namespace, suspendJobFlags.JobName, v1alpha1.AbortJobAction) } diff --git a/pkg/cli/job/util.go b/pkg/cli/job/util.go deleted file mode 100644 index 86d2e3fe12..0000000000 --- a/pkg/cli/job/util.go +++ /dev/null @@ -1,95 +0,0 @@ -/* -Copyright 2018 The Volcano Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package job - -import ( - "context" - "fmt" - "strings" - "time" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/resource" - "k8s.io/client-go/rest" - - vcbus "volcano.sh/apis/pkg/apis/bus/v1alpha1" - "volcano.sh/apis/pkg/apis/helpers" - "volcano.sh/apis/pkg/client/clientset/versioned" - "volcano.sh/volcano/pkg/cli/util" -) - -// populateResourceListV1 takes strings of form =,= -// and returns ResourceList. -func populateResourceListV1(spec string) (v1.ResourceList, error) { - // empty input gets a nil response to preserve generator test expected behaviors - if spec == "" { - return nil, nil - } - - result := v1.ResourceList{} - resourceStatements := strings.Split(spec, ",") - for _, resourceStatement := range resourceStatements { - parts := strings.Split(resourceStatement, "=") - if len(parts) != 2 { - return nil, fmt.Errorf("invalid argument syntax %v, expected =", resourceStatement) - } - resourceName := v1.ResourceName(parts[0]) - resourceQuantity, err := resource.ParseQuantity(parts[1]) - if err != nil { - return nil, err - } - result[resourceName] = resourceQuantity - } - return result, nil -} - -func createJobCommand(ctx context.Context, config *rest.Config, ns, name string, action vcbus.Action) error { - jobClient := versioned.NewForConfigOrDie(config) - job, err := jobClient.BatchV1alpha1().Jobs(ns).Get(context.TODO(), name, metav1.GetOptions{}) - if err != nil { - return err - } - - ctrlRef := metav1.NewControllerRef(job, helpers.JobKind) - cmd := &vcbus.Command{ - ObjectMeta: metav1.ObjectMeta{ - GenerateName: fmt.Sprintf("%s-%s-", - job.Name, strings.ToLower(string(action))), - Namespace: job.Namespace, - OwnerReferences: []metav1.OwnerReference{ - *ctrlRef, - }, - }, - TargetObject: ctrlRef, - Action: string(action), - } - - if _, err := jobClient.BusV1alpha1().Commands(ns).Create(ctx, cmd, metav1.CreateOptions{}); err != nil { - return err - } - - return nil -} - -func translateTimestampSince(timestamp metav1.Time) string { - if timestamp.IsZero() { - return "" - } - return util.HumanDuration(time.Since(timestamp.Time)) -} diff --git a/pkg/cli/job/util_test.go b/pkg/cli/job/util_test.go deleted file mode 100644 index 78aecff87a..0000000000 --- a/pkg/cli/job/util_test.go +++ /dev/null @@ -1,110 +0,0 @@ -/* -Copyright 2019 The Volcano Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package job - -import ( - "testing" - "volcano.sh/volcano/pkg/cli/util" - - "time" -) - -func TestJobUtil(t *testing.T) { - testCases := []struct { - Name string - Duration time.Duration - ExpectValue string - }{ - { - Name: "InvalidTime", - Duration: -time.Minute, - ExpectValue: "", - }, - { - Name: "SmallInvalieTime", - Duration: -time.Millisecond, - ExpectValue: "0s", - }, - { - Name: "NormalSeconds", - Duration: 62 * time.Second, - ExpectValue: "62s", - }, - { - Name: "NormalMinutes", - Duration: 180 * time.Second, - ExpectValue: "3m", - }, - { - Name: "NormalMinutesWithSecond", - Duration: 190 * time.Second, - ExpectValue: "3m10s", - }, - { - Name: "BiggerMinutesWithoutSecond", - Duration: 121*time.Minute + 56*time.Second, - ExpectValue: "121m", - }, - { - Name: "NormalHours", - Duration: 5*time.Hour + 9*time.Second, - ExpectValue: "5h", - }, - { - Name: "NormalHoursWithMinute", - Duration: 5*time.Hour + 7*time.Minute + 9*time.Second, - ExpectValue: "5h7m", - }, - { - Name: "BiggerHoursWithoutMinute", - Duration: 12*time.Hour + 7*time.Minute + 9*time.Second, - ExpectValue: "12h", - }, - { - Name: "NormalDays", - Duration: 5*24*time.Hour + 7*time.Minute + 9*time.Second, - ExpectValue: "5d", - }, - { - Name: "NormalDaysWithHours", - Duration: 5*24*time.Hour + 9*time.Hour, - ExpectValue: "5d9h", - }, - { - Name: "BiggerDayWithoutHours", - Duration: 531*24*time.Hour + 7*time.Minute + 9*time.Second, - ExpectValue: "531d", - }, - { - Name: "NormalYears", - Duration: (365*5+89)*24*time.Hour + 7*time.Minute + 9*time.Second, - ExpectValue: "5y89d", - }, - { - Name: "BiggerYears", - Duration: (365*12+15)*24*time.Hour + 7*time.Minute + 9*time.Second, - ExpectValue: "12y", - }, - } - - for i, testcase := range testCases { - answer := util.HumanDuration(testcase.Duration) - if answer != testcase.ExpectValue { - t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, answer) - } - } -} diff --git a/pkg/cli/job/view.go b/pkg/cli/job/view.go index 668d769726..8c2ad662d2 100644 --- a/pkg/cli/job/view.go +++ b/pkg/cli/job/view.go @@ -214,9 +214,9 @@ func PrintEvents(events []coreV1.Event, writer io.Writer) { for _, e := range events { var interval string if e.Count > 1 { - interval = fmt.Sprintf("%s (x%d over %s)", translateTimestampSince(e.LastTimestamp), e.Count, translateTimestampSince(e.FirstTimestamp)) + interval = fmt.Sprintf("%s (x%d over %s)", util.TranslateTimestampSince(e.LastTimestamp), e.Count, util.TranslateTimestampSince(e.FirstTimestamp)) } else { - interval = translateTimestampSince(e.FirstTimestamp) + interval = util.TranslateTimestampSince(e.FirstTimestamp) } EventSourceString := []string{e.Source.Component} if len(e.Source.Host) > 0 {