Skip to content

Commit b18b6c2

Browse files
committed
REORG: move Ingress handler in a seperate pacakge
1 parent 9d18d7f commit b18b6c2

File tree

17 files changed

+364
-359
lines changed

17 files changed

+364
-359
lines changed

controller/annotations/annotations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func Defaults(d *models.Defaults) []Annotation {
5959
}
6060
}
6161

62-
func Frontend(i store.Ingress, r *rules.Rules, m maps.MapFiles) []Annotation {
62+
func Frontend(i *store.Ingress, r *rules.Rules, m maps.MapFiles) []Annotation {
6363
reqRateLimit := ingress.NewReqRateLimit(r)
6464
httpsRedirect := ingress.NewHTTPSRedirect(r, i)
6565
hostRedirect := ingress.NewHostRedirect(r)

controller/annotations/ingress/basicAuth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import (
1212
type ReqAuth struct {
1313
authRule *rules.ReqBasicAuth
1414
rules *rules.Rules
15-
ingress store.Ingress
15+
ingress *store.Ingress
1616
}
1717

1818
type ReqAuthAnn struct {
1919
name string
2020
parent *ReqAuth
2121
}
2222

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

2727
func (p *ReqAuth) NewAnnotation(n string) ReqAuthAnn {
@@ -44,7 +44,7 @@ func (a ReqAuthAnn) Process(k store.K8s, annotations ...map[string]string) (err
4444
return fmt.Errorf("incorrect auth-type value '%s'. Only 'basic-auth' value is currently supported", input)
4545
}
4646
authGroup := "Global"
47-
if a.parent.ingress.Namespace != "" {
47+
if a.parent.ingress != nil {
4848
authGroup = fmt.Sprintf("%s-%s", a.parent.ingress.Namespace, a.parent.ingress.Name)
4949
}
5050
a.parent.authRule = &rules.ReqBasicAuth{

controller/annotations/ingress/httpsRedirect.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ import (
1313
type HTTPSRedirect struct {
1414
redirect *rules.RequestRedirect
1515
rules *rules.Rules
16-
ingress store.Ingress
16+
ingress *store.Ingress
1717
}
1818

1919
type HTTPSRedirectAnn struct {
2020
name string
2121
parent *HTTPSRedirect
2222
}
2323

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

2828
func (p *HTTPSRedirect) NewAnnotation(n string) HTTPSRedirectAnn {
@@ -86,7 +86,10 @@ func (a HTTPSRedirectAnn) Process(k store.K8s, annotations ...map[string]string)
8686
return
8787
}
8888

89-
func tlsEnabled(ingress store.Ingress) bool {
89+
func tlsEnabled(ingress *store.Ingress) bool {
90+
if ingress == nil {
91+
return false
92+
}
9093
for _, tls := range ingress.TLS {
9194
if tls.Status != store.DELETED {
9295
return true

controller/controller.go

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ import (
2121
"github.com/haproxytech/client-native/v2/models"
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/certs"
2524
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/process"
25+
"github.com/haproxytech/kubernetes-ingress/controller/ingress"
2626
"github.com/haproxytech/kubernetes-ingress/controller/route"
27-
"github.com/haproxytech/kubernetes-ingress/controller/status"
2827
"github.com/haproxytech/kubernetes-ingress/controller/store"
2928
"github.com/haproxytech/kubernetes-ingress/controller/utils"
3029
"k8s.io/apimachinery/pkg/watch"
@@ -40,7 +39,7 @@ type HAProxyController struct {
4039
PublishService *utils.NamespaceValue
4140
AuxCfgModTime int64
4241
eventChan chan SyncDataEvent
43-
statusChan chan status.SyncIngress
42+
ingressChan chan ingress.Sync
4443
k8s *K8s
4544
ready bool
4645
reload bool
@@ -122,8 +121,8 @@ func (c *HAProxyController) Start() {
122121
go c.monitorChanges()
123122
if c.PublishService != nil {
124123
// Update Ingress status
125-
c.statusChan = make(chan status.SyncIngress, watch.DefaultChanSize*6)
126-
go status.UpdateIngress(c.k8s.API, c.Store, c.statusChan)
124+
c.ingressChan = make(chan ingress.Sync, watch.DefaultChanSize*6)
125+
go ingress.UpdateStatus(c.k8s.API, c.Store, c.OSArgs.IngressClass, c.OSArgs.EmptyIngressClass, c.ingressChan)
127126
}
128127
}
129128

@@ -160,60 +159,23 @@ func (c *HAProxyController) updateHAProxy() {
160159
if !namespace.Relevant {
161160
continue
162161
}
163-
for _, ingress := range namespace.Ingresses {
164-
if ingress.Status == DELETED {
162+
for _, ingResource := range namespace.Ingresses {
163+
if ingResource.Status == DELETED {
165164
continue
166165
}
167-
if !c.igClassIsSupported(ingress) {
168-
logger.Debugf("ingress '%s/%s' ignored: no matching IngressClass", ingress.Namespace, ingress.Name)
166+
i := ingress.New(ingResource, c.OSArgs.IngressClass, c.OSArgs.EmptyIngressClass)
167+
if !i.Supported(c.Store) {
168+
logger.Debugf("ingress '%s/%s' ignored: no matching IngressClass", ingResource.Namespace, ingResource.Name)
169169
continue
170170
}
171-
if c.PublishService != nil && ingress.Status == ADDED {
171+
if c.PublishService != nil && ingResource.Status == ADDED {
172172
select {
173-
case c.statusChan <- status.SyncIngress{Ingress: ingress}:
173+
case c.ingressChan <- ingress.Sync{Ingress: ingResource}:
174174
default:
175-
logger.Errorf("Ingress %s/%s: unable to sync status: sync channel full", ingress.Namespace, ingress.Name)
176-
}
177-
}
178-
if ingress.DefaultBackend != nil {
179-
if reload, err = c.setDefaultService(ingress, []string{c.Cfg.FrontHTTP, c.Cfg.FrontHTTPS}); err != nil {
180-
logger.Errorf("Ingress '%s/%s': default backend: %s", ingress.Namespace, ingress.Name, err)
181-
} else {
182-
c.reload = c.reload || reload
183-
}
184-
}
185-
// Ingress secrets
186-
logger.Tracef("ingress '%s/%s': processing secrets...", ingress.Namespace, ingress.Name)
187-
for _, tls := range ingress.TLS {
188-
if tls.Status == store.DELETED {
189-
continue
190-
}
191-
secret, secErr := c.Store.GetSecret(ingress.Namespace, tls.SecretName)
192-
if secErr != nil {
193-
logger.Warningf("ingress '%s/%s': %s", ingress.Namespace, ingress.Name, secErr)
194-
continue
195-
}
196-
_, err = c.Cfg.Certificates.HandleTLSSecret(secret, certs.FT_CERT)
197-
logger.Error(err)
198-
}
199-
// Ingress annotations
200-
logger.Tracef("ingress '%s/%s': processing annotations...", ingress.Namespace, ingress.Name)
201-
if len(ingress.Rules) == 0 {
202-
logger.Debugf("Ingress %s/%s: no rules defined", ingress.Namespace, ingress.Name)
203-
continue
204-
}
205-
ruleIDs := c.handleIngressAnnotations(*ingress)
206-
// Ingress rules
207-
logger.Tracef("ingress '%s/%s': processing rules...", ingress.Namespace, ingress.Name)
208-
for _, rule := range ingress.Rules {
209-
for _, path := range rule.Paths {
210-
if reload, err = c.handleIngressPath(ingress, rule.Host, path, ruleIDs); err != nil {
211-
logger.Errorf("Ingress '%s/%s': %s", ingress.Namespace, ingress.Name, err)
212-
} else {
213-
c.reload = c.reload || reload
214-
}
175+
logger.Errorf("Ingress %s/%s: unable to sync status: sync channel full", ingResource.Namespace, ingResource.Name)
215176
}
216177
}
178+
c.reload = i.Update(c.Store, &c.Cfg, c.Client) || c.reload
217179
}
218180
}
219181

controller/global.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ import (
2020
"github.com/haproxytech/client-native/v2/models"
2121

2222
"github.com/haproxytech/kubernetes-ingress/controller/annotations"
23+
"github.com/haproxytech/kubernetes-ingress/controller/annotations/common"
2324
"github.com/haproxytech/kubernetes-ingress/controller/configuration"
2425
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/certs"
26+
"github.com/haproxytech/kubernetes-ingress/controller/ingress"
27+
"github.com/haproxytech/kubernetes-ingress/controller/service"
2528
"github.com/haproxytech/kubernetes-ingress/controller/store"
2629
)
2730

@@ -30,7 +33,7 @@ func (c *HAProxyController) handleGlobalConfig() (reload, restart bool) {
3033
reload = c.defaultsCfg() || reload
3134
c.handleDefaultCert()
3235
reload = c.handleDefaultService() || reload
33-
_ = c.handleIngressAnnotations(store.Ingress{})
36+
(&ingress.Ingress{}).HandleAnnotations(c.Store, &c.Cfg)
3437
return reload, restart
3538
}
3639

@@ -139,31 +142,23 @@ func (c *HAProxyController) defaultsCfg() (reload bool) {
139142

140143
// handleDefaultService configures HAProy default backend provided via cli param "default-backend-service"
141144
func (c *HAProxyController) handleDefaultService() (reload bool) {
142-
service, err := annotations.Service("default-backend-service", c.PodNamespace, c.Store, c.Store.ConfigMaps.Main.Annotations)
145+
var svc *service.Service
146+
ns, name, err := common.GetK8sPath("default-backend-service", c.Store.ConfigMaps.Main.Annotations)
143147
if err != nil {
144148
logger.Errorf("default service: %s", err)
145-
return
146149
}
147-
if service == nil {
148-
return
150+
ingressPath := &store.IngressPath{
151+
SvcNamespace: ns,
152+
SvcName: name,
153+
IsDefaultBackend: true,
154+
}
155+
if svc, err = service.New(c.Store, ingressPath, nil, false, c.Store.ConfigMaps.Main.Annotations); err == nil {
156+
reload, err = svc.SetDefaultBackend(c.Store, &c.Cfg, c.Client, []string{c.Cfg.FrontHTTP, c.Cfg.FrontHTTPS})
149157
}
150-
151-
ingress := &store.Ingress{
152-
Namespace: service.Namespace,
153-
Name: "DefaultService",
154-
Annotations: map[string]string{},
155-
DefaultBackend: &store.IngressPath{
156-
SvcName: service.Name,
157-
SvcPortInt: service.Ports[0].Port,
158-
IsDefaultBackend: true,
159-
},
160-
}
161-
reload, err = c.setDefaultService(ingress, []string{c.Cfg.FrontHTTP, c.Cfg.FrontHTTPS})
162158
if err != nil {
163159
logger.Errorf("default service: %s", err)
164-
return
165160
}
166-
return reload
161+
return
167162
}
168163

169164
// handleDefaultCert configures default/fallback HAProxy certificate to use for client HTTPS requests.

controller/handler.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ func (c *HAProxyController) initHandlers() {
4343
handler.ProxyProtocol{},
4444
handler.ErrorFile{},
4545
handler.TCPServices{
46-
SetDefaultService: c.setDefaultService,
47-
CertDir: c.Cfg.Env.FrontendCertDir,
48-
IPv4: !c.OSArgs.DisableIPV4,
49-
AddrIPv4: c.OSArgs.IPV4BindAddr,
50-
IPv6: !c.OSArgs.DisableIPV6,
51-
AddrIPv6: c.OSArgs.IPV6BindAddr,
46+
CertDir: c.Cfg.Env.FrontendCertDir,
47+
IPv4: !c.OSArgs.DisableIPV4,
48+
AddrIPv4: c.OSArgs.IPV4BindAddr,
49+
IPv6: !c.OSArgs.DisableIPV6,
50+
AddrIPv6: c.OSArgs.IPV6BindAddr,
5251
},
5352
handler.PatternFiles{},
5453
handler.Refresh{},

controller/handler/tcp-services.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import (
88
"github.com/haproxytech/client-native/v2/models"
99
config "github.com/haproxytech/kubernetes-ingress/controller/configuration"
1010
"github.com/haproxytech/kubernetes-ingress/controller/haproxy/api"
11+
"github.com/haproxytech/kubernetes-ingress/controller/service"
1112
"github.com/haproxytech/kubernetes-ingress/controller/store"
1213
"github.com/haproxytech/kubernetes-ingress/controller/utils"
1314
)
1415

1516
type TCPServices struct {
16-
SetDefaultService func(ingress *store.Ingress, frontends []string) (reload bool, err error)
17-
IPv4 bool
18-
IPv6 bool
19-
CertDir string
20-
AddrIPv4 string
21-
AddrIPv6 string
17+
IPv4 bool
18+
IPv6 bool
19+
CertDir string
20+
AddrIPv4 string
21+
AddrIPv6 string
2222
}
2323

2424
type tcpSvcParser struct {
@@ -50,7 +50,7 @@ func (t TCPServices) Update(k store.K8s, cfg *config.ControllerCfg, api api.HAPr
5050
}
5151
}
5252
// Update Frontend
53-
reload, err = t.updateTCPFrontend(api, frontend, p)
53+
reload, err = t.updateTCPFrontend(k, cfg, api, frontend, p)
5454
if err != nil {
5555
logger.Errorf("TCP frontend '%s': update failed: %s", frontendName, err)
5656
}
@@ -152,7 +152,7 @@ func (t TCPServices) createTCPFrontend(api api.HAProxyClient, frontendName, bind
152152
return frontend, true, nil
153153
}
154154

155-
func (t TCPServices) updateTCPFrontend(api api.HAProxyClient, frontend models.Frontend, p tcpSvcParser) (reload bool, err error) {
155+
func (t TCPServices) updateTCPFrontend(k store.K8s, cfg *config.ControllerCfg, api api.HAProxyClient, frontend models.Frontend, p tcpSvcParser) (reload bool, err error) {
156156
binds, err := api.FrontendBindsGet(frontend.Name)
157157
if err != nil {
158158
err = fmt.Errorf("failed to get bind lines: %w", err)
@@ -182,18 +182,17 @@ func (t TCPServices) updateTCPFrontend(api api.HAProxyClient, frontend models.Fr
182182
reload = true
183183
return
184184
}
185-
ingress := &store.Ingress{
186-
Namespace: p.service.Namespace,
187-
Annotations: make(map[string]string),
188-
DefaultBackend: &store.IngressPath{
189-
SvcName: p.service.Name,
190-
SvcPortInt: p.port,
191-
},
185+
186+
var svc *service.Service
187+
var r bool
188+
path := &store.IngressPath{
189+
SvcNamespace: p.service.Namespace,
190+
SvcName: p.service.Name,
191+
SvcPortInt: p.port,
192+
IsDefaultBackend: true,
192193
}
193-
r, err := t.SetDefaultService(ingress, []string{frontend.Name})
194-
if err != nil {
195-
return
194+
if svc, err = service.New(k, path, nil, true); err == nil {
195+
r, err = svc.SetDefaultBackend(k, cfg, api, []string{frontend.Name})
196196
}
197-
198197
return reload || r, err
199198
}

0 commit comments

Comments
 (0)