Skip to content

Commit

Permalink
MEDIUM: remove restart of HAProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmatmati authored and oktalz committed Oct 24, 2024
1 parent 87cda93 commit cdd5fae
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 107 deletions.
10 changes: 1 addition & 9 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,7 @@ func (c *HAProxyController) updateHAProxy() {
c.setToReady()
}

switch {
case instance.NeedRestart():
if err = c.haproxy.Service("restart"); err != nil {
logger.Error(err)
} else {
logger.Info("HAProxy restarted")
}
c.prometheusMetricsManager.UpdateRestartMetrics(err)
case instance.NeedReload():
if instance.NeedReload() {
if err = c.haproxy.Service("reload"); err != nil {
logger.Error(err)
} else {
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ func (c *HAProxyController) globalCfg() {
diff := newGlobal.Diff(*global)
if len(diff) != 0 {
logger.Error(c.haproxy.GlobalPushConfiguration(*newGlobal))
instance.Restart("Global config updated: %+v", diff)
instance.Reload("Global config updated: %+v", diff)
}
diff = newLg.Diff(lg)
if len(diff) != 0 {
logger.Error(c.haproxy.GlobalPushLogTargets(newLg))
instance.Restart("Global log targets updated: %+v", diff)
instance.Reload("Global log targets updated: %+v", diff)
}
c.globalCfgSnipp()
}
Expand All @@ -110,7 +110,7 @@ func (c *HAProxyController) globalCfgSnipp() {
}
updatedSnipp, errSnipp := annotations.UpdateGlobalCfgSnippet(c.haproxy)
logger.Error(errSnipp)
instance.RestartIf(len(updatedSnipp) != 0,
instance.ReloadIf(len(updatedSnipp) != 0,
"Global config-snippet updated: %s", strings.Join(updatedSnipp, "\n"))

updatedSnipp, errSnipp = annotations.UpdateFrontendCfgSnippet(c.haproxy, "http", "https", "stats")
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *HAProxyController) SyncData() {
case k8ssync.COMMAND:
c.auxCfgManager()
// create a NeedAction function.
if hadChanges || instance.NeedAction() {
if hadChanges || instance.NeedReload() {
c.updateHAProxy()
hadChanges = false
continue
Expand Down Expand Up @@ -97,7 +97,6 @@ func (c *HAProxyController) SyncData() {
}
}

// auxCfgManager returns restart or reload requirement based on state and transition of auxiliary configuration file.
func (c *HAProxyController) auxCfgManager() {
info, errStat := os.Stat(c.haproxy.AuxCFGFile)
var (
Expand All @@ -115,7 +114,7 @@ func (c *HAProxyController) auxCfgManager() {
c.haproxy.SetAuxCfgFile(auxCfgFile)
c.haproxy.UseAuxFile(useAuxFile)
// The file exists now (modifTime !=0 otherwise nothing changed case).
instance.RestartIf(c.auxCfgModTime == 0, "auxiliary configuration file created")
instance.ReloadIf(c.auxCfgModTime == 0, "auxiliary configuration file created")
instance.ReloadIf(c.auxCfgModTime != 0, "auxiliary configuration file modified")
c.auxCfgModTime = modifTime
if c.auxCfgModTime != 0 {
Expand All @@ -131,7 +130,7 @@ func (c *HAProxyController) auxCfgManager() {
// never existed before
return
}
instance.Restart("Auxiliary HAProxy config '%s' removed", c.haproxy.AuxCFGFile)
instance.Reload("Auxiliary HAProxy config '%s' removed", c.haproxy.AuxCFGFile)
return
}
// File exists
Expand Down
48 changes: 2 additions & 46 deletions pkg/haproxy/instance/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,21 @@ func Reload(reason string, args ...any) {
DefaultConfigurationManager.SetReload(reason, args...)
}

func Restart(reason string, args ...any) {
DefaultConfigurationManager.SetRestart(reason, args...)
}

func ReloadIf(reload bool, reason string, args ...any) {
DefaultConfigurationManager.SetReloadIf(reload, reason, args...)
}

func RestartIf(restart bool, reason string, args ...any) {
DefaultConfigurationManager.SetRestartIf(restart, reason, args...)
}

func NeedReload() bool {
return DefaultConfigurationManager.NeedReload()
}

func NeedRestart() bool {
return DefaultConfigurationManager.NeedRestart()
}

func Reset() {
DefaultConfigurationManager.Reset()
}

func NeedAction() bool {
return DefaultConfigurationManager.NeedAction()
}

type configurationManagerImpl struct {
logger utils.Logger
reload, restart bool
logger utils.Logger
reload bool
}

func NewConfigurationManager() *configurationManagerImpl {
Expand All @@ -60,27 +44,14 @@ func (cmi *configurationManagerImpl) SetReload(reason string, args ...any) {
cmi.logger.InfoSkipCallerf("reload required : "+reason, args...)
}

func (cmi *configurationManagerImpl) SetRestart(reason string, args ...any) {
cmi.restart = true
if !cmi.validReason(reason) {
return
}
cmi.logger.InfoSkipCallerf("restart required : "+reason, args...)
}

func (cmi *configurationManagerImpl) Reset() {
cmi.reload = false
cmi.restart = false
}

func (cmi *configurationManagerImpl) NeedReload() bool {
return cmi.reload
}

func (cmi *configurationManagerImpl) NeedRestart() bool {
return cmi.restart
}

func (cmi *configurationManagerImpl) SetReloadIf(reload bool, reason string, args ...any) {
if !reload {
return
Expand All @@ -92,21 +63,6 @@ func (cmi *configurationManagerImpl) SetReloadIf(reload bool, reason string, arg
cmi.logger.InfoSkipCallerf("reload required : "+reason, args...)
}

func (cmi *configurationManagerImpl) SetRestartIf(restart bool, reason string, args ...any) {
if !restart {
return
}
cmi.restart = true
if !cmi.validReason(reason) {
return
}
cmi.logger.InfoSkipCallerf("restart required : "+reason, args...)
}

func (cmi *configurationManagerImpl) NeedAction() bool {
return cmi.NeedReload() || cmi.NeedRestart()
}

func (cmi *configurationManagerImpl) validReason(reason string) bool {
if reason == "" {
errMsg := "empty reason for reload"
Expand Down
14 changes: 0 additions & 14 deletions pkg/haproxy/process/direct-control.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"os/exec"
"strconv"
"syscall"

"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
Expand Down Expand Up @@ -61,19 +60,6 @@ func (d *directControl) Service(action string) (err error) {
return d.Service("start")
}
return process.Signal(syscall.SIGUSR2)
case "restart":
if processErr != nil {
logger.Errorf("haproxy is not running, trying to start it")
return d.Service("start")
}
pid := strconv.Itoa(process.Pid)
cmd = exec.Command(d.Env.Binary, "-S", masterSocketArg, "-f", d.Env.MainCFGFile, "-sf", pid)
if d.useAuxFile {
cmd = exec.Command(d.Env.Binary, "-S", masterSocketArg, "-f", d.Env.MainCFGFile, "-f", d.Env.AuxCFGFile, "-sf", pid)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Start()
default:
return fmt.Errorf("unknown command '%s'", action)
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/haproxy/process/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ func (d *pebbleControl) Service(action string) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
case "restart":
cmd = exec.Command("pebble", "restart", "haproxy")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
default:
return fmt.Errorf("unknown command '%s'", action)
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/haproxy/process/s6-overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ func (d *s6Control) Service(action string) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
case "restart":
cmd = exec.Command("s6-svc", "-t", "/run/service/haproxy")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
default:
return fmt.Errorf("unknown command '%s'", action)
}
Expand Down
21 changes: 0 additions & 21 deletions pkg/metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ type PrometheusMetricsManager struct {
// reload
reloadsCounterVec *prometheus.CounterVec

// restart
restartsCounterVec *prometheus.CounterVec

// runtime socket
runtimeSocketCounterVec *prometheus.CounterVec
}
Expand All @@ -41,15 +38,6 @@ func New() PrometheusMetricsManager {
[]string{"result"},
)

// restart
restartCounter := promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "haproxy_restarts_total",
Help: "The number of haproxy restarts partitioned by result (success/failure)",
},
[]string{"result"},
)

// runtime socket
runtimeSocketCounter := promauto.NewCounterVec(
prometheus.CounterOpts{
Expand All @@ -61,21 +49,12 @@ func New() PrometheusMetricsManager {

pmm = PrometheusMetricsManager{
reloadsCounterVec: reloadCounter,
restartsCounterVec: restartCounter,
runtimeSocketCounterVec: runtimeSocketCounter,
}
})
return pmm
}

func (pmm PrometheusMetricsManager) UpdateRestartMetrics(err error) {
if err != nil {
pmm.restartsCounterVec.WithLabelValues(ResultFailure).Inc()
} else {
pmm.restartsCounterVec.WithLabelValues(ResultSuccess).Inc()
}
}

func (pmm PrometheusMetricsManager) UpdateReloadMetrics(err error) {
if err != nil {
pmm.reloadsCounterVec.WithLabelValues(ResultFailure).Inc()
Expand Down

0 comments on commit cdd5fae

Please sign in to comment.