-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
datastore.go
209 lines (187 loc) · 5.55 KB
/
datastore.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
package scheduler
import (
"strconv"
mesosproto "github.com/AVENTER-UG/mesos-m3s/proto"
cfg "github.com/AVENTER-UG/mesos-m3s/types"
logrus "github.com/AVENTER-UG/mesos-m3s/logger"
)
// StartDatastore is starting the datastore container
func (e *Scheduler) StartDatastore(taskID string) {
if e.Redis.CountRedisKey(e.Framework.FrameworkName+":datastore:*", "") >= e.Config.DSMax {
return
}
cmd := e.defaultCommand(taskID)
cmd.ContainerType = "DOCKER"
cmd.Privileged = false
cmd.Memory = e.Config.DSMEM
cmd.CPU = e.Config.DSCPU
cmd.Disk = e.Config.DSDISK
cmd.TaskName = e.Framework.FrameworkName + ":datastore"
cmd.Hostname = e.Framework.FrameworkName + "datastore" + e.Config.Domain
cmd.DockerParameter = e.addDockerParameter(make([]mesosproto.Parameter, 0), mesosproto.Parameter{Key: "cap-add", Value: "NET_ADMIN"})
cmd.Instances = e.Config.DSMax
cmd.Shell = false
// if mesos cni is unset, then use docker cni
if e.Framework.MesosCNI == "" {
// net-alias is only supported onuser-defined networks
if e.Config.DockerCNI != "bridge" {
cmd.DockerParameter = e.addDockerParameter(cmd.DockerParameter, mesosproto.Parameter{Key: "net-alias", Value: e.Framework.FrameworkName + "datastore"})
}
}
// if we use etcd as datastore
if e.Config.DSEtcd {
e.setETCD(&cmd)
}
// if we use mysql/maraidb as datastore
if e.Config.DSMySQL {
e.setMySQL(&cmd)
}
protocol := "tcp"
containerPort, _ := strconv.ParseUint(e.Config.DSPort, 10, 32)
cmd.DockerPortMappings = []mesosproto.ContainerInfo_DockerInfo_PortMapping{
{
HostPort: 0,
ContainerPort: uint32(containerPort),
Protocol: &protocol,
},
}
cmd.Discovery = mesosproto.DiscoveryInfo{
Visibility: 2,
Name: &cmd.TaskName,
Ports: &mesosproto.Ports{
Ports: []mesosproto.Port{
{
Number: cmd.DockerPortMappings[0].HostPort,
Name: func() *string { x := "datastore"; return &x }(),
Protocol: cmd.DockerPortMappings[0].Protocol,
},
},
},
}
// store mesos task in DB
logrus.WithField("func", "StartDatastore").Info("Schedule Datastore")
e.Redis.SaveTaskRedis(cmd)
}
// healthCheckDatastore check the health of all datastore ervers. Return true if all are fine.
func (e *Scheduler) healthCheckDatastore() bool {
// Hold the at all state of the datastore service.
dsState := false
keys := e.Redis.GetAllRedisKeys(e.Framework.FrameworkName + ":datastore:*")
for keys.Next(e.Redis.CTX) {
key := e.Redis.GetRedisKey(keys.Val())
task := e.Mesos.DecodeTask(key)
if task.State == "TASK_RUNNING" && len(task.NetworkInfo) > 0 {
// if the framework is running as container, and the task hostname is the same like the frameworks one,
// then use the containerport instead of the random hostport
dsState = true
}
}
return dsState
}
// set mysql parameter of the mesos task
func (e *Scheduler) setMySQL(cmd *cfg.Command) {
cmd.ContainerImage = e.Config.ImageMySQL
//cmd.Shell = true
// Enable TLS for Mariadb
if e.Config.DSMySQLSSL {
cmd.Arguments = e.appendString(make([]string, 0), "--ssl-ca=/var/lib/mysql/ca.pem")
cmd.Arguments = e.appendString(cmd.Arguments, "--ssl-cert=/var/lib/mysql/server-cert.pem")
cmd.Arguments = e.appendString(cmd.Arguments, "--ssl-key=/var/lib/mysql/server-key.pem")
}
cmd.Environment.Variables = []mesosproto.Environment_Variable{
{
Name: "SERVICE_NAME",
Value: &cmd.TaskName,
},
{
Name: "MYSQL_ROOT_PASSWORD",
Value: func() *string {
x := e.Config.DSMySQLPassword
return &x
}(),
},
{
Name: "MYSQL_DATABASE",
Value: func() *string {
x := "k3s"
return &x
}(),
},
{
Name: "TZ",
Value: &e.Config.TimeZone,
},
{
Name: "MESOS_TASK_ID",
Value: &cmd.TaskID,
},
}
cmd.Volumes = []mesosproto.Volume{
{
ContainerPath: "/var/lib/mysql",
Mode: mesosproto.RW.Enum(),
Source: &mesosproto.Volume_Source{
Type: mesosproto.Volume_Source_DOCKER_VOLUME,
DockerVolume: &mesosproto.Volume_Source_DockerVolume{
Driver: &e.Config.VolumeDriver,
Name: e.Config.VolumeDS,
},
},
},
}
cmd.EnableHealthCheck = true
cmd.Health.Command = &mesosproto.CommandInfo{
Value: func() *string {
x := "mysqladmin ping -h localhost"
return &x
}(),
}
}
// set etcd parameter of the mesos task
func (e *Scheduler) setETCD(cmd *cfg.Command) {
cmd.ContainerImage = e.Config.ImageETCD
cmd.Command = "/usr/local/bin/etcd --listen-client-urls http://0.0.0.0:" + e.Config.DSPort + " --election-timeout '50000' --heartbeat-interval '5000'"
cmd.Shell = true
AdvertiseURL := "http://" + cmd.Hostname + ":" + e.Config.DSPort
AllowNoneAuthentication := "yes"
cmd.Environment.Variables = []mesosproto.Environment_Variable{
{
Name: "SERVICE_NAME",
Value: &cmd.TaskName,
},
{
Name: "ALLOW_NONE_AUTHENTICATION",
Value: &AllowNoneAuthentication,
},
{
Name: "ETCD_ADVERTISE_CLIENT_URLS",
Value: &AdvertiseURL,
},
{
Name: "MESOS_TASK_ID",
Value: &cmd.TaskID,
},
}
cmd.Volumes = []mesosproto.Volume{
{
ContainerPath: "/default.etcd",
Mode: mesosproto.RW.Enum(),
Source: &mesosproto.Volume_Source{
Type: mesosproto.Volume_Source_DOCKER_VOLUME,
DockerVolume: &mesosproto.Volume_Source_DockerVolume{
Driver: &e.Config.VolumeDriver,
Name: e.Config.VolumeDS,
},
},
},
}
cmd.EnableHealthCheck = true
cmd.Health.DelaySeconds = func() *float64 { x := 60.0; return &x }()
cmd.Health.Command = &mesosproto.CommandInfo{
Environment: &cmd.Environment,
Value: func() *string {
x := "etcdctl endpoint health --endpoints=http://127.0.0.1:" + e.Config.DSPort
return &x
}(),
}
}