Skip to content

Commit

Permalink
BUG/MEDIUM: reload: wait until reload is done
Browse files Browse the repository at this point in the history
to avoid potential issues with reading files on fs,
wait until reload is done
  • Loading branch information
oktalz committed Oct 1, 2024
1 parent 15aa01e commit 1022b4a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
1 change: 1 addition & 0 deletions fs/etc/s6-overlay/s6-rc.d/haproxy/run
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ fi

echo "Memory limit for HAProxy: ${MEMLIMIT}MiB"

# if master socket is changed, that needs to be aligned in pkg/haproxy/process/interface.go
exec /usr/local/sbin/haproxy -W -db -m "${MEMLIMIT}" -S /var/run/haproxy-master.sock,level,admin -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/haproxy-aux.cfg
14 changes: 5 additions & 9 deletions pkg/haproxy/process/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (

var logger = utils.GetLogger()

// MUST be the same as in fs/etc/s6-overlay/s6-rc.d/haproxy/run
const MASTER_SOCKET_PATH = "/var/run/haproxy-master.sock" //nolint:stylecheck

type Process interface {
Service(action string) (err error)
UseAuxFile(useAuxFile bool)
Expand All @@ -22,16 +25,9 @@ type Process interface {
func New(env env.Env, osArgs utils.OSArgs, auxCfgFile string, api api.HAProxyClient) (p Process) { //nolint:ireturn
switch {
case osArgs.UseWiths6Overlay:
p = &s6Control{
Env: env,
OSArgs: osArgs,
API: api,
}
p = newS6Control(api, env, osArgs)
case osArgs.UseWithPebble:
p = &pebbleControl{
Env: env,
OSArgs: osArgs,
}
p = newPebbleControl(env, osArgs)
default:
p = &directControl{
Env: env,
Expand Down
37 changes: 35 additions & 2 deletions pkg/haproxy/process/pebble.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
package process

import (
"context"
"fmt"
"os"
"os/exec"

"github.com/haproxytech/client-native/v5/runtime"
"github.com/haproxytech/client-native/v5/runtime/options"
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
)

type pebbleControl struct {
Env env.Env
OSArgs utils.OSArgs
Env env.Env
OSArgs utils.OSArgs
masterSocket runtime.Runtime
masterSocketValid bool
logger utils.Logger
}

func newPebbleControl(env env.Env, osArgs utils.OSArgs) *pebbleControl {
pb := pebbleControl{
Env: env,
OSArgs: osArgs,
logger: utils.GetLogger(),
}

masterSocket, err := runtime.New(context.Background(), options.MasterSocket(MASTER_SOCKET_PATH, 1))
if err != nil {
pb.logger.Error(err)
return &pb
}
pb.masterSocketValid = true
pb.masterSocket = masterSocket

return &pb
}

func (d *pebbleControl) Service(action string) error {
Expand All @@ -30,6 +54,15 @@ func (d *pebbleControl) Service(action string) error {
// no need to stop it (pebble)
return nil
case "reload":
if d.masterSocketValid {
msg, err := d.masterSocket.Reload()
if err != nil {
d.logger.Error(err)
}
d.logger.Debug("Reload done")
d.logger.Debug(msg)
return err
}
cmd = exec.Command("pebble", "signal", "SIGUSR2", "haproxy")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
40 changes: 37 additions & 3 deletions pkg/haproxy/process/s6-overlay.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
package process

import (
"context"
"fmt"
"os"
"os/exec"

"github.com/haproxytech/client-native/v5/runtime"
"github.com/haproxytech/client-native/v5/runtime/options"
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
)

type s6Control struct {
API api.HAProxyClient
Env env.Env
OSArgs utils.OSArgs
API api.HAProxyClient
Env env.Env
OSArgs utils.OSArgs
masterSocket runtime.Runtime
masterSocketValid bool
logger utils.Logger
}

func newS6Control(api api.HAProxyClient, env env.Env, osArgs utils.OSArgs) *s6Control {
sc := s6Control{
API: api,
Env: env,
OSArgs: osArgs,
logger: utils.GetLogger(),
}

masterSocket, err := runtime.New(context.Background(), options.MasterSocket(MASTER_SOCKET_PATH, 1))
if err != nil {
sc.logger.Error(err)
return &sc
}
sc.masterSocketValid = true
sc.masterSocket = masterSocket

return &sc
}

func (d *s6Control) Service(action string) error {
Expand All @@ -31,6 +56,15 @@ func (d *s6Control) Service(action string) error {
// no need to stop it (s6)
return nil
case "reload":
if d.masterSocketValid {
msg, err := d.masterSocket.Reload()
if err != nil {
d.logger.Error(err)
}
d.logger.Debug(msg)
return err
}

cmd = exec.Command("s6-svc", "-2", "/run/service/haproxy")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down

0 comments on commit 1022b4a

Please sign in to comment.