Skip to content

Commit 21a2a57

Browse files
committed
REORG/MINOR: remove haproxy.Config package
Content of haproxy.Config is now in the main struct of haproxy package
1 parent 8d26f65 commit 21a2a57

File tree

18 files changed

+91
-108
lines changed

18 files changed

+91
-108
lines changed

deploy/tests/integration/customresources/suite_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/haproxytech/client-native/v2/models"
2323
corev1alpha1 "github.com/haproxytech/kubernetes-ingress/crs/api/core/v1alpha1"
2424
c "github.com/haproxytech/kubernetes-ingress/pkg/controller"
25-
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
25+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
2626
"github.com/haproxytech/kubernetes-ingress/pkg/k8s"
2727
"github.com/haproxytech/kubernetes-ingress/pkg/store"
2828
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
@@ -112,13 +112,13 @@ func (suite *CustomResourceSuite) GlobalCRFixture() (eventChan chan k8s.SyncData
112112
Name: "globalcrjob",
113113
}
114114

115-
haproxyEnv := config.Env{
115+
haproxyEnv := env.Env{
116116
Binary: "/usr/local/sbin/haproxy",
117117
MainCFGFile: filepath.Join(suite.test.TempDir, "haproxy.cfg"),
118118
CfgDir: suite.test.TempDir,
119119
RuntimeDir: filepath.Join(suite.test.TempDir, "run"),
120120
StateDir: filepath.Join(suite.test.TempDir, "state/haproxy/"),
121-
Proxies: config.Proxies{
121+
Proxies: env.Proxies{
122122
FrontHTTP: "http",
123123
FrontHTTPS: "https",
124124
FrontSSL: "ssl",

pkg/controller/builder.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
1212
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
1313
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
14-
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
14+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
1515
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/process"
1616
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/rules"
1717
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
@@ -23,7 +23,7 @@ import (
2323
type Builder struct {
2424
osArgs utils.OSArgs
2525
haproxyClient api.HAProxyClient
26-
haproxyEnv config.Env
26+
haproxyEnv env.Env
2727
haproxyProcess process.Process
2828
haproxyRules rules.Rules
2929
haproxyCfgFile []byte
@@ -34,13 +34,13 @@ type Builder struct {
3434
ingressChan chan ingress.Sync
3535
}
3636

37-
var defaultEnv = config.Env{
37+
var defaultEnv = env.Env{
3838
Binary: "/usr/local/sbin/haproxy",
3939
MainCFGFile: "/etc/haproxy/haproxy.cfg",
4040
CfgDir: "/etc/haproxy/",
4141
RuntimeDir: "/var/run",
4242
StateDir: "/var/state/haproxy/",
43-
Proxies: config.Proxies{
43+
Proxies: env.Proxies{
4444
FrontHTTP: "http",
4545
FrontHTTPS: "https",
4646
FrontSSL: "ssl",
@@ -90,7 +90,7 @@ func (builder *Builder) WithStore(store store.K8s) *Builder {
9090
return builder
9191
}
9292

93-
func (builder *Builder) WithHaproxyEnv(env config.Env) *Builder {
93+
func (builder *Builder) WithHaproxyEnv(env env.Env) *Builder {
9494
builder.haproxyEnv = env
9595
return builder
9696
}

pkg/controller/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (c *HAProxyController) setupHAProxyRules() error {
249249
return errors.Result()
250250
}
251251

252-
// clean controller state
252+
// clean haproxy config state
253253
func (c *HAProxyController) clean(failedSync bool) {
254254
c.haproxy.Clean()
255255
logger.Error(c.setupHAProxyRules())

pkg/controller/global.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
2323
"github.com/haproxytech/kubernetes-ingress/pkg/annotations/common"
2424
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/certs"
25-
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
25+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
2626
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
2727
"github.com/haproxytech/kubernetes-ingress/pkg/service"
2828
"github.com/haproxytech/kubernetes-ingress/pkg/store"
@@ -69,7 +69,7 @@ func (c *HAProxyController) globalCfg() (reload, restart bool) {
6969
}
7070
}
7171
}
72-
config.SetGlobal(newGlobal, &newLg, c.haproxy.Env)
72+
env.SetGlobal(newGlobal, &newLg, c.haproxy.Env)
7373
updated = deep.Equal(newGlobal, global)
7474
if len(updated) != 0 {
7575
logger.Error(c.haproxy.GlobalPushConfiguration(*newGlobal))
@@ -127,7 +127,7 @@ func (c *HAProxyController) defaultsCfg() (reload bool) {
127127
logger.Error(a.Process(c.store, c.store.ConfigMaps.Main.Annotations))
128128
}
129129
}
130-
config.SetDefaults(newDefaults)
130+
env.SetDefaults(newDefaults)
131131
updated := deep.Equal(newDefaults, defaults)
132132
if len(updated) != 0 {
133133
if err = c.haproxy.DefaultsPushConfiguration(*newDefaults); err != nil {

pkg/handler/globalcfg.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
2121
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
22-
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
22+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
2323
"github.com/haproxytech/kubernetes-ingress/pkg/store"
2424
)
2525

@@ -29,7 +29,7 @@ type GlobalCfg struct {
2929
func (handler GlobalCfg) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
3030
global := &models.Global{}
3131
logTargets := &models.LogTargets{}
32-
config.SetGlobal(global, logTargets, h.Env)
32+
env.SetGlobal(global, logTargets, h.Env)
3333
err = h.GlobalPushConfiguration(*global)
3434
if err != nil {
3535
return
@@ -39,7 +39,7 @@ func (handler GlobalCfg) Update(k store.K8s, h haproxy.HAProxy, a annotations.An
3939
return
4040
}
4141
defaults := &models.Defaults{}
42-
config.SetDefaults(defaults)
42+
env.SetDefaults(defaults)
4343
err = h.DefaultsPushConfiguration(*defaults)
4444
if err != nil {
4545
return

pkg/handler/https.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ func (handler HTTPS) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annota
153153
logger.Error(err)
154154

155155
// ssl-offload
156+
sslOffloadEnabled := h.FrontendSSLOffloadEnabled(h.FrontHTTPS)
156157
if h.FrontCertsInUse() {
157-
if !h.HTTPS {
158+
if !sslOffloadEnabled {
158159
logger.Panic(h.FrontendEnableSSLOffload(h.FrontHTTPS, handler.CertDir, handler.alpn, handler.strictSNI))
159-
h.HTTPS = true
160160
reload = true
161161
logger.Debug("SSLOffload enabled, reload required")
162162
}
@@ -165,26 +165,23 @@ func (handler HTTPS) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annota
165165
return r, err
166166
}
167167
reload = reload || r
168-
} else if h.HTTPS {
168+
} else if sslOffloadEnabled {
169169
logger.Panic(h.FrontendDisableSSLOffload(h.FrontHTTPS))
170-
h.HTTPS = false
171170
reload = true
172171
logger.Debug("SSLOffload disabled, reload required")
173172
}
174173
// ssl-passthrough
175174
_, errFtSSL := h.FrontendGet(h.FrontSSL)
176175
_, errBdSSL := h.BackendGet(h.BackSSL)
177-
if h.SSLPassthrough {
176+
if haproxy.SSLPassthrough {
178177
if errFtSSL != nil || errBdSSL != nil {
179178
logger.Error(handler.enableSSLPassthrough(h))
180-
h.SSLPassthrough = true
181179
reload = true
182180
logger.Debug("SSLPassthrough enabled, reload required")
183181
}
184182
logger.Error(handler.sslPassthroughRules(k, h, a))
185183
} else if errFtSSL == nil {
186184
logger.Error(handler.disableSSLPassthrough(h))
187-
h.SSLPassthrough = false
188185
reload = true
189186
logger.Debug("SSLPassthrough disabled, reload required")
190187
}
@@ -256,7 +253,7 @@ func (handler HTTPS) toggleSSLPassthrough(passthrough bool, h haproxy.HAProxy) (
256253
return err
257254
}
258255
}
259-
if h.HTTPS {
256+
if h.FrontendSSLOffloadEnabled(h.FrontHTTPS) {
260257
logger.Panic(h.FrontendEnableSSLOffload(h.FrontHTTPS, handler.CertDir, handler.alpn, handler.strictSNI))
261258
}
262259
return nil

pkg/handler/proxy-protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (handler ProxyProtocol) Update(k store.K8s, h haproxy.HAProxy, a annotation
5151
// Configure Annotation
5252
logger.Trace("Configuring ProxyProtcol annotation")
5353
frontends := []string{h.FrontHTTP, h.FrontHTTPS}
54-
if h.SSLPassthrough {
54+
if haproxy.SSLPassthrough {
5555
frontends = []string{h.FrontHTTP, h.FrontSSL}
5656
}
5757
for _, frontend := range frontends {

pkg/handler/refresh.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,20 @@ func (handler Refresh) Update(k store.K8s, h haproxy.HAProxy, a annotations.Anno
2929
if cleanCrtsAnn == "false" {
3030
cleanCrts = false
3131
}
32-
return h.Refresh(cleanCrts)
32+
// Certs
33+
if cleanCrts {
34+
reload = h.RefreshCerts()
35+
}
36+
// Rules
37+
reload = h.RefreshRules(h.HAProxyClient) || reload
38+
// Maps
39+
reload = h.RefreshMaps(h.HAProxyClient) || reload
40+
// Backends
41+
deleted, err := h.RefreshBackends()
42+
logger.Error(err)
43+
for _, backend := range deleted {
44+
logger.Debugf("Backend '%s' deleted", backend)
45+
annotations.RemoveBackendCfgSnippet(backend)
46+
}
47+
return
3348
}

pkg/haproxy/api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type HAProxyClient interface {
3939
FrontendEdit(frontend models.Frontend) error
4040
FrontendEnableSSLOffload(frontendName string, certDir string, alpn string, strictSNI bool) (err error)
4141
FrontendDisableSSLOffload(frontendName string) (err error)
42+
FrontendSSLOffloadEnabled(frontendName string) bool
4243
FrontendBindsGet(frontend string) (models.Binds, error)
4344
FrontendBindCreate(frontend string, bind models.Bind) error
4445
FrontendBindEdit(frontend string, bind models.Bind) error

pkg/haproxy/api/frontend.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ func (c *clientNative) FrontendDisableSSLOffload(frontendName string) (err error
9191
return err
9292
}
9393

94+
func (c *clientNative) FrontendSSLOffloadEnabled(frontendName string) bool {
95+
binds, err := c.FrontendBindsGet(frontendName)
96+
if err != nil {
97+
return false
98+
}
99+
for _, bind := range binds {
100+
if bind.Ssl {
101+
return true
102+
}
103+
}
104+
return false
105+
}
106+
94107
func (c *clientNative) FrontendBindsGet(frontend string) (models.Binds, error) {
95108
_, binds, err := c.nativeAPI.Configuration.GetBinds(frontend, c.activeTransaction)
96109
return binds, err

pkg/haproxy/config/main.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

pkg/haproxy/config/default.go renamed to pkg/haproxy/env/defaults.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package env
22

33
import (
44
"github.com/haproxytech/client-native/v2/models"

pkg/haproxy/config/env.go renamed to pkg/haproxy/env/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package config
15+
package env
1616

1717
import (
1818
"fmt"

pkg/haproxy/main.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,31 @@ import (
77

88
"github.com/google/renameio"
99

10-
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
1110
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
12-
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
11+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/certs"
12+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
13+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/maps"
1314
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/process"
1415
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/rules"
16+
"github.com/haproxytech/kubernetes-ingress/pkg/route"
1517
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
1618
)
1719

1820
var logger = utils.GetLogger()
1921

20-
// Instance describes and controls a HAProxy Instance
22+
var SSLPassthrough bool
23+
24+
// HAProxy holds haproxy config state
2125
type HAProxy struct {
2226
api.HAProxyClient
2327
process.Process
24-
config.Env
25-
*config.Config
28+
maps.Maps
29+
rules.Rules
30+
certs.Certificates
31+
env.Env
2632
}
2733

28-
func New(osArgs utils.OSArgs, env config.Env, cfgFile []byte, p process.Process, client api.HAProxyClient, rulesManager rules.Rules) (h HAProxy, err error) {
34+
func New(osArgs utils.OSArgs, env env.Env, cfgFile []byte, p process.Process, client api.HAProxyClient, rules rules.Rules) (h HAProxy, err error) {
2935
err = (&env).Init(osArgs)
3036
if err != nil {
3137
err = fmt.Errorf("failed to initialize haproxy environment: %w", err)
@@ -38,10 +44,14 @@ func New(osArgs utils.OSArgs, env config.Env, cfgFile []byte, p process.Process,
3844
err = fmt.Errorf("failed to write haproxy config file: %w", err)
3945
return
4046
}
41-
42-
h.Config, err = config.New(h.Env, rulesManager)
43-
if err != nil {
44-
err = fmt.Errorf("failed to initialize haproxy config state: %w", err)
47+
persistentMaps := []maps.Name{
48+
route.SNI,
49+
route.HOST,
50+
route.PATH_EXACT,
51+
route.PATH_PREFIX,
52+
}
53+
if h.Maps, err = maps.New(env.MapsDir, persistentMaps); err != nil {
54+
err = fmt.Errorf("failed to initialize haproxy maps: %w", err)
4555
return
4656
}
4757
if client == nil {
@@ -54,29 +64,22 @@ func New(osArgs utils.OSArgs, env config.Env, cfgFile []byte, p process.Process,
5464
if p == nil {
5565
h.Process = process.New(h.Env, osArgs, h.AuxCFGFile, h.HAProxyClient)
5666
}
67+
if h.Certificates, err = certs.New(env.Certs); err != nil {
68+
err = fmt.Errorf("failed to initialize haproxy certificates: %w", err)
69+
return
70+
}
71+
h.Rules = rules
5772
if !osArgs.Test {
5873
logVersion(h.Binary)
5974
}
6075
return
6176
}
6277

63-
func (h *HAProxy) Refresh(cleanCrts bool) (reload bool, err error) {
64-
// Certs
65-
if cleanCrts {
66-
reload = h.RefreshCerts()
67-
}
68-
// Rules
69-
reload = h.RefreshRules(h.HAProxyClient) || reload
70-
// Maps
71-
reload = h.RefreshMaps(h.HAProxyClient) || reload
72-
// Backends
73-
deleted, err := h.RefreshBackends()
74-
logger.Error(err)
75-
for _, backend := range deleted {
76-
logger.Debugf("Backend '%s' deleted", backend)
77-
annotations.RemoveBackendCfgSnippet(backend)
78-
}
79-
return
78+
func (h HAProxy) Clean() {
79+
SSLPassthrough = false
80+
h.CleanMaps()
81+
h.CleanCerts()
82+
h.CleanRules()
8083
}
8184

8285
func logVersion(program string) {

0 commit comments

Comments
 (0)