func main()

in ray-on-gke/tpu/kuberay-tpu-webhook/main.go [830:914]


func main() {
	flag.Parse()

	// use in-cluster config if kubeConfig path is not passed as a flag
	var client *kubernetes.Clientset
	if KubeConfigPath == "" {
		config, err := rest.InClusterConfig()
		if err != nil {
			panic(err)
		}
		client = kubernetes.NewForConfigOrDie(config)
	} else {
		config, err := clientcmd.BuildConfigFromFlags("", KubeConfigPath)
		if err != nil {
			panic(err)
		}
		client = kubernetes.NewForConfigOrDie(config)
	}

	// instantiate PodInformer for Ray worker pods in the GKE cluster
	tweakListOptionsFunc := func(options *metav1.ListOptions) {
		options.LabelSelector = "ray.io/node-type=worker,app.kubernetes.io/created-by=kuberay-operator"
	}
	factory := informers.NewFilteredSharedInformerFactory(client, 1*time.Minute, metav1.NamespaceAll, tweakListOptionsFunc)
	podInformer := factory.Core().V1().Pods().Informer()

	// start the PodInformer and wait for cache sync
	stopCh := make(chan struct{})
	factory.Start(stopCh)
	factory.WaitForCacheSync(stopCh)

	if !cache.WaitForCacheSync(stopCh, podInformer.HasSynced) {
		klog.Fatal("Timed out waiting for PodInformer to sync")
	}

	podLister := factory.Core().V1().Pods().Lister()

	if podLister == nil {
		klog.Fatal("Failed to initialize Pod Lister")
	}

	// close the PodInformer on exit
	defer close(stopCh)

	tpuWebhookServer := NewTPUWebhookServer(podLister)

	// Add custom event handler for Pod creation
	podInformer.AddEventHandler(
		cache.ResourceEventHandlerFuncs{
			AddFunc: tpuWebhookServer.addPod,
		},
	)

	mux := http.NewServeMux()

	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "kuberay-tpu-webhook")
	})

	mux.HandleFunc("/mutate", tpuWebhookServer.Mutate)

	mux.HandleFunc("/validate", tpuWebhookServer.Validate)

	srv := &http.Server{
		Addr:    BindAddr,
		Handler: mux,
	}

	if ServerCert != "" && ServerKey != "" {
		if err := writeCertfile(certPath, ServerCert); err != nil {
			klog.Fatalf("write server cert: %v", err)
		}
		if err := writeCertfile(keyPath, ServerKey); err != nil {
			klog.Fatalf("write server key: %v", err)
		}
	}

	if err := srv.ListenAndServeTLS(certPath, keyPath); err != nil {
		if err == http.ErrServerClosed {
			klog.V(0).Info("Server closed")
			return
		}
		klog.Error("Failed to start server")
	}
}