@@ -23,13 +23,13 @@ import (
23
23
"crypto/sha256"
24
24
"fmt"
25
25
"net"
26
- "os"
27
26
"slices"
28
27
"time"
29
28
30
29
"github.com/kelseyhightower/envconfig"
31
30
"github.com/pkg/errors"
32
31
"github.com/submariner-io/admiral/pkg/log"
32
+ "github.com/submariner-io/admiral/pkg/resource"
33
33
v1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
34
34
"github.com/submariner-io/submariner/pkg/cable"
35
35
"github.com/submariner-io/submariner/pkg/endpoint"
@@ -50,18 +50,20 @@ const (
50
50
// PublicKey is name (key) of publicKey entry in back-end map.
51
51
PublicKey = "publicKey"
52
52
53
- // KeepAliveInterval to use for wg peers.
54
- KeepAliveInterval = 10 * time .Second
55
-
56
- // handshakeTimeout is maximal time from handshake a connections is still considered connected.
57
- handshakeTimeout = 2 * time .Minute + 10 * time .Second
58
-
59
53
cableDriverName = "wireguard"
60
54
receiveBytes = "ReceiveBytes" // for peer connection status
61
55
transmitBytes = "TransmitBytes" // for peer connection status
62
56
lastChecked = "LastChecked" // for connection peer status
63
57
)
64
58
59
+ var (
60
+ // KeepAliveInterval to use for wg peers.
61
+ KeepAliveInterval = 10 * time .Second
62
+
63
+ // HandshakeTimeout is maximal time from handshake a connections is still considered connected.
64
+ HandshakeTimeout = 2 * time .Minute + 10 * time .Second
65
+ )
66
+
65
67
var logger = log.Logger {Logger : logf .Log .WithName ("wireguard" )}
66
68
67
69
func init () {
@@ -104,10 +106,6 @@ func NewDriver(localEndpoint *endpoint.Local, _ *types.SubmarinerCluster) (cable
104
106
105
107
// Create the controller.
106
108
if w .client , err = NewClient (); err != nil {
107
- if os .IsNotExist (err ) {
108
- return nil , errors .New ("wgctrl is not available on this system" )
109
- }
110
-
111
109
return nil , errors .Wrap (err , "failed to open wgctl client" )
112
110
}
113
111
@@ -173,10 +171,6 @@ func NewDriver(localEndpoint *endpoint.Local, _ *types.SubmarinerCluster) (cable
173
171
func (w * wireguard ) Init () error {
174
172
logger .V (log .DEBUG ).Infof ("Initializing WireGuard device for cluster %s" , w .localEndpoint .ClusterID )
175
173
176
- if len (w .connections ) != 0 {
177
- return fmt .Errorf ("cannot initialize with existing connections: %+v" , w .connections )
178
- }
179
-
180
174
l , err := w .netLink .InterfaceByName (DefaultDeviceName )
181
175
if err != nil {
182
176
return errors .Wrapf (err , "cannot get wireguard link by name %s" , DefaultDeviceName )
@@ -187,11 +181,7 @@ func (w *wireguard) Init() error {
187
181
return errors .Wrap (err , "wgctrl cannot find WireGuard device" )
188
182
}
189
183
190
- k , err := keyFromSpec (& w .localEndpoint )
191
- if err != nil {
192
- return errors .Wrapf (err , "endpoint is missing public key %s" , d .PublicKey )
193
- }
194
-
184
+ k , _ := keyFromSpec (& w .localEndpoint )
195
185
if k .String () != d .PublicKey .String () {
196
186
return fmt .Errorf ("endpoint public key %s is different from device key %s" , k , d .PublicKey )
197
187
}
@@ -227,10 +217,10 @@ func (w *wireguard) ConnectToEndpoint(endpointInfo *natdiscovery.NATEndpointInfo
227
217
// Parse remote public key.
228
218
remoteKey , err := keyFromSpec (& remoteEndpoint .Spec )
229
219
if err != nil {
230
- return "" , errors .Wrap (err , "failed to parse peer public key" )
220
+ return "" , errors .Wrapf (err , "failed to obtain public key for endpoint %s" , resource . ToJSON ( remoteEndpoint . Spec ) )
231
221
}
232
222
233
- logger .V (log .DEBUG ).Infof ("Connecting cluster %s endpoint %s with publicKey %s " ,
223
+ logger .V (log .DEBUG ).Infof ("Connecting cluster %q endpoint %q with publicKey %q " ,
234
224
remoteEndpoint .Spec .ClusterID , remoteIP , remoteKey )
235
225
236
226
// Delete or update old peers for ClusterID.
@@ -240,7 +230,7 @@ func (w *wireguard) ConnectToEndpoint(endpointInfo *natdiscovery.NATEndpointInfo
240
230
if oldKey .String () == remoteKey .String () {
241
231
// Existing connection, update status and skip.
242
232
w .updatePeerStatus (oldCon , oldKey )
243
- logger .V (log .DEBUG ).Infof ("Skipping connect for existing peer key %s " , oldKey )
233
+ logger .V (log .DEBUG ).Infof ("Skipping connect for existing peer key %q " , oldKey )
244
234
245
235
return ip , nil
246
236
}
@@ -254,9 +244,11 @@ func (w *wireguard) ConnectToEndpoint(endpointInfo *natdiscovery.NATEndpointInfo
254
244
// create connection, overwrite existing connection
255
245
connection := v1 .NewConnection (& remoteEndpoint .Spec , ip , endpointInfo .UseNAT )
256
246
connection .SetStatus (v1 .Connecting , "Connection has been created but not yet started" )
257
- logger .V (log .DEBUG ).Infof ("Adding connection for cluster %s, %v" , remoteEndpoint .Spec .ClusterID , connection )
258
247
w .connections [remoteEndpoint .Spec .ClusterID ] = connection
259
248
249
+ logger .V (log .DEBUG ).Infof ("Added connection for cluster %q: %s" , remoteEndpoint .Spec .ClusterID ,
250
+ resource .ToJSON (connection ))
251
+
260
252
port , err := remoteEndpoint .Spec .GetBackendPort (v1 .UDPPortConfig , w .spec .NATTPort )
261
253
if err != nil {
262
254
logger .Warningf ("Error parsing %q from remote endpoint %q - using port %dº instead: %v" , v1 .UDPPortConfig ,
@@ -293,7 +285,7 @@ func (w *wireguard) ConnectToEndpoint(endpointInfo *natdiscovery.NATEndpointInfo
293
285
logger .Errorf (err , "Failed to verify peer configuration" )
294
286
}
295
287
296
- logger .V (log .DEBUG ).Infof ("Done connecting endpoint peer %s@%s " , * remoteKey , remoteIP )
288
+ logger .V (log .DEBUG ).Infof ("Successfully connected endpoint peer %q with IP %q " , * remoteKey , remoteIP )
297
289
298
290
cable .RecordConnection (cableDriverName , & w .localEndpoint , & connection .Endpoint , string (v1 .Connected ), true , endpointInfo .UseFamily )
299
291
@@ -303,20 +295,17 @@ func (w *wireguard) ConnectToEndpoint(endpointInfo *natdiscovery.NATEndpointInfo
303
295
func keyFromSpec (ep * v1.EndpointSpec ) (* wgtypes.Key , error ) {
304
296
s , found := ep .BackendConfig [PublicKey ]
305
297
if ! found {
306
- return nil , errors .New ("endpoint is missing public key" )
298
+ return & wgtypes. Key {} , errors .New ("endpoint is missing public key" )
307
299
}
308
300
309
301
key , err := wgtypes .ParseKey (s )
310
- if err != nil {
311
- return nil , errors .Wrapf (err , "failed to parse public key %s" , s )
312
- }
313
302
314
- return & key , nil
303
+ return & key , errors . Wrapf ( err , "failed to parse public key %s" , s )
315
304
}
316
305
317
306
func (w * wireguard ) DisconnectFromEndpoint (remoteEndpoint * types.SubmarinerEndpoint , family k8snet.IPFamily ) error {
318
307
// We'll panic if remoteEndpoint is nil, this is intentional
319
- logger .V (log .DEBUG ).Infof ("Removing IPv%v endpoint %v+ " , family , remoteEndpoint )
308
+ logger .V (log .DEBUG ).Infof ("Removing IPv%v endpoint %s " , family , resource . ToJSON ( remoteEndpoint ) )
320
309
321
310
// parse remote public key
322
311
remoteKey , err := keyFromSpec (& remoteEndpoint .Spec )
@@ -335,7 +324,7 @@ func (w *wireguard) DisconnectFromEndpoint(remoteEndpoint *types.SubmarinerEndpo
335
324
336
325
delete (w .connections , remoteEndpoint .Spec .ClusterID )
337
326
338
- logger .V (log .DEBUG ).Infof ("Done removing endpoint for cluster %s " , remoteEndpoint .Spec .ClusterID )
327
+ logger .V (log .DEBUG ).Infof ("Done removing endpoint for cluster %q " , remoteEndpoint .Spec .ClusterID )
339
328
cable .RecordDisconnected (cableDriverName , & w .localEndpoint , & remoteEndpoint .Spec , family )
340
329
341
330
return nil
@@ -384,13 +373,8 @@ func (w *wireguard) removePeer(key *wgtypes.Key) error {
384
373
ReplacePeers : false ,
385
374
Peers : peerCfg ,
386
375
})
387
- if err != nil {
388
- return errors .Wrapf (err , "failed to remove WireGuard peer with key %s" , key )
389
- }
390
-
391
- logger .V (log .DEBUG ).Infof ("Done removing WireGuard peer with key %s" , key )
392
376
393
- return nil
377
+ return errors . Wrapf ( err , "failed to remove WireGuard peer with key %s" , key )
394
378
}
395
379
396
380
func (w * wireguard ) peerByKey (key * wgtypes.Key ) (* wgtypes.Peer , error ) {
@@ -441,12 +425,7 @@ func (w *wireguard) keyMismatch(cid string, key *wgtypes.Key) bool {
441
425
return true
442
426
}
443
427
444
- oldKey , err := keyFromSpec (& c .Endpoint )
445
- if err != nil {
446
- logger .Warningf ("Could not find old key of cluster %s, mismatched endpoint key %s" , cid , key )
447
- return true
448
- }
449
-
428
+ oldKey , _ := keyFromSpec (& c .Endpoint )
450
429
if oldKey .String () != key .String () {
451
430
logger .Warningf ("Key mismatch, cluster %s key is %s, endpoint key is %s" , cid , oldKey , key )
452
431
return true
@@ -469,9 +448,7 @@ func (w *wireguard) Cleanup() error {
469
448
return errors .Wrapf (err , "error retrieving the wireguard interface %q" , DefaultDeviceName )
470
449
}
471
450
472
- if err := w .netLink .LinkDel (link ); err != nil {
473
- return errors .Wrapf (err , "failed to delete existing WireGuard device %q" , DefaultDeviceName )
474
- }
451
+ err = w .netLink .LinkDel (link )
475
452
476
- return nil
453
+ return errors . Wrapf ( err , "failed to delete existing WireGuard device %q" , DefaultDeviceName )
477
454
}
0 commit comments