func Run(stopCh <-chan struct{}) error {
	hostName, err := os.Hostname()
	if err != nil {
		klog.Fatalf("failed to get hostname: %v", err)
	}

	ns := os.Getenv("NAMESPACE")
	if ns == "" {
		klog.Fatal("NAMESPACE environment variable not set")
	}

	cfg, err := rest.InClusterConfig()
	if err != nil {
		klog.Fatalf("failed to get config: %v", err)
	}

	cli, err := versioned.NewForConfig(cfg)
	if err != nil {
		klog.Fatalf("failed to create Clientset: %v", err)
	}
	var kubeCli kubernetes.Interface
	kubeCli, err = kubernetes.NewForConfig(cfg)
	if err != nil {
		klog.Fatalf("failed to get kubernetes Clientset: %v", err)
	}
	genericCli, err := client.New(cfg, client.Options{Scheme: scheme.Scheme})
	if err != nil {
		klog.Fatalf("failed to get the generic kube-apiserver client: %v", err)
	}

	var informerFactory informers.SharedInformerFactory
	var kubeInformerFactory kubeinformers.SharedInformerFactory
	var options []informers.SharedInformerOption
	var kubeoptions []kubeinformers.SharedInformerOption
	informerFactory = informers.NewSharedInformerFactoryWithOptions(cli, controller.ResyncDuration, options...)
	kubeInformerFactory = kubeinformers.NewSharedInformerFactoryWithOptions(kubeCli, controller.ResyncDuration, kubeoptions...)

	rl := resourcelock.EndpointsLock{
		EndpointsMeta: metav1.ObjectMeta{
			Namespace: ns,
			Name:      "tikv-controller-manager",
		},
		Client: kubeCli.CoreV1(),
		LockConfig: resourcelock.ResourceLockConfig{
			Identity:      hostName,
			EventRecorder: &record.FakeRecorder{},
		},
	}

	controllerCtx, cancel := context.WithCancel(context.Background())
	defer cancel()

	onStarted := func(ctx context.Context) {
		_ = genericCli
		tcController := tikvcluster.NewController(kubeCli, cli, genericCli, informerFactory, kubeInformerFactory, autoFailover, pdFailoverPeriod, tikvFailoverPeriod)

		// Start informer factories after all controller are initialized.
		informerFactory.Start(ctx.Done())
		kubeInformerFactory.Start(ctx.Done())

		// Wait for all started informers' cache were synced.
		for v, synced := range informerFactory.WaitForCacheSync(wait.NeverStop) {
			if !synced {
				klog.Fatalf("error syncing informer for %v", v)
			}
		}
		for v, synced := range kubeInformerFactory.WaitForCacheSync(wait.NeverStop) {
			if !synced {
				klog.Fatalf("error syncing informer for %v", v)
			}
		}
		klog.Infof("cache of informer factories sync successfully")

		wait.Forever(func() { tcController.Run(workers, ctx.Done()) }, waitDuration)
	}

	onStopped := func() {
		klog.Fatalf("leader election lost")
	}

	// leader election for multiple tikv-controller-manager instances
	go wait.Forever(func() {
		leaderelection.RunOrDie(controllerCtx, leaderelection.LeaderElectionConfig{
			Lock:          &rl,
			LeaseDuration: leaseDuration,
			RenewDeadline: renewDuration,
			RetryPeriod:   retryPeriod,
			Callbacks: leaderelection.LeaderCallbacks{
				OnStartedLeading: onStarted,
				OnStoppedLeading: onStopped,
			},
		})
	}, waitDuration)

	healthz.InstallHandler(http.DefaultServeMux)
	klog.Fatal(http.ListenAndServe(":6060", nil))
	return nil
}
// Run runs the controller-manager. This should never exit.
Run runs the controller-manager. This should never exit.
// Run runs the controller - manager . This should never exit .
func Run(stopCh <-chan struct{}) error {
	hostName, err := os.Hostname()
	if err != nil {
		klog.Fatalf("failed to get hostname: %v", err)
	}
	ns := os.Getenv("NAMESPACE")
	if ns == "" {
		klog.Fatal("NAMESPACE environment variable not set")
	}
	cfg, err := rest.InClusterConfig()
	if err != nil {
		klog.Fatalf("failed to get config: %v", err)
	}
	cli, err := versioned.NewForConfig(cfg)
	if err != nil {
		klog.Fatalf("failed to create Clientset: %v", err)
	}
	var kubeCli kubernetes.Interface
	kubeCli, err = kubernetes.NewForConfig(cfg)
	if err != nil {
		klog.Fatalf("failed to get kubernetes Clientset: %v", err)
	}
	genericCli, err := client.New(cfg, client.Options{Scheme: scheme.Scheme})
	if err != nil {
		klog.Fatalf("failed to get the generic kube-apiserver client: %v", err)
	}
	var informerFactory informers.SharedInformerFactory
	var kubeInformerFactory kubeinformers.SharedInformerFactory
	var options []informers.SharedInformerOption
	var kubeoptions []kubeinformers.SharedInformerOption
	informerFactory = informers.NewSharedInformerFactoryWithOptions(cli, controller.ResyncDuration, options...)
	kubeInformerFactory = kubeinformers.NewSharedInformerFactoryWithOptions(kubeCli, controller.ResyncDuration, kubeoptions...)
	rl := resourcelock.EndpointsLock{
		EndpointsMeta: metav1.ObjectMeta{
			Namespace: ns,
			Name:      "tikv-controller-manager",
		},
		Client: kubeCli.CoreV1(),
		LockConfig: resourcelock.ResourceLockConfig{
			Identity:      hostName,
			EventRecorder: &record.FakeRecorder{},
		},
	}
	controllerCtx, cancel := context.WithCancel(context.Background())
	defer cancel()
	onStarted := func(ctx context.Context) {
		_ = genericCli
		tcController := tikvcluster.NewController(kubeCli, cli, genericCli, informerFactory, kubeInformerFactory, autoFailover, pdFailoverPeriod, tikvFailoverPeriod)
		informerFactory.Start(ctx.Done())
		kubeInformerFactory.Start(ctx.Done())
		for v, synced := range informerFactory.WaitForCacheSync(wait.NeverStop) {
			if !synced {
				klog.Fatalf("error syncing informer for %v", v)
			}
		}
		for v, synced := range kubeInformerFactory.WaitForCacheSync(wait.NeverStop) {
			if !synced {
				klog.Fatalf("error syncing informer for %v", v)
			}
		}
		klog.Infof("cache of informer factories sync successfully")
		wait.Forever(func() { tcController.Run(workers, ctx.Done()) }, waitDuration)
	}
	onStopped := func() {
		klog.Fatalf("leader election lost")
	}
	go wait.Forever(func() {
		leaderelection.RunOrDie(controllerCtx, leaderelection.LeaderElectionConfig{
			Lock:          &rl,
			LeaseDuration: leaseDuration,
			RenewDeadline: renewDuration,
			RetryPeriod:   retryPeriod,
			Callbacks: leaderelection.LeaderCallbacks{
				OnStartedLeading: onStarted,
				OnStoppedLeading: onStopped,
			},
		})
	}, waitDuration)
	healthz.InstallHandler(http.DefaultServeMux)
	klog.Fatal(http.ListenAndServe(":6060", nil))
	return nil
}
func Run ( stopCh <- chan struct { } ) error { hostName , err := os . Hostname ( ) if err != nil { klog . Fatalf ( " " , err ) } ns := os . Getenv ( " " ) if ns == " " { klog . Fatal ( " " ) } cfg , err := rest . InClusterConfig ( ) if err != nil { klog . Fatalf ( " " , err ) } cli , err := versioned . NewForConfig ( cfg ) if err != nil { klog . Fatalf ( " " , err ) } var kubeCli kubernetes . Interface kubeCli , err = kubernetes . NewForConfig ( cfg ) if err != nil { klog . Fatalf ( " " , err ) } genericCli , err := client . New ( cfg , client . Options { Scheme : scheme . Scheme } ) if err != nil { klog . Fatalf ( " " , err ) } var informerFactory informers . SharedInformerFactory var kubeInformerFactory kubeinformers . SharedInformerFactory var options [ ] informers . SharedInformerOption var kubeoptions [ ] kubeinformers . SharedInformerOption informerFactory = informers . NewSharedInformerFactoryWithOptions ( cli , controller . ResyncDuration , options ... ) kubeInformerFactory = kubeinformers . NewSharedInformerFactoryWithOptions ( kubeCli , controller . ResyncDuration , kubeoptions ... ) rl := resourcelock . EndpointsLock { EndpointsMeta : metav1 . ObjectMeta { Namespace : ns , Name : " " , } , Client : kubeCli . CoreV1 ( ) , LockConfig : resourcelock . ResourceLockConfig { Identity : hostName , EventRecorder : & record . FakeRecorder { } , } , } controllerCtx , cancel := context . WithCancel ( context . Background ( ) ) defer cancel ( ) onStarted := func ( ctx context . Context ) { _ = genericCli tcController := tikvcluster . NewController ( kubeCli , cli , genericCli , informerFactory , kubeInformerFactory , autoFailover , pdFailoverPeriod , tikvFailoverPeriod ) informerFactory . Start ( ctx . Done ( ) ) kubeInformerFactory . Start ( ctx . Done ( ) ) for v , synced := range informerFactory . WaitForCacheSync ( wait . NeverStop ) { if ! synced { klog . Fatalf ( " " , v ) } } for v , synced := range kubeInformerFactory . WaitForCacheSync ( wait . NeverStop ) { if ! synced { klog . Fatalf ( " " , v ) } } klog . Infof ( " " ) wait . Forever ( func ( ) { tcController . Run ( workers , ctx . Done ( ) ) } , waitDuration ) } onStopped := func ( ) { klog . Fatalf ( " " ) } go wait . Forever ( func ( ) { leaderelection . RunOrDie ( controllerCtx , leaderelection . LeaderElectionConfig { Lock : & rl , LeaseDuration : leaseDuration , RenewDeadline : renewDuration , RetryPeriod : retryPeriod , Callbacks : leaderelection . LeaderCallbacks { OnStartedLeading : onStarted , OnStoppedLeading : onStopped , } , } ) } , waitDuration ) healthz . InstallHandler ( http . DefaultServeMux ) klog . Fatal ( http . ListenAndServe ( " " , nil ) ) return nil }
Run runs the controller-manager.
Run runs the controller - manager .
// Start informer factories after all controller are initialized. // Wait for all started informers' cache were synced. // leader election for multiple tikv-controller-manager instances