func main()

in kubelet-to-gcm/monitor/main/daemon.go [66:126]


func main() {
	flag.Set("logtostderr", "true") // This spoofs glog into teeing logs to stderr.

	defer log.Flush()
	flag.Parse()
	log.Infof("Invoked by %v", os.Args)

	resolution := time.Second * time.Duration(*res)

	monitoredResourceLabels := parseMonitoredResourceLabels(*monitoredResourceLabels)
	// Initialize the configuration.
	kubeletCfg, ctrlCfg, err := config.NewConfigs(*zone, *project, *cluster, *clusterLocation, *kubeletHost, *kubeletInstance, *schemaPrefix, *certificateLocation, monitoredResourceLabels, *kubeletPort, *ctrlPort, resolution)
	if err != nil {
		log.Fatalf("Failed to initialize configuration: %v", err)
	}

	// Create objects for kubelet monitoring.
	kubeletSrc, err := kubelet.NewSource(kubeletCfg)
	if err != nil {
		log.Fatalf("Failed to create a kubelet source with config %v: %v", kubeletCfg, err)
	}
	log.Infof("The kubelet source is initialized with config %v.", kubeletCfg)

	var ctrlSrc *controller.Source
	if *ctrlPort != 0 {
		// Create objects for kube-controller monitoring.
		ctrlSrc, err = controller.NewSource(ctrlCfg)
		if err != nil {
			log.Fatalf("Failed to create a kube-controller source with config %v: %v", ctrlCfg, err)
		}
		log.Infof("The kube-controller source is initialized with config %v.", ctrlCfg)
	}

	// Create a GCM client.
	client, err := google.DefaultClient(context.Background(), scope)
	if err != nil {
		log.Fatalf("Failed to create a client with default context and scope %s, err: %v", scope, err)
	}
	service, err := v3.New(client)
	if err != nil {
		log.Fatalf("Failed to create a GCM v3 API service object: %v", err)
	}
	// Determine the GCE endpoint.
	if *gcmEndpoint != "" {
		service.BasePath = *gcmEndpoint
	}
	log.Infof("Using GCM endpoint %q", service.BasePath)

	go func() {
		http.Handle("/metrics", promhttp.Handler())
		log.Error(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
	}()

	for {
		go monitor.Once(kubeletSrc, service)
		if *ctrlPort != 0 {
			go monitor.Once(ctrlSrc, service)
		}
		time.Sleep(resolution)
	}
}