From d17d2f8a230a8c3a6fb0761117478fda96b8a180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20G=C3=BCne=C5=9F?= Date: Wed, 17 Jan 2024 14:34:54 +0300 Subject: [PATCH 1/2] K8SPS-417: Enable PROXY protocol --- api/v1alpha1/perconaservermysql_types.go | 18 ++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ pkg/haproxy/haproxy.go | 8 ++++++++ 4 files changed, 29 insertions(+) diff --git a/api/v1alpha1/perconaservermysql_types.go b/api/v1alpha1/perconaservermysql_types.go index 861ae026e..0fd7f5055 100644 --- a/api/v1alpha1/perconaservermysql_types.go +++ b/api/v1alpha1/perconaservermysql_types.go @@ -24,6 +24,7 @@ import ( "strings" cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1" + "gopkg.in/ini.v1" "github.com/percona/percona-server-mysql-operator/pkg/platform" "github.com/percona/percona-server-mysql-operator/pkg/version" @@ -947,3 +948,20 @@ const ( UpgradeStrategyRecommended = "recommended" UpgradeStrategyLatest = "latest" ) + +// MySQLConfigHasKey check if cr.Spec.MySQL.Configuration has given key in given section +func (cr *PerconaServerMySQL) MySQLConfigHasKey(section, key string) (bool, error) { + file, err := ini.LoadSources(ini.LoadOptions{AllowBooleanKeys: true}, []byte(cr.Spec.MySQL.Configuration)) + if err != nil { + return false, errors.Wrap(err, "load configuration") + } + + s, err := file.GetSection(section) + if err != nil && strings.Contains(err.Error(), "does not exist") { + return false, nil + } else if err != nil { + return false, errors.Wrap(err, "get section") + } + + return s.HasKey(key), nil +} diff --git a/go.mod b/go.mod index ffd6d9063..75a233a75 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( ) require ( + github.com/go-ini/ini v1.67.0 // indirect github.com/google/gnostic-models v0.6.8 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/moby/spdystream v0.2.0 // indirect diff --git a/go.sum b/go.sum index bab1a45d7..5f7617d86 100644 --- a/go.sum +++ b/go.sum @@ -58,6 +58,8 @@ github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3 h1:fmFk0Wt3bBxxwZnu4 github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3/go.mod h1:bJWSKrZyQvfTnb2OudyUjurSG4/edverV7n82+K3JiM= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= +github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= diff --git a/pkg/haproxy/haproxy.go b/pkg/haproxy/haproxy.go index 9af536637..d076e235b 100644 --- a/pkg/haproxy/haproxy.go +++ b/pkg/haproxy/haproxy.go @@ -348,6 +348,14 @@ func mysqlMonitContainer(cr *apiv1alpha1.PerconaServerMySQL) corev1.Container { } env = append(env, spec.Env...) + proxyProtocolEnabled, err := cr.MySQLConfigHasKey("", "proxy_protocol_networks") + if err == nil && proxyProtocolEnabled { + env = append(env, corev1.EnvVar{ + Name: "IS_PROXY_PROTOCOL", + Value: "yes", + }) + } + return corev1.Container{ Name: "mysql-monit", Image: spec.Image, From 184f3729488ba0ec586a3294627c784aedef1a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20G=C3=BCne=C5=9F?= Date: Fri, 2 Feb 2024 09:20:08 +0300 Subject: [PATCH 2/2] configure bind_address --- build/ps-entrypoint.sh | 1 + cmd/bootstrap/main.go | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/build/ps-entrypoint.sh b/build/ps-entrypoint.sh index 78a6053b1..51fe286f9 100755 --- a/build/ps-entrypoint.sh +++ b/build/ps-entrypoint.sh @@ -153,6 +153,7 @@ create_default_cnf() { echo '[mysqld]' >$CFG sed -i "/\[mysqld\]/a read_only=ON" $CFG sed -i "/\[mysqld\]/a server_id=${SERVER_ID}" $CFG + sed -i "/\[mysqld\]/a bind_address=${POD_IP}" $CFG sed -i "/\[mysqld\]/a admin-address=${POD_IP}" $CFG sed -i "/\[mysqld\]/a report_host=${FQDN}" $CFG sed -i "/\[mysqld\]/a report_port=3306" $CFG diff --git a/cmd/bootstrap/main.go b/cmd/bootstrap/main.go index 19b4a9f45..9107302cd 100644 --- a/cmd/bootstrap/main.go +++ b/cmd/bootstrap/main.go @@ -9,6 +9,11 @@ import ( "github.com/percona/percona-server-mysql-operator/pkg/mysql" ) +var ( + GitCommit string + BuildTime string +) + const ( fullClusterCrashFile = "/var/lib/mysql/full-cluster-crash" manualRecoveryFile = "/var/lib/mysql/sleep-forever" @@ -22,6 +27,8 @@ func main() { defer f.Close() log.SetOutput(f) + log.Printf("starting bootstrap... GitCommit: %s BuildTime: %s", GitCommit, BuildTime) + fullClusterCrash, err := fileExists(fullClusterCrashFile) if err == nil && fullClusterCrash { log.Printf("%s exists. exiting...", fullClusterCrashFile)