Skip to content

Commit

Permalink
Merge pull request #3585 from lx1036/feature/use-slices-pkg
Browse files Browse the repository at this point in the history
fix: use built-in slices package from go1.21 for simplify code
  • Loading branch information
volcano-sh-bot authored Jul 15, 2024
2 parents dc0aa64 + 29e796e commit 7a4bc9a
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 18 deletions.
5 changes: 3 additions & 2 deletions pkg/controllers/podgroup/pg_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package podgroup

import (
"slices"

"k8s.io/apimachinery/pkg/util/wait"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/informers"
Expand All @@ -35,7 +37,6 @@ import (
schedulinglister "volcano.sh/apis/pkg/client/listers/scheduling/v1beta1"
"volcano.sh/volcano/pkg/controllers/framework"
"volcano.sh/volcano/pkg/features"
commonutil "volcano.sh/volcano/pkg/util"
)

func init() {
Expand Down Expand Up @@ -160,7 +161,7 @@ func (pg *pgcontroller) processNextReq() bool {
return true
}

if !commonutil.Contains(pg.schedulerNames, pod.Spec.SchedulerName) {
if !slices.Contains(pg.schedulerNames, pod.Spec.SchedulerName) {
klog.V(5).Infof("pod %v/%v field SchedulerName is not matched", pod.Namespace, pod.Name)
return true
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/scheduler/cache/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"math"
"slices"
"strconv"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -47,7 +48,6 @@ import (

schedulingapi "volcano.sh/volcano/pkg/scheduler/api"
"volcano.sh/volcano/pkg/scheduler/metrics"
commonutil "volcano.sh/volcano/pkg/util"
)

var DefaultAttachableVolumeQuantity int64 = math.MaxInt32
Expand All @@ -60,7 +60,7 @@ func isTerminated(status schedulingapi.TaskStatus) bool {
// pi.Pod.Spec.SchedulerName is same as volcano scheduler's name, otherwise it will return nil.
func (sc *SchedulerCache) getOrCreateJob(pi *schedulingapi.TaskInfo) *schedulingapi.JobInfo {
if len(pi.Job) == 0 {
if !commonutil.Contains(sc.schedulerNames, pi.Pod.Spec.SchedulerName) {
if !slices.Contains(sc.schedulerNames, pi.Pod.Spec.SchedulerName) {
klog.V(4).Infof("Pod %s/%s will not scheduled by %#v, skip creating PodGroup and Job for it",
pi.Pod.Namespace, pi.Pod.Name, sc.schedulerNames)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/scheduler/cache/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cache
import (
"fmt"
"os"
"slices"
"strconv"
"strings"

Expand All @@ -27,14 +28,13 @@ import (
"stathat.com/c/consistent"

scheduling "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
commonutil "volcano.sh/volcano/pkg/util"
)

// responsibleForPod returns false at following conditions:
// 1. The current scheduler is not specified scheduler in Pod's spec.
// 2. The Job which the Pod belongs is not assigned to current scheduler based on the hash algorithm in multi-schedulers scenario
func responsibleForPod(pod *v1.Pod, schedulerNames []string, mySchedulerPodName string, c *consistent.Consistent) bool {
if !commonutil.Contains(schedulerNames, pod.Spec.SchedulerName) {
if !slices.Contains(schedulerNames, pod.Spec.SchedulerName) {
return false
}
if c != nil {
Expand Down
10 changes: 0 additions & 10 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ var (
defaultElectionRetryPeriod = metav1.Duration{Duration: 2 * time.Second}
)

// Contains check if slice contains element
func Contains(slice []string, element string) bool {
for _, item := range slice {
if item == element {
return true
}
}
return false
}

// GenerateComponentName generate component name volcano
func GenerateComponentName(schedulerNames []string) string {
if len(schedulerNames) == 1 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhooks/admission/pods/validate/admit_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package validate
import (
"context"
"fmt"
"slices"
"strconv"
"strings"

Expand All @@ -32,7 +33,6 @@ import (

"volcano.sh/apis/pkg/apis/helpers"
vcv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
commonutil "volcano.sh/volcano/pkg/util"
"volcano.sh/volcano/pkg/webhooks/router"
"volcano.sh/volcano/pkg/webhooks/schema"
"volcano.sh/volcano/pkg/webhooks/util"
Expand Down Expand Up @@ -101,7 +101,7 @@ allow pods to create when
3. check pod budget annotations configure
*/
func validatePod(pod *v1.Pod, reviewResponse *admissionv1.AdmissionResponse) string {
if !commonutil.Contains(config.SchedulerNames, pod.Spec.SchedulerName) {
if !slices.Contains(config.SchedulerNames, pod.Spec.SchedulerName) {
return ""
}
pgName := ""
Expand Down

0 comments on commit 7a4bc9a

Please sign in to comment.