-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.go
185 lines (156 loc) · 5.29 KB
/
app.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
package main
import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"net/http"
"time"
"github.com/AVENTER-UG/mesos-m3s/api"
"github.com/AVENTER-UG/mesos-m3s/controller"
logrus "github.com/AVENTER-UG/mesos-m3s/logger"
mesosproto "github.com/AVENTER-UG/mesos-m3s/proto"
"github.com/AVENTER-UG/mesos-m3s/redis"
"github.com/AVENTER-UG/mesos-m3s/scheduler"
cfg "github.com/AVENTER-UG/mesos-m3s/types"
util "github.com/AVENTER-UG/util/util"
)
// BuildVersion of m3s
var BuildVersion string
// GitVersion is the revision and commit number
var GitVersion string
// convert Base64 Encodes PEM Certificate to tls object
func decodeBase64Cert(pemCert string) []byte {
sslPem, err := base64.URLEncoding.DecodeString(pemCert)
if err != nil {
logrus.Fatal("Error decoding SSL PEM from Base64: ", err.Error())
}
return sslPem
}
func main() {
// Prints out current version
var version bool
flag.BoolVar(&version, "v", false, "Prints current version")
flag.Parse()
if version {
fmt.Print(GitVersion)
return
}
util.SetLogging(config.LogLevel, config.EnableSyslog, config.AppName)
logrus.Println(config.AppName + " build " + BuildVersion + " git " + GitVersion)
listen := fmt.Sprintf(":%s", framework.FrameworkPort)
failoverTimeout := 5000.0
checkpoint := true
webuiurl := fmt.Sprintf("http://%s%s", framework.FrameworkHostname, listen)
if config.SSLCrt != "" && config.SSLKey != "" {
webuiurl = fmt.Sprintf("https://%s%s", framework.FrameworkHostname, listen)
}
framework.CommandChan = make(chan cfg.Command, 100)
config.Hostname = framework.FrameworkHostname
config.Listen = listen
framework.State = map[string]cfg.State{}
framework.FrameworkInfo.User = util.StringToPointer(framework.FrameworkUser)
framework.FrameworkInfo.Name = util.StringToPointer(framework.FrameworkName)
framework.FrameworkInfo.Roles = []string{framework.FrameworkRole}
framework.FrameworkInfo.WebuiUrl = &webuiurl
framework.FrameworkInfo.FailoverTimeout = &failoverTimeout
framework.FrameworkInfo.Checkpoint = &checkpoint
framework.FrameworkInfo.Principal = &config.Principal
framework.FrameworkInfo.Capabilities = []*mesosproto.FrameworkInfo_Capability{
{
Type: mesosproto.FrameworkInfo_Capability_MULTI_ROLE.Enum(),
},
}
// connect to redis
r := redis.New(&config, &framework)
if !r.Connect() {
logrus.WithField("func", "main").Fatal("Could not connect to redis DB")
}
// load framework state from DB
var oldFramework cfg.FrameworkConfig
key := r.GetRedisKey(framework.FrameworkName + ":framework")
if key != "" {
json.Unmarshal([]byte(key), &oldFramework)
framework.FrameworkInfo.Id = oldFramework.FrameworkInfo.Id
framework.MesosStreamID = oldFramework.MesosStreamID
}
// restore variable data from the old config
var oldconfig cfg.Config
key = r.GetRedisKey(framework.FrameworkName + ":framework_config")
if key != "" {
json.Unmarshal([]byte(key), &oldconfig)
config.K3SServerContainerPort = oldconfig.K3SServerContainerPort
config.K3SServerHostname = oldconfig.K3SServerHostname
config.K3SServerPort = oldconfig.K3SServerPort
config.K3SServerURL = oldconfig.K3SServerURL
config.K3SAgentMax = oldconfig.K3SAgentMax
config.DSMax = oldconfig.DSMax
// Add Missing Restore Values to handle the usecase of starting a hibernated cluster after reboot/framework restarts
config.K3SServerMax = oldconfig.K3SServerMax
config.DSMaxRestore = oldconfig.DSMaxRestore
config.K3SServerMaxRestore = oldconfig.K3SServerMaxRestore
config.K3SAgentMaxRestore = oldconfig.K3SAgentMaxRestore
// Add DisableScheduling if set in existing config
config.K3SDisableScheduling = oldconfig.K3SDisableScheduling
}
r.SaveConfig(config)
r.SaveFrameworkRedis(&framework)
// set current m3s version
config.Version.M3SVersion.GitVersion = GitVersion
config.Version.M3SVersion.BuildDate = BuildVersion
// The Hostname should ever be set after reading the state file.
framework.FrameworkInfo.Hostname = &framework.FrameworkHostname
// get API
a := api.New(&config, &framework)
a.Redis = r
server := &http.Server{
Addr: listen,
Handler: a.Commands(),
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 2 * time.Second,
TLSConfig: &tls.Config{
ClientAuth: tls.RequestClientCert,
MinVersion: tls.VersionTLS12,
},
}
if config.SSLCrt != "" && config.SSLKey != "" {
logrus.WithField("func", "main").Debug("Enable TLS")
crt := decodeBase64Cert(config.SSLCrt)
key := decodeBase64Cert(config.SSLKey)
certs, err := tls.X509KeyPair(crt, key)
if err != nil {
logrus.WithField("func", "main.main").Fatal("TLS Server Error: ", err.Error())
}
server.TLSConfig.Certificates = []tls.Certificate{certs}
}
go func() {
if config.SSLCrt != "" && config.SSLKey != "" {
server.ListenAndServeTLS("", "")
} else {
server.ListenAndServe()
}
}()
go loadPlugins(r)
// create a kubernetes client
k8 := controller.New(&config, &framework)
k8.Redis = r
// add kubernetes pointer to API
a.Kubernetes = k8
// this loop is for resubscribtion purpose
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
// nolint:gosimple
for {
select {
case <-ticker.C:
e := scheduler.Subscribe(&config, &framework)
e.API = a
e.Redis = r
e.Kubernetes = k8
e.EventLoop()
}
}
}