volcano.sh/volcano@v1.9.0/pkg/scheduler/cache/util.go (about)

     1  /*
     2  Copyright 2021 The Volcano Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cache
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"strconv"
    23  	"strings"
    24  
    25  	v1 "k8s.io/api/core/v1"
    26  	"k8s.io/klog/v2"
    27  	"stathat.com/c/consistent"
    28  
    29  	scheduling "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    30  	commonutil "volcano.sh/volcano/pkg/util"
    31  )
    32  
    33  // responsibleForPod returns false at following conditions:
    34  // 1. The current scheduler is not specified scheduler in Pod's spec.
    35  // 2. The Job which the Pod belongs is not assigned to current scheduler based on the hash algorithm in multi-schedulers scenario
    36  func responsibleForPod(pod *v1.Pod, schedulerNames []string, mySchedulerPodName string, c *consistent.Consistent) bool {
    37  	if !commonutil.Contains(schedulerNames, pod.Spec.SchedulerName) {
    38  		return false
    39  	}
    40  	if c != nil {
    41  		var key string
    42  		if len(pod.OwnerReferences) != 0 {
    43  			key = pod.OwnerReferences[0].Name
    44  		} else {
    45  			key = pod.Name
    46  		}
    47  		schedulerPodName, err := c.Get(key)
    48  		if err != nil {
    49  			klog.Errorf("Failed to get scheduler by hash algorithm, err: %v", err)
    50  		}
    51  		if schedulerPodName != mySchedulerPodName {
    52  			return false
    53  		}
    54  	}
    55  
    56  	klog.V(4).Infof("schedulerPodName %v is responsible to Pod %v/%v", mySchedulerPodName, pod.Namespace, pod.Name)
    57  	return true
    58  }
    59  
    60  // responsibleForNode returns true if the Node is assigned to current scheduler in multi-scheduler scenario
    61  func responsibleForNode(nodeName string, mySchedulerPodName string, c *consistent.Consistent) bool {
    62  	if c != nil {
    63  		schedulerPodName, err := c.Get(nodeName)
    64  		if err != nil {
    65  			klog.Errorf("Failed to get scheduler by hash algorithm, err: %v", err)
    66  		}
    67  		if schedulerPodName != mySchedulerPodName {
    68  			return false
    69  		}
    70  	}
    71  
    72  	klog.V(4).Infof("schedulerPodName %v is responsible to Node %v", mySchedulerPodName, nodeName)
    73  	return true
    74  }
    75  
    76  // responsibleForPodGroup returns true if Job which PodGroup belongs is assigned to current scheduler in multi-schedulers scenario
    77  func responsibleForPodGroup(pg *scheduling.PodGroup, mySchedulerPodName string, c *consistent.Consistent) bool {
    78  	if c != nil {
    79  		var key string
    80  		if len(pg.OwnerReferences) != 0 {
    81  			key = pg.OwnerReferences[0].Name
    82  		} else {
    83  			key = pg.Name
    84  		}
    85  		schedulerPodName, err := c.Get(key)
    86  		if err != nil {
    87  			klog.Errorf("Failed to get scheduler by hash algorithm, err: %v", err)
    88  		}
    89  		if schedulerPodName != mySchedulerPodName {
    90  			return false
    91  		}
    92  	}
    93  
    94  	klog.V(4).Infof("schedulerPodName %v is responsible to PodGroup %v/%v", mySchedulerPodName, pg.Namespace, pg.Name)
    95  	return true
    96  }
    97  
    98  // getMultiSchedulerInfo return the Pod name of current scheduler and the hash table for all schedulers
    99  func getMultiSchedulerInfo() (schedulerPodName string, c *consistent.Consistent) {
   100  	multiSchedulerEnable := os.Getenv("MULTI_SCHEDULER_ENABLE")
   101  	mySchedulerPodName := os.Getenv("SCHEDULER_POD_NAME")
   102  	c = nil
   103  	if multiSchedulerEnable == "true" {
   104  		klog.V(3).Infof("multiSchedulerEnable true")
   105  		schedulerNumStr := os.Getenv("SCHEDULER_NUM")
   106  		schedulerNum, err := strconv.Atoi(schedulerNumStr)
   107  		if err != nil {
   108  			schedulerNum = 1
   109  		}
   110  		index := strings.LastIndex(mySchedulerPodName, "-")
   111  		baseName := mySchedulerPodName[0:index]
   112  		c = consistent.New()
   113  		for i := 0; i < schedulerNum; i++ {
   114  			name := fmt.Sprintf("%s-%d", baseName, i)
   115  			c.Add(name)
   116  		}
   117  	}
   118  	return mySchedulerPodName, c
   119  }