Skip to content

Commit e5cd2b6

Browse files
ivanmatmatiMo3m3n
authored andcommitted
REORG/MINOR: add main interface to annotatins package
1 parent 20b3f38 commit e5cd2b6

File tree

18 files changed

+130
-97
lines changed

18 files changed

+130
-97
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func main() {
106106
go k.MonitorChanges(eventChan, ingressChan, stop)
107107
go c.Start()
108108
if publishService != nil {
109-
go ingress.UpdateStatus(k.GetClientset(), s, osArgs.IngressClass, osArgs.EmptyIngressClass, ingressChan)
109+
go ingress.UpdateStatus(k.GetClientset(), s, osArgs.IngressClass, osArgs.EmptyIngressClass, ingressChan, annotations.New())
110110
}
111111

112112
// Catch QUIT signals

pkg/annotations/annotations.go

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,36 @@ type Annotation interface {
2222
Process(k store.K8s, annotations ...map[string]string) error
2323
}
2424

25-
func GlobalCfgSnipp() []Annotation {
25+
type Annotations interface {
26+
GlobalCfgSnipp() []Annotation
27+
Global(g *models.Global, l *models.LogTargets) []Annotation
28+
Defaults(d *models.Defaults) []Annotation
29+
Backend(b *models.Backend, s store.K8s, c *certs.Certificates) []Annotation
30+
Frontend(i *store.Ingress, r *rules.Rules, m maps.MapFiles) []Annotation
31+
Secret(name, defaultNs string, k store.K8s, annotations ...map[string]string) (secret *store.Secret, err error)
32+
Timeout(name string, annotations ...map[string]string) (out *int64, err error)
33+
String(name string, annotations ...map[string]string) string
34+
}
35+
36+
type annImpl struct{}
37+
38+
func New() Annotations {
39+
return annImpl{}
40+
}
41+
42+
func (a annImpl) String(name string, annotations ...map[string]string) string {
43+
return String(name, annotations...)
44+
}
45+
46+
func (a annImpl) Secret(name, defaultNs string, k store.K8s, annotations ...map[string]string) (secret *store.Secret, err error) {
47+
return Secret(name, defaultNs, k, annotations...)
48+
}
49+
50+
func (a annImpl) Timeout(name string, annotations ...map[string]string) (out *int64, err error) {
51+
return Timeout(name, annotations...)
52+
}
53+
54+
func (a annImpl) GlobalCfgSnipp() []Annotation {
2655
return []Annotation{
2756
NewGlobalCfgSnippet("global-config-snippet"),
2857
NewFrontendCfgSnippet("frontend-config-snippet", "http"),
@@ -31,7 +60,7 @@ func GlobalCfgSnipp() []Annotation {
3160
}
3261
}
3362

34-
func Global(g *models.Global, l *models.LogTargets) []Annotation {
63+
func (a annImpl) Global(g *models.Global, l *models.LogTargets) []Annotation {
3564
return []Annotation{
3665
global.NewSyslogServers("syslog-server", l),
3766
global.NewNbthread("nbthread", g),
@@ -40,7 +69,7 @@ func Global(g *models.Global, l *models.LogTargets) []Annotation {
4069
}
4170
}
4271

43-
func Defaults(d *models.Defaults) []Annotation {
72+
func (a annImpl) Defaults(d *models.Defaults) []Annotation {
4473
return []Annotation{
4574
global.NewOption("http-server-close", d),
4675
global.NewOption("http-keep-alive", d),
@@ -59,7 +88,7 @@ func Defaults(d *models.Defaults) []Annotation {
5988
}
6089
}
6190

62-
func Frontend(i *store.Ingress, r *rules.Rules, m maps.MapFiles) []Annotation {
91+
func (a annImpl) Frontend(i *store.Ingress, r *rules.Rules, m maps.MapFiles) []Annotation {
6392
reqRateLimit := ingress.NewReqRateLimit(r)
6493
httpsRedirect := ingress.NewHTTPSRedirect(r, i)
6594
hostRedirect := ingress.NewHostRedirect(r)
@@ -100,7 +129,7 @@ func Frontend(i *store.Ingress, r *rules.Rules, m maps.MapFiles) []Annotation {
100129
}
101130
}
102131

103-
func Backend(b *models.Backend, s store.K8s, c *certs.Certificates) []Annotation {
132+
func (a annImpl) Backend(b *models.Backend, s store.K8s, c *certs.Certificates) []Annotation {
104133
annotations := []Annotation{
105134
service.NewAbortOnClose("abortonclose", b),
106135
service.NewTimeoutCheck("timeout-check", b),
@@ -183,27 +212,6 @@ func Secret(name, defaultNs string, k store.K8s, annotations ...map[string]strin
183212
return
184213
}
185214

186-
func Service(name, defaultNs string, k store.K8s, annotations ...map[string]string) (service *store.Service, err error) {
187-
var svcNs, svcName string
188-
svcNs, svcName, err = common.GetK8sPath(name, annotations...)
189-
if err != nil {
190-
err = fmt.Errorf("annotation '%s': %w", name, err)
191-
return
192-
}
193-
if svcName == "" {
194-
return
195-
}
196-
if svcNs == "" {
197-
svcNs = defaultNs
198-
}
199-
service, err = k.GetService(svcNs, svcName)
200-
if err != nil {
201-
err = fmt.Errorf("annotation '%s': %w", name, err)
202-
return
203-
}
204-
return
205-
}
206-
207215
func String(name string, annotations ...map[string]string) string {
208216
return common.GetValue(name, annotations...)
209217
}

pkg/controller/builder.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/prometheus/client_golang/prometheus/promhttp"
1010

11+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
1112
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
1213
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
1314
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
@@ -24,6 +25,7 @@ type Builder struct {
2425
haproxyEnv config.Env
2526
haproxyProcess process.Process
2627
haproxyCfgFile []byte
28+
annotations annotations.Annotations
2729
store store.K8s
2830
publishService *utils.NamespaceValue
2931
eventChan chan k8s.SyncDataEvent
@@ -45,14 +47,21 @@ var defaultEnv = config.Env{
4547
}
4648

4749
func NewBuilder() *Builder {
48-
return &Builder{haproxyEnv: defaultEnv}
50+
return &Builder{haproxyEnv: defaultEnv,
51+
annotations: annotations.New(),
52+
}
4953
}
5054

5155
func (builder *Builder) WithHAProxyProcess(process process.Process) *Builder {
5256
builder.haproxyProcess = process
5357
return builder
5458
}
5559

60+
func (builder *Builder) WithAnnotations(a annotations.Annotations) *Builder {
61+
builder.annotations = a
62+
return builder
63+
}
64+
5665
func (builder *Builder) WithHaproxyClient(haproxyClient api.HAProxyClient) *Builder {
5766
builder.haproxyClient = haproxyClient
5867
return builder
@@ -125,5 +134,6 @@ func (builder *Builder) Build() *HAProxyController {
125134
eventChan: builder.eventChan,
126135
ingressChan: builder.ingressChan,
127136
publishService: builder.publishService,
137+
annotations: builder.annotations,
128138
}
129139
}

pkg/controller/controller.go

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

2121
"github.com/haproxytech/client-native/v2/models"
22+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
2223
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
2324
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/maps"
2425
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/rules"
@@ -36,6 +37,7 @@ type HAProxyController struct {
3637
haproxy haproxy.HAProxy
3738
osArgs utils.OSArgs
3839
store store.K8s
40+
annotations annotations.Annotations
3941
publishService *utils.NamespaceValue
4042
auxCfgModTime int64
4143
eventChan chan k8s.SyncDataEvent
@@ -111,7 +113,7 @@ func (c *HAProxyController) updateHAProxy() {
111113
continue
112114
}
113115
for _, ingResource := range namespace.Ingresses {
114-
i := ingress.New(c.store, ingResource, c.osArgs.IngressClass, c.osArgs.EmptyIngressClass)
116+
i := ingress.New(c.store, ingResource, c.osArgs.IngressClass, c.osArgs.EmptyIngressClass, c.annotations)
115117
if i == nil {
116118
logger.Debugf("ingress '%s/%s' ignored: no matching IngressClass", ingResource.Namespace, ingResource.Name)
117119
continue
@@ -123,12 +125,12 @@ func (c *HAProxyController) updateHAProxy() {
123125
logger.Errorf("Ingress %s/%s: unable to sync status: sync channel full", ingResource.Namespace, ingResource.Name)
124126
}
125127
}
126-
c.reload = i.Update(c.store, c.haproxy) || c.reload
128+
c.reload = i.Update(c.store, c.haproxy, c.annotations) || c.reload
127129
}
128130
}
129131

130132
for _, handler := range c.updateHandlers {
131-
reload, err = handler.Update(c.store, c.haproxy)
133+
reload, err = handler.Update(c.store, c.haproxy, c.annotations)
132134
logger.Error(err)
133135
c.reload = c.reload || reload
134136
}

pkg/controller/global.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (c *HAProxyController) handleGlobalConfig() (reload, restart bool) {
3333
reload = c.defaultsCfg() || reload
3434
c.handleDefaultCert()
3535
reload = c.handleDefaultService() || reload
36-
(&ingress.Ingress{}).HandleAnnotations(c.store, c.haproxy)
36+
ingress.HandleCfgMapAnnotations(c.store, c.haproxy, c.annotations)
3737
return reload, restart
3838
}
3939

@@ -62,7 +62,7 @@ func (c *HAProxyController) globalCfg() (reload, restart bool) {
6262
}
6363
if newGlobal == nil {
6464
newGlobal = &models.Global{}
65-
for _, a := range annotations.Global(newGlobal, &newLg) {
65+
for _, a := range c.annotations.Global(newGlobal, &newLg) {
6666
err = a.Process(c.store, c.store.ConfigMaps.Main.Annotations)
6767
if err != nil {
6868
logger.Errorf("annotation %s: %s", a.GetName(), err)
@@ -89,7 +89,7 @@ func (c *HAProxyController) globalCfg() (reload, restart bool) {
8989

9090
func (c *HAProxyController) globalCfgSnipp() (reload, restart bool) {
9191
var err error
92-
for _, a := range annotations.GlobalCfgSnipp() {
92+
for _, a := range c.annotations.GlobalCfgSnipp() {
9393
err = a.Process(c.store, c.store.ConfigMaps.Main.Annotations)
9494
if err != nil {
9595
logger.Errorf("annotation %s: %s", a.GetName(), err)
@@ -123,7 +123,7 @@ func (c *HAProxyController) defaultsCfg() (reload bool) {
123123
}
124124
if newDefaults == nil {
125125
newDefaults = &models.Defaults{}
126-
for _, a := range annotations.Defaults(newDefaults) {
126+
for _, a := range c.annotations.Defaults(newDefaults) {
127127
logger.Error(a.Process(c.store, c.store.ConfigMaps.Main.Annotations))
128128
}
129129
}
@@ -156,7 +156,7 @@ func (c *HAProxyController) handleDefaultService() (reload bool) {
156156
IsDefaultBackend: true,
157157
}
158158
if svc, err = service.New(c.store, ingressPath, nil, false, c.store.ConfigMaps.Main.Annotations); err == nil {
159-
reload, err = svc.SetDefaultBackend(c.store, c.haproxy, []string{c.haproxy.FrontHTTP, c.haproxy.FrontHTTPS})
159+
reload, err = svc.SetDefaultBackend(c.store, c.haproxy, []string{c.haproxy.FrontHTTP, c.haproxy.FrontHTTPS}, c.annotations)
160160
}
161161
if err != nil {
162162
logger.Errorf("default service: %s", err)

pkg/controller/handler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
package controller
1616

1717
import (
18+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
1819
"github.com/haproxytech/kubernetes-ingress/pkg/handler"
1920
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
2021
"github.com/haproxytech/kubernetes-ingress/pkg/store"
2122
)
2223

2324
type UpdateHandler interface {
24-
Update(k store.K8s, h haproxy.HAProxy) (reload bool, err error)
25+
Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error)
2526
}
2627

2728
func (c *HAProxyController) initHandlers() {
@@ -71,7 +72,7 @@ func (c *HAProxyController) startupHandlers() error {
7172
IPv6Addr: c.osArgs.IPV6BindAddr,
7273
}}
7374
for _, handler := range handlers {
74-
_, err := handler.Update(c.store, c.haproxy)
75+
_, err := handler.Update(c.store, c.haproxy, c.annotations)
7576
if err != nil {
7677
return err
7778
}

pkg/handler/errorfiles.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/haproxytech/client-native/v2/models"
2323

24+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
2425
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
2526
"github.com/haproxytech/kubernetes-ingress/pkg/store"
2627
)
@@ -29,7 +30,7 @@ type ErrorFiles struct {
2930
files files
3031
}
3132

32-
func (handler *ErrorFiles) Update(k store.K8s, h haproxy.HAProxy) (reload bool, err error) {
33+
func (handler *ErrorFiles) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
3334
handler.files.dir = h.ErrFileDir
3435
if k.ConfigMaps.Errorfiles == nil {
3536
return false, nil

pkg/handler/globalcfg.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package handler
1717
import (
1818
"github.com/haproxytech/client-native/v2/models"
1919

20+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
2021
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
2122
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/config"
2223
"github.com/haproxytech/kubernetes-ingress/pkg/store"
@@ -25,7 +26,7 @@ import (
2526
type GlobalCfg struct {
2627
}
2728

28-
func (handler GlobalCfg) Update(k store.K8s, h haproxy.HAProxy) (reload bool, err error) {
29+
func (handler GlobalCfg) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
2930
global := &models.Global{}
3031
logTargets := &models.LogTargets{}
3132
config.SetGlobal(global, logTargets, h.Env)

pkg/handler/http-bind.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package handler
1717
import (
1818
"github.com/haproxytech/client-native/v2/models"
1919

20+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
2021
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
2122
"github.com/haproxytech/kubernetes-ingress/pkg/store"
2223
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
@@ -33,7 +34,7 @@ type HTTPBind struct {
3334
IPv6Addr string
3435
}
3536

36-
func (handler HTTPBind) Update(k store.K8s, h haproxy.HAProxy) (reload bool, err error) {
37+
func (handler HTTPBind) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
3738
var errors utils.Errors
3839
frontends := make(map[string]int64, 2)
3940
protos := make(map[string]string, 2)

pkg/handler/https.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ func (handler HTTPS) handleClientTLSAuth(k store.K8s, h haproxy.HAProxy) (reload
140140
return
141141
}
142142

143-
func (handler HTTPS) Update(k store.K8s, h haproxy.HAProxy) (reload bool, err error) {
143+
func (handler HTTPS) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
144144
if !handler.Enabled {
145145
logger.Debug("Cannot proceed with SSL Passthrough update, HTTPS is disabled")
146146
return false, nil
147147
}
148148

149149
// Fetch tls-alpn value for when SSL offloading is enabled
150-
handler.alpn = annotations.String("tls-alpn", k.ConfigMaps.Main.Annotations)
150+
handler.alpn = a.String("tls-alpn", k.ConfigMaps.Main.Annotations)
151151

152152
handler.strictSNI, err = annotations.Bool("client-strict-sni", k.ConfigMaps.Main.Annotations)
153153
logger.Error(err)
@@ -180,7 +180,7 @@ func (handler HTTPS) Update(k store.K8s, h haproxy.HAProxy) (reload bool, err er
180180
reload = true
181181
logger.Debug("SSLPassthrough enabled, reload required")
182182
}
183-
logger.Error(handler.sslPassthroughRules(k, h))
183+
logger.Error(handler.sslPassthroughRules(k, h, a))
184184
} else if errFtSSL == nil {
185185
logger.Error(handler.disableSSLPassthrough(h))
186186
h.SSLPassthrough = false
@@ -261,8 +261,8 @@ func (handler HTTPS) toggleSSLPassthrough(passthrough bool, h haproxy.HAProxy) (
261261
return nil
262262
}
263263

264-
func (handler HTTPS) sslPassthroughRules(k store.K8s, h haproxy.HAProxy) error {
265-
inspectTimeout, err := annotations.Timeout("timeout-client", k.ConfigMaps.Main.Annotations)
264+
func (handler HTTPS) sslPassthroughRules(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) error {
265+
inspectTimeout, err := a.Timeout("timeout-client", k.ConfigMaps.Main.Annotations)
266266
if inspectTimeout == nil {
267267
if err != nil {
268268
logger.Errorf("SSL Passthrough: %s", err)

pkg/handler/pattern-files.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/google/renameio"
2222

23+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
2324
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
2425
"github.com/haproxytech/kubernetes-ingress/pkg/store"
2526
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
@@ -29,7 +30,7 @@ type PatternFiles struct {
2930
files files
3031
}
3132

32-
func (handler *PatternFiles) Update(k store.K8s, h haproxy.HAProxy) (reload bool, err error) {
33+
func (handler *PatternFiles) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
3334
handler.files.dir = h.Env.PatternDir
3435
if k.ConfigMaps.PatternFiles == nil {
3536
return false, nil

pkg/handler/pprof.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package handler
1717
import (
1818
"github.com/haproxytech/client-native/v2/models"
1919

20+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
2021
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
2122
"github.com/haproxytech/kubernetes-ingress/pkg/route"
2223
"github.com/haproxytech/kubernetes-ingress/pkg/store"
@@ -25,7 +26,7 @@ import (
2526
type Pprof struct {
2627
}
2728

28-
func (handler Pprof) Update(k store.K8s, h haproxy.HAProxy) (reload bool, err error) {
29+
func (handler Pprof) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
2930
pprofBackend := "pprof"
3031

3132
_, err = h.BackendGet(pprofBackend)

0 commit comments

Comments
 (0)