@@ -29,6 +29,7 @@ import (
29
29
"k8s.io/apimachinery/pkg/runtime"
30
30
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
31
31
"k8s.io/client-go/informers"
32
+ clientgoscheme "k8s.io/client-go/kubernetes/scheme"
32
33
corelisters "k8s.io/client-go/listers/core/v1"
33
34
policylisters "k8s.io/client-go/listers/policy/v1"
34
35
"k8s.io/client-go/tools/cache"
@@ -39,23 +40,29 @@ import (
39
40
"k8s.io/kubernetes/pkg/scheduler/framework/preemption"
40
41
"k8s.io/kubernetes/pkg/scheduler/metrics"
41
42
schedutil "k8s.io/kubernetes/pkg/scheduler/util"
43
+ ctrlruntimecache "sigs.k8s.io/controller-runtime/pkg/cache"
44
+ "sigs.k8s.io/controller-runtime/pkg/client"
42
45
43
46
"sigs.k8s.io/scheduler-plugins/apis/scheduling"
44
47
"sigs.k8s.io/scheduler-plugins/apis/scheduling/v1alpha1"
45
- "sigs.k8s.io/scheduler-plugins/pkg/generated/clientset/versioned"
46
- schedinformer "sigs.k8s.io/scheduler-plugins/pkg/generated/informers/externalversions"
47
- externalv1alpha1 "sigs.k8s.io/scheduler-plugins/pkg/generated/listers/scheduling/v1alpha1"
48
48
"sigs.k8s.io/scheduler-plugins/pkg/util"
49
49
)
50
50
51
+ var scheme = runtime .NewScheme ()
52
+
53
+ func init () {
54
+ utilruntime .Must (clientgoscheme .AddToScheme (scheme ))
55
+ utilruntime .Must (v1alpha1 .AddToScheme (scheme ))
56
+ }
57
+
51
58
// CapacityScheduling is a plugin that implements the mechanism of capacity scheduling.
52
59
type CapacityScheduling struct {
53
60
sync.RWMutex
54
- fh framework.Handle
55
- podLister corelisters.PodLister
56
- pdbLister policylisters.PodDisruptionBudgetLister
57
- elasticQuotaLister externalv1alpha1. ElasticQuotaLister
58
- elasticQuotaInfos ElasticQuotaInfos
61
+ fh framework.Handle
62
+ podLister corelisters.PodLister
63
+ pdbLister policylisters.PodDisruptionBudgetLister
64
+ client client. Client
65
+ elasticQuotaInfos ElasticQuotaInfos
59
66
}
60
67
61
68
// PreFilterState computed at PreFilter and used at PostFilter or Reserve.
@@ -119,42 +126,43 @@ func New(obj runtime.Object, handle framework.Handle) (framework.Plugin, error)
119
126
pdbLister : getPDBLister (handle .SharedInformerFactory ()),
120
127
}
121
128
122
- client , err := versioned . NewForConfig (handle .KubeConfig ())
129
+ client , err := client . New (handle .KubeConfig (), client. Options { Scheme : scheme } )
123
130
if err != nil {
124
131
return nil , err
125
132
}
126
133
127
- schedSharedInformerFactory := schedinformer .NewSharedInformerFactory (client , 0 )
128
- c .elasticQuotaLister = schedSharedInformerFactory .Scheduling ().V1alpha1 ().ElasticQuotas ().Lister ()
129
- elasticQuotaInformer := schedSharedInformerFactory .Scheduling ().V1alpha1 ().ElasticQuotas ().Informer ()
130
- elasticQuotaInformer .AddEventHandler (
131
- cache.FilteringResourceEventHandler {
132
- FilterFunc : func (obj interface {}) bool {
133
- switch t := obj .(type ) {
134
- case * v1alpha1.ElasticQuota :
134
+ c .client = client
135
+ dynamicCache , err := ctrlruntimecache .New (handle .KubeConfig (), ctrlruntimecache.Options {Scheme : scheme })
136
+ if err != nil {
137
+ return nil , err
138
+ }
139
+ // TODO: pass in context.
140
+ elasticQuotaInformer , err := dynamicCache .GetInformer (context .Background (), & v1alpha1.ElasticQuota {})
141
+ if err != nil {
142
+ return nil , err
143
+ }
144
+ elasticQuotaInformer .AddEventHandler (cache.FilteringResourceEventHandler {
145
+ FilterFunc : func (obj interface {}) bool {
146
+ switch t := obj .(type ) {
147
+ case * v1alpha1.ElasticQuota :
148
+ return true
149
+ case cache.DeletedFinalStateUnknown :
150
+ if _ , ok := t .Obj .(* v1alpha1.ElasticQuota ); ok {
135
151
return true
136
- case cache.DeletedFinalStateUnknown :
137
- if _ , ok := t .Obj .(* v1alpha1.ElasticQuota ); ok {
138
- return true
139
- }
140
- utilruntime .HandleError (fmt .Errorf ("cannot convert to *v1alpha1.ElasticQuota: %v" , obj ))
141
- return false
142
- default :
143
- utilruntime .HandleError (fmt .Errorf ("unable to handle object in %T" , obj ))
144
- return false
145
152
}
146
- },
147
- Handler : cache.ResourceEventHandlerFuncs {
148
- AddFunc : c .addElasticQuota ,
149
- UpdateFunc : c .updateElasticQuota ,
150
- DeleteFunc : c .deleteElasticQuota ,
151
- },
152
- })
153
-
154
- schedSharedInformerFactory .Start (nil )
155
- if ! cache .WaitForCacheSync (nil , elasticQuotaInformer .HasSynced ) {
156
- return nil , fmt .Errorf ("timed out waiting for caches to sync %v" , Name )
157
- }
153
+ utilruntime .HandleError (fmt .Errorf ("cannot convert to *v1alpha1.ElasticQuota: %v" , obj ))
154
+ return false
155
+ default :
156
+ utilruntime .HandleError (fmt .Errorf ("unable to handle object in %T" , obj ))
157
+ return false
158
+ }
159
+ },
160
+ Handler : cache.ResourceEventHandlerFuncs {
161
+ AddFunc : c .addElasticQuota ,
162
+ UpdateFunc : c .updateElasticQuota ,
163
+ DeleteFunc : c .deleteElasticQuota ,
164
+ },
165
+ })
158
166
159
167
podInformer := handle .SharedInformerFactory ().Core ().V1 ().Pods ().Informer ()
160
168
podInformer .AddEventHandler (
@@ -688,12 +696,13 @@ func (c *CapacityScheduling) addPod(obj interface{}) {
688
696
elasticQuotaInfo := c .elasticQuotaInfos [pod .Namespace ]
689
697
// If elasticQuotaInfo is nil, try to list ElasticQuotas through elasticQuotaLister
690
698
if elasticQuotaInfo == nil {
691
- eqs , err := c . elasticQuotaLister . ElasticQuotas ( pod . Namespace ). List ( labels . NewSelector ())
692
- if err != nil {
699
+ var eqList v1alpha1. ElasticQuotaList
700
+ if err := c . client . List ( context . Background (), & eqList , client . InNamespace ( pod . Namespace )); err != nil {
693
701
klog .ErrorS (err , "Failed to get elasticQuota" , "elasticQuota" , pod .Namespace )
694
702
return
695
703
}
696
704
705
+ eqs := eqList .Items
697
706
// If the length of elasticQuotas is 0, return.
698
707
if len (eqs ) == 0 {
699
708
return
0 commit comments