-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
agent_test.go
237 lines (200 loc) · 8.41 KB
/
agent_test.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package agent
import (
"context"
"net"
"testing"
. "github.com/cilium/checkmate"
"golang.org/x/sys/unix"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"github.com/cilium/cilium/pkg/checker"
"github.com/cilium/cilium/pkg/cidr"
iputil "github.com/cilium/cilium/pkg/ip"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/source"
)
type AgentSuite struct{}
var _ = Suite(&AgentSuite{})
func Test(t *testing.T) {
TestingT(t)
}
type fakeWgClient struct{}
func (f *fakeWgClient) Close() error {
return nil
}
func (f *fakeWgClient) Devices() ([]*wgtypes.Device, error) {
return nil, nil
}
func (f *fakeWgClient) Device(name string) (*wgtypes.Device, error) {
return nil, unix.ENODEV
}
func (f *fakeWgClient) ConfigureDevice(name string, cfg wgtypes.Config) error {
return nil
}
var (
k8s1NodeName = "k8s1"
k8s1PubKey = "YKQF5gwcQrsZWzxGd4ive+IeCOXjPN4aS9jiMSpAlCg="
k8s1NodeIPv4 = net.ParseIP("192.168.60.11")
k8s1NodeIPv6 = net.ParseIP("fd01::b")
k8s2NodeName = "k8s2"
k8s2PubKey = "lH+Xsa0JClu1syeBVbXN0LZNQVB6rTPBzbzWOHwQLW4="
k8s2NodeIPv4 = net.ParseIP("192.168.60.12")
k8s2NodeIPv6 = net.ParseIP("fd01::c")
pod1IPv4Str = "10.0.0.1"
pod1IPv4 = iputil.IPToPrefix(net.ParseIP(pod1IPv4Str))
pod1IPv6Str = "fd00::1"
pod1IPv6 = iputil.IPToPrefix(net.ParseIP(pod1IPv6Str))
pod2IPv4Str = "10.0.0.2"
pod2IPv4 = iputil.IPToPrefix(net.ParseIP(pod2IPv4Str))
pod2IPv6Str = "fd00::2"
pod2IPv6 = iputil.IPToPrefix(net.ParseIP(pod2IPv6Str))
pod3IPv4Str = "10.0.0.3"
pod3IPv4 = iputil.IPToPrefix(net.ParseIP(pod3IPv4Str))
pod3IPv6Str = "fd00::3"
pod3IPv6 = iputil.IPToPrefix(net.ParseIP(pod3IPv6Str))
)
func containsIP(allowedIPs []net.IPNet, ipnet *net.IPNet) bool {
for _, allowedIP := range allowedIPs {
if cidr.Equal(&allowedIP, ipnet) {
return true
}
}
return false
}
func newTestAgent(ctx context.Context) (*Agent, *ipcache.IPCache) {
ipCache := ipcache.NewIPCache(&ipcache.Configuration{
Context: ctx,
})
wgAgent := &Agent{
wgClient: &fakeWgClient{},
ipCache: ipCache,
listenPort: listenPort,
peerByNodeName: map[string]*peerConfig{},
nodeNameByNodeIP: map[string]string{},
nodeNameByPubKey: map[wgtypes.Key]string{},
}
ipCache.AddListener(wgAgent)
return wgAgent, ipCache
}
func (a *AgentSuite) TestAgent_PeerConfig(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wgAgent, ipCache := newTestAgent(ctx)
defer ipCache.Shutdown()
// Test that IPCache updates before UpdatePeer are handled correctly
ipCache.Upsert(pod1IPv4Str, k8s1NodeIPv4, 0, nil, ipcache.Identity{ID: 1, Source: source.Kubernetes})
ipCache.Upsert(pod1IPv6Str, k8s1NodeIPv6, 0, nil, ipcache.Identity{ID: 1, Source: source.Kubernetes})
ipCache.Upsert(pod2IPv4Str, k8s1NodeIPv4, 0, nil, ipcache.Identity{ID: 2, Source: source.Kubernetes})
ipCache.Upsert(pod2IPv6Str, k8s1NodeIPv6, 0, nil, ipcache.Identity{ID: 2, Source: source.Kubernetes})
err := wgAgent.UpdatePeer(k8s1NodeName, k8s1PubKey, k8s1NodeIPv4, k8s1NodeIPv6)
c.Assert(err, IsNil)
k8s1 := wgAgent.peerByNodeName[k8s1NodeName]
c.Assert(k8s1, NotNil)
c.Assert(k8s1.nodeIPv4, checker.DeepEquals, k8s1NodeIPv4)
c.Assert(k8s1.nodeIPv6, checker.DeepEquals, k8s1NodeIPv6)
c.Assert(k8s1.pubKey.String(), Equals, k8s1PubKey)
c.Assert(k8s1.allowedIPs, HasLen, 6)
c.Assert(containsIP(k8s1.allowedIPs, iputil.IPToPrefix(k8s1NodeIPv4)), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, iputil.IPToPrefix(k8s1NodeIPv6)), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, pod1IPv4), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, pod1IPv6), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, pod2IPv4), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, pod2IPv6), Equals, true)
// Tests that IPCache updates are blocked by a concurrent UpdatePeer.
// We test this by issuing an UpdatePeer request while holding
// the agent lock (meaning the UpdatePeer call will first take the IPCache
// lock and then wait for the agent lock to become available),
// then issuing an IPCache update (which will be blocked because
// UpdatePeer already holds the IPCache lock), and then releasing the
// agent lock to allow both operations to proceed.
wgAgent.Lock()
agentUpdated := make(chan struct{})
agentUpdatePending := make(chan struct{})
go func() {
close(agentUpdatePending)
err = wgAgent.UpdatePeer(k8s2NodeName, k8s2PubKey, k8s2NodeIPv4, k8s2NodeIPv6)
c.Assert(err, IsNil)
close(agentUpdated)
}()
// wait for the above goroutine to be scheduled
<-agentUpdatePending
ipCacheUpdated := make(chan struct{})
ipCacheUpdatePending := make(chan struct{})
go func() {
close(ipCacheUpdatePending)
// Insert pod3
ipCache.Upsert(pod3IPv4Str, k8s2NodeIPv4, 0, nil, ipcache.Identity{ID: 3, Source: source.Kubernetes})
ipCache.Upsert(pod3IPv6Str, k8s2NodeIPv6, 0, nil, ipcache.Identity{ID: 3, Source: source.Kubernetes})
// Update pod2
ipCache.Upsert(pod2IPv4Str, k8s1NodeIPv4, 0, nil, ipcache.Identity{ID: 22, Source: source.Kubernetes})
ipCache.Upsert(pod2IPv6Str, k8s1NodeIPv6, 0, nil, ipcache.Identity{ID: 22, Source: source.Kubernetes})
// Delete pod1
ipCache.Delete(pod1IPv4Str, source.Kubernetes)
ipCache.Delete(pod1IPv6Str, source.Kubernetes)
close(ipCacheUpdated)
}()
// wait for the above goroutine to be scheduled
<-ipCacheUpdatePending
// At this point we know both goroutines have been scheduled. We assume
// that they are now both blocked by checking they haven't closed the
// channel yet. Thus once release the lock we expect them to make progress
select {
case <-agentUpdated:
c.Fatal("agent update not blocked by agent lock")
case <-ipCacheUpdated:
c.Fatal("ipcache update not blocked by agent lock")
default:
}
wgAgent.Unlock()
// Ensure that both operations succeeded without a deadlock
<-agentUpdated
<-ipCacheUpdated
k8s1 = wgAgent.peerByNodeName[k8s1NodeName]
c.Assert(k8s1.nodeIPv4, checker.DeepEquals, k8s1NodeIPv4)
c.Assert(k8s1.nodeIPv6, checker.DeepEquals, k8s1NodeIPv6)
c.Assert(k8s1.pubKey.String(), Equals, k8s1PubKey)
c.Assert(k8s1.allowedIPs, HasLen, 4)
c.Assert(containsIP(k8s1.allowedIPs, iputil.IPToPrefix(k8s1NodeIPv4)), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, iputil.IPToPrefix(k8s1NodeIPv6)), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, pod2IPv4), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, pod2IPv6), Equals, true)
k8s2 := wgAgent.peerByNodeName[k8s2NodeName]
c.Assert(k8s2.nodeIPv4, checker.DeepEquals, k8s2NodeIPv4)
c.Assert(k8s2.nodeIPv6, checker.DeepEquals, k8s2NodeIPv6)
c.Assert(k8s2.pubKey.String(), Equals, k8s2PubKey)
c.Assert(k8s2.allowedIPs, HasLen, 4)
c.Assert(containsIP(k8s2.allowedIPs, iputil.IPToPrefix(k8s2NodeIPv4)), Equals, true)
c.Assert(containsIP(k8s2.allowedIPs, iputil.IPToPrefix(k8s2NodeIPv6)), Equals, true)
c.Assert(containsIP(k8s2.allowedIPs, pod3IPv4), Equals, true)
c.Assert(containsIP(k8s2.allowedIPs, pod3IPv6), Equals, true)
// Tests that duplicate public keys are rejected (k8s2 imitates k8s1)
err = wgAgent.UpdatePeer(k8s2NodeName, k8s1PubKey, k8s2NodeIPv4, k8s2NodeIPv6)
c.Assert(err, ErrorMatches, "detected duplicate public key.*")
// Node Deletion
wgAgent.DeletePeer(k8s1NodeName)
wgAgent.DeletePeer(k8s2NodeName)
c.Assert(wgAgent.peerByNodeName, HasLen, 0)
c.Assert(wgAgent.nodeNameByNodeIP, HasLen, 0)
c.Assert(wgAgent.nodeNameByPubKey, HasLen, 0)
}
func (a *AgentSuite) TestAgent_PeerConfig_WithEncryptNode(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wgAgent, ipCache := newTestAgent(ctx)
defer ipCache.Shutdown()
ipCache.Upsert(pod1IPv4Str, k8s1NodeIPv4, 0, nil, ipcache.Identity{ID: 1, Source: source.Kubernetes})
ipCache.Upsert(pod2IPv4Str, k8s1NodeIPv4, 0, nil, ipcache.Identity{ID: 2, Source: source.Kubernetes})
err := wgAgent.UpdatePeer(k8s1NodeName, k8s1PubKey, k8s1NodeIPv4, k8s1NodeIPv6)
c.Assert(err, IsNil)
k8s1 := wgAgent.peerByNodeName[k8s1NodeName]
c.Assert(k8s1, NotNil)
c.Assert(k8s1.nodeIPv4, checker.DeepEquals, k8s1NodeIPv4)
c.Assert(k8s1.nodeIPv6, checker.DeepEquals, k8s1NodeIPv6)
c.Assert(k8s1.pubKey.String(), Equals, k8s1PubKey)
c.Assert(k8s1.allowedIPs, HasLen, 4)
c.Assert(containsIP(k8s1.allowedIPs, pod1IPv4), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, pod2IPv4), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, iputil.IPToPrefix(k8s1NodeIPv4)), Equals, true)
c.Assert(containsIP(k8s1.allowedIPs, iputil.IPToPrefix(k8s1NodeIPv6)), Equals, true)
}