-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathmain.go
More file actions
215 lines (190 loc) · 7.72 KB
/
Copy pathmain.go
File metadata and controls
215 lines (190 loc) · 7.72 KB
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
package main
import (
"context"
"flag"
"fmt"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/beego/beego"
"github.com/beego/beego/logs"
"github.com/spf13/cobra"
logsapi "k8s.io/component-base/logs/api/v1"
"github.com/casosorg/casos/casdoor"
"github.com/casosorg/casos/conf"
"github.com/casosorg/casos/controllers"
"github.com/casosorg/casos/deploy"
"github.com/casosorg/casos/object"
"github.com/casosorg/casos/proxy"
"github.com/casosorg/casos/routers"
"github.com/casosorg/casos/server"
"github.com/casosorg/casos/util"
)
// Build metadata, set through -ldflags "-X main.version=..." when GoReleaser
// builds a release. A binary built any other way reports the fallbacks, which
// is how a development build tells itself apart from a released one.
var (
version = "dev"
commit = "none"
date = "unknown"
)
func versionString() string {
return fmt.Sprintf("casos %s (commit %s, built %s, %s %s/%s)",
version, commit, date, runtime.Version(), runtime.GOOS, runtime.GOARCH)
}
func init() {
// CasOS runs kube-scheduler and kube-controller-manager in-process, and both
// are cobra commands (see server.StartScheduler and
// server.StartControllerManager). On Windows, cobra assumes a cobra command
// launched from Explorer is a CLI the user double-clicked by mistake, and
// answers by printing a notice and calling os.Exit(1) — which takes the whole
// of CasOS down with it, seconds after an apparently successful startup, and
// only ever when started by double-click.
//
// CasOS is a server that merely embeds those commands, so that guard is never
// wanted here. Clearing the text disables it, and clearing it unconditionally
// means no second launch detector has to agree with cobra's for CasOS to
// survive.
cobra.MousetrapHelpText = ""
}
func main() {
// Allow multiple in-process Kubernetes components to reinitialise the global
// logging singleton without killing the process.
logsapi.ReapplyHandling = logsapi.ReapplyHandlingIgnoreUnchanged
// Registered before InitFlag, which is what parses the command line.
showVersion := flag.Bool("version", false, "print the CasOS version and exit")
object.InitFlag()
// Answered before any subsystem starts, so the flag works on a machine with
// no database, no config file and no writable data directory.
if *showVersion {
fmt.Println(versionString())
return
}
proxy.EnsureClusterNoProxy()
object.InitAdapter()
object.CreateTables()
// Before anything can read them: a Helm task still marked active belongs to
// a previous run of this process, and left alone it keeps a release stuck
// showing an install that nothing is performing any more.
if interrupted, err := object.FailInterruptedHelmOperationTasks(); err != nil {
logs.Warning("recover interrupted Helm operations: %v", err)
} else if interrupted > 0 {
logs.Info("marked %d interrupted Helm operation(s) as failed", interrupted)
}
object.InitSite()
object.InitUsers()
if err := object.SeedDefaultPolicies(); err != nil {
logs.Warning("casbin seed: %v", err)
}
if err := object.ReloadAllEnforcers(); err != nil {
logs.Warning("casbin enforcer init: %v", err)
}
casdoor.InitCasdoorConfig()
proxy.InitHttpClient()
srvCfg, err := server.ConfigFromAppConf()
if err != nil {
panic(err)
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
go shutdownOnSignal(ctx, stop)
deploy.Init(ctx, deploy.ConfigFromServerConfig(srvCfg))
readyCh, err := server.Start(ctx, srvCfg)
if err != nil {
panic(err)
}
controllers.SetServerConfig(&srvCfg)
if err := server.StartWebhookServer(srvCfg); err != nil {
logs.Warning("webhook server: %v", err)
}
go func() {
select {
case <-readyCh:
adminCfg := server.AdminRestConfig(srvCfg)
controllers.SetAdminRestConfig(adminCfg)
deploy.SetRestConfig(adminCfg)
logs.Info("apiserver ready — kubectl endpoint: https://127.0.0.1:%d", srvCfg.ApiserverPort)
if err := server.Bootstrap(ctx, adminCfg, srvCfg); err != nil {
logs.Warning("bootstrap: %v", err)
}
if srvCfg.ServiceLBEnabled {
if err := server.StartServiceLB(ctx, adminCfg, srvCfg); err != nil {
logs.Warning("start service load balancer: %v", err)
}
}
if err := server.StartScheduler(ctx, srvCfg); err != nil {
logs.Warning("start scheduler: %v", err)
}
if err := server.StartControllerManager(ctx, srvCfg); err != nil {
logs.Warning("start controller-manager: %v", err)
}
server.RegisterInstallImageVulnerabilityReporter()
// Last, because it deploys a worker node and therefore needs the
// scheduler, the controller-manager and the cluster networking that
// Bootstrap installs to be running already.
deploy.StartLocalNodeBootstrap()
case <-ctx.Done():
}
}()
routers.InitAPI()
apiserverOrigin := fmt.Sprintf("https://127.0.0.1:%d", srvCfg.ApiserverPort)
beego.InsertFilter("*", beego.BeforeRouter, routers.CorsFilter)
beego.InsertFilter("/k8s", beego.BeforeRouter, routers.K8sProxyFilter(apiserverOrigin))
beego.InsertFilter("/k8s/*", beego.BeforeRouter, routers.K8sProxyFilter(apiserverOrigin))
beego.InsertFilter("/", beego.BeforeRouter, routers.StaticFilter)
beego.InsertFilter("/*", beego.BeforeRouter, routers.StaticFilter)
beego.InsertFilter("/api/*", beego.BeforeRouter, routers.ApiFilter)
beego.BConfig.CopyRequestBody = true
beego.BConfig.EnableGzip = true
beego.BConfig.WebConfig.Session.SessionOn = true
beego.BConfig.WebConfig.Session.SessionProvider = "file"
beego.BConfig.WebConfig.Session.SessionProviderConfig = "./tmp"
beego.BConfig.WebConfig.Session.SessionGCMaxLifetime = 3600 * 24 * 365
port := conf.GetConfigIntDefault("httpport", defaultHTTPPort)
// beego.Run exits the process when the port is taken, so the port has to be
// free by the time it is called.
if stopped, err := util.ReclaimPort("", port); err != nil {
logs.Error("web server port: %v", err)
} else if stopped != "" {
logs.Warning("port %d was held by %s, which has been stopped so the web server can use it", port, stopped)
}
logs.Info("casos listening on :%d", port)
if util.StartedByDoubleClick() {
go openWhenReady(port)
}
beego.Run(fmt.Sprintf(":%v", port))
}
// defaultHTTPPort is where the web UI and the REST API listen when nothing
// sets httpport. It sits in the 20000 block CasOS reserves for its fixed
// listeners, for the reasons set out in server.defaultApiserverPort.
const defaultHTTPPort = 20080
// How long openWhenReady waits before telling the user CasOS did not come up.
// The web server binds within milliseconds of this timer starting; the
// allowance is for a machine still busy initialising the control plane, not for
// the listener itself.
const startupTimeout = 60 * time.Second
// openWhenReady opens the CasOS web UI once the server answers, and reports the
// failure in a dialog if it never does.
//
// It runs only for a CasOS started by double-clicking casos.exe. That user has
// no terminal to read a URL out of and no reason to expect one, and the console
// window they do get is buried under Kubernetes logs within seconds — so both
// the address and any startup failure have to reach them some other way.
func openWhenReady(port int) {
webURL := fmt.Sprintf("http://localhost:%d/", port)
if err := util.WaitForServer(webURL, startupTimeout); err != nil {
logs.Warning("startup: %v", err)
util.ReportStartupFailure(fmt.Sprintf(
"CasOS did not finish starting within %s.\n\n"+
"The CasOS console window shows what went wrong. The most common "+
"cause is port %d already being in use by another program — change "+
"httpport in conf/app.conf to use a different one.",
startupTimeout, port))
return
}
logs.Info("casos ready — opening %s", webURL)
if err := util.OpenBrowser(webURL); err != nil {
logs.Warning("open browser: %v", err)
}
}