-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (51 loc) · 1.82 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
package main
import (
"net"
"net/http"
"os"
"strings"
"time"
"github.com/sirupsen/logrus"
"hedisam/kubemock/api"
)
func main() {
logger := logrus.New()
if strings.ToLower(os.Getenv("VERBOSE")) == "enabled" {
logger.SetLevel(logrus.DebugLevel)
}
kubeHandler := api.NewKubeHandler(logger)
mux := http.NewServeMux()
// this is an actual kube endpoint that is called by HC Vault
mux.Handle("/apis/authentication.k8s.io/v1/tokenreviews", http.HandlerFunc(kubeHandler.LoginHandler))
// this is a custom endpoint that will be called directly by our unit tests to register a fake service account
// and generate a valid jwt token for it so that the jwt can later be validated by Vault via the login endpoint above.
mux.Handle("/api/v1/testing/serviceaccounts", http.HandlerFunc(kubeHandler.RegisterServiceAccountHandler))
mux.Handle("/api/v1/testing/health", http.HandlerFunc(kubeHandler.HealthHandler))
// reset endpoint to clean up service account registry before running a test
mux.Handle("/api/v1/testing/reset", http.HandlerFunc(kubeHandler.ResetHandler))
// handle the root endpoint for any unexpected request
mux.Handle("/", http.HandlerFunc(kubeHandler.UnimplementedHandler))
netAddr := "0.0.0.0:6443"
ln, err := net.Listen("tcp", netAddr)
if err != nil {
logger.WithField("net_addr", netAddr).WithError(err).Fatal("Could not start tcp listener")
}
addr := "http://" + netAddr
s := http.Server{
Handler: mux,
ReadTimeout: 5 * time.Second,
Addr: addr,
}
defer func() {
err := s.Close()
if err != nil {
logger.WithError(err).Error("Failed to close kube http server")
}
}()
logger.WithField("addr", s.Addr).Info("Starting kube http server")
err = s.Serve(ln)
if err != nil {
logger.WithError(err).Fatal("Kube http server closed with unexpected error")
}
logger.Info("Kube server stopped")
}