-
Notifications
You must be signed in to change notification settings - Fork 69
/
mysqlctld.go
167 lines (154 loc) · 5.26 KB
/
mysqlctld.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
/*
Copyright 2019 PlanetScale Inc.
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 vttablet
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/pointer"
planetscalev2 "planetscale.dev/vitess-operator/pkg/apis/planetscale/v2"
"planetscale.dev/vitess-operator/pkg/operator/lazy"
)
const (
vtRootInitScript = `set -ex
mkdir -p /mnt/vt/bin
cp --no-clobber /vt/bin/mysqlctld /mnt/vt/bin/
mkdir -p /mnt/vt/config
if [[ -d /vt/config/mycnf ]]; then
cp --no-clobber -R /vt/config/mycnf /mnt/vt/config/
else
mkdir -p /mnt/vt/config/mycnf
fi
mkdir -p /mnt/vt/vtdataroot
ln -sf /dev/stderr /mnt/vt/config/stderr.symlink
echo "log-error = /vt/config/stderr.symlink" > /mnt/vt/config/mycnf/log-error.cnf
echo "binlog_format=row" > /mnt/vt/config/mycnf/rbr.cnf
echo "socket = ` + mysqlSocketPath + `" > /mnt/vt/config/mycnf/socket.cnf
`
mysqlSocketInitScript = `set -ex
cd ` + vtDataRootPath + `
for mycnf in $(find . -mindepth 2 -maxdepth 2 -path './vt_*/my.cnf'); do
sed -i -e 's,^socket[ \t]*=.*$,socket = ` + mysqlSocketPath + `,' "${mycnf}"
done
`
)
func init() {
// Copy Vitess files needed by mysqlctld into the mysqld container,
// which might be using a stock MySQL image.
tabletVolumes.Add(func(s lazy.Spec) []corev1.Volume {
return []corev1.Volume{
{
Name: vtRootVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
}
})
tabletInitContainers.Add(func(s lazy.Spec) []corev1.Container {
spec := s.(*Spec)
securityContext := &corev1.SecurityContext{}
if planetscalev2.DefaultVitessRunAsUser >= 0 {
securityContext.RunAsUser = pointer.Int64Ptr(planetscalev2.DefaultVitessRunAsUser)
}
// Use an init container to copy only the files we need from the Vitess image.
// Note specifically that we don't even copy init_db.sql to avoid accidentally using it.
initContainers := []corev1.Container{
{
Name: "init-vt-root",
SecurityContext: securityContext,
Image: spec.Images.Vttablet,
ImagePullPolicy: spec.ImagePullPolicies.Vttablet,
VolumeMounts: []corev1.VolumeMount{
{
Name: vtRootVolumeName,
MountPath: "/mnt/vt",
},
},
Command: []string{"bash", "-c"},
Args: []string{vtRootInitScript},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewMilliQuantity(planetscalev2.DefaultInitCPURequestMillis, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(planetscalev2.DefaultInitMemoryRequestBytes, resource.BinarySI),
},
},
},
}
// If we're using a PVC, add an init container to migrate the mysql UNIX
// socket location before vttablet and mysqlctld start up. This is
// needed to safely update tablet Pods with persistent volumes that were
// deployed before we started customizing the UNIX socket location.
if spec.DataVolumePVCSpec != nil {
initContainers = append(initContainers, corev1.Container{
Name: "init-mysql-socket",
SecurityContext: securityContext,
Image: spec.Images.Vttablet,
ImagePullPolicy: spec.ImagePullPolicies.Vttablet,
VolumeMounts: []corev1.VolumeMount{
{
Name: pvcVolumeName,
MountPath: vtDataRootPath,
SubPath: "vtdataroot",
},
},
Command: []string{"bash", "-c"},
Args: []string{mysqlSocketInitScript},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewMilliQuantity(planetscalev2.DefaultInitCPURequestMillis, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(planetscalev2.DefaultInitMemoryRequestBytes, resource.BinarySI),
},
},
})
}
return initContainers
})
// Add mysqld-specific volume mounts.
mysqldVolumeMounts.Add(func(s lazy.Spec) []corev1.VolumeMount {
return []corev1.VolumeMount{
{
Name: vtRootVolumeName,
ReadOnly: true,
MountPath: vtBinPath,
SubPath: "bin",
},
}
})
// Add the config mount to both mysqld and vttablet, to make sure they
// generate the same my.cnf.
tabletVolumeMounts.Add(func(s lazy.Spec) []corev1.VolumeMount {
return []corev1.VolumeMount{
{
Name: vtRootVolumeName,
ReadOnly: true,
MountPath: vtConfigPath,
SubPath: "config",
},
{
Name: vtRootVolumeName,
ReadOnly: false,
MountPath: vtSocketPath,
SubPath: "socket",
},
}
})
// Tell mysqld to log to stderr instead of a file, so we can rely on
// automatic rotation of container logs. Also configure the location of the
// UNIX socket file. These config files are written out by vtRootInitScript.
extraMyCnf.Add(func(s lazy.Spec) []string {
return []string{
vtMycnfPath + "/log-error.cnf",
vtMycnfPath + "/socket.cnf",
}
})
}