/
store.go
76 lines (65 loc) · 1.78 KB
/
store.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
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package kubernetes
import (
"sync"
corev1 "k8s.io/api/core/v1"
)
type nodeDetails struct {
name string
address string
}
// kubernetesProviderStore holds cached information for the kubernetes provider.
type kubernetesProviderStore struct {
// nodes holds information required for updating Gateway status with the Node
// addresses, in case the Gateway is exposed on every Node of the cluster, using
// Service of type NodePort.
nodes map[string]nodeDetails
mu sync.Mutex
}
func newProviderStore() *kubernetesProviderStore {
return &kubernetesProviderStore{
nodes: make(map[string]nodeDetails),
}
}
func (p *kubernetesProviderStore) addNode(n *corev1.Node) {
details := nodeDetails{name: n.Name}
var internalIP, externalIP string
for _, addr := range n.Status.Addresses {
if addr.Type == corev1.NodeExternalIP {
externalIP = addr.Address
}
if addr.Type == corev1.NodeInternalIP {
internalIP = addr.Address
}
}
// In certain scenarios (like in local KinD clusters), the Node
// externalIP is not provided, in that case we default back
// to the internalIP of the Node.
if externalIP != "" {
details.address = externalIP
} else if internalIP != "" {
details.address = internalIP
}
p.mu.Lock()
defer p.mu.Unlock()
p.nodes[n.Name] = details
}
func (p *kubernetesProviderStore) removeNode(n *corev1.Node) {
p.mu.Lock()
defer p.mu.Unlock()
delete(p.nodes, n.Name)
}
func (p *kubernetesProviderStore) listNodeAddresses() []string {
addrs := []string{}
p.mu.Lock()
defer p.mu.Unlock()
for _, n := range p.nodes {
if n.address != "" {
addrs = append(addrs, n.address)
}
}
return addrs
}