-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
69 lines (56 loc) · 1.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"log"
"net/http"
"path/filepath"
"time"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"bitsbeats/velero-pvc-watcher/watcher"
)
const (
ListenAddr = ":2121"
)
var ()
func main() {
clientset, err := loadClientset()
if err != nil {
log.Fatalf("unable to connect to kubernetes: %s", err)
}
// start informer factory
stopper := make(chan struct{}, 1)
factory := informers.NewSharedInformerFactory(clientset, 1*time.Hour)
factory.Start(stopper)
log.Printf("connecting to k8s and warm-up caches")
w := watcher.NewWatcher(factory, stopper)
w.Run(stopper)
err = prometheus.Register(w)
if err != nil {
log.Fatalf("unable to register prometheus metrics: %s", err)
}
http.Handle("/metrics", promhttp.Handler())
log.Printf("listening on %s", ListenAddr)
http.ListenAndServe(ListenAddr, nil)
}
// load matching clientset
func loadClientset() (*kubernetes.Clientset, error) {
config, err := rest.InClusterConfig()
if err == rest.ErrNotInCluster {
log.Printf("using out of cluster config...")
home := homedir.HomeDir()
kubeconfig := filepath.Join(home, ".kube", "config")
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
}
if err != nil {
return nil, fmt.Errorf("unable to load config: %w", err)
}
// load k8s config
log.Printf("loading k8s config")
return kubernetes.NewForConfig(config)
}