@@ -187,8 +187,8 @@ func runTunnel(ctx context.Context, cfg *configv1alpha.Config, client versioned.
187187tunnelNode .Status .ExternalAddress = tun .ExternalAddress ().String ()
188188tunnelNode .Status .InternalAddress = tun .InternalAddress ().String ()
189189
190- // Create the TunnelNode object in the API.
191- slog .Debug ("Creating TunnelNode" , slog .String ("name" , tunnelNode .Name ))
190+ // Create/update the TunnelNode object in the API.
191+ slog .Debug ("Creating/updating TunnelNode" , slog .String ("name" , tunnelNode .Name ))
192192
193193if err := upsertTunnelNode (ctx , client , tunnelNode ); err != nil {
194194return err
@@ -357,7 +357,10 @@ func syncTunnelNode(tunnelNodeLister corev1alphaclient.TunnelNodeLister,
357357AllowedIPs : []string {peerTunnelNode .Status .InternalAddress },
358358}
359359
360- slog .Debug ("Adding peer" , slog .String ("name" , peerTunnelNode .Name ))
360+ slog .Debug ("Adding peer" ,
361+ slog .String ("name" , peerTunnelNode .Name ),
362+ slog .String ("publicKey" , peerPublicKey ),
363+ slog .String ("endpoint" , peerTunnelNode .Status .ExternalAddress ))
361364
362365if err := tun .AddPeer (peerConf ); err != nil {
363366slog .Error ("Failed to add peer" , slog .String ("name" , peerTunnelNode .Name ), slog .Any ("error" , err ))
@@ -377,28 +380,32 @@ func syncTunnelNode(tunnelNodeLister corev1alphaclient.TunnelNodeLister,
377380// upsertTunnelNode creates or updates a TunnelNode object in the API.
378381func upsertTunnelNode (ctx context.Context , client versioned.Interface , tunnelNode * corev1alpha.TunnelNode ) error {
379382return retry .RetryOnConflict (retry .DefaultBackoff , func () error {
380- _ , err := client .CoreV1alpha ().TunnelNodes ().Create (ctx , tunnelNode , metav1.CreateOptions {})
381- if errors .IsAlreadyExists (err ) {
382- existingTunnelNode , err := client .CoreV1alpha ().TunnelNodes ().Get (ctx , tunnelNode .Name , metav1.GetOptions {})
383- if err != nil {
384- return fmt .Errorf ("failed to get existing TunnelNode: %w" , err )
385- }
386-
383+ existingTunnelNode , err := client .CoreV1alpha ().TunnelNodes ().Get (ctx , tunnelNode .Name , metav1.GetOptions {})
384+ if err == nil {
385+ // Update the existing TunnelNode.
387386tunnelNode .ResourceVersion = existingTunnelNode .ResourceVersion
388387
389388_ , err = client .CoreV1alpha ().TunnelNodes ().Update (ctx , tunnelNode , metav1.UpdateOptions {})
390389if err != nil {
391390return fmt .Errorf ("failed to update existing TunnelNode: %w" , err )
392391}
392+ } else {
393+ // Create a new TunnelNode.
394+ if _ , err := client .CoreV1alpha ().TunnelNodes ().Create (ctx , tunnelNode , metav1.CreateOptions {}); err != nil {
395+ return fmt .Errorf ("failed to create TunnelNode: %w" , err )
396+ }
393397
394- _ , err = client .CoreV1alpha ().TunnelNodes ().UpdateStatus (ctx , tunnelNode , metav1.UpdateOptions {})
398+ existingTunnelNode , err : = client .CoreV1alpha ().TunnelNodes ().Get (ctx , tunnelNode . Name , metav1.GetOptions {})
395399if err != nil {
396- return fmt .Errorf ("failed to update existing TunnelNode status : %w" , err )
400+ return fmt .Errorf ("failed to get newly created TunnelNode : %w" , err )
397401}
398402
399- return nil
400- } else if err != nil {
401- return fmt .Errorf ("failed to create TunnelNode: %w" , err )
403+ tunnelNode .ResourceVersion = existingTunnelNode .ResourceVersion
404+ }
405+
406+ _ , err = client .CoreV1alpha ().TunnelNodes ().UpdateStatus (ctx , tunnelNode , metav1.UpdateOptions {})
407+ if err != nil {
408+ return fmt .Errorf ("failed to update TunnelNode status: %w" , err )
402409}
403410
404411return nil
0 commit comments