Skip to content

Commit

Permalink
chore: update proxysql addon for 1.0 (#1404)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1cad11b)
  • Loading branch information
kubeJocker committed Jan 15, 2025
1 parent fc618fc commit 96fa6b9
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 62 deletions.
1 change: 0 additions & 1 deletion addons-cluster/mysql/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ spec:
{{- end }}
{{- if eq .Values.topology "server-with-proxy" }}
- name: proxysql
componentDef: proxysql
replicas: {{.Values.proxysql.replicas}}
resources:
limits:
Expand Down
22 changes: 4 additions & 18 deletions addons/mysql/config/proxysql.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{{- $proxysql_component := fromJson "{}" }}
{{- range $i, $e := $.cluster.spec.componentSpecs }}
{{- if index $e "componentDef" }}
{{- if hasPrefix "proxy" $e.componentDef }}
{{- if hasPrefix "proxysql" $e.componentDef }}
{{- $proxysql_component = $e }}
{{- end }}
{{- end }}
Expand All @@ -28,24 +28,22 @@ admin_variables=
refresh_interval="2000"
cluster_proxysql_servers_save_to_disk="true"
cluster_mysql_servers_diffs_before_sync="3"
cluster_password="nb2wZpZ9OXXTF2Mv"
mysql_ifaces="0.0.0.0:6032"
cluster_check_status_frequency="100"
cluster_mysql_users_diffs_before_sync="3"
cluster_proxysql_servers_diffs_before_sync="3"
admin_credentials="admin:admin;cluster:nb2wZpZ9OXXTF2Mv"
admin_credentials="admin:${PROXYSQL_ADMIN_PASSWORD};cluster:${PROXYSQL_CLUSTER_PASSWORD}"
admin-hash_passwords="true"
cluster_check_interval_ms="200"
cluster_mysql_servers_save_to_disk="true"
cluster_mysql_users_save_to_disk="true"
cluster_mysql_query_rules_diffs_before_sync="3"
cluster_mysql_query_rules_save_to_disk="true"
cluster_username="cluster"
}
mysql_variables=
{
threads="4"
monitor_password="proxysql"
monitor_password="${PROXYSQL_MONITOR_PASSWORD}"
poll_timeout="2000"
ssl_p2s_cert="/var/lib/certs/tls.crt"
server_version="8.0.27"
Expand Down Expand Up @@ -122,17 +120,5 @@ proxysql_servers=

mysql_servers=
(
{{- range $i, $e := until $mysql_replicas }}
{{- $mysql_service_host := printf "%s-%s-mysql-%d.%s" $clusterName $mysql_component.name $i $namespace }}

{{- $hostgroup_id := 3 }}
{{- if eq $i 0 }}
{{- $hostgroup_id = 2 }}
{{- end }}
{{- if eq $i (sub $mysql_replicas 1) }}
{ hostgroup_id = {{$hostgroup_id}} , hostname = "{{$mysql_service_host}}", port = 3306, weight = 1, use_ssl = 0 }
{{- else }}
{ hostgroup_id = {{$hostgroup_id}} , hostname = "{{$mysql_service_host}}", port = 3306, weight = 1, use_ssl = 0 },
{{- end }}
{{- end }}
${MYSQL_SERVERS}
)
18 changes: 4 additions & 14 deletions addons/mysql/scripts-ut-spec/proxysql_entry_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,15 @@ Describe "ProxySQL Entry Script Tests"
AfterAll 'cleanup'

Describe "Run proxysql-entry.sh with FRONTEND_TLS_ENABLED=false"

It "runs successfully"
replace_config_variables() {
return 0
}
When run source ../scripts/proxysql-entry.sh
The status should be failure
The stdout should include "Configuring proxysql ..."
The stderr should include "/scripts/proxysql/configure-proxysql.sh: No such file or directory"
The stderr should include "../scripts/proxysql-entry.sh"
End
End


Describe "Log Function Tests"
Include ../scripts/proxysql-entry.sh

It "outputs a log message"
When call log "INFO" "Test log message"
The status should be success
The stdout should include "INFO"
The stdout should include "Test log message"
End
End

End
10 changes: 5 additions & 5 deletions addons/mysql/scripts/configure-proxysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ CREATE USER 'monitor'@'%' IDENTIFIED BY 'monitor';
GRANT USAGE, REPLICATION CLIENT ON *.* TO 'monitor'@'%';
EOF
# wait for proxysql process to run
wait_for_mysql admin admin 127.0.0.1 6032
wait_for_mysql admin ${PROXYSQL_ADMIN_PASSWORD} 127.0.0.1 6032

log "INFO" "CURRENT CONFIGURATION"

Expand All @@ -108,9 +108,9 @@ select * from runtime_proxysql_servers;
"

mysql -uadmin -padmin -h127.0.0.1 -P6032 -vvve "$configuration_sql"
mysql -uadmin -p${PROXYSQL_ADMIN_PASSWORD} -h127.0.0.1 -P6032 -vvve "$configuration_sql"


mysql -uadmin -padmin -h127.0.0.1 -P6032 -vvve "insert or replace into mysql_users (username,password) values ('$MYSQL_ROOT_USER','$MYSQL_ROOT_PASSWORD');LOAD MYSQL USERS TO RUNTIME;SAVE MYSQL USERS TO DISK;"
mysql -uadmin -padmin -h127.0.0.1 -P6032 -vvve "insert into mysql_replication_hostgroups ( writer_hostgroup, reader_hostgroup, comment) values (2,3,'proxy');load mysql servers to runtime;save mysql servers to disk;"
mysql -uadmin -padmin -h127.0.0.1 -P6032 -vvve "select * from main.runtime_mysql_replication_hostgroups; select * from main.mysql_replication_hostgroups; select * from mysql_replication_hostgroups;"
mysql -uadmin -p${PROXYSQL_ADMIN_PASSWORD} -h127.0.0.1 -P6032 -vvve "insert or replace into mysql_users (username,password) values ('$MYSQL_ROOT_USER','$MYSQL_ROOT_PASSWORD');LOAD MYSQL USERS TO RUNTIME;SAVE MYSQL USERS TO DISK;"
mysql -uadmin -p${PROXYSQL_ADMIN_PASSWORD} -h127.0.0.1 -P6032 -vvve "insert into mysql_replication_hostgroups ( writer_hostgroup, reader_hostgroup, comment) values (2,3,'proxy');load mysql servers to runtime;save mysql servers to disk;"
mysql -uadmin -p${PROXYSQL_ADMIN_PASSWORD} -h127.0.0.1 -P6032 -vvve "select * from main.runtime_mysql_replication_hostgroups; select * from main.mysql_replication_hostgroups; select * from mysql_replication_hostgroups;"
3 changes: 0 additions & 3 deletions addons/mysql/scripts/init-mysql-instance-for-orc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ GRANT SUPER, PROCESS, REPLICATION SLAVE, RELOAD ON *.* TO '$topology_user'@'%';
GRANT SELECT ON mysql.slave_master_info TO '$topology_user'@'%';
GRANT DROP ON _pseudo_gtid_.* to '$topology_user'@'%';
GRANT ALL ON kb_orc_meta_cluster.* TO '$topology_user'@'%';
CREATE USER IF NOT EXISTS 'proxysql'@'%' IDENTIFIED BY 'proxysql';
GRANT SELECT ON performance_schema.* TO 'proxysql'@'%';
GRANT SELECT ON sys.* TO 'proxysql'@'%';
set global slave_net_timeout = 4;
EOF

Expand Down
48 changes: 38 additions & 10 deletions addons/mysql/scripts/proxysql-entry.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/bin/bash
set -e

# This is magic for shellspec ut framework.
# Sometime, functions are defined in a single shell script.
# You will want to test it. but you do not want to run the script.
# When included from shellspec, __SOURCED__ variable defined and script
# end here. The script path is assigned to the __SOURCED__ variable.
${__SOURCED__:+false} : || return 0

# use the current scrip name while putting log
script_name=${0##*/}

Expand All @@ -13,14 +20,25 @@ if [ $FRONTEND_TLS_ENABLED == "true" ]; then
cp /var/lib/frontend/server/tls.key /var/lib/proxysql/proxysql-key.pem
fi

function timestamp() {
date +"%Y/%m/%d %T"
}
function gen_mysql_servers {
IFS=',' read -r -a MYSQL_FQDNS_ARRAY <<< "$MYSQL_FQDNS"
result=""

for fqdn in "${MYSQL_FQDNS_ARRAY[@]}"; do
index=$(echo "$fqdn" | grep -oP '\d+(?=\.)')
HOSTGROUP_ID=3
if [ "$index" -eq 0 ]; then
HOSTGROUP_ID=2
fi
config=" { hostgroup_id = $HOSTGROUP_ID , hostname = \"$fqdn\", port = 3306, weight = 1, use_ssl = 0 }"
if [ -z "$result" ]; then
result="$config"
else
result="$result, \n$config"
fi
done

function log() {
local log_type="$1"
local msg="$2"
echo "$(timestamp) [$script_name] [$log_type] $msg"
echo "$result"
}

# If command has arguments, prepend proxysql
Expand All @@ -33,13 +51,23 @@ if [ "${__SOURCED__:+x}" ]; then
return 0
fi

function replace_config_variables() {
mysql_servers=$(gen_mysql_servers)
awk -v mysql_servers="$mysql_servers" '{gsub(/\${MYSQL_SERVERS}/, mysql_servers); print}' /config/custom-config/proxysql.tpl > /proxysql.cnf

sed -i "s|\${PROXYSQL_MONITOR_PASSWORD}|${PROXYSQL_MONITOR_PASSWORD}|g" /proxysql.cnf
sed -i "s|\${PROXYSQL_CLUSTER_PASSWORD}|${PROXYSQL_CLUSTER_PASSWORD}|g" /proxysql.cnf
sed -i "s|\${PROXYSQL_ADMIN_PASSWORD}|${PROXYSQL_ADMIN_PASSWORD}|g" /proxysql.cnf
}

echo "Configuring proxysql ..."
replace_config_variables
# Start ProxySQL with PID 1
exec proxysql -c /etc/custom-config/proxysql.cnf -f $CMDARG &
exec proxysql -c /proxysql.cnf -f $CMDARG &
pid=$!

log "INFO" "Configuring proxysql ..."
/scripts/proxysql/configure-proxysql.sh

log "INFO" "Waiting for proxysql ..."
echo "Waiting for proxysql ..."
wait $pid

34 changes: 32 additions & 2 deletions addons/mysql/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ systemAccounts:
numDigits: 5
numSymbols: 0
letterCase: MixedCases
- name: proxysql
statement: CREATE USER IF NOT EXISTS '${KB_ACCOUNT_NAME}' IDENTIFIED BY '${KB_ACCOUNT_PASSWORD}'; GRANT SELECT ON performance_schema.* TO '${KB_ACCOUNT_NAME}'; GRANT SELECT ON sys.* TO '${KB_ACCOUNT_NAME}';
vars:
- name: CLUSTER_NAME
valueFrom:
Expand Down Expand Up @@ -138,6 +140,20 @@ vars:
name: root
password: Required
lifecycleActions:
accountProvision:
exec:
container: mysql
image: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}:8.0.33
command:
- /bin/sh
- -c
- |
set -ex
eval statement=\"${KB_ACCOUNT_STATEMENT}\"
mysql -u${MYSQL_ROOT_USER} -p${MYSQL_ROOT_PASSWORD} -P3306 -h127.0.0.1 -e "${statement}"
targetPodSelector: Role
matchingKey: primary

roleProbe:
periodSeconds: {{ .Values.roleProbe.periodSeconds }}
timeoutSeconds: {{ .Values.roleProbe.timeoutSeconds }}
Expand Down Expand Up @@ -188,8 +204,7 @@ serviceRefDeclarations:
- serviceKind: orchestrator
serviceVersion: "^*"
services:
- name: mysql-server
serviceName: mysql-server
- name: default
spec:
ports:
- name: mysql
Expand Down Expand Up @@ -220,6 +235,8 @@ systemAccounts:
numDigits: 5
numSymbols: 0
letterCase: MixedCases
- name: proxysql
statement: CREATE USER IF NOT EXISTS '${KB_ACCOUNT_NAME}' IDENTIFIED BY '${KB_ACCOUNT_PASSWORD}'; GRANT SELECT ON performance_schema.* TO '${KB_ACCOUNT_NAME}'; GRANT SELECT ON sys.* TO '${KB_ACCOUNT_NAME}';
roles:
- name: primary
serviceable: true
Expand Down Expand Up @@ -276,6 +293,19 @@ exporter:


{{- define "mysql-orc.spec.lifecycle.common" }}
accountProvision:
exec:
container: mysql
image: {{ .Values.image.registry | default "docker.io" }}/{{ .Values.image.repository }}:8.0.33
command:
- /bin/sh
- -c
- |
set -ex
eval statement=\"${KB_ACCOUNT_STATEMENT}\"
mysql -u${MYSQL_ROOT_USER} -p${MYSQL_ROOT_PASSWORD} -P3306 -h127.0.0.1 -e "${statement}"
targetPodSelector: Role
matchingKey: primary
roleProbe:
exec:
env:
Expand Down
9 changes: 8 additions & 1 deletion addons/mysql/templates/_names.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ Define mysql component definition regex regular
{{- printf "^%s" (include "mysql.cmpdNamePrefix" .) -}}-\d+\.\d+.*$
{{- end -}}

{{/*
Define mysql component definition common regex regular (semisync and orc)
*/}}
{{- define "mysql.componentDefCommonRegex" -}}
{{- printf "^%s" (include "mysql.cmpdNamePrefix" .) -}}(?:-[\w\d]+)?-\d+\.\d+.*$
{{- end -}}

{{/*
Define mysql component definition name
*/}}
Expand Down Expand Up @@ -58,5 +65,5 @@ Define mysql component definition name
Define mysql component definition name
*/}}
{{- define "proxysql.componentDefName" -}}
{{- printf "%s-proxysql-%s" (include "mysql.cmpdNamePrefix" .) .Chart.Version -}}
{{- printf "proxysql-%s-%s" (include "mysql.cmpdNamePrefix" .) .Chart.Version -}}
{{- end -}}
55 changes: 48 additions & 7 deletions addons/mysql/templates/cmpd-proxysql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,40 @@ spec:
- name: db
port: 6033
targetPort: db
systemAccounts:
- name: admin
initAccount: true
passwordGenerationPolicy:
length: 16
numDigits: 8
numSymbols: 0
letterCase: MixedCases
- name: cluster
initAccount: true
passwordGenerationPolicy:
length: 16
numDigits: 8
numSymbols: 0
letterCase: MixedCases
vars:
- name: PROXYSQL_ADMIN_PASSWORD
valueFrom:
credentialVarRef:
name: admin
password: Required
- name: PROXYSQL_CLUSTER_PASSWORD
valueFrom:
credentialVarRef:
name: admin
password: Required
- name: PROXYSQL_MONITOR_PASSWORD
valueFrom:
credentialVarRef:
compDef: {{ include "mysql.componentDefCommonRegex" . }}
name: proxysql
optional: false
password: Required

- name: CLUSTER_NAME
valueFrom:
clusterVarRef:
Expand All @@ -61,33 +94,41 @@ spec:
- name: BACKEND_SERVER
valueFrom:
serviceVarRef:
compDef: {{ include "mysql.componentDefRegex" . }}
name: mysql-server
compDef: {{ include "mysql.componentDefCommonRegex" . }}
name: default
host: Required
optional: false
- name: MYSQL_ROOT_USER
valueFrom:
credentialVarRef:
## reference the current component definition name
compDef: {{ include "mysql.componentDefRegex" . }}
compDef: {{ include "mysql.componentDefCommonRegex" . }}
name: root
username: Required
- name: MYSQL_ROOT_PASSWORD
valueFrom:
credentialVarRef:
## reference the current component definition name
compDef: {{ include "mysql.componentDefRegex" . }}
compDef: {{ include "mysql.componentDefCommonRegex" . }}
name: root
password: Required
- name: MYSQL_PORT
valueFrom:
serviceVarRef:
compDef: {{ include "mysql.componentDefRegex" . }}
name: mysql-server
compDef: {{ include "mysql.componentDefCommonRegex" . }}
name: default
optional: false
port:
name: mysql
option: Required
- name: MYSQL_FQDNS
valueFrom:
componentVarRef:
compDef: {{ include "mysql.componentDefCommonRegex" . }}
name: default
optional: false
podFQDNs: Required

runtime:
volumes:
containers:
Expand All @@ -98,7 +139,7 @@ spec:
- /scripts/proxysql/proxysql-entry.sh
- --reload
volumeMounts:
- mountPath: /etc/custom-config
- mountPath: /config/custom-config
name: proxy-config
- mountPath: /scripts/proxysql
name: proxysql-scripts
Expand Down
2 changes: 1 addition & 1 deletion addons/mysql/templates/configmap-proxysql.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apiVersion: v1
data:
proxysql.cnf: |-
proxysql.tpl: |-
{{- .Files.Get "config/proxysql.tpl" | nindent 4 }}
kind: ConfigMap
metadata:
Expand Down
Loading

0 comments on commit 96fa6b9

Please sign in to comment.