Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K8SPS-417: Enable PROXY protocol #523

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions api/v1alpha1/perconaservermysql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -950,3 +951,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
}
1 change: 1 addition & 0 deletions build/ps-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
case "$f" in
*.sh)
echo "$0: running $f"
. "$f"

Check warning on line 61 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L61 <ShellCheck.SC1090>

ShellCheck can't follow non-constant source. Use a directive to specify location.
Raw output
./build/ps-entrypoint.sh:61:6: warning: ShellCheck can't follow non-constant source. Use a directive to specify location. (ShellCheck.SC1090)
;;
*.sql)
echo "$0: running $f"
Expand Down Expand Up @@ -153,6 +153,7 @@
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hors @Tusamarco is there any danger of setting bind_address to pod's IP?

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
Expand Down Expand Up @@ -197,7 +198,7 @@
exit 1
fi

if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then

Check warning on line 201 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L201 <ShellCheck.SC2166>

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
Raw output
./build/ps-entrypoint.sh:201:22: warning: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. (ShellCheck.SC2166)
# still need to check config, container may have started with --user
_check_config "$@"

Expand All @@ -213,7 +214,7 @@
touch /var/lib/mysql/bootstrap.lock
file_env 'MYSQL_ROOT_PASSWORD' '' 'root'
{ set +x; } 2>/dev/null
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then

Check warning on line 217 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L217 <ShellCheck.SC2166>

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
Raw output
./build/ps-entrypoint.sh:217:34: warning: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. (ShellCheck.SC2166)

Check warning on line 217 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L217 <ShellCheck.SC2166>

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
Raw output
./build/ps-entrypoint.sh:217:70: warning: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. (ShellCheck.SC2166)
echo >&2 'error: database is uninitialized and password option is not specified '
echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
exit 1
Expand Down Expand Up @@ -271,7 +272,7 @@
rootCreate=
# default root to listen for connections from anywhere
file_env 'MYSQL_ROOT_HOST' '%'
if [ -n "$MYSQL_ROOT_HOST" -a "$MYSQL_ROOT_HOST" != 'localhost' ]; then

Check warning on line 275 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L275 <ShellCheck.SC2166>

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
Raw output
./build/ps-entrypoint.sh:275:30: warning: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. (ShellCheck.SC2166)
# no, we don't care if read finds a terminating character in this heredoc
# https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
read -r -d '' rootCreate <<-EOSQL || true
Expand Down Expand Up @@ -358,7 +359,7 @@
file_env 'MYSQL_USER'
file_env 'MYSQL_PASSWORD'
{ set +x; } 2>/dev/null
if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then

Check warning on line 362 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L362 <ShellCheck.SC2166>

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
Raw output
./build/ps-entrypoint.sh:362:22: warning: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. (ShellCheck.SC2166)
echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | "${mysql[@]}"

if [ "$MYSQL_DATABASE" ]; then
Expand Down Expand Up @@ -406,7 +407,7 @@
if [[ -f /var/lib/mysql/full-cluster-crash ]]; then
set +o xtrace
node_name=$(hostname -f)
cluster_name=$(hostname | cut -d '-' -f1) # TODO: This won't work if CR has `-` in its name.

Check warning on line 410 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L410 <ShellCheck.SC2034>

cluster_name appears unused. Verify use (or export if used externally).
Raw output
./build/ps-entrypoint.sh:410:2: warning: cluster_name appears unused. Verify use (or export if used externally). (ShellCheck.SC2034)
gtid_executed=$(</var/lib/mysql/full-cluster-crash)
namespace=$(</var/run/secrets/kubernetes.io/serviceaccount/namespace)

Expand Down
7 changes: 7 additions & 0 deletions cmd/bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/evanphx/json-patch/v5 v5.8.0 h1:lRj6N9Nci7MvzrXuX6HFzU8XjmhPiXPlsKEy1
github.com/evanphx/json-patch/v5 v5.8.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3 h1:fmFk0Wt3bBxxwZnu48jqMdaOR/IZ4vdtJFuaFV8MpIE=
github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3/go.mod h1:bJWSKrZyQvfTnb2OudyUjurSG4/edverV7n82+K3JiM=
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/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
Expand Down
8 changes: 8 additions & 0 deletions pkg/haproxy/haproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading