forked from bitpoke/mysql-operator
/
mysqlcluster.go
223 lines (182 loc) · 6.87 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
/*
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"
"github.com/presslabs/mysql-operator/pkg/options"
"strings"
"github.com/blang/semver"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
api "github.com/presslabs/mysql-operator/pkg/apis/mysql/v1alpha1"
"github.com/presslabs/mysql-operator/pkg/util/constants"
)
const (
// HeadlessSVCName is the name of the headless service that is commonly used for all clusters
HeadlessSVCName = "mysql"
)
// 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": "mysql",
"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": "mysql",
"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"
// HealthyNodesService is the name of a service that continas 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 HeadlessSVC:
return HeadlessSVCName
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.mysqlImage 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
}
}
// 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 strings.Contains(c.GetMysqlImage(), "percona") && expectedRange(c.GetMySQLSemVer())
}