Skip to content

Commit d17a5c8

Browse files
ivanmatmatiMo3m3n
authored andcommitted
MINOR: add haproxy.Process to controller builder
1 parent a1a9d74 commit d17a5c8

File tree

5 files changed

+34
-30
lines changed

5 files changed

+34
-30
lines changed

pkg/controller/builder.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
1212
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
13+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/process"
1314
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
1415
"github.com/haproxytech/kubernetes-ingress/pkg/k8s"
1516
"github.com/haproxytech/kubernetes-ingress/pkg/store"
@@ -19,9 +20,10 @@ import (
1920
type Builder struct {
2021
osArgs utils.OSArgs
2122
haproxyEnv config.Env
23+
haproxyProcess process.Process
24+
haproxyCfgFile []byte
2225
store store.K8s
2326
publishService *utils.NamespaceValue
24-
haproxyCfgFile []byte
2527
eventChan chan k8s.SyncDataEvent
2628
ingressChan chan ingress.Sync
2729
}
@@ -44,6 +46,11 @@ func NewBuilder() *Builder {
4446
return &Builder{haproxyEnv: defaultEnv}
4547
}
4648

49+
func (builder *Builder) WithHAProxyProcess(process process.Process) *Builder {
50+
builder.haproxyProcess = process
51+
return builder
52+
}
53+
4754
func (builder *Builder) WithEventChan(eventChan chan k8s.SyncDataEvent) *Builder {
4855
builder.eventChan = eventChan
4956
return builder
@@ -97,7 +104,7 @@ func (builder *Builder) Build() *HAProxyController {
97104
}()
98105
}
99106

100-
haproxy, err := haproxy.New(builder.osArgs, builder.haproxyEnv, builder.haproxyCfgFile)
107+
haproxy, err := haproxy.New(builder.osArgs, builder.haproxyEnv, builder.haproxyCfgFile, builder.haproxyProcess)
101108
logger.Panic(err)
102109

103110
prefix, errPrefix := utils.GetPodPrefix(os.Getenv("POD_NAME"))

pkg/haproxy/main.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package haproxy
22

33
import (
44
"fmt"
5-
"os"
65
"os/exec"
76
"strings"
87

@@ -25,7 +24,7 @@ type HAProxy struct {
2524
*config.Config
2625
}
2726

28-
func New(osArgs utils.OSArgs, env config.Env, cfgFile []byte) (h HAProxy, err error) {
27+
func New(osArgs utils.OSArgs, env config.Env, cfgFile []byte, p process.Process) (h HAProxy, err error) {
2928
err = (&env).Init(osArgs)
3029
if err != nil {
3130
err = fmt.Errorf("failed to initialize haproxy environment: %w", err)
@@ -50,16 +49,9 @@ func New(osArgs utils.OSArgs, env config.Env, cfgFile []byte) (h HAProxy, err er
5049
err = fmt.Errorf("failed to initialize haproxy API client: %w", err)
5150
return
5251
}
53-
54-
if osArgs.UseWiths6Overlay {
55-
h.Process = process.NewControlOverS6(h.Env, osArgs, h.HAProxyClient)
56-
} else {
57-
h.Process = process.NewDirectControl(h.Env, osArgs, h.HAProxyClient)
58-
if _, err := os.Stat(h.AuxCFGFile); err == nil {
59-
h.UseAuxFile(true)
60-
}
52+
if p == nil {
53+
h.Process = process.New(h.Env, osArgs, h.AuxCFGFile, h.HAProxyClient)
6154
}
62-
6355
if !osArgs.Test {
6456
logVersion(h.Binary)
6557
}

pkg/haproxy/process/direct-control.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ type directControl struct {
1919
useAuxFile bool
2020
}
2121

22-
func NewDirectControl(env config.Env, oSArgs utils.OSArgs, api api.HAProxyClient) Process {
23-
return &directControl{
24-
Env: env,
25-
OSArgs: oSArgs,
26-
API: api,
27-
}
28-
}
29-
3022
func (d *directControl) Service(action string) (err error) {
3123
if d.OSArgs.Test {
3224
logger.Infof("HAProxy would be %sed now", action)

pkg/haproxy/process/interface.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,36 @@ import (
77
"syscall"
88

99
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
10+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
1011
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
1112
)
1213

14+
var logger = utils.GetLogger()
15+
1316
type Process interface {
1417
Service(action string) (err error)
1518
UseAuxFile(useAuxFile bool)
1619
}
1720

18-
var logger = utils.GetLogger()
21+
func New(env config.Env, osArgs utils.OSArgs, auxCfgFile string, api api.HAProxyClient) (p Process) {
22+
if osArgs.UseWiths6Overlay {
23+
p = &s6Control{
24+
Env: env,
25+
OSArgs: osArgs,
26+
API: api,
27+
}
28+
} else {
29+
p = &directControl{
30+
Env: env,
31+
OSArgs: osArgs,
32+
API: api,
33+
}
34+
if _, err := os.Stat(auxCfgFile); err == nil {
35+
p.UseAuxFile(true)
36+
}
37+
}
38+
return p
39+
}
1940

2041
// Return HAProxy master process if it exists.
2142
func haproxyProcess(pidFile string) (*os.Process, error) {

pkg/haproxy/process/s6-overlay.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ type s6Control struct {
1616
API api.HAProxyClient
1717
}
1818

19-
func NewControlOverS6(env config.Env, oSArgs utils.OSArgs, api api.HAProxyClient) Process {
20-
return &s6Control{
21-
Env: env,
22-
OSArgs: oSArgs,
23-
API: api,
24-
}
25-
}
26-
2719
func (d *s6Control) Service(action string) (err error) {
2820
if d.OSArgs.Test {
2921
logger.Infof("HAProxy would be %sed now", action)

0 commit comments

Comments
 (0)