forked from iximiuz/kexp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
210 lines (176 loc) · 5.88 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"context"
"embed"
"fmt"
"io/fs"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/iximiuz/kexp/api"
restkubecontexts "github.com/iximiuz/kexp/api/rest/kube/contexts"
restkubeobjects "github.com/iximiuz/kexp/api/rest/kube/objects"
restkuberesources "github.com/iximiuz/kexp/api/rest/kube/resources"
"github.com/iximiuz/kexp/api/stream"
streamrpc "github.com/iximiuz/kexp/api/stream/rpc"
streamkubeobjects "github.com/iximiuz/kexp/api/stream/rpc/kube/objects"
"github.com/iximiuz/kexp/kubeclient"
)
var (
version = "v0.0.0"
commit = "HEAD"
date = "unknown"
versionString = fmt.Sprintf("%s (build: %s commit: %s)", version, date, commit)
)
type flagpole struct {
*genericclioptions.ConfigFlags
host string
port string
}
// [--kubeconfig] [--namespace] [--context]
func main() {
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(logrus.TraceLevel)
flags := flagpole{
ConfigFlags: genericclioptions.NewConfigFlags(false), // false is import here!
}
cmd := &cobra.Command{
Version: versionString,
Use: "kexp",
Run: run(&flags),
}
flags.AddFlags(cmd.PersistentFlags())
cmd.PersistentFlags().StringVar(&flags.host, "host", "127.0.0.1", "Listening host")
cmd.PersistentFlags().StringVar(&flags.port, "port", "5173", "Listening port")
if err := cmd.Execute(); err != nil {
logrus.WithError(err).Fatal("Command failed")
}
}
//go:embed ui/dist/*
var uiStaticFS embed.FS
func run(flags *flagpole) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
kubeClientPool, err := initKubeClientPoolWithRetry(cmd.Context(), flags, 300*time.Second)
if err != nil {
logrus.
WithError(err).
Fatal("Could not initialize Kubernetes client pool")
}
logrus.
WithField("contexts", kubeClientPool.Contexts()).
Debug("Kube context discovery finished")
logrus.Infof("Starting server on %v:%v", flags.host, flags.port)
router := gin.New()
router.Use(gin.Logger())
router.Use(api.MiddlewareRequestID)
kubeContextsHandler := restkubecontexts.NewHandler(
kubeClientPool,
logrus.NewEntry(logrus.StandardLogger()),
)
kubeContextsv1 := router.Group("/api/kube/v1/contexts")
kubeContextsv1.GET("/", kubeContextsHandler.List)
kubeResourcesHandler := restkuberesources.NewHandler(
kubeClientPool,
logrus.NewEntry(logrus.StandardLogger()),
)
kubeResourcesv1 := router.Group("/api/kube/v1/contexts/:ctx/resources")
kubeResourcesv1.GET("/", kubeResourcesHandler.List)
kubeObjectsHandler := restkubeobjects.NewHandler(
kubeClientPool,
logrus.NewEntry(logrus.StandardLogger()),
)
kubeObjectsv1 := router.Group("/api/kube/v1/contexts/:ctx/resources")
kubeObjectsv1.GET("/:group/:version/:resource/", kubeObjectsHandler.List)
kubeObjectsv1.GET("/:group/:version/namespaces/:namespace/:resource/", kubeObjectsHandler.List)
kubeObjectsv1.GET("/:group/:version/:resource/:name/", kubeObjectsHandler.Get)
kubeObjectsv1.GET("/:group/:version/namespaces/:namespace/:resource/:name/", kubeObjectsHandler.Get)
kubeObjectsv1.PUT("/:group/:version/:resource/:name/", kubeObjectsHandler.Update)
kubeObjectsv1.PUT("/:group/:version/namespaces/:namespace/:resource/:name/", kubeObjectsHandler.Update)
kubeObjectsv1.DELETE("/:group/:version/:resource/:name/", kubeObjectsHandler.Delete)
kubeObjectsv1.DELETE("/:group/:version/namespaces/:namespace/:resource/:name/", kubeObjectsHandler.Delete)
rpcCallDispatcher := streamrpc.NewCallDispatcher()
rpcCallDispatcher.RegisterCallHandler(
streamkubeobjects.Watch,
streamkubeobjects.NewWatchHandler(kubeClientPool),
)
streamHandler := stream.NewHandler(logrus.NewEntry(logrus.StandardLogger()))
streamHandler.RegisterMessageHandler(streamrpc.MessageTypeCall, rpcCallDispatcher)
streamv1 := router.Group("/api/stream/v1")
streamv1.GET("/", streamHandler.Connect)
if uiStaticFS, err := fs.Sub(uiStaticFS, "ui/dist"); err != nil {
logrus.WithError(err).Fatal("Could not load static files")
} else {
router.StaticFS("/ui", http.FS(uiStaticFS))
router.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/ui")
})
}
if err := router.Run(flags.host + ":" + flags.port); err != nil {
logrus.WithError(err).Fatal("Router failed")
}
}
}
func initKubeClientPool(ctx context.Context, flags *flagpole) (*kubeclient.ClientPool, error) {
rawConfig, err := flags.ToRawKubeConfigLoader().RawConfig()
if err != nil {
return nil, err
}
curKubeCtx := *flags.Context
if curKubeCtx == "" {
curKubeCtx = rawConfig.CurrentContext
}
pool := kubeclient.NewPool()
for name, kctx := range rawConfig.Contexts {
// TODO: This might be an overkill.
// TODO: Restore the original flags.Context value.
flags.Context = &name
config, err := flags.ToRawKubeConfigLoader().ClientConfig()
if err != nil {
logrus.
WithField("context", name).
WithError(err).
Warnf("couldn't load REST config for a context")
if name == curKubeCtx {
curKubeCtx = ""
}
continue
}
if err := pool.Add(ctx, name, kctx.AuthInfo, kctx.Cluster, kctx.Namespace, config); err != nil {
logrus.
WithField("context", name).
WithError(err).
Warnf("couldn't add a context to the pool")
if name == curKubeCtx {
curKubeCtx = ""
}
continue
}
}
if len(pool.Contexts()) == 0 {
return nil, fmt.Errorf("no functional contexts found")
}
if curKubeCtx != "" {
if err := pool.SetCurrent(curKubeCtx); err != nil {
return nil, err
}
}
return pool, nil
}
func initKubeClientPoolWithRetry(
ctx context.Context,
flags *flagpole,
maxWait time.Duration,
) (pool *kubeclient.ClientPool, err error) {
startAt := time.Now()
for time.Since(startAt) < maxWait {
pool, err = initKubeClientPool(ctx, flags)
if err == nil {
return pool, nil
}
time.Sleep(1 * time.Second)
}
return nil, err
}