forked from onepanelio/onepanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
298 lines (259 loc) · 10.5 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"context"
"flag"
"fmt"
migrations "github.com/onepanelio/core/db/go"
"github.com/pressly/goose"
"math"
"net"
"net/http"
"path/filepath"
"strings"
"github.com/gorilla/handlers"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/jmoiron/sqlx"
api "github.com/onepanelio/core/api/gen"
v1 "github.com/onepanelio/core/pkg"
"github.com/onepanelio/core/pkg/util/env"
"github.com/onepanelio/core/server"
"github.com/onepanelio/core/server/auth"
log "github.com/sirupsen/logrus"
"github.com/tmc/grpc-websocket-proxy/wsproxy"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
corev1 "k8s.io/api/core/v1"
apiv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
k8runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
var (
rpcPort = flag.String("rpc-port", ":8887", "RPC Port")
httpPort = flag.String("http-port", ":8888", "RPC Port")
recoveryFunc grpc_recovery.RecoveryHandlerFunc
)
func main() {
flag.Parse()
// stopCh is used to indicate when the RPC server should reload.
// We do this when the configuration has been changed, so the server has the latest configuration
stopCh := make(chan struct{})
go func() {
kubeConfig := v1.NewConfig()
client, err := v1.NewClient(kubeConfig, nil, nil)
if err != nil {
log.Fatalf("Failed to connect to Kubernetes cluster: %v", err)
}
client.ClearSystemConfigCache()
sysConfig, err := client.GetSystemConfig()
if err != nil {
log.Fatalf("Failed to get system config: %v", err)
}
dbDriverName, databaseDataSourceName := sysConfig.DatabaseConnection()
// sqlx.MustConnect will panic when it can't connect to DB. In that case, this whole application will crash.
// This is okay, as the pod will restart and try connecting to DB again.
// dbDriverName may be nil, but sqlx will then panic.
db := sqlx.MustConnect(dbDriverName, databaseDataSourceName)
goose.SetTableName("goose_db_version")
if err := goose.Run("up", db.DB, filepath.Join("db", "sql")); err != nil {
log.Fatalf("Failed to run database sql migrations: %v", err)
db.Close()
}
goose.SetTableName("goose_db_go_version")
migrations.Initialize()
if err := goose.Run("up", db.DB, filepath.Join("db", "go")); err != nil {
log.Fatalf("Failed to run database go migrations: %v", err)
db.Close()
}
go watchConfigmapChanges("onepanel", stopCh, func(configMap *corev1.ConfigMap) error {
log.Printf("Configmap changed")
stopCh <- struct{}{}
return nil
})
for {
client.ClearSystemConfigCache()
sysConfig, err = client.GetSystemConfig()
if err != nil {
log.Fatalf("Failed to get system config: %v", err)
}
dbDriverName, databaseDataSourceName = sysConfig.DatabaseConnection()
db = sqlx.MustConnect(dbDriverName, databaseDataSourceName)
s := startRPCServer(v1.NewDB(db), kubeConfig, sysConfig, stopCh)
<-stopCh
s.Stop()
if err := db.Close(); err != nil {
log.Printf("[error] closing db connection %v", err.Error())
}
}
}()
startHTTPProxy()
}
func startRPCServer(db *v1.DB, kubeConfig *v1.Config, sysConfig v1.SystemConfig, stopCh chan struct{}) *grpc.Server {
log.Printf("Starting RPC server on port %v", *rpcPort)
lis, err := net.Listen("tcp", *rpcPort)
if err != nil {
log.Fatalf("Failed to start RPC listener: %v", err)
}
// Recovery settings
recoveryFunc = func(p interface{}) (err error) {
return status.Errorf(codes.Unknown, "panic triggered: %v", p)
}
recoveryOpts := []grpc_recovery.Option{
grpc_recovery.WithRecoveryHandler(recoveryFunc),
}
// Logger settings
stdLogger := log.StandardLogger()
reportCaller := env.GetEnv("LOGGING_ENABLE_CALLER_TRACE", "false")
if reportCaller == "true" {
stdLogger.SetReportCaller(true)
}
logEntry := log.NewEntry(stdLogger)
s := grpc.NewServer(grpc.UnaryInterceptor(
grpc_middleware.ChainUnaryServer(
grpc_logrus.UnaryServerInterceptor(logEntry),
grpc_recovery.UnaryServerInterceptor(recoveryOpts...),
auth.UnaryInterceptor(kubeConfig, db, sysConfig)),
), grpc.StreamInterceptor(
grpc_middleware.ChainStreamServer(
grpc_logrus.StreamServerInterceptor(logEntry),
grpc_recovery.StreamServerInterceptor(recoveryOpts...),
auth.StreamingInterceptor(kubeConfig, db, sysConfig)),
), grpc.MaxRecvMsgSize(math.MaxInt64), grpc.MaxSendMsgSize(math.MaxInt64))
api.RegisterWorkflowTemplateServiceServer(s, server.NewWorkflowTemplateServer())
api.RegisterCronWorkflowServiceServer(s, server.NewCronWorkflowServer())
api.RegisterWorkflowServiceServer(s, server.NewWorkflowServer())
api.RegisterSecretServiceServer(s, server.NewSecretServer())
api.RegisterNamespaceServiceServer(s, server.NewNamespaceServer())
api.RegisterAuthServiceServer(s, server.NewAuthServer())
api.RegisterLabelServiceServer(s, server.NewLabelServer())
api.RegisterWorkspaceTemplateServiceServer(s, server.NewWorkspaceTemplateServer())
api.RegisterWorkspaceServiceServer(s, server.NewWorkspaceServer())
api.RegisterConfigServiceServer(s, server.NewConfigServer())
api.RegisterServiceServiceServer(s, server.NewServiceServer())
api.RegisterFileServiceServer(s, server.NewFileServer())
api.RegisterInferenceServiceServer(s, server.NewInferenceService())
go func() {
if err := s.Serve(lis); err != nil {
log.Fatalf("Failed to serve RPC server: %v", err)
}
log.Printf("Server finished")
}()
return s
}
func startHTTPProxy() {
endpoint := "localhost" + *rpcPort
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Register gRPC server endpoint
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(customHeaderMatcher))
opts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(math.MaxInt64),
grpc.MaxCallRecvMsgSize(math.MaxInt64))}
registerHandler(api.RegisterWorkflowTemplateServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterWorkflowServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterCronWorkflowServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterSecretServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterNamespaceServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterAuthServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterLabelServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterWorkspaceTemplateServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterWorkspaceServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterConfigServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterServiceServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterFileServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
registerHandler(api.RegisterInferenceServiceHandlerFromEndpoint, ctx, mux, endpoint, opts)
log.Printf("Starting HTTP proxy on port %v", *httpPort)
// Allow all origins
ogValidator := func(str string) bool {
return true
}
// Allow Content-Type for JSON
allowedHeaders := handlers.AllowedHeaders([]string{"Content-Type", "Authorization"})
// Allow PUT. Have to include all others as it clears them out.
allowedMethods := handlers.AllowedMethods([]string{"HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"})
if err := http.ListenAndServe(*httpPort, wsproxy.WebsocketProxy(
handlers.CORS(
handlers.AllowedOriginValidator(ogValidator), allowedHeaders, allowedMethods)(mux),
wsproxy.WithTokenCookieName("auth-token"),
)); err != nil {
log.Fatalf("Failed to serve HTTP listener: %v", err)
}
}
type registerFunc func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error
func registerHandler(register registerFunc, ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) {
err := register(ctx, mux, endpoint, opts)
if err != nil {
log.Fatalf("Failed to register handler: %v", err)
}
}
// watchConfigmapChanges sets up a listener for configmap changes and calls the onChange function when it happens
func watchConfigmapChanges(namespace string, stopCh <-chan struct{}, onChange func(*corev1.ConfigMap) error) {
client, err := kubernetes.NewForConfig(v1.NewConfig())
if err != nil {
return
}
restClient := client.CoreV1().RESTClient()
resource := "configmaps"
fieldSelector := fields.ParseSelectorOrDie(fmt.Sprintf("metadata.name=%s", "onepanel"))
listFunc := func(options apiv1.ListOptions) (k8runtime.Object, error) {
options.FieldSelector = fieldSelector.String()
req := restClient.Get().
Namespace(namespace).
Resource(resource).
VersionedParams(&options, apiv1.ParameterCodec)
return req.Do().Get()
}
watchFunc := func(options apiv1.ListOptions) (watch.Interface, error) {
options.Watch = true
options.FieldSelector = fieldSelector.String()
req := restClient.Get().
Namespace(namespace).
Resource(resource).
VersionedParams(&options, apiv1.ParameterCodec)
return req.Watch()
}
source := &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
_, controller := cache.NewInformer(
source,
&corev1.ConfigMap{},
0,
cache.ResourceEventHandlerFuncs{
UpdateFunc: func(old, new interface{}) {
oldCM := old.(*corev1.ConfigMap)
newCM := new.(*corev1.ConfigMap)
if oldCM.ResourceVersion == newCM.ResourceVersion {
return
}
if newCm, ok := new.(*corev1.ConfigMap); ok {
log.Infof("Detected ConfigMap update.")
if err := onChange(newCm); err != nil {
log.Errorf("Error on calling onChange callback: %v", err)
}
}
},
})
// We don't want the watcher to ever stop, so give it a channel that will never be hit.
neverStopCh := make(chan struct{})
controller.Run(neverStopCh)
}
// customHeaderMatcher is used to allow certain headers so we don't require a grpc-gateway prefix
func customHeaderMatcher(key string) (string, bool) {
lowerCaseKey := strings.ToLower(key)
switch lowerCaseKey {
case "onepanel-auth-token":
return lowerCaseKey, true
case "onepanel-access-token":
return lowerCaseKey, true
case "cookie":
return lowerCaseKey, true
default:
return runtime.DefaultHeaderMatcher(key)
}
}