-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathmysqlcluster.go
294 lines (244 loc) · 9.32 KB
/
mysqlcluster.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
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
284
285
286
287
288
289
290
291
292
293
294
/*
Copyright 2018 Pressinfra SRL
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 mysqlcluster
import (
"fmt"
"strings"
"github.com/blang/semver"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
api "github.com/bitpoke/mysql-operator/pkg/apis/mysql/v1alpha1"
"github.com/bitpoke/mysql-operator/pkg/options"
"github.com/bitpoke/mysql-operator/pkg/util/constants"
)
// MysqlCluster is the wrapper for api.MysqlCluster type
type MysqlCluster struct {
*api.MysqlCluster
}
// NodeInitializedConditionType is the extended new pod condition that marks the pod as initialized from MySQL
// point of view.
const NodeInitializedConditionType core.PodConditionType = "mysql.presslabs.org/NodeInitialized"
// New returns a wrapper for mysqlcluster
func New(mc *api.MysqlCluster) *MysqlCluster {
return &MysqlCluster{
MysqlCluster: mc,
}
}
// Unwrap returns the api mysqlcluster object
func (c *MysqlCluster) Unwrap() *api.MysqlCluster {
return c.MysqlCluster
}
// GetLabels returns cluster labels
func (c *MysqlCluster) GetLabels() labels.Set {
instance := c.Name
if inst, ok := c.Annotations["app.kubernetes.io/instance"]; ok {
instance = inst
}
component := "database"
if comp, ok := c.Annotations["app.kubernetes.io/component"]; ok {
component = comp
}
labels := labels.Set{
"mysql.presslabs.org/cluster": c.Name,
"app.kubernetes.io/name": fmt.Sprintf("%s-mysql-headless", c.Name),
"app.kubernetes.io/instance": instance,
"app.kubernetes.io/version": c.GetMySQLSemVer().String(),
"app.kubernetes.io/component": component,
"app.kubernetes.io/managed-by": "mysql.presslabs.org",
}
if part, ok := c.Annotations["app.kubernetes.io/part-of"]; ok {
labels["app.kubernetes.io/part-of"] = part
}
return labels
}
// GetSelectorLabels returns the labels that will be used as selector
func (c *MysqlCluster) GetSelectorLabels() labels.Set {
return labels.Set{
"mysql.presslabs.org/cluster": c.Name,
"app.kubernetes.io/name": fmt.Sprintf("%s-mysql-headless", c.Name),
"app.kubernetes.io/managed-by": "mysql.presslabs.org",
}
}
// ResourceName is the type for aliasing resources that will be created.
type ResourceName string
const (
// OldHeadlessSVC is the name of the old headless service
// DEPRECATED
OldHeadlessSVC = "old-headless"
// HeadlessSVC is the alias of the headless service resource
HeadlessSVC ResourceName = "headless"
// StatefulSet is the alias of the statefulset resource
StatefulSet ResourceName = "mysql"
// ConfigMap is the alias for mysql configs, the config map resource
ConfigMap ResourceName = "config-files"
// MasterService is the name of the service that points to master node
MasterService ResourceName = "master-service"
// HealthyReplicasService is the name of a service that points healthy replicas (excludes master)
HealthyReplicasService ResourceName = "healthy-replicas-service"
// HealthyNodesService is the name of a service that contains all healthy nodes
HealthyNodesService ResourceName = "healthy-nodes-service"
// PodDisruptionBudget is the name of pod disruption budget for the stateful set
PodDisruptionBudget ResourceName = "pdb"
// Secret is the name of the "private" secret that contains operator related credentials
Secret ResourceName = "operated-secret"
)
// GetNameForResource returns the name of a resource from above
func (c *MysqlCluster) GetNameForResource(name ResourceName) string {
return GetNameForResource(name, c.Name)
}
// GetNameForResource returns the name of a resource for a cluster
func GetNameForResource(name ResourceName, clusterName string) string {
switch name {
case StatefulSet, ConfigMap, HealthyNodesService, PodDisruptionBudget:
return fmt.Sprintf("%s-mysql", clusterName)
case MasterService:
return fmt.Sprintf("%s-mysql-master", clusterName)
case HealthyReplicasService:
return fmt.Sprintf("%s-mysql-replicas", clusterName)
case HeadlessSVC:
return fmt.Sprintf("%s-mysql-headless", clusterName)
case OldHeadlessSVC:
return fmt.Sprintf("%s-mysql-nodes", clusterName)
case Secret:
return fmt.Sprintf("%s-mysql-operated", clusterName)
default:
return fmt.Sprintf("%s-mysql", clusterName)
}
}
// GetPodHostname returns for an index the pod hostname of a cluster
func (c *MysqlCluster) GetPodHostname(p int) string {
return fmt.Sprintf("%s-%d.%s.%s", c.GetNameForResource(StatefulSet), p,
c.GetNameForResource(HeadlessSVC),
c.Namespace)
}
// GetClusterAlias returns the cluster alias that as it is in orchestrator
func (c *MysqlCluster) GetClusterAlias() string {
return fmt.Sprintf("%s.%s", c.Name, c.Namespace)
}
// GetMasterHost returns name of current master host in a cluster
func (c *MysqlCluster) GetMasterHost() string {
masterHost := c.GetPodHostname(0)
for _, ns := range c.Status.Nodes {
if cond := c.GetNodeCondition(ns.Name, api.NodeConditionMaster); cond != nil &&
cond.Status == core.ConditionTrue {
masterHost = ns.Name
}
}
return masterHost
}
// GetMySQLSemVer returns the MySQL server version in semver format, or the default one
func (c *MysqlCluster) GetMySQLSemVer() semver.Version {
version := c.Spec.MysqlVersion
// lookup for an alias, usually this will solve 5.7 to 5.7.x
if v, ok := constants.MySQLTagsToSemVer[version]; ok {
version = v
}
sv, err := semver.Make(version)
if err != nil {
log.Error(err, "failed to parse given MySQL version", "input", version)
}
// if there is an error will return 0.0.0
return sv
}
// GetMysqlImage returns the mysql image for current mysql cluster
func (c *MysqlCluster) GetMysqlImage() string {
if len(c.Spec.Image) != 0 {
return c.Spec.Image
}
// check if the user set some overrides
opt := options.GetOptions()
if img, ok := opt.MySQLVersionImageOverride[c.GetMySQLSemVer().String()]; ok {
return img
}
if img, ok := constants.MysqlImageVersions[c.GetMySQLSemVer().String()]; ok {
return img
}
// this means the cluster has a wrong MysqlVersion set
log.Error(nil, "no image found with given MySQL version, the image can manually be set by setting .spec.image on cluster",
"version", c.GetMySQLSemVer())
return ""
}
// UpdateSpec updates the cluster specs that need to be saved
func (c *MysqlCluster) UpdateSpec() {
// TODO: remove this in next major release (v0.4)
if len(c.Spec.InitBucketURL) == 0 {
c.Spec.InitBucketURL = c.Spec.InitBucketURI
}
}
// IsPerconaImage checks the MySQL image is percona or not
func (c *MysqlCluster) IsPerconaImage() bool {
return strings.Contains(c.GetMysqlImage(), "percona")
}
// ShouldHaveInitContainerForMysql checks the MySQL version and returns true or false if the docker image supports or not init only
func (c *MysqlCluster) ShouldHaveInitContainerForMysql() bool {
expectedRange := semver.MustParseRange(">=5.7.26 <8.0.0 || >=8.0.15")
return c.IsPerconaImage() && expectedRange(c.GetMySQLSemVer())
}
// String returns the cluster name and namespace
func (c *MysqlCluster) String() string {
return fmt.Sprintf("%s/%s", c.Namespace, c.Name)
}
// ExporterDataSourcePort returns a MySQL port mysqld-exporter should connect to.
// Returns `extra_port` if defined in the cluster spec and if `extra_max_connections`
// is defined and larger than the default 1. Otherwise, returns the default MySQL port.
// https://www.percona.com/doc/percona-server/5.7/performance/threadpool.html#extra_port
func (c *MysqlCluster) ExporterDataSourcePort() int {
extraPortSettings := []string{"extra_port", "extra-port"}
extraMaxConnectionsSettings := []string{"extra_max_connections", "extra-max-connections"}
for _, setting := range extraPortSettings {
if port, ok := c.Spec.MysqlConf[setting]; ok {
for _, setting := range extraMaxConnectionsSettings {
if conns, ok := c.Spec.MysqlConf[setting]; ok && conns.IntValue() > 1 {
return port.IntValue()
}
}
}
}
return constants.MysqlPort
}
// GetNamespacedName return the cluster key. Usually used for logging or for runtime.Client.Get as key
func (c *MysqlCluster) GetNamespacedName() types.NamespacedName {
return types.NamespacedName{
Namespace: c.Namespace,
Name: c.Name,
}
}
// GetSidecarImage selects the sidecar docker image based on mysql version
func (c *MysqlCluster) GetSidecarImage() string {
// decide the sidecar image based on mysql version
sidecarImage := options.GetOptions().SidecarMysql57Image
if c.GetMySQLSemVer().Major == 8 {
sidecarImage = options.GetOptions().SidecarMysql8Image
}
return sidecarImage
}
// IsClusterReady checks if the cluster is ready or not.
func (c *MysqlCluster) IsClusterReady() bool {
isReady := false
for _, cond := range c.Status.Conditions {
if cond.Type == api.ClusterConditionReady && cond.Status == core.ConditionTrue {
isReady = true
break
}
}
return isReady
}
// IsMysqlClusterKind for the given kind checks if CRD kind is for MysqlCluster CRD
func IsMysqlClusterKind(kind string) bool {
switch kind {
case "MysqlCluster", "mysqlcluster", "mysqlclusters":
return true
}
return false
}