Skip to content

Commit d755ff2

Browse files
ivanmatmatioktalz
authored andcommitted
MINOR: update to management of ingress statuses
1 parent 818adff commit d755ff2

File tree

12 files changed

+136
-81
lines changed

12 files changed

+136
-81
lines changed

deploy/tests/tnr/routeacl/suite_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import (
1818
"os"
1919
"testing"
2020

21+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
2122
c "github.com/haproxytech/kubernetes-ingress/pkg/controller"
23+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
2224
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
2325
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
2426
"github.com/haproxytech/kubernetes-ingress/pkg/k8s"
@@ -35,6 +37,13 @@ type UseBackendSuite struct {
3537
test Test
3638
}
3739

40+
type updateStatusManager struct{}
41+
42+
func (m *updateStatusManager) AddIngress(ingress *ingress.Ingress) {}
43+
func (m *updateStatusManager) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
44+
return
45+
}
46+
3847
func TestUseBackend(t *testing.T) {
3948
suite.Run(t, new(UseBackendSuite))
4049
}
@@ -118,7 +127,7 @@ func (suite *UseBackendSuite) UseBackendFixture() (eventChan chan k8s.SyncDataEv
118127
WithEventChan(eventChan).
119128
WithStore(s).
120129
WithHaproxyEnv(haproxyEnv).
121-
WithUpdatePublishServiceFunc(func(ingresses []*ingress.Ingress, publishServiceAddresses []string) {}).
130+
WithUpdateStatusManager(&updateStatusManager{}).
122131
WithArgs(osArgs).Build()
123132

124133
go controller.Start()

main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ func main() {
113113
WithHaproxyCfgFile(haproxyConf).
114114
WithEventChan(eventChan).
115115
WithStore(s).
116-
WithUpdatePublishServiceFunc(k.UpdatePublishService).
117116
WithClientSet(k.GetClientset()).
118117
WithRestClientSet(k.GetRestClientset()).
119118
WithArgs(osArgs).Build()

pkg/controller/builder.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/rules"
3737
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
3838
"github.com/haproxytech/kubernetes-ingress/pkg/k8s"
39+
"github.com/haproxytech/kubernetes-ingress/pkg/status"
3940
"github.com/haproxytech/kubernetes-ingress/pkg/store"
4041
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
4142
)
@@ -54,6 +55,7 @@ type Builder struct {
5455
haproxyCfgFile []byte
5556
store store.K8s
5657
osArgs utils.OSArgs
58+
updateStatusManager status.UpdateStatusManager
5759
}
5860

5961
var defaultEnv = env.Env{
@@ -123,11 +125,6 @@ func (builder *Builder) WithArgs(osArgs utils.OSArgs) *Builder {
123125
return builder
124126
}
125127

126-
func (builder *Builder) WithUpdatePublishServiceFunc(updatePublishServiceFunc func(ingresses []*ingress.Ingress, publishServiceAddresses []string)) *Builder {
127-
builder.updatePublishServiceFunc = updatePublishServiceFunc
128-
return builder
129-
}
130-
131128
func (builder *Builder) WithClientSet(clientSet *kubernetes.Clientset) *Builder {
132129
builder.clientSet = clientSet
133130
return builder
@@ -143,6 +140,11 @@ func (builder *Builder) WithGatewayManager(gatewayManager gateway.GatewayManager
143140
return builder
144141
}
145142

143+
func (builder *Builder) WithUpdateStatusManager(updateStatusManager status.UpdateStatusManager) *Builder {
144+
builder.updateStatusManager = updateStatusManager
145+
return builder
146+
}
147+
146148
func (builder *Builder) Build() *HAProxyController {
147149
if builder.haproxyCfgFile == nil {
148150
logger.Panic(errors.New("no HAProxy Config file provided"))
@@ -170,6 +172,10 @@ func (builder *Builder) Build() *HAProxyController {
170172
if gatewayManager == nil {
171173
gatewayManager = gateway.New(builder.store, haproxy.HAProxyClient, builder.osArgs, builder.restClientSet)
172174
}
175+
updateStatusManager := builder.updateStatusManager
176+
if updateStatusManager == nil {
177+
updateStatusManager = status.New(builder.clientSet)
178+
}
173179
return &HAProxyController{
174180
osArgs: builder.osArgs,
175181
haproxy: haproxy,
@@ -181,6 +187,7 @@ func (builder *Builder) Build() *HAProxyController {
181187
chShutdown: chShutdown,
182188
updatePublishServiceFunc: builder.updatePublishServiceFunc,
183189
gatewayManager: gatewayManager,
190+
updateStatusManager: updateStatusManager,
184191
}
185192
}
186193

pkg/controller/controller.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
2828
"github.com/haproxytech/kubernetes-ingress/pkg/k8s"
2929
"github.com/haproxytech/kubernetes-ingress/pkg/route"
30+
"github.com/haproxytech/kubernetes-ingress/pkg/status"
3031
"github.com/haproxytech/kubernetes-ingress/pkg/store"
3132
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
3233
)
@@ -50,6 +51,7 @@ type HAProxyController struct {
5051
restart bool
5152
reload bool
5253
ready bool
54+
updateStatusManager status.UpdateStatusManager
5355
}
5456

5557
// Wrapping a Native-Client transaction and commit it.
@@ -111,29 +113,25 @@ func (c *HAProxyController) updateHAProxy() {
111113
logger.Error(route.CustomRoutesReset(c.haproxy))
112114
}
113115

114-
ingresses := []*ingress.Ingress{}
115116
for _, namespace := range c.store.Namespaces {
116117
if !namespace.Relevant {
117118
continue
118119
}
119120
c.store.SecretsProcessed = map[string]struct{}{}
120121
for _, ingResource := range namespace.Ingresses {
121122
i := ingress.New(c.store, ingResource, c.osArgs.IngressClass, c.osArgs.EmptyIngressClass, c.annotations)
122-
if i == nil {
123+
if !i.Supported(c.store, c.annotations) {
123124
logger.Debugf("ingress '%s/%s' ignored: no matching IngressClass", ingResource.Namespace, ingResource.Name)
124-
continue
125+
ingResource.Addresses = []string{""}
126+
} else {
127+
c.reload = i.Update(c.store, c.haproxy, c.annotations) || c.reload
125128
}
126129
if ingResource.Status == store.ADDED || ingResource.ClassUpdated {
127-
ingresses = append(ingresses, i)
130+
c.updateStatusManager.AddIngress(i)
128131
}
129-
c.reload = i.Update(c.store, c.haproxy, c.annotations) || c.reload
130132
}
131133
}
132134

133-
if len(ingresses) > 0 {
134-
go c.updatePublishServiceFunc(ingresses, c.store.PublishServiceAddresses)
135-
}
136-
137135
gatewayReload := c.gatewayManager.ManageGateway()
138136
c.reload = gatewayReload || c.reload
139137

pkg/controller/handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func (c *HAProxyController) initHandlers() {
5151
},
5252
&handler.PatternFiles{},
5353
&handler.BackendCfgSnippet{},
54+
c.updateStatusManager,
5455
}
5556
if c.osArgs.PprofEnabled {
5657
c.updateHandlers = append(c.updateHandlers, handler.Pprof{})

pkg/ingress/ingress.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,16 @@ type Ingress struct {
3939
// If the k8s ingress resource is not assigned to the controller (no matching IngressClass)
4040
// then New will return nil
4141
func New(k store.K8s, resource *store.Ingress, class string, emptyClass bool, a annotations.Annotations) *Ingress {
42-
i := &Ingress{resource: resource, controllerClass: class, allowEmptyClass: emptyClass, annotations: a}
43-
if i.resource == nil || !i.supported(k, a) {
44-
return nil
45-
}
46-
return i
42+
return &Ingress{resource: resource, controllerClass: class, allowEmptyClass: emptyClass, annotations: a}
4743
}
4844

49-
// supported verifies if the IngressClass matches the ControllerClass
45+
// Supported verifies if the IngressClass matches the ControllerClass
5046
// and in such case returns true otherwise false
5147
//
5248
// According to https://github.com/kubernetes/api/blob/master/networking/v1/types.go#L257
5349
// ingress.class annotation should have precedence over the IngressClass mechanism implemented
5450
// in "networking.k8s.io".
55-
func (i Ingress) supported(k8s store.K8s, a annotations.Annotations) (supported bool) {
51+
func (i Ingress) Supported(k8s store.K8s, a annotations.Annotations) (supported bool) {
5652
var igClassAnn, igClassSpec string
5753
igClassAnn = a.String("ingress.class", i.resource.Annotations)
5854
if igClassResource := k8s.IngressClasses[i.resource.Class]; igClassResource != nil {
@@ -256,3 +252,7 @@ func (i *Ingress) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotatio
256252
}
257253
return
258254
}
255+
256+
func (i Ingress) GetAddresses() []string {
257+
return i.resource.Addresses
258+
}

pkg/ingress/status.go

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

88
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
99
"github.com/haproxytech/kubernetes-ingress/pkg/store"
10-
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
1110
networkingv1 "k8s.io/api/networking/v1"
1211
k8serror "k8s.io/apimachinery/pkg/api/errors"
1312
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -20,20 +19,16 @@ func NewStatusIngressUpdater(client *kubernetes.Clientset, k store.K8s, class st
2019
return func(ingresses []*store.Ingress, publishServiceAddresses []string) {
2120
for _, ingress := range ingresses {
2221
if ing := New(k, ingress, class, emptyClass, a); ing != nil {
23-
logger.Error(ing.UpdateStatus(client, publishServiceAddresses))
22+
logger.Error(ing.UpdateStatus(client))
2423
}
2524
}
2625
}
2726
}
2827

29-
func (i *Ingress) UpdateStatus(client *kubernetes.Clientset, addresses []string) (err error) {
28+
func (i *Ingress) UpdateStatus(client *kubernetes.Clientset) (err error) {
3029
var lbi []networkingv1.IngressLoadBalancerIngress
3130

32-
if utils.EqualSliceStringsWithoutOrder(i.resource.Addresses, addresses) {
33-
return
34-
}
35-
36-
for _, addr := range addresses {
31+
for _, addr := range i.resource.Addresses {
3732
if net.ParseIP(addr) == nil {
3833
lbi = append(lbi, networkingv1.IngressLoadBalancerIngress{Hostname: addr})
3934
} else {
@@ -61,12 +56,5 @@ func (i *Ingress) UpdateStatus(client *kubernetes.Clientset, addresses []string)
6156
}
6257
logger.Tracef("Successful update of LoadBalancer status in ingress %s/%s", i.resource.Namespace, i.resource.Name)
6358
// Allow to store the publish service addresses affected to the ingress for future comparison in update test.
64-
i.resource.Addresses = addresses
6559
return nil
6660
}
67-
68-
func UpdatePublishService(ingresses []*Ingress, api *kubernetes.Clientset, publishServiceAddresses []string) {
69-
for _, i := range ingresses {
70-
logger.Error(i.UpdateStatus(api, publishServiceAddresses))
71-
}
72-
}

pkg/k8s/main.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030

3131
crclientset "github.com/haproxytech/kubernetes-ingress/crs/generated/clientset/versioned"
3232
crinformers "github.com/haproxytech/kubernetes-ingress/crs/generated/informers/externalversions"
33-
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
3433
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
3534
crdclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
3635
errGw "k8s.io/apimachinery/pkg/api/errors"
@@ -57,7 +56,6 @@ type K8s interface {
5756
GetRestClientset() client.Client
5857
GetClientset() *k8sclientset.Clientset
5958
MonitorChanges(eventChan chan SyncDataEvent, stop chan struct{}, osArgs utils.OSArgs, gatewayAPIInstalled bool)
60-
UpdatePublishService(ingresses []*ingress.Ingress, publishServiceAddresses []string)
6159
IsGatewayAPIInstalled(gatewayControllerName string) bool
6260
}
6361

@@ -152,13 +150,6 @@ func (k k8s) GetClientset() *k8sclientset.Clientset {
152150
return k.builtInClient
153151
}
154152

155-
func (k k8s) UpdatePublishService(ingresses []*ingress.Ingress, publishServiceAddresses []string) {
156-
clientSet := k.GetClientset()
157-
for _, i := range ingresses {
158-
logger.Error(i.UpdateStatus(clientSet, publishServiceAddresses))
159-
}
160-
}
161-
162153
func (k k8s) MonitorChanges(eventChan chan SyncDataEvent, stop chan struct{}, osArgs utils.OSArgs, gatewayAPIInstalled bool) {
163154
informersSynced := &[]cache.InformerSynced{}
164155
k.runPodInformer(eventChan, stop, informersSynced)

pkg/status/updatestatus.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package status
2+
3+
import (
4+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
5+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
6+
"github.com/haproxytech/kubernetes-ingress/pkg/ingress"
7+
"github.com/haproxytech/kubernetes-ingress/pkg/store"
8+
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
9+
"k8s.io/client-go/kubernetes"
10+
)
11+
12+
type UpdateStatusManager interface {
13+
AddIngress(ingress *ingress.Ingress)
14+
Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error)
15+
}
16+
17+
type UpdateStatusManagerImpl struct {
18+
updateIngresses []*ingress.Ingress
19+
client *kubernetes.Clientset
20+
}
21+
22+
func New(client *kubernetes.Clientset) UpdateStatusManager {
23+
return &UpdateStatusManagerImpl{
24+
client: client,
25+
}
26+
}
27+
28+
func (m *UpdateStatusManagerImpl) AddIngress(ingress *ingress.Ingress) {
29+
m.updateIngresses = append(m.updateIngresses, ingress)
30+
}
31+
32+
func (m *UpdateStatusManagerImpl) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
33+
errs := utils.Errors{}
34+
defer func() {
35+
err = errs.Result()
36+
}()
37+
38+
ingresses := m.updateIngresses
39+
40+
if k.UpdateAllIngresses {
41+
ingresses = nil
42+
for _, namespace := range k.Namespaces {
43+
if !namespace.Relevant {
44+
continue
45+
}
46+
47+
for _, ingResource := range namespace.Ingresses {
48+
previousAddresses := ingResource.Addresses
49+
i := ingress.New(k, ingResource, "haproxy", false, a)
50+
supported := i.Supported(k, a)
51+
if supported {
52+
ingResource.Addresses = k.PublishServiceAddresses
53+
}
54+
// If ingress is not managed by us, three cases can occur:
55+
// - it has no adddresses.
56+
// - it has addresses and they are different (maybe managed by someone else).
57+
// - it has addresses and they are ours. We managed it but not now anymore.
58+
// You can see we can't easily manage the case when while the IC is stopped, the ingress switches to unmanaged state (ingress class change) and the publish service addresses have also changed.
59+
if !supported && !utils.EqualSliceStringsWithoutOrder(previousAddresses, ingResource.Addresses) {
60+
continue
61+
}
62+
ingresses = append(ingresses, i)
63+
}
64+
}
65+
}
66+
67+
if len(ingresses) > 0 {
68+
go func() {
69+
for _, ing := range ingresses {
70+
if ing != nil {
71+
errs.Add(ing.UpdateStatus(m.client))
72+
}
73+
}
74+
}()
75+
}
76+
77+
k.UpdateAllIngresses = false
78+
m.updateIngresses = nil
79+
return
80+
}

pkg/store/convert.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type ingressNetworkingV1Strategy struct {
6060
}
6161

6262
func (n ingressNetworkingV1Strategy) ConvertIngress() *Ingress {
63-
return &Ingress{
63+
ing := &Ingress{
6464
IngressCore: IngressCore{
6565
APIVersion: NETWORKINGV1,
6666
Namespace: n.ig.GetNamespace(),
@@ -138,6 +138,16 @@ func (n ingressNetworkingV1Strategy) ConvertIngress() *Ingress {
138138
}(n.ig.Spec.TLS),
139139
},
140140
}
141+
addresses := []string{}
142+
for _, ingLoadBalancer := range n.ig.Status.LoadBalancer.Ingress {
143+
address := ingLoadBalancer.IP
144+
if ingLoadBalancer.Hostname != "" {
145+
address = ingLoadBalancer.Hostname
146+
}
147+
addresses = append(addresses, address)
148+
}
149+
ing.Addresses = addresses
150+
return ing
141151
}
142152

143153
func (n ingressNetworkingV1Strategy) ConvertClass() *IngressClass {

0 commit comments

Comments
 (0)