Skip to content

Commit de665c8

Browse files
committed
Add support for the official MySQL Docker image
1 parent f16e4fe commit de665c8

5 files changed

Lines changed: 77 additions & 15 deletions

File tree

build/ps-entrypoint.sh

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ CFG=/etc/my.cnf.d/node.cnf
139139
TLS_DIR=/etc/mysql/mysql-tls-secret
140140
CUSTOM_CONFIG_FILES=("/etc/mysql/config/auto-config.cnf" "/etc/mysql/config/my-config.cnf" "/etc/mysql/config/my-secret.cnf")
141141

142+
if [[ ${HOSTNAME} =~ "-xb-" ]]; then
143+
FQDN=${HOSTNAME}
144+
else
145+
CLUSTER_NAME="$(echo ${HOSTNAME%-[0-9]})"
146+
SERVER_NUM=${HOSTNAME/$CLUSTER_NAME-/}
147+
SERVER_ID=${CLUSTER_HASH}${SERVER_NUM}
148+
FQDN="${HOSTNAME}.${SERVICE_NAME}.$(</var/run/secrets/kubernetes.io/serviceaccount/namespace)"
149+
fi
150+
142151
install_keyring_component() {
143152
echo -n '{ "components": "file://component_keyring_vault" }' >/var/lib/mysql/mysqld.my
144153
cp ${KEYRING_VAULT_PATH} /var/lib/mysql/component_keyring_vault.cnf
@@ -167,17 +176,6 @@ add_encryption_options() {
167176
}
168177

169178
create_default_cnf() {
170-
POD_IP=$(hostname -I | awk '{print $1}')
171-
172-
if [[ ${HOSTNAME} =~ "-xb-" ]]; then
173-
FQDN=${HOSTNAME}
174-
else
175-
CLUSTER_NAME="$(hostname -f | cut -d'.' -f2)"
176-
SERVER_NUM=${HOSTNAME/$CLUSTER_NAME-/}
177-
SERVER_ID=${CLUSTER_HASH}${SERVER_NUM}
178-
FQDN="${HOSTNAME}.${SERVICE_NAME}.$(</var/run/secrets/kubernetes.io/serviceaccount/namespace)"
179-
fi
180-
181179
echo '[mysqld]' >$CFG
182180
sed -i "/\[mysqld\]/a read_only=ON" $CFG
183181
sed -i "/\[mysqld\]/a server_id=${SERVER_ID}" $CFG
@@ -218,8 +216,6 @@ create_default_cnf() {
218216
}
219217

220218
load_group_replication_plugin() {
221-
POD_IP=$(hostname -I | awk '{print $1}')
222-
223219
sed -i "/\[mysqld\]/a plugin_load_add=group_replication.so" $CFG
224220
sed -i "/\[mysqld\]/a group_replication_exit_state_action=ABORT_SERVER" $CFG
225221
}
@@ -229,6 +225,13 @@ ensure_read_only() {
229225
sed -i "/\[mysqld\]/a super_read_only=ON" $CFG
230226
}
231227

228+
# Sync node.cnf to /etc/mysql/conf.d so Oracle MySQL image picks it up (it uses !includedir /etc/mysql/conf.d/ only).
229+
sync_node_cnf_for_oracle() {
230+
if [ -d /etc/mysql/conf.d ]; then
231+
cp -f "$CFG" /etc/mysql/conf.d/node.cnf
232+
fi
233+
}
234+
232235
escape_special() {
233236
{ set +x; } 2>/dev/null
234237
echo "$1" \
@@ -435,6 +438,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
435438

436439
load_group_replication_plugin
437440
ensure_read_only
441+
sync_node_cnf_for_oracle
438442

439443
# exit when MYSQL_INIT_ONLY environment variable is set to avoid starting mysqld
440444
if [ -n "$MYSQL_INIT_ONLY" ]; then
@@ -455,7 +459,7 @@ fi
455459

456460
if [[ -f /var/lib/mysql/full-cluster-crash ]]; then
457461
set +o xtrace
458-
node_name=$(hostname -f)
462+
node_name=${HOSTNAME}
459463
gtid_executed=$(</var/lib/mysql/full-cluster-crash)
460464
namespace=$(</var/run/secrets/kubernetes.io/serviceaccount/namespace)
461465

@@ -464,7 +468,7 @@ if [[ -f /var/lib/mysql/full-cluster-crash ]]; then
464468
echo "MySQL pods will be up and running in read only mode."
465469
echo "Latest GTID_EXECUTED on this node is ${gtid_executed}"
466470
echo "If you have spec.mysql.autoRecovery disabled, wait for all pods to be up and running and connect to one of them using mysql-shell:"
467-
echo "kubectl -n ${namespace} exec -it $(hostname) -- mysqlsh root:<password>@localhost"
471+
echo "kubectl -n ${namespace} exec -it $FQDN -- mysqlsh root:<password>@localhost"
468472
echo "and run the following command to reboot cluster:"
469473
echo "dba.rebootClusterFromCompleteOutage()"
470474
echo "and delete /var/lib/mysql/full-cluster-crash file in each pod."

deploy/cr.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ spec:
179179
# loadBalancerSourceRanges:
180180
# - 10.0.0.0/8
181181
# type: ClusterIP
182+
containerSecurityContext:
183+
runAsUser: 1001
184+
runAsGroup: 1001
185+
podSecurityContext:
186+
fsGroup: 1001
182187
volumeSpec:
183188
# emptyDir: {}
184189
# hostPath:

e2e-tests/vars.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ if [[ -z ${MYSQL_VERSION-} && -n ${IMAGE_MYSQL-} ]]; then
1616
else
1717
export MYSQL_VERSION=${MYSQL_VERSION:-"8.4"}
1818
fi
19+
20+
# for tests
21+
export IMAGE_MYSQL="mysql:8.4"
22+
export IMAGE_ROUTER="percona/percona-mysql-router:8.4"
23+
export MYSQL_VERSION="8.4"
24+
1925
export IMAGE_MYSQL=${IMAGE_MYSQL:-"perconalab/percona-server-mysql-operator:main-psmysql${MYSQL_VERSION}"}
2026
export IMAGE_BACKUP=${IMAGE_BACKUP:-"perconalab/percona-server-mysql-operator:main-backup${MYSQL_VERSION}"}
2127
export IMAGE_ORCHESTRATOR=${IMAGE_ORCHESTRATOR:-"perconalab/percona-server-mysql-operator:main-orchestrator"}

pkg/mysql/mysql.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const (
2626
CustomConfigKey = "my.cnf"
2727
configVolumeName = "config"
2828
configMountPath = "/etc/mysql/config"
29+
nodeCnfVolumeName = "node-cnf"
30+
nodeCnfMountPath = "/etc/my.cnf.d"
31+
nodeCnfConfDVolumeName = "node-cnf-conf-d" // Oracle MySQL image uses !includedir /etc/mysql/conf.d/
32+
nodeCnfConfDMountPath = "/etc/mysql/conf.d"
2933
credsVolumeName = "users"
3034
mysqlshVolumeName = "mysqlsh"
3135
mysqlshMountPath = "/.mysqlsh"
@@ -309,6 +313,23 @@ func volumes(cr *apiv1.PerconaServerMySQL) []corev1.Volume {
309313
},
310314
}
311315

316+
if cr.CompareVersion("1.1.0") >= 0 {
317+
volumes = append(volumes,
318+
corev1.Volume{
319+
Name: nodeCnfVolumeName,
320+
VolumeSource: corev1.VolumeSource{
321+
EmptyDir: &corev1.EmptyDirVolumeSource{},
322+
},
323+
},
324+
corev1.Volume{
325+
Name: nodeCnfConfDVolumeName,
326+
VolumeSource: corev1.VolumeSource{
327+
EmptyDir: &corev1.EmptyDirVolumeSource{},
328+
},
329+
},
330+
)
331+
}
332+
312333
if cr.CompareVersion("0.11.0") >= 0 {
313334
volumes = append(volumes, corev1.Volume{
314335
Name: vaultSecretVolumeName,
@@ -616,6 +637,19 @@ func mysqldVolumeMounts(cr *apiv1.PerconaServerMySQL) []corev1.VolumeMount {
616637
},
617638
}
618639

640+
if cr.CompareVersion("1.1.0") >= 0 {
641+
mounts = append(mounts,
642+
corev1.VolumeMount{
643+
Name: nodeCnfVolumeName,
644+
MountPath: nodeCnfMountPath,
645+
},
646+
corev1.VolumeMount{
647+
Name: nodeCnfConfDVolumeName,
648+
MountPath: nodeCnfConfDMountPath,
649+
},
650+
)
651+
}
652+
619653
if cr.CompareVersion("0.11.0") >= 0 {
620654
mounts = append(mounts, corev1.VolumeMount{
621655
Name: vaultSecretVolumeName,
@@ -669,6 +703,17 @@ func mysqldContainer(cr *apiv1.PerconaServerMySQL) corev1.Container {
669703
}
670704
env = append(env, spec.Env...)
671705

706+
if cr.CompareVersion("1.1.0") >= 0 {
707+
env = append(env, corev1.EnvVar{
708+
Name: naming.EnvPodIP,
709+
ValueFrom: &corev1.EnvVarSource{
710+
FieldRef: &corev1.ObjectFieldSelector{
711+
FieldPath: "status.podIP",
712+
},
713+
},
714+
})
715+
}
716+
672717
if cr.CompareVersion("0.12.0") >= 0 {
673718
env = append(env, corev1.EnvVar{
674719
Name: "KEYRING_VAULT_PATH",

pkg/naming/env.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ const (
1111
EnvBootstrapReadTimeout = "BOOTSTRAP_READ_TIMEOUT"
1212

1313
EnvBootstrapCloneTimeout = "BOOTSTRAP_CLONE_TIMEOUT"
14+
15+
EnvPodIP = "POD_IP"
1416
)

0 commit comments

Comments
 (0)