|
| 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 | +} |
0 commit comments