-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (87 loc) · 2.39 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
package main
import (
"flag"
"fmt"
"github.com/bcollard/porthole/pkg/controllers"
"github.com/gin-gonic/gin"
"golang.org/x/sync/errgroup"
"k8s.io/klog/v2"
"log"
"net/http"
"os"
"time"
)
var (
g errgroup.Group
)
// Used by the discovery REST API
// and the debug REST API
func restRouter() http.Handler {
restRouter := gin.Default()
// get namespaces
restRouter.GET("/explore", controllers.GetNamespaces)
restRouter.GET("/explore/ns", controllers.GetNamespaces)
restRouter.GET("/explore/namespaces", controllers.GetNamespaces)
// get pods
restRouter.GET("/explore/:ns", controllers.GetPods)
restRouter.GET("/explore/ns/:ns", controllers.GetPods)
restRouter.GET("/explore/ns/:ns/pods", controllers.GetPods)
restRouter.GET("/explore/namespace/:ns", controllers.GetPods)
restRouter.GET("/explore/namespace/:ns/pods", controllers.GetPods)
restRouter.GET("/explore/namespaces/:ns", controllers.GetPods)
restRouter.GET("/explore/namespaces/:ns/pods", controllers.GetPods)
// debug endpoints
restRouter.POST("/debug/inject", controllers.Inject)
restRouter.POST("/debug/exec", controllers.Exec)
restRouter.GET("/debug/list", controllers.List)
return restRouter
}
// Used by the attach websocket
// and the home web page
func wsRouter() http.Handler {
wsRouter := gin.New()
wsRouter.GET("/echo", controllers.EchoWs)
wsRouter.GET("/term/:ns/:pod/:ctr", controllers.AttachWs)
wsRouter.GET("/", controllers.HomeWs)
return wsRouter
}
func main() {
setLogging()
// get restPort from env
restPort := os.Getenv("PORT")
if restPort == "" {
restPort = "8081"
}
// get wsPort from env
wsPort := os.Getenv("WS_PORT")
if wsPort == "" {
wsPort = "8082"
}
restServer := &http.Server{
Addr: "0.0.0.0:" + restPort,
Handler: restRouter(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
wsServer := &http.Server{
Addr: "0.0.0.0:" + wsPort,
Handler: wsRouter(),
}
g.Go(func() error {
fmt.Printf("REST server listening on port %s\n", restPort)
return restServer.ListenAndServe()
})
g.Go(func() error {
fmt.Printf("WS server listening on port %s\n", wsPort)
return wsServer.ListenAndServe()
})
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}
// enable the k8s logging system
func setLogging() {
klog.InitFlags(nil) // initializing the flags
defer klog.Flush() // flushes all pending log I/O
flag.Parse() // parses the command-line flags
}