From 17342bdb8cc392584ee683d53f003744d31c3414 Mon Sep 17 00:00:00 2001 From: Hao Fan <1032773840@qq.com> Date: Tue, 14 Mar 2023 19:11:16 +0800 Subject: [PATCH 1/2] fix: resolve restmapper getAPIGroupResourcesres error when restConfig is nil Signed-off-by: Hao Fan <1032773840@qq.com> --- pkg/framework/kubescheduler.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pkg/framework/kubescheduler.go b/pkg/framework/kubescheduler.go index eec6593..6c110da 100644 --- a/pkg/framework/kubescheduler.go +++ b/pkg/framework/kubescheduler.go @@ -227,16 +227,23 @@ func WithTerminatingPods(with bool) Option { // for kubeconfig nor for apiserver url func NewKubeSchedulerFramework(kubeSchedulerConfig *schedconfig.CompletedConfig, restConfig *restclient.Config, options ...Option) (pkg.Framework, error) { kubeSchedulerConfig.InformerFactory.InformerFor(&corev1.Pod{}, newPodInformer) - - dynamicClient := dynamic.NewForConfigOrDie(restConfig) - restMapper, err := apiutil.NewDynamicRESTMapper(restConfig) - if err != nil { - return nil, err + var err error + var restMapper meta.RESTMapper + var dynamicClient *dynamic.DynamicClient + var dynInformerFactory dynamicinformer.DynamicSharedInformerFactory + if restConfig != nil { + if restMapper, err = apiutil.NewDynamicRESTMapper(restConfig); err != nil { + return nil, err + } + dynamicClient = dynamic.NewForConfigOrDie(restConfig) + // only for latest k8s version + dynInformerFactory = dynamicinformer.NewFilteredDynamicSharedInformerFactory(dynamicClient, 0, corev1.NamespaceAll, nil) } s := &kubeschedulerFramework{ fakeClient: kubeSchedulerConfig.Client, dynamicClient: dynamicClient, + dynInformerFactory: dynInformerFactory, restMapper: restMapper, stopCh: make(chan struct{}), fakeInformerFactory: kubeSchedulerConfig.InformerFactory, @@ -251,12 +258,6 @@ func NewKubeSchedulerFramework(kubeSchedulerConfig *schedconfig.CompletedConfig, option(s) } - // only for latest k8s version - if restConfig != nil { - dynClient := dynamic.NewForConfigOrDie(restConfig) - s.dynInformerFactory = dynamicinformer.NewFilteredDynamicSharedInformerFactory(dynClient, 0, corev1.NamespaceAll, nil) - } - scheduler, err := s.createScheduler(kubeSchedulerConfig) if err != nil { return nil, err @@ -293,7 +294,7 @@ func (s *kubeschedulerFramework) GetPodsByNode(nodeName string) ([]*corev1.Pod, // InitTheWorld use objs outside or default init resources to initialize the scheduler // the objs outside must be typed object. func (s *kubeschedulerFramework) InitTheWorld(objs ...runtime.Object) error { - if len(objs) == 0 { + if len(objs) == 0 && s.dynamicClient != nil { // black magic klog.V(2).InfoS("Init the world form running cluster") initObjects := getInitObjects(s.restMapper, s.dynamicClient) From 2425a3ddf26e335b70230b29b83f16ef519032ca Mon Sep 17 00:00:00 2001 From: Hao Fan <1032773840@qq.com> Date: Tue, 14 Mar 2023 19:13:37 +0800 Subject: [PATCH 2/2] fix: resolve scheduler not exit when call kubescheduler Stop Signed-off-by: Hao Fan <1032773840@qq.com> --- pkg/framework/kubescheduler.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/framework/kubescheduler.go b/pkg/framework/kubescheduler.go index 6c110da..633de1a 100644 --- a/pkg/framework/kubescheduler.go +++ b/pkg/framework/kubescheduler.go @@ -400,7 +400,10 @@ func (s *kubeschedulerFramework) Run() error { if s.dynInformerFactory != nil { s.dynInformerFactory.WaitForCacheSync(s.informerCh) } - go s.scheduler.Run(context.TODO()) + + ctx, cancel := context.WithCancel(context.TODO()) + defer cancel() + go s.scheduler.Run(ctx) <-s.stopCh