-
Notifications
You must be signed in to change notification settings - Fork 993
/
pg_controller.go
144 lines (117 loc) · 3.94 KB
/
pg_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
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 podgroup
import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog"
scheduling "volcano.sh/volcano/pkg/apis/scheduling/v1alpha2"
vcclientset "volcano.sh/volcano/pkg/client/clientset/versioned"
informerfactory "volcano.sh/volcano/pkg/client/informers/externalversions"
schedulinginformer "volcano.sh/volcano/pkg/client/informers/externalversions/scheduling/v1alpha2"
schedulinglister "volcano.sh/volcano/pkg/client/listers/scheduling/v1alpha2"
)
// Controller the Podgroup Controller type
type Controller struct {
kubeClient kubernetes.Interface
vcClient vcclientset.Interface
podInformer coreinformers.PodInformer
pgInformer schedulinginformer.PodGroupInformer
// A store of pods
podLister corelisters.PodLister
podSynced func() bool
// A store of podgroups
pgLister schedulinglister.PodGroupLister
pgSynced func() bool
queue workqueue.RateLimitingInterface
}
// NewPodgroupController create new Podgroup Controller
func NewPodgroupController(
kubeClient kubernetes.Interface,
vcClient vcclientset.Interface,
sharedInformers informers.SharedInformerFactory,
schedulerName string,
) *Controller {
cc := &Controller{
kubeClient: kubeClient,
vcClient: vcClient,
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
}
cc.podInformer = sharedInformers.Core().V1().Pods()
cc.podLister = cc.podInformer.Lister()
cc.podSynced = cc.podInformer.Informer().HasSynced
cc.podInformer.Informer().AddEventHandler(
cache.FilteringResourceEventHandler{
FilterFunc: func(obj interface{}) bool {
switch obj.(type) {
case *v1.Pod:
pod := obj.(*v1.Pod)
if pod.Spec.SchedulerName == schedulerName &&
(pod.Annotations == nil || pod.Annotations[scheduling.GroupNameAnnotationKey] == "") {
return true
}
return false
default:
return false
}
},
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: cc.addPod,
},
})
cc.pgInformer = informerfactory.NewSharedInformerFactory(cc.vcClient, 0).Scheduling().V1alpha2().PodGroups()
cc.pgLister = cc.pgInformer.Lister()
cc.pgSynced = cc.pgInformer.Informer().HasSynced
return cc
}
// Run start NewPodgroupController
func (cc *Controller) Run(stopCh <-chan struct{}) {
go cc.podInformer.Informer().Run(stopCh)
go cc.pgInformer.Informer().Run(stopCh)
cache.WaitForCacheSync(stopCh, cc.podSynced, cc.pgSynced)
go wait.Until(cc.worker, 0, stopCh)
klog.Infof("PodgroupController is running ...... ")
}
func (cc *Controller) worker() {
for cc.processNextReq() {
}
}
func (cc *Controller) processNextReq() bool {
obj, shutdown := cc.queue.Get()
if shutdown {
klog.Errorf("Fail to pop item from queue")
return false
}
req := obj.(podRequest)
defer cc.queue.Done(req)
pod, err := cc.podLister.Pods(req.podNamespace).Get(req.podName)
if err != nil {
klog.Errorf("Failed to get pod by <%v> from cache: %v", req, err)
return true
}
// normal pod use volcano
if err := cc.createNormalPodPGIfNotExist(pod); err != nil {
klog.Errorf("Failed to handle Pod <%s/%s>: %v", pod.Namespace, pod.Name, err)
cc.queue.AddRateLimited(req)
return true
}
// If no error, forget it.
cc.queue.Forget(req)
return true
}