@@ -64,6 +64,7 @@ type Options struct {
64
64
SSHMaxTimeout time.Duration
65
65
TailnetListenPort uint16
66
66
Subsystem codersdk.AgentSubsystem
67
+ Addresses []netip.Prefix
67
68
68
69
PrometheusRegistry * prometheus.Registry
69
70
}
@@ -132,6 +133,7 @@ func New(options Options) Agent {
132
133
connStatsChan :make (chan * agentsdk.Stats ,1 ),
133
134
sshMaxTimeout :options .SSHMaxTimeout ,
134
135
subsystem :options .Subsystem ,
136
+ addresses :options .Addresses ,
135
137
136
138
prometheusRegistry :prometheusRegistry ,
137
139
metrics :newAgentMetrics (prometheusRegistry ),
@@ -177,6 +179,7 @@ type agent struct {
177
179
lifecycleStates []agentsdk.PostLifecycleRequest
178
180
179
181
network * tailnet.Conn
182
+ addresses []netip.Prefix
180
183
connStatsChan chan * agentsdk.Stats
181
184
latestStat atomic.Pointer [agentsdk.Stats ]
182
185
@@ -545,6 +548,10 @@ func (a *agent) run(ctx context.Context) error {
545
548
}
546
549
a .logger .Info (ctx ,"fetched manifest" ,slog .F ("manifest" ,manifest ))
547
550
551
+ if manifest .AgentID == uuid .Nil {
552
+ return xerrors .New ("nil agentID returned by manifest" )
553
+ }
554
+
548
555
// Expand the directory and send it back to coderd so external
549
556
// applications that rely on the directory can use it.
550
557
//
@@ -630,7 +637,7 @@ func (a *agent) run(ctx context.Context) error {
630
637
network := a .network
631
638
a .closeMutex .Unlock ()
632
639
if network == nil {
633
- network ,err = a .createTailnet (ctx ,manifest .DERPMap ,manifest .DisableDirectConnections )
640
+ network ,err = a .createTailnet (ctx ,manifest .AgentID , manifest . DERPMap ,manifest .DisableDirectConnections )
634
641
if err != nil {
635
642
return xerrors .Errorf ("create tailnet: %w" ,err )
636
643
}
@@ -648,6 +655,11 @@ func (a *agent) run(ctx context.Context) error {
648
655
649
656
a .startReportingConnectionStats (ctx )
650
657
}else {
658
+ // Update the wireguard IPs if the agent ID changed.
659
+ err := network .SetAddresses (a .wireguardAddresses (manifest .AgentID ))
660
+ if err != nil {
661
+ a .logger .Error (ctx ,"update tailnet addresses" ,slog .Error (err ))
662
+ }
651
663
// Update the DERP map and allow/disallow direct connections.
652
664
network .SetDERPMap (manifest .DERPMap )
653
665
network .SetBlockEndpoints (manifest .DisableDirectConnections )
@@ -661,6 +673,20 @@ func (a *agent) run(ctx context.Context) error {
661
673
return nil
662
674
}
663
675
676
+ func (a * agent )wireguardAddresses (agentID uuid.UUID ) []netip.Prefix {
677
+ if len (a .addresses )== 0 {
678
+ return []netip.Prefix {
679
+ // This is the IP that should be used primarily.
680
+ netip .PrefixFrom (tailnet .IPFromUUID (agentID ),128 ),
681
+ // We also listen on the legacy codersdk.WorkspaceAgentIP. This
682
+ // allows for a transition away from wsconncache.
683
+ netip .PrefixFrom (codersdk .WorkspaceAgentIP ,128 ),
684
+ }
685
+ }
686
+
687
+ return a .addresses
688
+ }
689
+
664
690
func (a * agent )trackConnGoroutine (fn func ())error {
665
691
a .closeMutex .Lock ()
666
692
defer a .closeMutex .Unlock ()
@@ -675,9 +701,9 @@ func (a *agent) trackConnGoroutine(fn func()) error {
675
701
return nil
676
702
}
677
703
678
- func (a * agent )createTailnet (ctx context.Context ,derpMap * tailcfg.DERPMap ,disableDirectConnections bool ) (_ * tailnet.Conn ,err error ) {
704
+ func (a * agent )createTailnet (ctx context.Context ,agentID uuid. UUID , derpMap * tailcfg.DERPMap ,disableDirectConnections bool ) (_ * tailnet.Conn ,err error ) {
679
705
network ,err := tailnet .NewConn (& tailnet.Options {
680
- Addresses :[]netip. Prefix { netip . PrefixFrom ( codersdk . WorkspaceAgentIP , 128 )} ,
706
+ Addresses :a . wireguardAddresses ( agentID ) ,
681
707
DERPMap :derpMap ,
682
708
Logger :a .logger .Named ("tailnet" ),
683
709
ListenPort :a .tailnetListenPort ,