|
| 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 job |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + "golang.org/x/time/rate" |
| 24 | + |
| 25 | + "github.com/golang/glog" |
| 26 | + |
| 27 | + "k8s.io/api/core/v1" |
| 28 | + "k8s.io/apimachinery/pkg/api/errors" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/client-go/util/workqueue" |
| 31 | +) |
| 32 | + |
| 33 | +func newRateLimitingQueue() workqueue.RateLimitingInterface { |
| 34 | + return workqueue.NewRateLimitingQueue(workqueue.NewMaxOfRateLimiter( |
| 35 | + workqueue.NewItemExponentialFailureRateLimiter(5*time.Millisecond, 180*time.Second), |
| 36 | + // 10 qps, 100 bucket size. This is only for retry speed and its only the overall factor (not per item) |
| 37 | + &workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(rate.Limit(10), 100)}, |
| 38 | + )) |
| 39 | +} |
| 40 | + |
| 41 | +func (cc *Controller) processResyncTask() { |
| 42 | + obj, shutdown := cc.errTasks.Get() |
| 43 | + if shutdown { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + // one task only resync 10 times |
| 48 | + if cc.errTasks.NumRequeues(obj) > 10 { |
| 49 | + cc.errTasks.Forget(obj) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + defer cc.errTasks.Done(obj) |
| 54 | + |
| 55 | + task, ok := obj.(*v1.Pod) |
| 56 | + if !ok { |
| 57 | + glog.Errorf("failed to convert %v to *v1.Pod", obj) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + if err := cc.syncTask(task); err != nil { |
| 62 | + glog.Errorf("Failed to sync pod <%v/%v>, retry it, err %v", task.Namespace, task.Name, err) |
| 63 | + cc.resyncTask(task) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func (cc *Controller) syncTask(oldTask *v1.Pod) error { |
| 68 | + cc.Mutex.Lock() |
| 69 | + defer cc.Mutex.Unlock() |
| 70 | + |
| 71 | + newPod, err := cc.kubeClients.CoreV1().Pods(oldTask.Namespace).Get(oldTask.Name, metav1.GetOptions{}) |
| 72 | + if err != nil { |
| 73 | + if errors.IsNotFound(err) { |
| 74 | + if err := cc.cache.DeletePod(oldTask); err != nil { |
| 75 | + glog.Errorf("failed to delete cache pod <%v/%v>, err %v.", oldTask.Namespace, oldTask.Name, err) |
| 76 | + return err |
| 77 | + } |
| 78 | + glog.V(3).Infof("Pod <%v/%v> was deleted, removed from cache.", oldTask.Namespace, oldTask.Name) |
| 79 | + |
| 80 | + return nil |
| 81 | + } |
| 82 | + return fmt.Errorf("failed to get Pod <%v/%v>: err %v", oldTask.Namespace, oldTask.Name, err) |
| 83 | + } |
| 84 | + |
| 85 | + return cc.cache.UpdatePod(newPod) |
| 86 | +} |
| 87 | + |
| 88 | +func (cc *Controller) resyncTask(task *v1.Pod) { |
| 89 | + cc.errTasks.AddRateLimited(task) |
| 90 | +} |
0 commit comments