func Run()

in cmd/controller/app/server.go [55:119]


func Run(s *ServerRunOptions) error {
	ctx := context.Background()
	config, err := newConfig(s.KubeConfig, s.MasterUrl, s.InCluster)
	if err != nil {
		klog.Fatal(err)
	}
	config.QPS = float32(s.ApiServerQPS)
	config.Burst = s.ApiServerBurst
	stopCh := server.SetupSignalHandler()
	schedClient := schedclientset.NewForConfigOrDie(config)
	kubeClient := kubernetes.NewForConfigOrDie(config)

	schedInformerFactory := schedformers.NewSharedInformerFactory(schedClient, 0)
	pgInformer := schedInformerFactory.Scheduling().V1alpha1().PodGroups()
	eqInformer := schedInformerFactory.Scheduling().V1alpha1().ElasticQuotas()

	coreInformerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
	podInformer := coreInformerFactory.Core().V1().Pods()
	pgCtrl := controller.NewPodGroupController(kubeClient, pgInformer, podInformer, schedClient)
	eqCtrl := controller.NewElasticQuotaController(kubeClient, eqInformer, podInformer, schedClient)

	run := func(ctx context.Context) {
		go pgCtrl.Run(s.Workers, ctx.Done())
		go eqCtrl.Run(s.Workers, ctx.Done())
		select {}
	}
	schedInformerFactory.Start(stopCh)
	coreInformerFactory.Start(stopCh)
	if !s.EnableLeaderElection {
		run(ctx)
	} else {
		id, err := os.Hostname()
		if err != nil {
			return err
		}
		// add a uniquifier so that two processes on the same host don't accidentally both become active
		id = id + "_" + string(uuid.NewUUID())

		rl, err := resourcelock.New("endpoints",
			"kube-system",
			"sched-plugins-controller",
			kubeClient.CoreV1(),
			kubeClient.CoordinationV1(),
			resourcelock.ResourceLockConfig{
				Identity: id,
			})
		if err != nil {
			klog.Fatalf("error creating lock: %v", err)
		}

		leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{
			Lock: rl,
			Callbacks: leaderelection.LeaderCallbacks{
				OnStartedLeading: run,
				OnStoppedLeading: func() {
					klog.Fatalf("leaderelection lost")
				},
			},
			Name: "scheduler-plugins controller",
		})
	}

	<-stopCh
	return nil
}