-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (63 loc) · 1.76 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
70
71
72
73
package main
import (
"fmt"
"log"
"net/http"
"path/filepath"
"github.com/kelseyhightower/envconfig"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
type (
// Config holds the configuration specified via environment
Config struct {
Namespace string `envconfig:"NAMESPACE" required:"true"`
Deployment string `envconfig:"DEPLOYMENT" required:"true"`
Listen string `envconfig:"LISTEN" default:":8080"`
Token []byte `envconfig:"TOKEN" reqired:"true"`
LinuxHome string `envconfig:"HOME"`
WindowsHome string `envconfig:"USERPROFILE"`
}
)
func main() {
// config
cfg := &Config{}
err := envconfig.Process("", cfg)
if err != nil {
log.Fatalf("unable to parse env: %s", err)
}
home := cfg.LinuxHome
if home == "" {
home = cfg.WindowsHome
}
kubeconfig := filepath.Join(home, ".kube", "config")
// create kubernetes client
config, err := rest.InClusterConfig()
if err != nil {
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
log.Fatalf("unable to load in-cluster config: %s", err)
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("unable to create client: %s", err)
}
// add non-app handlers
http.Handle("/metrics", promhttp.Handler())
http.Handle("/healthz", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok\n")
}))
// create app and inject into http
app := NewApp(cfg, clientset)
http.HandleFunc("/", app.Handle)
logged := app.Middleware(http.DefaultServeMux)
// run webserber
log.Printf("listening on: %s", cfg.Listen)
err = http.ListenAndServe(cfg.Listen, logged)
if err != nil {
log.Fatalf("unable to listen: %s", err)
}
}