forked from deis/k8s-claimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (98 loc) · 2.89 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"errors"
"log"
"net/http"
"github.com/tentsk8s/k8s-claimer/handlers"
"github.com/tentsk8s/k8s-claimer/htp"
// "github.com/tentsk8s/k8s-claimer/providers/azure"
"github.com/tentsk8s/k8s-claimer/providers/gke"
)
const (
appName = "k8s-claimer"
authTokenKey = "Authorization"
)
var (
errNilConfig = errors.New("nil config")
)
// func kubeNamespacesFromConfig() func(*k8s.KubeConfig) (k8s.NamespaceListerDeleter, error) {
// return func(conf *k8s.KubeConfig) (k8s.NamespaceListerDeleter, error) {
// if conf == nil {
// return nil, errNilConfig
// }
// cl, err := k8s.CreateKubeClientFromConfig(conf)
// if err != nil {
// return nil, err
// }
// return cl.Namespaces(), nil
// }
// }
func configureRoutesWithAuth(
serveMux *http.ServeMux,
createLeaseHandler http.Handler,
deleteLeaseHandler http.Handler,
authToken string,
) {
createLeaseHandler = htp.MethodMux(
map[htp.Method]http.Handler{htp.Post: createLeaseHandler},
)
deleteLeaseHandler = htp.MethodMux(
map[htp.Method]http.Handler{htp.Delete: deleteLeaseHandler},
)
serveMux.Handle("/lease", handlers.WithAuth(authToken, authTokenKey, createLeaseHandler))
serveMux.Handle("/lease/", handlers.WithAuth(authToken, authTokenKey, deleteLeaseHandler))
}
//CreateHealthzHandler returns an http.Handler
func CreateHealthzHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
}
func main() {
leaseStorage := leases.NewStorage()
serverConf, err := parseServerConfig(appName)
if err != nil {
log.Fatalf("Error getting server config (%s)", err)
}
serverConf.Print()
googleConfig, err := parseGoogleConfig(appName)
if err != nil {
log.Fatalf("Error getting google cloud config (%s) -- %+v", err, googleConfig)
}
azureConfig, err := parseAzureConfig(appName)
if err != nil {
log.Fatalf("Error getting azure config (%s) -- %+v", err, azureConfig)
}
containerService, err := gke.GetContainerService(
googleConfig.AccountFile.ClientEmail,
gke.PrivateKey(googleConfig.AccountFile.PrivateKey),
)
if err != nil {
log.Fatalf("Error creating GKE client (%s)", err)
}
gkeClusterLister := gke.NewGKEClusterLister(containerService)
azureClusterLister := azure.NewAzureClusterLister(azureConfig)
mux := http.NewServeMux()
createLeaseHandler := handlers.CreateLease(
// services,
serverConf.ServiceName,
gkeClusterLister,
azureClusterLister,
azureConfig,
googleConfig,
)
deleteLeaseHandler := handlers.DeleteLease(
// services,
serverConf.ServiceName,
gkeClusterLister,
azureClusterLister,
azureConfig,
googleConfig,
serverConf.ClearNamespaces,
// kubeNamespacesFromConfig(),
)
mux.Handle("/healthz", CreateHealthzHandler())
configureRoutesWithAuth(mux, createLeaseHandler, deleteLeaseHandler, serverConf.AuthToken)
log.Println("k8s claimer started!")
http.ListenAndServe(serverConf.HostStr(), mux)
}