Skip to content

Commit c7161e4

Browse files
committed
REORG/MINOR: move haproxy maps to haproxy/maps
1 parent d23c76e commit c7161e4

File tree

9 files changed

+72
-57
lines changed

9 files changed

+72
-57
lines changed

controller/annotations/annotations.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/haproxytech/kubernetes-ingress/controller/annotations/ingress"
1212
"github.com/haproxytech/kubernetes-ingress/controller/annotations/service"
1313
"github.com/haproxytech/kubernetes-ingress/controller/haproxy"
14+
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/maps"
1415
"github.com/haproxytech/kubernetes-ingress/controller/store"
1516
"github.com/haproxytech/kubernetes-ingress/controller/utils"
1617
)
@@ -57,7 +58,7 @@ func Defaults(d *models.Defaults) []Annotation {
5758
}
5859
}
5960

60-
func Frontend(i store.Ingress, r *haproxy.Rules, m haproxy.Maps) []Annotation {
61+
func Frontend(i store.Ingress, r *haproxy.Rules, m maps.MapFiles) []Annotation {
6162
reqRateLimit := ingress.NewReqRateLimit(r)
6263
httpsRedirect := ingress.NewHTTPSRedirect(r, i)
6364
hostRedirect := ingress.NewHostRedirect(r)

controller/annotations/ingress/accessControl.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/haproxytech/kubernetes-ingress/controller/annotations/common"
99
"github.com/haproxytech/kubernetes-ingress/controller/haproxy"
10+
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/maps"
1011
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/rules"
1112
"github.com/haproxytech/kubernetes-ingress/controller/store"
1213
"github.com/haproxytech/kubernetes-ingress/controller/utils"
@@ -15,15 +16,15 @@ import (
1516
type AccessControl struct {
1617
name string
1718
rules *haproxy.Rules
18-
maps haproxy.Maps
19+
maps maps.MapFiles
1920
whitelist bool
2021
}
2122

22-
func NewBlackList(n string, rules *haproxy.Rules, m haproxy.Maps) *AccessControl {
23+
func NewBlackList(n string, rules *haproxy.Rules, m maps.MapFiles) *AccessControl {
2324
return &AccessControl{name: n, rules: rules, maps: m}
2425
}
2526

26-
func NewWhiteList(n string, rules *haproxy.Rules, m haproxy.Maps) *AccessControl {
27+
func NewWhiteList(n string, rules *haproxy.Rules, m maps.MapFiles) *AccessControl {
2728
return &AccessControl{name: n, rules: rules, maps: m, whitelist: true}
2829
}
2930

@@ -36,13 +37,13 @@ func (a *AccessControl) Process(k store.K8s, annotations ...map[string]string) (
3637
if input == "" {
3738
return
3839
}
39-
var mapName string
40+
var mapName maps.Name
4041
var whitelist bool
4142
if a.whitelist {
42-
mapName = "whitelist-" + utils.Hash([]byte(input))
43+
mapName = maps.Name("whitelist-" + utils.Hash([]byte(input)))
4344
whitelist = true
4445
} else {
45-
mapName = "blacklist-" + utils.Hash([]byte(input))
46+
mapName = maps.Name("blacklist-" + utils.Hash([]byte(input)))
4647
}
4748
if !a.maps.Exists(mapName) {
4849
for _, address := range strings.Split(input, ",") {
@@ -56,7 +57,7 @@ func (a *AccessControl) Process(k store.K8s, annotations ...map[string]string) (
5657
}
5758
}
5859
a.rules.Add(&rules.ReqDeny{
59-
SrcIPsMap: mapName,
60+
SrcIPsMap: maps.GetPath(mapName),
6061
Whitelist: whitelist,
6162
})
6263
return

controller/configuration/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ import (
2020
"path/filepath"
2121

2222
"github.com/haproxytech/kubernetes-ingress/controller/haproxy"
23+
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/maps"
2324
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/rules"
2425
"github.com/haproxytech/kubernetes-ingress/controller/utils"
2526
)
2627

2728
type ControllerCfg struct {
28-
MapFiles *haproxy.Maps
29+
MapFiles *maps.MapFiles
2930
HAProxyRules haproxy.SectionRules
3031
Certificates *haproxy.Certificates
3132
ActiveBackends map[string]struct{}
@@ -68,7 +69,7 @@ func (c *ControllerCfg) Init() (err error) {
6869
if err = c.envInit(); err != nil {
6970
return err
7071
}
71-
c.MapFiles = haproxy.NewMapFiles(c.Env.MapDir)
72+
c.MapFiles = maps.New(c.Env.MapDir)
7273
if err := c.haproxyRulesInit(); err != nil {
7374
return err
7475
}
@@ -112,23 +113,23 @@ func (c *ControllerCfg) haproxyRulesInit() error {
112113
c.HAProxyRules.AddRule(rules.ReqSetVar{
113114
Name: "host_match",
114115
Scope: "txn",
115-
Expression: fmt.Sprintf("var(txn.host),map(%s)", haproxy.GetMapPath(haproxy.MAP_HOST)),
116+
Expression: fmt.Sprintf("var(txn.host),map(%s)", maps.GetPath(maps.HOST)),
116117
}, false, frontend),
117118
c.HAProxyRules.AddRule(rules.ReqSetVar{
118119
Name: "host_match",
119120
Scope: "txn",
120-
Expression: fmt.Sprintf("var(txn.host),regsub(^[^.]*,,),map(%s,'')", haproxy.GetMapPath(haproxy.MAP_HOST)),
121+
Expression: fmt.Sprintf("var(txn.host),regsub(^[^.]*,,),map(%s,'')", maps.GetPath(maps.HOST)),
121122
CondTest: "!{ var(txn.host_match) -m found }",
122123
}, false, frontend),
123124
c.HAProxyRules.AddRule(rules.ReqSetVar{
124125
Name: "path_match",
125126
Scope: "txn",
126-
Expression: fmt.Sprintf("var(txn.host_match),concat(,txn.path,),map(%s)", haproxy.GetMapPath(haproxy.MAP_PATH_EXACT)),
127+
Expression: fmt.Sprintf("var(txn.host_match),concat(,txn.path,),map(%s)", maps.GetPath(maps.PATH_EXACT)),
127128
}, false, frontend),
128129
c.HAProxyRules.AddRule(rules.ReqSetVar{
129130
Name: "path_match",
130131
Scope: "txn",
131-
Expression: fmt.Sprintf("var(txn.host_match),concat(,txn.path,),map_beg(%s)", haproxy.GetMapPath(haproxy.MAP_PATH_PREFIX)),
132+
Expression: fmt.Sprintf("var(txn.host_match),concat(,txn.path,),map_beg(%s)", maps.GetPath(maps.PATH_PREFIX)),
132133
CondTest: "!{ var(txn.path_match) -m found }",
133134
}, false, frontend),
134135
)

controller/handler/https.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
config "github.com/haproxytech/kubernetes-ingress/controller/configuration"
2525
"github.com/haproxytech/kubernetes-ingress/controller/haproxy"
2626
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/api"
27+
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/maps"
2728
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/rules"
2829
"github.com/haproxytech/kubernetes-ingress/controller/store"
2930
"github.com/haproxytech/kubernetes-ingress/controller/utils"
@@ -195,7 +196,7 @@ func (h HTTPS) enableSSLPassthrough(cfg *config.ControllerCfg, api api.HAProxyCl
195196
frontend := models.Frontend{
196197
Name: cfg.FrontSSL,
197198
Mode: "tcp",
198-
LogFormat: "'%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs haproxy.MAP_SNI: %[var(sess.sni)]'",
199+
LogFormat: "'%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs SNI: %[var(sess.sni)]'",
199200
DefaultBackend: cfg.BackSSL,
200201
}
201202
err = api.FrontendCreate(frontend)
@@ -276,12 +277,12 @@ func (h HTTPS) sslPassthroughRules(k store.K8s, cfg *config.ControllerCfg) error
276277
cfg.HAProxyRules.AddRule(rules.ReqSetVar{
277278
Name: "sni_match",
278279
Scope: "txn",
279-
Expression: fmt.Sprintf("req_ssl_sni,map(%s)", haproxy.GetMapPath(haproxy.MAP_SNI)),
280+
Expression: fmt.Sprintf("req_ssl_sni,map(%s)", maps.GetPath(maps.SNI)),
280281
}, false, cfg.FrontSSL),
281282
cfg.HAProxyRules.AddRule(rules.ReqSetVar{
282283
Name: "sni_match",
283284
Scope: "txn",
284-
Expression: fmt.Sprintf("req_ssl_sni,regsub(^[^.]*,,),map(%s)", haproxy.GetMapPath(haproxy.MAP_SNI)),
285+
Expression: fmt.Sprintf("req_ssl_sni,regsub(^[^.]*,,),map(%s)", maps.GetPath(maps.SNI)),
285286
CondTest: "!{ var(txn.sni_match) -m found }",
286287
}, false, cfg.FrontSSL),
287288
)

controller/handler/proxy-protocol.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/haproxytech/kubernetes-ingress/controller/annotations"
2222
config "github.com/haproxytech/kubernetes-ingress/controller/configuration"
2323
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/api"
24+
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/maps"
2425
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/rules"
2526
"github.com/haproxytech/kubernetes-ingress/controller/store"
2627
"github.com/haproxytech/kubernetes-ingress/controller/utils"
@@ -35,7 +36,7 @@ func (p ProxyProtocol) Update(k store.K8s, cfg *config.ControllerCfg, api api.HA
3536
return false, nil
3637
}
3738
// Validate annotation
38-
mapName := "proxy-protocol-" + utils.Hash([]byte(annProxyProtocol))
39+
mapName := maps.Name("proxy-protocol-" + utils.Hash([]byte(annProxyProtocol)))
3940
if !cfg.MapFiles.Exists(mapName) {
4041
for _, address := range strings.Split(annProxyProtocol, ",") {
4142
address = strings.TrimSpace(address)
@@ -55,7 +56,7 @@ func (p ProxyProtocol) Update(k store.K8s, cfg *config.ControllerCfg, api api.HA
5556
frontends = []string{cfg.FrontHTTP, cfg.FrontSSL}
5657
}
5758
for _, frontend := range frontends {
58-
err = cfg.HAProxyRules.AddRule(rules.ReqProxyProtocol{SrcIPsMap: mapName}, false, frontend)
59+
err = cfg.HAProxyRules.AddRule(rules.ReqProxyProtocol{SrcIPsMap: maps.GetPath(mapName)}, false, frontend)
5960
if err != nil {
6061
return
6162
}

controller/haproxy/maps.go renamed to controller/haproxy/maps/main.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 HAProxy Technologies LLC
1+
// CopyriFiles 2019 HAProxy Technologies LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package haproxy
15+
package maps
1616

1717
import (
1818
"hash/fnv"
@@ -22,18 +22,26 @@ import (
2222
"strings"
2323

2424
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/api"
25+
"github.com/haproxytech/kubernetes-ingress/controller/utils"
2526
)
2627

27-
type Maps map[string]*mapFile
28+
type MapFiles map[Name]*mapFile
29+
30+
type Name string
31+
32+
type Path string
33+
34+
// module logger
35+
var logger = utils.GetLogger()
2836

2937
var mapDir string
3038

3139
//nolint:golint,stylecheck
3240
const (
33-
MAP_SNI = "sni"
34-
MAP_HOST = "host"
35-
MAP_PATH_EXACT = "path-exact"
36-
MAP_PATH_PREFIX = "path-prefix"
41+
SNI Name = "sni"
42+
HOST Name = "host"
43+
PATH_EXACT Name = "path-exact"
44+
PATH_PREFIX Name = "path-prefix"
3745
)
3846

3947
type mapFile struct {
@@ -55,24 +63,24 @@ func (mf *mapFile) getContent() (string, uint64) {
5563
return content, h.Sum64()
5664
}
5765

58-
func NewMapFiles(path string) *Maps {
66+
func New(path string) *MapFiles {
5967
mapDir = path
60-
var maps Maps = map[string]*mapFile{
68+
var maps MapFiles = map[Name]*mapFile{
6169
// Map files required for HAProxy Rules
62-
MAP_SNI: {preserve: true},
63-
MAP_HOST: {preserve: true},
64-
MAP_PATH_EXACT: {preserve: true},
65-
MAP_PATH_PREFIX: {preserve: true},
70+
SNI: {preserve: true},
71+
HOST: {preserve: true},
72+
PATH_EXACT: {preserve: true},
73+
PATH_PREFIX: {preserve: true},
6674
}
6775
return &maps
6876
}
6977

70-
func (m Maps) Exists(name string) bool {
78+
func (m MapFiles) Exists(name Name) bool {
7179
return m[name] != nil && len(m[name].rows) != 0
7280
}
7381

7482
// AppendRow appends row to mapFile
75-
func (m Maps) AppendRow(name string, row string) {
83+
func (m MapFiles) AppendRow(name Name, row string) {
7684
if row == "" {
7785
return
7886
}
@@ -82,13 +90,13 @@ func (m Maps) AppendRow(name string, row string) {
8290
m[name].rows = append(m[name].rows, row)
8391
}
8492

85-
func (m Maps) Clean() {
93+
func (m MapFiles) Clean() {
8694
for _, mapFile := range m {
8795
mapFile.rows = []string{}
8896
}
8997
}
9098

91-
func (m Maps) Refresh(client api.HAProxyClient) (reload bool) {
99+
func (m MapFiles) Refresh(client api.HAProxyClient) (reload bool) {
92100
for name, mapFile := range m {
93101
content, hash := mapFile.getContent()
94102
if mapFile.hash == hash {
@@ -97,12 +105,12 @@ func (m Maps) Refresh(client api.HAProxyClient) (reload bool) {
97105
mapFile.hash = hash
98106
var f *os.File
99107
var err error
100-
filename := GetMapPath(name)
108+
filename := GetPath(name)
101109
if content == "" && !mapFile.preserve {
102-
logger.Error(os.Remove(filename))
110+
logger.Error(os.Remove(string(filename)))
103111
delete(m, name)
104112
continue
105-
} else if f, err = os.Create(filename); err != nil {
113+
} else if f, err = os.Create(string(filename)); err != nil {
106114
logger.Error(err)
107115
continue
108116
}
@@ -126,6 +134,6 @@ func (m Maps) Refresh(client api.HAProxyClient) (reload bool) {
126134
return reload
127135
}
128136

129-
func GetMapPath(name string) string {
130-
return path.Join(mapDir, name) + ".map"
137+
func GetPath(name Name) Path {
138+
return Path(path.Join(mapDir, string(name)) + ".map")
131139
}

controller/haproxy/rules/reqDeny.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77

88
"github.com/haproxytech/kubernetes-ingress/controller/haproxy"
99
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/api"
10+
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/maps"
1011
"github.com/haproxytech/kubernetes-ingress/controller/utils"
1112
)
1213

1314
type ReqDeny struct {
14-
SrcIPsMap string
15+
SrcIPsMap maps.Path
1516
Whitelist bool
1617
}
1718

@@ -20,7 +21,6 @@ func (r ReqDeny) GetType() haproxy.RuleType {
2021
}
2122

2223
func (r ReqDeny) Create(client api.HAProxyClient, frontend *models.Frontend, ingressACL string) error {
23-
srcIpsMap := haproxy.GetMapPath(r.SrcIPsMap)
2424
not := ""
2525
if r.Whitelist {
2626
not = "!"
@@ -31,7 +31,7 @@ func (r ReqDeny) Create(client api.HAProxyClient, frontend *models.Frontend, ing
3131
Type: "content",
3232
Action: "reject",
3333
Cond: "if",
34-
CondTest: fmt.Sprintf("%s{ src -f %s }", not, srcIpsMap),
34+
CondTest: fmt.Sprintf("%s{ src -f %s }", not, r.SrcIPsMap),
3535
}
3636
return client.FrontendTCPRequestRuleCreate(frontend.Name, tcpRule, ingressACL)
3737
}
@@ -40,7 +40,7 @@ func (r ReqDeny) Create(client api.HAProxyClient, frontend *models.Frontend, ing
4040
Type: "deny",
4141
DenyStatus: utils.PtrInt64(403),
4242
Cond: "if",
43-
CondTest: fmt.Sprintf("%s{ src -f %s }", not, srcIpsMap),
43+
CondTest: fmt.Sprintf("%s{ src -f %s }", not, r.SrcIPsMap),
4444
}
4545
return client.FrontendHTTPRequestRuleCreate(frontend.Name, httpRule, ingressACL)
4646
}

controller/haproxy/rules/reqProxyProtocol.go

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

88
"github.com/haproxytech/kubernetes-ingress/controller/haproxy"
99
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/api"
10+
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/maps"
1011
"github.com/haproxytech/kubernetes-ingress/controller/utils"
1112
)
1213

1314
type ReqProxyProtocol struct {
14-
SrcIPsMap string
15+
SrcIPsMap maps.Path
1516
}
1617

1718
func (r ReqProxyProtocol) GetType() haproxy.RuleType {
@@ -24,7 +25,7 @@ func (r ReqProxyProtocol) Create(client api.HAProxyClient, frontend *models.Fron
2425
Type: "connection",
2526
Action: models.TCPRequestRuleActionExpectProxy,
2627
Cond: "if",
27-
CondTest: fmt.Sprintf("{ src -f %s }", haproxy.GetMapPath(r.SrcIPsMap)),
28+
CondTest: fmt.Sprintf("{ src -f %s }", r.SrcIPsMap),
2829
}
2930
return client.FrontendTCPRequestRuleCreate(frontend.Name, tcpRule, ingressACL)
3031
}

0 commit comments

Comments
 (0)