-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
resources.go
227 lines (193 loc) · 8.09 KB
/
resources.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package envoy
import (
"errors"
"fmt"
"net"
"sort"
"sync"
envoyAPI "github.com/cilium/proxy/go/cilium/api"
"github.com/sirupsen/logrus"
cmtypes "github.com/cilium/cilium/pkg/clustermesh/types"
"github.com/cilium/cilium/pkg/envoy/xds"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/logging/logfields"
)
const (
// ListenerTypeURL is the type URL of Listener resources.
ListenerTypeURL = "type.googleapis.com/envoy.config.listener.v3.Listener"
// RouteTypeURL is the type URL of HTTP Route resources.
RouteTypeURL = "type.googleapis.com/envoy.config.route.v3.RouteConfiguration"
// ClusterTypeURL is the type URL of Cluster resources.
ClusterTypeURL = "type.googleapis.com/envoy.config.cluster.v3.Cluster"
// HttpConnectionManagerTypeURL is the type URL of HttpConnectionManager filter.
HttpConnectionManagerTypeURL = "type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager"
// TCPProxyTypeURL is the type URL of TCPProxy filter.
TCPProxyTypeURL = "type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy"
// EndpointTypeURL is the type URL of Endpoint resources.
EndpointTypeURL = "type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment"
// SecretTypeURL is the type URL of Endpoint resources.
SecretTypeURL = "type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.Secret"
// NetworkPolicyTypeURL is the type URL of NetworkPolicy resources.
NetworkPolicyTypeURL = "type.googleapis.com/cilium.NetworkPolicy"
// NetworkPolicyHostsTypeURL is the type URL of NetworkPolicyHosts resources.
NetworkPolicyHostsTypeURL = "type.googleapis.com/cilium.NetworkPolicyHosts"
// HealthCheckSinkPipeTypeURL is the type URL of NetworkPolicyHosts resources.
HealthCheckSinkPipeTypeURL = "type.googleapis.com/cilium.health_check.event_sink.pipe"
// DownstreamTlsContextURL is the type URL of DownstreamTlsContext
DownstreamTlsContextURL = "type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext"
)
// NPHDSCache is a cache of resources in the Network Policy Hosts Discovery
// Service.
//
// NetworkPolicyHostsCache is the global cache of resources of type
// NetworkPolicyHosts. Resources in this cache must have the
// NetworkPolicyHostsTypeURL type URL.
type NPHDSCache struct {
*xds.Cache
ipcache IPCacheEventSource
}
type IPCacheEventSource interface {
AddListener(ipcache.IPIdentityMappingListener)
}
func newNPHDSCache(ipcache IPCacheEventSource) NPHDSCache {
return NPHDSCache{Cache: xds.NewCache(), ipcache: ipcache}
}
var (
observerOnce = sync.Once{}
)
// HandleResourceVersionAck is required to implement ResourceVersionAckObserver.
// We use this to start the IP Cache listener on the first ACK so that we only
// start the IP Cache listener if there is an Envoy node that uses NPHDS (e.g.,
// Istio node, or host proxy running on kernel w/o LPM bpf map support).
func (cache *NPHDSCache) HandleResourceVersionAck(ackVersion uint64, nackVersion uint64, nodeIP string, resourceNames []string, typeURL string, detail string) {
// Start caching for IP/ID mappings on the first indication someone wants them
observerOnce.Do(func() {
cache.ipcache.AddListener(cache)
})
}
// OnIPIdentityCacheChange pushes modifications to the IP<->Identity mapping
// into the Network Policy Host Discovery Service (NPHDS).
//
// Note that the caller is responsible for passing 'oldID' when 'cidrCluster' has been
// associated with a different ID before, as this function does not search for conflicting
// IP/ID mappings.
func (cache *NPHDSCache) OnIPIdentityCacheChange(modType ipcache.CacheModification, cidrCluster cmtypes.PrefixCluster,
oldHostIP, newHostIP net.IP, oldID *ipcache.Identity, newID ipcache.Identity,
encryptKey uint8, k8sMeta *ipcache.K8sMetadata) {
cidr := cidrCluster.AsIPNet()
cidrStr := cidr.String()
resourceName := newID.ID.StringID()
scopedLog := log.WithFields(logrus.Fields{
logfields.IPAddr: cidrStr,
logfields.Identity: resourceName,
logfields.Modification: modType,
})
// Look up the current resources for the specified Identity.
msg, err := cache.Lookup(NetworkPolicyHostsTypeURL, resourceName)
if err != nil {
scopedLog.WithError(err).Warning("Can't lookup NPHDS cache")
return
}
var npHost *envoyAPI.NetworkPolicyHosts
if msg != nil {
npHost = msg.(*envoyAPI.NetworkPolicyHosts)
}
switch modType {
case ipcache.Upsert:
// Delete ID to IP mapping before adding the new mapping,
// but only if the old ID is different.
if oldID != nil && oldID.ID != newID.ID {
// Recursive call to delete the 'cidr' from the 'oldID'
cache.OnIPIdentityCacheChange(ipcache.Delete, cidrCluster, nil, nil, nil, *oldID, encryptKey, k8sMeta)
}
err := cache.handleIPUpsert(npHost, resourceName, cidrStr, newID.ID)
if err != nil {
scopedLog.WithError(err).Warning("NPHSD upsert failed")
}
case ipcache.Delete:
err := cache.handleIPDelete(npHost, resourceName, cidrStr)
if err != nil {
scopedLog.WithError(err).Warning("NPHDS delete failed")
}
}
}
// handleIPUpsert adds elements to the NPHDS cache with the specified peer IP->ID mapping.
func (cache *NPHDSCache) handleIPUpsert(npHost *envoyAPI.NetworkPolicyHosts, identityStr, cidrStr string, newID identity.NumericIdentity) error {
var hostAddresses []string
if npHost == nil {
hostAddresses = make([]string, 0, 1)
hostAddresses = append(hostAddresses, cidrStr)
} else {
// Resource already exists, create a copy of it and insert
// the new IP address into its HostAddresses list, if not already there.
for _, addr := range npHost.HostAddresses {
if addr == cidrStr {
// IP already exists, nothing to add
return nil
}
}
hostAddresses = make([]string, 0, len(npHost.HostAddresses)+1)
hostAddresses = append(hostAddresses, npHost.HostAddresses...)
hostAddresses = append(hostAddresses, cidrStr)
sort.Strings(hostAddresses)
}
newNpHost := envoyAPI.NetworkPolicyHosts{
Policy: uint64(newID),
HostAddresses: hostAddresses,
}
if err := newNpHost.Validate(); err != nil {
return fmt.Errorf("Could not validate NPHDS resource update on upsert: %s (%w)", newNpHost.String(), err)
}
_, updated, _ := cache.Upsert(NetworkPolicyHostsTypeURL, identityStr, &newNpHost)
if !updated {
return fmt.Errorf("NPHDS cache not updated when expected adding: %s", newNpHost.String())
}
return nil
}
// handleIPUpsert deletes elements from the NPHDS cache with the specified peer IP->ID mapping.
func (cache *NPHDSCache) handleIPDelete(npHost *envoyAPI.NetworkPolicyHosts, identityStr, cidrStr string) error {
if npHost == nil {
// Doesn't exist; already deleted.
return nil
}
targetIndex := -1
for i, endpointIP := range npHost.HostAddresses {
if endpointIP == cidrStr {
targetIndex = i
break
}
}
if targetIndex < 0 {
return errors.New("Can't find IP in NPHDS cache")
}
// If removing this host would result in empty list, delete it.
// Otherwise, update to a list that doesn't contain the target IP
if len(npHost.HostAddresses) <= 1 {
cache.Delete(NetworkPolicyHostsTypeURL, identityStr)
} else {
// If the resource is to be updated, create a copy of it before
// removing the IP address from its HostAddresses list.
hostAddresses := make([]string, 0, len(npHost.HostAddresses)-1)
if len(npHost.HostAddresses) == targetIndex {
hostAddresses = append(hostAddresses, npHost.HostAddresses[0:targetIndex]...)
} else {
hostAddresses = append(hostAddresses, npHost.HostAddresses[0:targetIndex]...)
hostAddresses = append(hostAddresses, npHost.HostAddresses[targetIndex+1:]...)
}
newNpHost := envoyAPI.NetworkPolicyHosts{
Policy: uint64(npHost.Policy),
HostAddresses: hostAddresses,
}
if err := newNpHost.Validate(); err != nil {
return fmt.Errorf("Could not validate NPHDS resource update on delete: %s (%w)", newNpHost.String(), err)
}
_, updated, _ := cache.Upsert(NetworkPolicyHostsTypeURL, identityStr, &newNpHost)
if !updated {
return fmt.Errorf("NPHDS cache not updated when expected deleting: %s", newNpHost.String())
}
}
return nil
}