Skip to content

Commit

Permalink
Merge pull request #721 from jcmoraisjr/jm-read-state
Browse files Browse the repository at this point in the history
Create state file only if load-server-state is enabled
  • Loading branch information
jcmoraisjr authored Jan 17, 2021
2 parents ca118ff + 0a2f952 commit 3615dd8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
6 changes: 5 additions & 1 deletion pkg/haproxy/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,12 @@ func (i *instance) reload() error {
}

func (i *instance) reloadEmbedded() error {
state := "0"
if i.config.Global().LoadServerState {
state = "1"
}
// TODO Move all magic strings to a single place
out, err := exec.Command("/haproxy-reload.sh", i.options.ReloadStrategy, i.options.HAProxyCfgDir).CombinedOutput()
out, err := exec.Command("/haproxy-reload.sh", i.options.ReloadStrategy, i.options.HAProxyCfgDir, state).CombinedOutput()
outstr := string(out)
if len(outstr) > 0 {
i.logger.Warn("output from haproxy:\n%v", outstr)
Expand Down
65 changes: 34 additions & 31 deletions rootfs/haproxy-reload.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
#
# Copyright 2017 The Kubernetes Authors. All rights reserved.
# Copyright 2021 The HAProxy Ingress Controller Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,53 +14,56 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# A script to help with haproxy reloads. Needs sudo for :80.
#
# Receives the reload strategy as the first parameter:
# native <.cfg>
# A script to help with haproxy reloads. Needs sudo if haproxy uses :80 / :443.
#
# ./haproxy-reload.sh <strategy> <cfg> [<need-state>]
#
# <strategy>: `native`
# Uses native HAProxy soft restart. Running it for the first time starts
# HAProxy, each subsequent invocation will perform a soft-reload.
# reusesocket <.cfg>
# <strategy>: `reusesocket` or any other string != `native`
# Pass the listening sockets to the new HAProxy process instead of
# rebinding them, allowing hitless reloads.
#
# <cfg>: configuration file or directory
#
# <need-state>: optional, defaults to `false`, anything != 0 means `true`
#
# HAProxy options:
# -f config file
# -p pid file
# -D run as daemon
# -sf soft reload, wait for pids to finish handling requests
# send pids a resume signal if reload of new config fails
# -x get the listening sockets from the old HAProxy process
#

set -e

PARAM_STRATEGY="$1"
PARAM_CFG="$2"
PARAM_STATE="${3:-0}"

HAPROXY_SOCKET=/var/run/haproxy/admin.sock
HAPROXY_STATE=/var/lib/haproxy/state-global
if [ -S $HAPROXY_SOCKET ]; then
echo "show servers state" | socat $HAPROXY_SOCKET - > /tmp/state && mv /tmp/state $HAPROXY_STATE
HAPROXY_PID=/var/run/haproxy/haproxy.pid
OLD_PID=$(cat "$HAPROXY_PID" 2>/dev/null || :)

# Only create the state file if the configuration need it
if [ "$PARAM_STATE" != "0" ]; then
if [ -S "$HAPROXY_SOCKET" ]; then
echo "show servers state" | socat "$HAPROXY_SOCKET" - > /tmp/state && mv /tmp/state "$HAPROXY_STATE"
fi
if [ ! -s "$HAPROXY_STATE" ]; then
echo "#" > "$HAPROXY_STATE"
fi
fi
if [ ! -s $HAPROXY_STATE ]; then
echo "#" > $HAPROXY_STATE

# Any strategy != `native` means `reusesocket` or `multibinder`
# If there isn't a unix socket (eg first start) fallback to native
if [ "$PARAM_STRATEGY" != "native" ] && [ -S "$HAPROXY_SOCKET" ]; then
haproxy -f "$PARAM_CFG" -p "$HAPROXY_PID" -D -sf $OLD_PID -x "$HAPROXY_SOCKET"
else
haproxy -f "$PARAM_CFG" -p "$HAPROXY_PID" -D -sf $OLD_PID
fi
case "$1" in
native)
CONFIG="$2"
HAPROXY_PID=/var/run/haproxy/haproxy.pid
haproxy -f "$CONFIG" -p "$HAPROXY_PID" -D -sf $(cat "$HAPROXY_PID" 2>/dev/null || :)
;;
reusesocket|multibinder)
# multibinder is now deprecated and, if used, is an alias to reusesocket
CONFIG="$2"
HAPROXY_PID=/var/run/haproxy/haproxy.pid
OLD_PID=$(cat "$HAPROXY_PID" 2>/dev/null || :)
if [ -S "$HAPROXY_SOCKET" ]; then
haproxy -f "$CONFIG" -p "$HAPROXY_PID" -D -sf $OLD_PID -x "$HAPROXY_SOCKET"
else
haproxy -f "$CONFIG" -p "$HAPROXY_PID" -D -sf $OLD_PID
fi
;;
*)
echo "Unsupported reload strategy: $1"
exit 1
;;
esac

0 comments on commit 3615dd8

Please sign in to comment.