-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathmain.go
More file actions
283 lines (243 loc) · 8.32 KB
/
main.go
File metadata and controls
283 lines (243 loc) · 8.32 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
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
// Copyright 2019 HAProxy Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"syscall"
_ "github.com/KimMachineGun/automemlimit"
loads "github.com/go-openapi/loads"
"github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/security"
"github.com/haproxytech/client-native/v6/models"
"github.com/haproxytech/client-native/v6/storage"
"github.com/haproxytech/dataplaneapi"
"github.com/haproxytech/dataplaneapi/configuration"
"github.com/haproxytech/dataplaneapi/log"
"github.com/haproxytech/dataplaneapi/operations"
socket_runtime "github.com/haproxytech/dataplaneapi/runtime"
flags "github.com/jessevdk/go-flags"
"github.com/joho/godotenv"
_ "go.uber.org/automaxprocs"
)
// GitRepo ...
var GitRepo = ""
// GitTag ...
var GitTag = ""
// GitCommit ...
var GitCommit = "dev"
// GitDirty ...
var GitDirty = ".dirty"
// BuildTime ...
var BuildTime = ""
var cliOptions struct {
Version bool `short:"v" long:"version" description:"Version and build information"`
}
func main() {
_ = godotenv.Load()
cancelDebugServer := startRuntimeDebugServer()
cfg := configuration.Get()
for {
restart := startServer(cfg, cancelDebugServer)
if !restart.Load() {
break
}
}
}
func startRuntimeDebugServer() context.CancelFunc {
ctx := context.Background()
ctx, cancelDebugServer := context.WithCancel(ctx)
debugServer := socket_runtime.GetServer()
debugServer.DAPIVersion = fmt.Sprintf("%s %s%s", GitTag, GitCommit, GitDirty)
go debugServer.Start(ctx, cancelDebugServer)
return cancelDebugServer
}
func startServer(cfg *configuration.Configuration, cancelDebugServer context.CancelFunc) (reload configuration.AtomicBool) { //nolint: maintidx
swaggerSpec, err := loads.Embedded(dataplaneapi.SwaggerJSON, dataplaneapi.FlatSwaggerJSON)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dataplaneapi.BuildTime = BuildTime
dataplaneapi.Version = fmt.Sprintf("%s %s%s", GitTag, GitCommit, GitDirty)
api := operations.NewDataPlaneAPI(swaggerSpec)
server := dataplaneapi.NewServer(api)
parser := flags.NewParser(server, flags.Default)
parser.ShortDescription = "HAProxy Data Plane API"
parser.LongDescription = "API for editing and managing haproxy instances"
server.ConfigureFlags()
for _, optsGroup := range api.CommandLineOptionsGroups {
_, err = parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
_, err = parser.AddGroup("Show version", "Show build and version information", &cliOptions)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if _, err = parser.Parse(); err != nil {
if fe, ok := err.(*flags.Error); ok {
if fe.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Println(err)
os.Exit(1)
}
}
}
if cliOptions.Version {
fmt.Printf("HAProxy Data Plane API %s %s%s\n\n", GitTag, GitCommit, GitDirty)
fmt.Printf("Build from: %s\n", GitRepo)
fmt.Printf("Build date: %s\n\n", BuildTime)
return reload
}
var loadMsg []string
_, err = cfg.Load()
if err != nil {
fmt.Println("configuration error:", err)
os.Exit(1)
}
if cfg.HAProxy.UID != 0 {
if err = syscall.Setuid(cfg.HAProxy.UID); err != nil {
fmt.Println("set uid:", err)
os.Exit(1)
}
}
if cfg.HAProxy.GID != 0 {
if err = syscall.Setgid(cfg.HAProxy.GID); err != nil {
fmt.Println("set gid:", err)
os.Exit(1)
}
}
loadMsg, err = cfg.LoadDataplaneStorageConfig()
if err != nil {
fmt.Println("configuration error:", err)
os.Exit(1)
}
cfg.FlagLoadDapiStorageData = true
// incorporate changes from file to global settings
dataplaneapi.SyncWithFileSettings(server, cfg)
err = cfg.LoadRuntimeVars(dataplaneapi.SwaggerJSON, server.Host, server.Port)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configuration.HandlePIDFile(cfg.HAProxy)
if cfg.Mode.Load() == configuration.ModeCluster {
if cfg.Cluster.CertificateFetched.Load() {
log.Info("HAProxy Data Plane API in cluster mode")
server.TLSCertificate = flags.Filename(path.Join(cfg.GetClusterCertDir(), fmt.Sprintf("dataplane-%s.crt", cfg.Name.Load())))
server.TLSCertificateKey = flags.Filename(path.Join(cfg.GetClusterCertDir(), fmt.Sprintf("dataplane-%s.key", cfg.Name.Load())))
server.EnabledListeners = []string{"https"}
if server.TLSPort == 0 {
server.TLSPort = server.Port
}
// override storage dir location
storageDir := cfg.Cluster.StorageDir.Load()
if storageDir != "" {
cfg.HAProxy.MapsDir = path.Join(storageDir, string(storage.MapsType))
cfg.HAProxy.SSLCertsDir = path.Join(storageDir, string(storage.SSLType))
cfg.HAProxy.GeneralStorageDir = path.Join(storageDir, string(storage.GeneralType))
cfg.HAProxy.DataplaneStorageDir = filepath.Clean(path.Join(storageDir, "dataplane"))
cfg.HAProxy.SpoeDir = path.Join(storageDir, string(storage.SpoeType))
cfg.HAProxy.SpoeTransactionDir = path.Join(storageDir, string(storage.SpoeTransactionsType))
cfg.HAProxy.BackupsDir = path.Join(storageDir, string(storage.BackupsType))
cfg.HAProxy.TransactionDir = path.Join(storageDir, string(storage.TransactionsType))
// dataplane internal
cfg.HAProxy.ClusterTLSCertDir = path.Join(storageDir, "certs-cluster")
cfg.Cluster.CertificateDir.Store(path.Join(storageDir, "certs-cluster"))
}
} else if cfg.Cluster.ActiveBootstrapKey.Load() != "" {
cfg.Notify.BootstrapKeyChanged.NotifyWithRetry()
}
}
clusterLogTargets := parseClusterLogTargets(cfg)
if err = log.InitWithConfiguration(cfg.LogTargets, cfg.Logging, cfg.Syslog, clusterLogTargets, cfg.Cluster.ID.Load()); err != nil {
fmt.Println(err)
os.Exit(1)
}
cfg.InitSignalHandler()
log.Infof("HAProxy Data Plane API %s %s%s", GitTag, GitCommit, GitDirty)
log.Infof("Build from: %s", GitRepo)
log.Infof("Build date: %s", BuildTime)
log.Infof("Reload strategy: %s", cfg.HAProxy.ReloadStrategy)
// log deprecation message
if len(loadMsg) > 0 {
for _, msg := range loadMsg {
log.Warning(msg)
}
}
err = cfg.Save()
if err != nil {
log.Fatalf("Error saving configuration: %s", err.Error())
}
// Applies when the Authorization header is set with the Basic scheme
api.BasicAuthAuth = configuration.AuthenticateUser
api.BasicAuthenticator = func(authentication security.UserPassAuthentication) runtime.Authenticator {
// if mTLS is enabled with backing Certificate Authority, skipping basic authentication
if len(server.TLSCACertificate) > 0 && server.TLSPort > 0 {
return runtime.AuthenticatorFunc(func(i any) (bool, any, error) {
return true, "", nil
})
}
return security.BasicAuthRealm("", authentication)
}
dataplaneapi.ContextHandler.Init()
go func() {
<-cfg.Notify.Reload.Subscribe("main")
log.Info("HAProxy Data Plane API reloading")
reload.Store(true)
cfg.UnSubscribeAll()
cfg.StopSignalHandler()
dataplaneapi.ContextHandler.Cancel()
err = server.Shutdown()
if err != nil {
log.Fatalf("Error reloading HAProxy Data Plane API: %s", err.Error())
}
}()
go func() {
select {
case <-cfg.Notify.Shutdown.Subscribe("main"):
log.Info("HAProxy Data Plane API shutting down")
err = server.Shutdown()
if err != nil {
log.Fatalf("Error shutting down HAProxy Data Plane API: %s", err.Error())
}
cancelDebugServer()
os.Exit(0)
case <-dataplaneapi.ContextHandler.Context().Done():
return
}
}()
server.ConfigureAPI()
dataplaneapi.SetServerStartedCallback(cfg.Notify.ServerStarted.Notify)
if err := server.Serve(); err != nil {
log.Fatalf("Error running HAProxy Data Plane API: %s", err.Error())
}
defer server.Shutdown() //nolint:errcheck
return reload
}
func parseClusterLogTargets(cfg *configuration.Configuration) []*models.ClusterLogTarget {
if cfg.Mode.Load() == "cluster" && cfg.Cluster.ClusterLogTargets != nil && len(cfg.Cluster.ClusterLogTargets) > 0 {
return cfg.Cluster.ClusterLogTargets
}
return []*models.ClusterLogTarget{}
}