Skip to content

Commit

Permalink
MINOR: uniform local peer management
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmatmati authored and oktalz committed Jun 3, 2024
1 parent 38d0299 commit 54820fc
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 13 deletions.
4 changes: 4 additions & 0 deletions deploy/tests/config/3.ingress-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
---
apiVersion: v1
kind: Service
Expand Down
2 changes: 1 addition & 1 deletion fs/usr/local/etc/haproxy/haproxy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defaults haproxytech
timeout http-keep-alive 60000

peers localinstance
peer local 127.0.0.1:10000


frontend https
mode http
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ func (builder *Builder) Build() *HAProxyController {
if updateStatusManager == nil {
updateStatusManager = status.New(builder.clientSet, builder.osArgs.IngressClass, builder.osArgs.EmptyIngressClass)
}
hostname, _ := os.Hostname()
podIP := utils.GetIP()
if podIP == "" {
podIP = "127.0.0.1"
}
return &HAProxyController{
osArgs: builder.osArgs,
haproxy: haproxy,
Expand All @@ -190,6 +195,8 @@ func (builder *Builder) Build() *HAProxyController {
gatewayManager: gatewayManager,
updateStatusManager: updateStatusManager,
prometheusMetricsManager: metrics.New(),
PodIP: podIP,
Hostname: hostname,
}
}

Expand Down
22 changes: 11 additions & 11 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type HAProxyController struct {
updateStatusManager status.UpdateStatusManager
beforeUpdateHandlers []UpdateHandler
prometheusMetricsManager metrics.PrometheusMetricsManager
PodIP string
Hostname string
}

// Wrapping a Native-Client transaction and commit it.
Expand All @@ -80,6 +82,15 @@ func (c *HAProxyController) clientAPIClosure(fn func() error) (err error) {

// Start initializes and runs HAProxyController
func (c *HAProxyController) Start() {
logger.Panic(c.clientAPIClosure(func() error {
return c.haproxy.PeerEntryCreateOrEdit("localinstance",
models.PeerEntry{
Name: c.Hostname,
Address: &c.PodIP,
Port: &c.osArgs.LocalPeerPort,
},
)
}))
c.initHandlers()
logger.Error(c.setupHAProxyRules())
logger.Error(os.Chdir(c.haproxy.Env.CfgDir))
Expand Down Expand Up @@ -231,17 +242,6 @@ func (c *HAProxyController) setToReady() {
}))
}

logger.Panic(c.clientAPIClosure(func() error {
ip := "127.0.0.1"
return c.haproxy.PeerEntryEdit("localinstance",
models.PeerEntry{
Name: "local",
Address: &ip,
Port: &c.osArgs.LocalPeerPort,
},
)
}))

logger.Panic(c.clientAPIClosure(func() error {
return c.haproxy.FrontendBindCreate("stats",
models.Bind{
Expand Down
1 change: 1 addition & 0 deletions pkg/haproxy/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type HAProxyClient interface { //nolint:interfacebloat
GlobalCfgSnippet(snippet []string) error
GetMap(mapFile string) (*models.Map, error)
PeerEntryEdit(peerSection string, peer models.PeerEntry) error
PeerEntryCreateOrEdit(peerSection string, peer models.PeerEntry) error
RefreshBackends() (deleted []string, err error)
SetMapContent(mapFile string, payload []string) error
SetServerAddrAndState([]RuntimeServerData) error
Expand Down
13 changes: 13 additions & 0 deletions pkg/haproxy/api/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,16 @@ func (c *clientNative) PeerEntryEdit(peerSection string, peerEntry models.PeerEn
c.activeTransactionHasChanges = true
return configuration.EditPeerEntry(peerEntry.Name, peerSection, &peerEntry, c.activeTransaction, 0)
}

func (c *clientNative) PeerEntryCreateOrEdit(peerSection string, peerEntry models.PeerEntry) error {
configuration, err := c.nativeAPI.Configuration()
if err != nil {
return err
}
c.activeTransactionHasChanges = true
err = configuration.EditPeerEntry(peerEntry.Name, peerSection, &peerEntry, c.activeTransaction, 0)
if err != nil {
err = configuration.CreatePeerEntry(peerSection, &peerEntry, c.activeTransaction, 0)
}
return err
}
1 change: 0 additions & 1 deletion pkg/haproxy/env/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ func SetGlobal(global *models.Global, logTargets *models.LogTargets, env Env) {
// Enforced values
global.MasterWorker = true
global.Pidfile = env.PIDFile
global.Localpeer = "local"
runtimeAPIs := []*models.RuntimeAPI{}
if env.RuntimeSocket != "" {
runtimeAPIs = append(runtimeAPIs, &models.RuntimeAPI{
Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,7 @@ func PointerIfNotDefault[T comparable](arg T) *T {
}
return nil
}

func GetIP() string {
return os.Getenv("POD_IP")
}

0 comments on commit 54820fc

Please sign in to comment.