Skip to content

Commit 4c2877b

Browse files
ivanmatmatiMo3m3n
authored andcommitted
REORG/MINOR: Use an interface in haproxy.rules package
1 parent 2c6e0a2 commit 4c2877b

21 files changed

+142
-125
lines changed

pkg/annotations/annotations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Annotations interface {
2727
Global(g *models.Global, l *models.LogTargets) []Annotation
2828
Defaults(d *models.Defaults) []Annotation
2929
Backend(b *models.Backend, s store.K8s, c *certs.Certificates) []Annotation
30-
Frontend(i *store.Ingress, r *rules.Rules, m maps.MapFiles) []Annotation
30+
Frontend(i *store.Ingress, r *rules.List, m maps.MapFiles) []Annotation
3131
Secret(name, defaultNs string, k store.K8s, annotations ...map[string]string) (secret *store.Secret, err error)
3232
Timeout(name string, annotations ...map[string]string) (out *int64, err error)
3333
String(name string, annotations ...map[string]string) string
@@ -88,7 +88,7 @@ func (a annImpl) Defaults(d *models.Defaults) []Annotation {
8888
}
8989
}
9090

91-
func (a annImpl) Frontend(i *store.Ingress, r *rules.Rules, m maps.MapFiles) []Annotation {
91+
func (a annImpl) Frontend(i *store.Ingress, r *rules.List, m maps.MapFiles) []Annotation {
9292
reqRateLimit := ingress.NewReqRateLimit(r)
9393
httpsRedirect := ingress.NewHTTPSRedirect(r, i)
9494
hostRedirect := ingress.NewHostRedirect(r)

pkg/annotations/ingress/accessControl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import (
1414

1515
type AccessControl struct {
1616
name string
17-
rules *rules.Rules
17+
rules *rules.List
1818
maps maps.MapFiles
1919
whitelist bool
2020
}
2121

22-
func NewBlackList(n string, r *rules.Rules, m maps.MapFiles) *AccessControl {
22+
func NewBlackList(n string, r *rules.List, m maps.MapFiles) *AccessControl {
2323
return &AccessControl{name: n, rules: r, maps: m}
2424
}
2525

26-
func NewWhiteList(n string, r *rules.Rules, m maps.MapFiles) *AccessControl {
26+
func NewWhiteList(n string, r *rules.List, m maps.MapFiles) *AccessControl {
2727
return &AccessControl{name: n, rules: r, maps: m, whitelist: true}
2828
}
2929

pkg/annotations/ingress/basicAuth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
type ReqAuth struct {
1313
authRule *rules.ReqBasicAuth
14-
rules *rules.Rules
14+
rules *rules.List
1515
ingress *store.Ingress
1616
}
1717

@@ -20,7 +20,7 @@ type ReqAuthAnn struct {
2020
parent *ReqAuth
2121
}
2222

23-
func NewReqAuth(rules *rules.Rules, i *store.Ingress) *ReqAuth {
23+
func NewReqAuth(rules *rules.List, i *store.Ingress) *ReqAuth {
2424
return &ReqAuth{rules: rules, ingress: i}
2525
}
2626

pkg/annotations/ingress/hostRedirect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111

1212
type HostRedirect struct {
1313
redirect *rules.RequestRedirect
14-
rules *rules.Rules
14+
rules *rules.List
1515
}
1616

1717
type HostRedirectAnn struct {
1818
name string
1919
parent *HostRedirect
2020
}
2121

22-
func NewHostRedirect(r *rules.Rules) *HostRedirect {
22+
func NewHostRedirect(r *rules.List) *HostRedirect {
2323
return &HostRedirect{rules: r}
2424
}
2525

pkg/annotations/ingress/httpsRedirect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
type HTTPSRedirect struct {
1414
redirect *rules.RequestRedirect
15-
rules *rules.Rules
15+
rules *rules.List
1616
ingress *store.Ingress
1717
}
1818

@@ -21,7 +21,7 @@ type HTTPSRedirectAnn struct {
2121
parent *HTTPSRedirect
2222
}
2323

24-
func NewHTTPSRedirect(rules *rules.Rules, i *store.Ingress) *HTTPSRedirect {
24+
func NewHTTPSRedirect(rules *rules.List, i *store.Ingress) *HTTPSRedirect {
2525
return &HTTPSRedirect{rules: rules, ingress: i}
2626
}
2727

pkg/annotations/ingress/reqCapture.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import (
1212

1313
type ReqCapture struct {
1414
capture []*rules.ReqCapture
15-
rules *rules.Rules
15+
rules *rules.List
1616
}
1717

1818
type ReqCaptureAnn struct {
1919
name string
2020
parent *ReqCapture
2121
}
2222

23-
func NewReqCapture(r *rules.Rules) *ReqCapture {
23+
func NewReqCapture(r *rules.List) *ReqCapture {
2424
return &ReqCapture{rules: r}
2525
}
2626

pkg/annotations/ingress/reqPathRewrite.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111

1212
type ReqPathRewrite struct {
1313
name string
14-
rules *rules.Rules
14+
rules *rules.List
1515
}
1616

17-
func NewReqPathRewrite(n string, r *rules.Rules) *ReqPathRewrite {
17+
func NewReqPathRewrite(n string, r *rules.List) *ReqPathRewrite {
1818
return &ReqPathRewrite{name: n, rules: r}
1919
}
2020

pkg/annotations/ingress/reqRateLimit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import (
1313
type ReqRateLimit struct {
1414
limit *rules.ReqRateLimit
1515
track *rules.ReqTrack
16-
rules *rules.Rules
16+
rules *rules.List
1717
}
1818

1919
type ReqRateLimitAnn struct {
2020
name string
2121
parent *ReqRateLimit
2222
}
2323

24-
func NewReqRateLimit(r *rules.Rules) *ReqRateLimit {
24+
func NewReqRateLimit(r *rules.List) *ReqRateLimit {
2525
return &ReqRateLimit{rules: r}
2626
}
2727

pkg/annotations/ingress/reqSetHdr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111

1212
type SetHdr struct {
1313
name string
14-
rules *rules.Rules
14+
rules *rules.List
1515
response bool
1616
}
1717

18-
func NewReqSetHdr(n string, r *rules.Rules) *SetHdr {
18+
func NewReqSetHdr(n string, r *rules.List) *SetHdr {
1919
return &SetHdr{name: n, rules: r}
2020
}
2121

22-
func NewResSetHdr(n string, r *rules.Rules) *SetHdr {
22+
func NewResSetHdr(n string, r *rules.List) *SetHdr {
2323
return &SetHdr{name: n, rules: r, response: true}
2424
}
2525

pkg/annotations/ingress/reqSetHost.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88

99
type ReqSetHost struct {
1010
name string
11-
rules *rules.Rules
11+
rules *rules.List
1212
}
1313

14-
func NewReqSetHost(n string, r *rules.Rules) *ReqSetHost {
14+
func NewReqSetHost(n string, r *rules.List) *ReqSetHost {
1515
return &ReqSetHost{name: n, rules: r}
1616
}
1717

pkg/annotations/ingress/resSetCORS.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
const corsVarName = "cors_origin"
1414

1515
type ResSetCORS struct {
16-
rules *rules.Rules
16+
rules *rules.List
1717
acl string
1818
methods map[string]struct{}
1919
}
@@ -23,7 +23,7 @@ type ResSetCORSAnn struct {
2323
parent *ResSetCORS
2424
}
2525

26-
func NewResSetCORS(r *rules.Rules) *ResSetCORS {
26+
func NewResSetCORS(r *rules.List) *ResSetCORS {
2727
return &ResSetCORS{
2828
rules: r,
2929
methods: map[string]struct{}{"GET": {}, "POST": {}, "PUT": {}, "DELETE": {}, "HEAD": {}, "CONNECT": {}, "OPTIONS": {}, "TRACE": {}, "PATCH": {}},

pkg/annotations/ingress/srcIPHdr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88

99
type SrcIPHdr struct {
1010
name string
11-
rules *rules.Rules
11+
rules *rules.List
1212
}
1313

14-
func NewSrcIPHdr(n string, r *rules.Rules) *SrcIPHdr {
14+
func NewSrcIPHdr(n string, r *rules.List) *SrcIPHdr {
1515
return &SrcIPHdr{name: n, rules: r}
1616
}
1717

pkg/controller/builder.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
1414
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
1515
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/process"
16+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/rules"
1617
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
1718
"github.com/haproxytech/kubernetes-ingress/pkg/k8s"
1819
"github.com/haproxytech/kubernetes-ingress/pkg/store"
@@ -24,6 +25,7 @@ type Builder struct {
2425
haproxyClient api.HAProxyClient
2526
haproxyEnv config.Env
2627
haproxyProcess process.Process
28+
haproxyRules rules.Rules
2729
haproxyCfgFile []byte
2830
annotations annotations.Annotations
2931
store store.K8s
@@ -48,7 +50,8 @@ var defaultEnv = config.Env{
4850

4951
func NewBuilder() *Builder {
5052
return &Builder{haproxyEnv: defaultEnv,
51-
annotations: annotations.New(),
53+
annotations: annotations.New(),
54+
haproxyRules: rules.New(),
5255
}
5356
}
5457

@@ -62,6 +65,11 @@ func (builder *Builder) WithAnnotations(a annotations.Annotations) *Builder {
6265
return builder
6366
}
6467

68+
func (builder *Builder) WithHAProxyRules(rules rules.Rules) *Builder {
69+
builder.haproxyRules = rules
70+
return builder
71+
}
72+
6573
func (builder *Builder) WithHaproxyClient(haproxyClient api.HAProxyClient) *Builder {
6674
builder.haproxyClient = haproxyClient
6775
return builder
@@ -120,7 +128,7 @@ func (builder *Builder) Build() *HAProxyController {
120128
}()
121129
}
122130

123-
haproxy, err := haproxy.New(builder.osArgs, builder.haproxyEnv, builder.haproxyCfgFile, builder.haproxyProcess, builder.haproxyClient)
131+
haproxy, err := haproxy.New(builder.osArgs, builder.haproxyEnv, builder.haproxyCfgFile, builder.haproxyProcess, builder.haproxyClient, builder.haproxyRules)
124132
logger.Panic(err)
125133

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

pkg/controller/controller.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,51 +199,51 @@ func (c *HAProxyController) setupHAProxyRules() error {
199199
var errors utils.Errors
200200
errors.Add(
201201
// ForwardedProto rule
202-
c.haproxy.AddRule(rules.SetHdr{
202+
c.haproxy.AddRule(c.haproxy.FrontHTTPS, rules.SetHdr{
203203
ForwardedProto: true,
204-
}, false, c.haproxy.FrontHTTPS),
204+
}, false),
205205
)
206206
for _, frontend := range []string{c.haproxy.FrontHTTP, c.haproxy.FrontHTTPS} {
207207
errors.Add(
208208
// txn.base var used for logging
209-
c.haproxy.AddRule(rules.ReqSetVar{
209+
c.haproxy.AddRule(frontend, rules.ReqSetVar{
210210
Name: "base",
211211
Scope: "txn",
212212
Expression: "base",
213-
}, false, frontend),
213+
}, false),
214214
// Backend switching rules.
215-
c.haproxy.AddRule(rules.ReqSetVar{
215+
c.haproxy.AddRule(frontend, rules.ReqSetVar{
216216
Name: "path",
217217
Scope: "txn",
218218
Expression: "path",
219-
}, false, frontend),
220-
c.haproxy.AddRule(rules.ReqSetVar{
219+
}, false),
220+
c.haproxy.AddRule(frontend, rules.ReqSetVar{
221221
Name: "host",
222222
Scope: "txn",
223223
Expression: "req.hdr(Host),field(1,:),lower",
224-
}, false, frontend),
225-
c.haproxy.AddRule(rules.ReqSetVar{
224+
}, false),
225+
c.haproxy.AddRule(frontend, rules.ReqSetVar{
226226
Name: "host_match",
227227
Scope: "txn",
228228
Expression: fmt.Sprintf("var(txn.host),map(%s)", maps.GetPath(route.HOST)),
229-
}, false, frontend),
230-
c.haproxy.AddRule(rules.ReqSetVar{
229+
}, false),
230+
c.haproxy.AddRule(frontend, rules.ReqSetVar{
231231
Name: "host_match",
232232
Scope: "txn",
233233
Expression: fmt.Sprintf("var(txn.host),regsub(^[^.]*,,),map(%s,'')", maps.GetPath(route.HOST)),
234234
CondTest: "!{ var(txn.host_match) -m found }",
235-
}, false, frontend),
236-
c.haproxy.AddRule(rules.ReqSetVar{
235+
}, false),
236+
c.haproxy.AddRule(frontend, rules.ReqSetVar{
237237
Name: "path_match",
238238
Scope: "txn",
239239
Expression: fmt.Sprintf("var(txn.host_match),concat(,txn.path,),map(%s)", maps.GetPath(route.PATH_EXACT)),
240-
}, false, frontend),
241-
c.haproxy.AddRule(rules.ReqSetVar{
240+
}, false),
241+
c.haproxy.AddRule(frontend, rules.ReqSetVar{
242242
Name: "path_match",
243243
Scope: "txn",
244244
Expression: fmt.Sprintf("var(txn.host_match),concat(,txn.path,),map_beg(%s)", maps.GetPath(route.PATH_PREFIX)),
245245
CondTest: "!{ var(txn.path_match) -m found }",
246-
}, false, frontend),
246+
}, false),
247247
)
248248
}
249249
return errors.Result()

pkg/handler/https.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (handler HTTPS) disableSSLPassthrough(h haproxy.HAProxy) (err error) {
238238
if err != nil {
239239
return err
240240
}
241-
h.DeleteFrontend(h.FrontSSL)
241+
h.DeleteFTRules(h.FrontSSL)
242242
err = h.BackendDelete(h.BackSSL)
243243
if err != nil {
244244
return err
@@ -270,26 +270,26 @@ func (handler HTTPS) sslPassthroughRules(k store.K8s, h haproxy.HAProxy, a annot
270270
inspectTimeout = utils.PtrInt64(5000)
271271
}
272272
errors := utils.Errors{}
273-
errors.Add(h.AddRule(rules.ReqAcceptContent{}, false, h.FrontSSL),
274-
h.AddRule(rules.ReqInspectDelay{
273+
errors.Add(h.Rules.AddRule(h.FrontSSL, rules.ReqAcceptContent{}, false),
274+
h.Rules.AddRule(h.FrontSSL, rules.ReqInspectDelay{
275275
Timeout: inspectTimeout,
276-
}, false, h.FrontSSL),
277-
h.AddRule(rules.ReqSetVar{
276+
}, false),
277+
h.AddRule(h.FrontSSL, rules.ReqSetVar{
278278
Name: "sni",
279279
Scope: "sess",
280280
Expression: "req_ssl_sni",
281-
}, false, h.FrontSSL),
282-
h.AddRule(rules.ReqSetVar{
281+
}, false),
282+
h.AddRule(h.FrontSSL, rules.ReqSetVar{
283283
Name: "sni_match",
284284
Scope: "txn",
285285
Expression: fmt.Sprintf("req_ssl_sni,map(%s)", maps.GetPath(route.SNI)),
286-
}, false, h.FrontSSL),
287-
h.AddRule(rules.ReqSetVar{
286+
}, false),
287+
h.AddRule(h.FrontSSL, rules.ReqSetVar{
288288
Name: "sni_match",
289289
Scope: "txn",
290290
Expression: fmt.Sprintf("req_ssl_sni,regsub(^[^.]*,,),map(%s)", maps.GetPath(route.SNI)),
291291
CondTest: "!{ var(txn.sni_match) -m found }",
292-
}, false, h.FrontSSL),
292+
}, false),
293293
)
294294
return errors.Result()
295295
}

pkg/handler/proxy-protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (handler ProxyProtocol) Update(k store.K8s, h haproxy.HAProxy, a annotation
5555
frontends = []string{h.FrontHTTP, h.FrontSSL}
5656
}
5757
for _, frontend := range frontends {
58-
err = h.AddRule(rules.ReqProxyProtocol{SrcIPsMap: maps.GetPath(mapName)}, false, frontend)
58+
err = h.AddRule(frontend, rules.ReqProxyProtocol{SrcIPsMap: maps.GetPath(mapName)}, false)
5959
if err != nil {
6060
return
6161
}

0 commit comments

Comments
 (0)