-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
184 lines (154 loc) · 6.35 KB
/
main.go
File metadata and controls
184 lines (154 loc) · 6.35 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
package main
import (
"context"
"fmt"
"net/http"
"net/http/pprof"
"os"
"github.com/gorilla/mux"
"github.com/Scalingo/go-handlers"
"github.com/Scalingo/go-utils/errors/v2"
"github.com/Scalingo/go-utils/etcd"
"github.com/Scalingo/go-utils/logger"
"github.com/Scalingo/go-utils/logger/plugins/rollbarplugin"
"github.com/Scalingo/link/v3/config"
"github.com/Scalingo/link/v3/endpoint"
"github.com/Scalingo/link/v3/locker"
"github.com/Scalingo/link/v3/migrations"
"github.com/Scalingo/link/v3/models"
"github.com/Scalingo/link/v3/plugin"
"github.com/Scalingo/link/v3/plugin/arp"
outscalepublicip "github.com/Scalingo/link/v3/plugin/outscale_public_ip"
"github.com/Scalingo/link/v3/scheduler"
"github.com/Scalingo/link/v3/web"
)
// Version is the current LinK version. During release build it will be overwritten by the compiler
var Version = "dev"
func main() {
rollbarplugin.Register()
log := logger.Default()
ctx := logger.ToCtx(context.Background(), log)
config, err := config.Build(ctx)
if err != nil {
log.WithError(err).Error("Fail to init config")
panic(err)
}
log.WithField("hostname", config.Hostname).Info("LinK starting")
etcd, err := etcd.ClientFromEnv()
if err != nil {
log.WithError(err).Error("Fail to get etcd client")
panic(err)
}
storage := models.NewEtcdStorage(config)
leaseManager := locker.NewEtcdLeaseManager(ctx, config, storage, etcd)
encryptedStorage, err := models.NewEncryptedStorage(ctx, config, storage)
if err != nil {
log.WithError(err).Error("Fail to init encrypted storage")
panic(err)
}
pluginRegistry := plugin.NewRegistry()
err = initPlugins(ctx, pluginRegistry, encryptedStorage)
if err != nil {
log.WithError(err).Error("Fail to init plugins")
panic(err)
}
migrationRunner, err := migrations.NewMigrationRunner(ctx, config, storage, leaseManager)
if err != nil {
log.WithError(err).Error("Fail to init migration runner")
panic(err)
}
// We run the migration in a goroutine. Because the migrations can take a long time and locks might expires.
// This could cause unwanted failover.
// All major versions of LinK should be compatible with the previous and next major data version.
go func(ctx context.Context) {
err := migrationRunner.Run(ctx)
if err != nil {
log.WithError(err).Error("Fail to run migrations")
return
}
}(ctx)
err = leaseManager.Start(ctx)
if err != nil {
log.WithError(err).Error("Fail to start lease manager")
panic(err)
}
scheduler := scheduler.NewEndpointScheduler(config, etcd, storage, leaseManager, pluginRegistry)
endpoints, err := storage.GetEndpoints(ctx)
if err != nil {
log.WithError(err).Error("Fail to list configured endpoints")
panic(err)
}
if len(endpoints) > 0 {
log.Info("Restarting endpoint schedulers...")
for _, endpoint := range endpoints {
ctx, log := logger.WithStructToCtx(ctx, "endpoint", endpoint)
log.Info("Starting an endpoint scheduler")
_, err := scheduler.Start(ctx, endpoint)
if err != nil {
panic(err)
}
}
}
endpointCreator := endpoint.NewCreator(config, storage, scheduler, pluginRegistry)
ipController := web.NewIPController(scheduler, storage, endpointCreator)
endpointController := web.NewEndpointController(scheduler, storage, endpointCreator, encryptedStorage)
encryptedStorageController := web.NewEncryptedStorageController(encryptedStorage)
versionController := web.NewVersionController(Version)
r := handlers.NewRouter(log)
r.Use(handlers.ErrorMiddleware)
if config.User != "" || config.Password != "" {
r.Use(handlers.AuthMiddleware(func(user, password string) bool {
return user == config.User && password == config.Password
}))
}
// Retro compatibility with v2 API.
// This will be removed in a future version.
r.HandleFunc("/ips", ipController.List).Methods(http.MethodGet)
r.HandleFunc("/ips", ipController.Create).Methods(http.MethodPost)
r.HandleFunc("/ips/{id}", endpointController.Delete).Methods(http.MethodDelete)
r.HandleFunc("/ips/{id}", ipController.Get).Methods(http.MethodGet)
r.HandleFunc("/ips/{id}", endpointController.Update).Methods(http.MethodPut, http.MethodPatch)
r.HandleFunc("/ips/{id}/failover", endpointController.Failover).Methods(http.MethodPost)
r.HandleFunc("/endpoints", endpointController.List).Methods(http.MethodGet)
r.HandleFunc("/endpoints", endpointController.Create).Methods(http.MethodPost)
r.HandleFunc("/endpoints/{id}", endpointController.Delete).Methods(http.MethodDelete)
r.HandleFunc("/endpoints/{id}", endpointController.Get).Methods(http.MethodGet)
r.HandleFunc("/endpoints/{id}", endpointController.Update).Methods(http.MethodPut, http.MethodPatch)
r.HandleFunc("/endpoints/{id}/failover", endpointController.Failover).Methods(http.MethodPost)
r.HandleFunc("/endpoints/{id}/hosts", endpointController.GetHosts).Methods(http.MethodGet)
r.HandleFunc("/encrypted_storage/key_rotation", encryptedStorageController.RotateEncryptionKey).Methods(http.MethodPost)
r.HandleFunc("/version", versionController.Version).Methods(http.MethodGet)
globalRouter := mux.NewRouter()
if os.Getenv("PPROF_ENABLED") == "true" {
pprofPrefix := "/debug/pprof"
log.Info("Enabling pprof endpoints under " + pprofPrefix)
pprofRouter := mux.NewRouter()
pprofRouter.HandleFunc(pprofPrefix+"/", pprof.Index)
pprofRouter.HandleFunc(pprofPrefix+"/profile", pprof.Profile)
pprofRouter.HandleFunc(pprofPrefix+"/symbol", pprof.Symbol)
pprofRouter.HandleFunc(pprofPrefix+"/cmdline", pprof.Cmdline)
pprofRouter.HandleFunc(pprofPrefix+"/trace", pprof.Trace)
pprofRouter.Handle(pprofPrefix+"/heap", pprof.Handler("heap"))
pprofRouter.Handle(pprofPrefix+"/goroutine", pprof.Handler("goroutine"))
pprofRouter.Handle(pprofPrefix+"/threadcreate", pprof.Handler("threadcreate"))
pprofRouter.Handle(pprofPrefix+"/block", pprof.Handler("block"))
globalRouter.Handle(pprofPrefix+"/{prop:.*}", pprofRouter)
}
globalRouter.Handle("/{any:.+}", r)
log.Infof("Listening on %v", config.Port)
err = http.ListenAndServe(fmt.Sprintf(":%v", config.Port), globalRouter)
if err != nil {
panic(err)
}
}
func initPlugins(ctx context.Context, registry plugin.Registry, encryptedStorage models.EncryptedStorage) error {
err := arp.Register(ctx, registry)
if err != nil {
return errors.Wrap(ctx, err, "register arp plugin")
}
err = outscalepublicip.Register(ctx, registry, encryptedStorage)
if err != nil {
return errors.Wrap(ctx, err, "register outscale public ip plugin")
}
return nil
}