volcano.sh/volcano@v1.9.0/pkg/controllers/queue/queue_controller_handler.go (about)

     1  /*
     2  Copyright 2019 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 queue
    18  
    19  import (
    20  	"k8s.io/client-go/tools/cache"
    21  	"k8s.io/klog/v2"
    22  
    23  	busv1alpha1 "volcano.sh/apis/pkg/apis/bus/v1alpha1"
    24  	schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    25  	"volcano.sh/volcano/pkg/controllers/apis"
    26  )
    27  
    28  func (c *queuecontroller) enqueue(req *apis.Request) {
    29  	c.queue.Add(req)
    30  }
    31  
    32  func (c *queuecontroller) addQueue(obj interface{}) {
    33  	queue := obj.(*schedulingv1beta1.Queue)
    34  
    35  	req := &apis.Request{
    36  		QueueName: queue.Name,
    37  
    38  		Event:  busv1alpha1.OutOfSyncEvent,
    39  		Action: busv1alpha1.SyncQueueAction,
    40  	}
    41  
    42  	c.enqueue(req)
    43  }
    44  
    45  func (c *queuecontroller) deleteQueue(obj interface{}) {
    46  	queue, ok := obj.(*schedulingv1beta1.Queue)
    47  	if !ok {
    48  		tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
    49  		if !ok {
    50  			klog.Errorf("Couldn't get object from tombstone %#v.", obj)
    51  			return
    52  		}
    53  		queue, ok = tombstone.Obj.(*schedulingv1beta1.Queue)
    54  		if !ok {
    55  			klog.Errorf("Tombstone contained object that is not a Queue: %#v.", obj)
    56  			return
    57  		}
    58  	}
    59  
    60  	c.pgMutex.Lock()
    61  	defer c.pgMutex.Unlock()
    62  	delete(c.podGroups, queue.Name)
    63  }
    64  
    65  func (c *queuecontroller) updateQueue(_, _ interface{}) {
    66  	// currently do not care about queue update
    67  }
    68  
    69  func (c *queuecontroller) addPodGroup(obj interface{}) {
    70  	pg := obj.(*schedulingv1beta1.PodGroup)
    71  	key, _ := cache.MetaNamespaceKeyFunc(obj)
    72  
    73  	c.pgMutex.Lock()
    74  	defer c.pgMutex.Unlock()
    75  
    76  	if c.podGroups[pg.Spec.Queue] == nil {
    77  		c.podGroups[pg.Spec.Queue] = make(map[string]struct{})
    78  	}
    79  	c.podGroups[pg.Spec.Queue][key] = struct{}{}
    80  
    81  	req := &apis.Request{
    82  		QueueName: pg.Spec.Queue,
    83  
    84  		Event:  busv1alpha1.OutOfSyncEvent,
    85  		Action: busv1alpha1.SyncQueueAction,
    86  	}
    87  
    88  	c.enqueue(req)
    89  }
    90  
    91  func (c *queuecontroller) updatePodGroup(old, new interface{}) {
    92  	oldPG := old.(*schedulingv1beta1.PodGroup)
    93  	newPG := new.(*schedulingv1beta1.PodGroup)
    94  
    95  	// Note: we have no use case update PodGroup.Spec.Queue
    96  	// So do not consider it here.
    97  	if oldPG.Status.Phase != newPG.Status.Phase {
    98  		c.addPodGroup(newPG)
    99  	}
   100  }
   101  
   102  func (c *queuecontroller) deletePodGroup(obj interface{}) {
   103  	pg, ok := obj.(*schedulingv1beta1.PodGroup)
   104  	if !ok {
   105  		tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
   106  		if !ok {
   107  			klog.Errorf("Couldn't get object from tombstone %#v.", obj)
   108  			return
   109  		}
   110  		pg, ok = tombstone.Obj.(*schedulingv1beta1.PodGroup)
   111  		if !ok {
   112  			klog.Errorf("Tombstone contained object that is not a PodGroup: %#v.", obj)
   113  			return
   114  		}
   115  	}
   116  
   117  	key, _ := cache.MetaNamespaceKeyFunc(obj)
   118  
   119  	c.pgMutex.Lock()
   120  	defer c.pgMutex.Unlock()
   121  
   122  	delete(c.podGroups[pg.Spec.Queue], key)
   123  
   124  	req := &apis.Request{
   125  		QueueName: pg.Spec.Queue,
   126  
   127  		Event:  busv1alpha1.OutOfSyncEvent,
   128  		Action: busv1alpha1.SyncQueueAction,
   129  	}
   130  
   131  	c.enqueue(req)
   132  }
   133  
   134  func (c *queuecontroller) addCommand(obj interface{}) {
   135  	cmd, ok := obj.(*busv1alpha1.Command)
   136  	if !ok {
   137  		klog.Errorf("Obj %v is not command.", obj)
   138  		return
   139  	}
   140  
   141  	c.commandQueue.Add(cmd)
   142  }
   143  
   144  func (c *queuecontroller) getPodGroups(key string) []string {
   145  	c.pgMutex.RLock()
   146  	defer c.pgMutex.RUnlock()
   147  
   148  	if c.podGroups[key] == nil {
   149  		return nil
   150  	}
   151  	podGroups := make([]string, 0, len(c.podGroups[key]))
   152  	for pgKey := range c.podGroups[key] {
   153  		podGroups = append(podGroups, pgKey)
   154  	}
   155  
   156  	return podGroups
   157  }
   158  
   159  func (c *queuecontroller) recordEventsForQueue(name, eventType, reason, message string) {
   160  	queue, err := c.queueLister.Get(name)
   161  	if err != nil {
   162  		klog.Errorf("Get queue %s failed for %v.", name, err)
   163  		return
   164  	}
   165  
   166  	c.recorder.Event(queue, eventType, reason, message)
   167  }