/
client.go
246 lines (227 loc) · 9.87 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package state
import (
"context"
"fmt"
"github.com/rs/zerolog/log"
v1 "k8s.io/api/networking/v1"
v1Net "k8s.io/api/networking/v1"
v1Meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/informers"
v1ClientCore "k8s.io/client-go/informers/core/v1"
v1ClientNet "k8s.io/client-go/informers/networking/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/retry"
"net"
"sync"
"sync/atomic"
)
// kubernetesClients provides informers and ingress kubernetes clients for ingress updates.
type kubernetesClients struct {
IngressInformer v1ClientNet.IngressInformer
ServiceInformer v1ClientCore.ServiceInformer
SecretInformer v1ClientCore.SecretInformer
factories []informers.SharedInformerFactory
addUpdDelChan chan struct{}
addUpdDelCallbackMu sync.Mutex
addUpdDelCallbackQueued atomic.Bool
client kubernetes.Interface
}
type ingressStatusUpdate struct {
Ingress *v1Net.Ingress
Status *v1Net.IngressLoadBalancerIngress
}
// AddUpdDelChan returns a signal channel that is triggered on add, update or delete calls from Kubernetes.
// The channel is triggered after syncing of the internal informers has been restored.
// Multiple calls during the resync period are automatically debounced to one callback call.
func (c *kubernetesClients) AddUpdDelChan() <-chan struct{} {
return c.addUpdDelChan
}
// newKubernetesClients creates a new kubernetesClients struct. The ctx can be used to cancel the listening to updates from the Kubernetes API.
func newKubernetesClients(ctx context.Context, client kubernetes.Interface) (*kubernetesClients, error) {
factoryGeneral := informers.NewSharedInformerFactory(client, 0)
factorySecrets := informers.NewSharedInformerFactoryWithOptions(client, 0, informers.WithTweakListOptions(
func(list *v1Meta.ListOptions) {
list.FieldSelector = fields.OneTermEqualSelector("type", "kubernetes.io/tls").String()
}))
clients := &kubernetesClients{
factories: []informers.SharedInformerFactory{factoryGeneral, factorySecrets},
IngressInformer: factoryGeneral.Networking().V1().Ingresses(),
ServiceInformer: factoryGeneral.Core().V1().Services(),
SecretInformer: factorySecrets.Core().V1().Secrets(),
addUpdDelChan: make(chan struct{}),
client: client,
}
return clients, clients.startInformers(ctx)
}
// updateIngressStatus updates the ingress status and syncs the result with Kubernetes (if changes have occurred)
func (c *kubernetesClients) updateIngressStatus(ctx context.Context, ingress *v1.Ingress, updatedStatus *v1Net.IngressLoadBalancerIngress) error {
currentStatus, _, ok := findIngressStatus(ingress.Status.LoadBalancer.Ingress, updatedStatus.IP)
// we set the message for both ports equal so no need to differentiate here
if ok && statusEqual(currentStatus, updatedStatus) {
return nil
}
return c.syncIngressStatus(ctx, ingress, func(ingressStatus []v1.IngressLoadBalancerIngress) ([]v1.IngressLoadBalancerIngress, bool) {
log.Debug().Msgf("Setting/Updating ingress status for %s in namespace %s", ingress.Name, ingress.Namespace)
if ok && statusContained(ingressStatus, updatedStatus) {
return ingressStatus, false
}
return setIngressStatus(ingressStatus, updatedStatus), true
})
}
// cleanIngressStatus removes all status fields for the given hostIp
func (c *kubernetesClients) cleanIngressStatus(ctx context.Context, ingress *v1.Ingress, hostIp net.IP) error {
_, _, ok := findIngressStatus(ingress.Status.LoadBalancer.Ingress, hostIp.String())
if !ok {
return nil
}
return c.syncIngressStatus(ctx, ingress, func(ingressStatus []v1.IngressLoadBalancerIngress) ([]v1.IngressLoadBalancerIngress, bool) {
log.Debug().Msgf("Cleaning ingress status for %s in namespace %s", ingress.Name, ingress.Namespace)
_, i, ok := findIngressStatus(ingressStatus, hostIp.String())
if !ok {
return ingressStatus, false
}
return append(ingress.Status.LoadBalancer.Ingress[:i], ingress.Status.LoadBalancer.Ingress[i+1:]...), true
})
}
// ingressPatchStatusFunc patches an ingress status and returns a boolean whether this needs to be synced to the kubernetes api.
// Usually false for this value makes only sense when the ingress state is already as desired.
type ingressPatchStatusFunc func([]v1.IngressLoadBalancerIngress) (patchedStatus []v1.IngressLoadBalancerIngress, doSync bool)
// syncIngressStatus syncs the ingress status to the kubernetes api.
func (c *kubernetesClients) syncIngressStatus(ctx context.Context, ingress *v1.Ingress, patchStatus ingressPatchStatusFunc) error {
client := c.client.NetworkingV1().Ingresses(ingress.Namespace)
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
current, err := client.Get(ctx, ingress.Name, v1Meta.GetOptions{})
if err != nil {
log.Debug().Err(err).Msgf("ingress update error when fetching current ingress state")
return fmt.Errorf("ingress update error when fetching current ingress state: %v", err)
}
var needSync bool
current.Status.LoadBalancer.Ingress, needSync = patchStatus(current.Status.LoadBalancer.Ingress)
if !needSync {
return nil
}
_, err = client.UpdateStatus(ctx, current, v1Meta.UpdateOptions{})
if err != nil {
log.Debug().Err(err).Msgf("ingress update error when saving updated ingress")
return fmt.Errorf("ingress update error when saving updated ingress: %v", err)
}
return nil
})
}
// either replaces the matching ingress status or (if none matches) appends the status
func setIngressStatus(status []v1.IngressLoadBalancerIngress, target *v1.IngressLoadBalancerIngress) []v1.IngressLoadBalancerIngress {
for i, el := range status {
if el.IP == target.IP {
status[i] = *target
return status
}
}
return append(status, *target)
}
// findIngressStatus returns the ingress status with the matching ip address
func findIngressStatus(status []v1.IngressLoadBalancerIngress, hostIP string) (result *v1.IngressLoadBalancerIngress, index int, ok bool) {
for i, el := range status {
if el.IP == hostIP {
return &el, i, true
}
}
return nil, -1, false
}
// statusContained returns whether the list contains a status element. The ports array is checked on a per element basis (order-sensitive)
func statusContained(list []v1.IngressLoadBalancerIngress, el *v1.IngressLoadBalancerIngress) bool {
listEl, _, ok := findIngressStatus(list, el.IP)
return ok && statusEqual(listEl, el)
}
// statusEqual returns whether the two ingress status are equal. The ports array is checked on a per element basis (order-sensitive)
func statusEqual(el1 *v1.IngressLoadBalancerIngress, el2 *v1.IngressLoadBalancerIngress) bool {
if el1.Hostname != el2.Hostname || el1.IP != el2.IP || len(el1.Ports) != len(el2.Ports) {
return false
}
// we set the ports ourselves so order is fixed
for i, port1 := range el1.Ports {
port2 := el2.Ports[i]
if port1.Port != port2.Port || port1.Protocol != port2.Protocol ||
(port1.Error != nil && port2.Error != nil && *port1.Error != *port2.Error) ||
((port1.Error == nil && port2.Error != nil) || (port1.Error != nil && port2.Error == nil)) {
return false
}
return false
}
return true
}
// setupInformers setups and start all internal informers and sets the refetchState function as handler for AddFunc, UpdateFunc, DeleteFunc.
func (c *kubernetesClients) startInformers(ctx context.Context) error {
if err := c.setupInformer(ctx, c.IngressInformer.Informer(), true); err != nil {
return fmt.Errorf("failed to setup ingress informer: %v", err)
}
if err := c.setupInformer(ctx, c.ServiceInformer.Informer(), true); err != nil {
return fmt.Errorf("failed to setup services informer: %v", err)
}
if err := c.setupInformer(ctx, c.SecretInformer.Informer(), false); err != nil {
return fmt.Errorf("failed to setup secret informer: %v", err)
}
for _, factory := range c.factories {
factory.Start(ctx.Done())
}
return nil
}
// setupInformer setups the given informer and sets the refetchState function as handler for AddFunc, UpdateFunc, DeleteFunc.
func (c *kubernetesClients) setupInformer(ctx context.Context, informer cache.SharedIndexInformer, logDebug bool) error {
wrappedHandler := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if logDebug {
log.Debug().Msgf("Received k8s add update %v", obj)
}
go c.signalUpdateAfterSync(ctx)
},
UpdateFunc: func(oldObj, newObj interface{}) {
if logDebug {
log.Debug().Msgf("Received k8s update, new: %v, old: %v", oldObj, newObj)
}
go c.signalUpdateAfterSync(ctx)
},
DeleteFunc: func(obj interface{}) {
if logDebug {
log.Debug().Msgf("Received k8s delete update %v", obj)
}
go c.signalUpdateAfterSync(ctx)
},
}
_, err := informer.AddEventHandler(wrappedHandler)
return err
}
// signalUpdateAfterSync calls the callback after syncing of the internal informers has been restored.
// Multiple calls during the resync period are automatically debounced to one callback call.
func (c *kubernetesClients) signalUpdateAfterSync(ctx context.Context) {
log.Debug().Msg("k8s update called")
if !c.addUpdDelCallbackQueued.CompareAndSwap(false, true) {
log.Debug().Msg("k8s update already queued")
return
}
log.Debug().Msg("k8s update waits for k8s informers to sync")
c.waitForSync(ctx)
c.addUpdDelCallbackMu.Lock()
defer c.addUpdDelCallbackMu.Unlock()
c.addUpdDelCallbackQueued.Store(false)
log.Debug().Msg("signalling k8s update")
c.addUpdDelChan <- struct{}{}
}
// waitFroSync waits till all factories sync. No specific order is enforced.
func (c *kubernetesClients) waitForSync(ctx context.Context) {
if len(c.factories) == 0 {
return
}
var wg sync.WaitGroup
wg.Add(len(c.factories))
for i, el := range c.factories {
go func(index int, factory informers.SharedInformerFactory) {
log.Debug().Msgf("Waiting for informer from factory %d", index)
factory.WaitForCacheSync(ctx.Done())
log.Debug().Msgf("Waited for informer from factory %d", index)
wg.Done()
}(i, el)
}
wg.Wait()
}