@@ -108,8 +108,8 @@ type WorkloadClustersMux struct {
108
108
debugServer http.Server
109
109
muxServer http.Server
110
110
workloadClusterListeners map [string ]* WorkloadClusterListener
111
- // workloadClusterNameByHost maps from Host to workload cluster name.
112
- workloadClusterNameByHost map [string ]string
111
+ // workloadClusterNameByPort maps from Port to workload cluster name.
112
+ workloadClusterNameByPort map [string ]string
113
113
114
114
lock sync.RWMutex
115
115
log logr.Logger
@@ -131,7 +131,7 @@ func NewWorkloadClustersMux(manager inmemoryruntime.Manager, host string, opts .
131
131
portIndex : options .MinPort ,
132
132
manager : manager ,
133
133
workloadClusterListeners : map [string ]* WorkloadClusterListener {},
134
- workloadClusterNameByHost : map [string ]string {},
134
+ workloadClusterNameByPort : map [string ]string {},
135
135
log : log .Log ,
136
136
}
137
137
@@ -169,7 +169,12 @@ func (m *WorkloadClustersMux) mixedHandler() http.Handler {
169
169
resourceGroupResolver := func (host string ) (string , error ) {
170
170
m .lock .RLock ()
171
171
defer m .lock .RUnlock ()
172
- wclName , ok := m .workloadClusterNameByHost [host ]
172
+
173
+ _ , port , err := net .SplitHostPort (host )
174
+ if err != nil {
175
+ return "" , err
176
+ }
177
+ wclName , ok := m .workloadClusterNameByPort [port ]
173
178
if ! ok {
174
179
return "" , errors .Errorf ("failed to get workloadClusterListener for host %s" , host )
175
180
}
@@ -211,18 +216,22 @@ func (m *WorkloadClustersMux) getCertificate(info *tls.ClientHelloInfo) (*tls.Ce
211
216
defer m .lock .RUnlock ()
212
217
213
218
// Identify which workloadCluster/resourceGroup a request targets to.
214
- hostPort := info .Conn .LocalAddr ().String ()
215
- wclName , ok := m .workloadClusterNameByHost [hostPort ]
219
+ _ , port , err := net .SplitHostPort (info .Conn .LocalAddr ().String ())
220
+ if err != nil {
221
+ return nil , err
222
+ }
223
+
224
+ wclName , ok := m .workloadClusterNameByPort [port ]
216
225
if ! ok {
217
- err := errors .Errorf ("failed to get listener name for workload cluster serving on %s" , hostPort )
226
+ err := errors .Errorf ("failed to get listener name for workload cluster serving on %s" , port )
218
227
m .log .Error (err , "Error resolving certificates" )
219
228
return nil , err
220
229
}
221
230
222
231
// Gets the listener config for the target workloadCluster.
223
232
wcl , ok := m .workloadClusterListeners [wclName ]
224
233
if ! ok {
225
- err := errors .Errorf ("failed to get listener with name %s for workload cluster serving on %s" , wclName , hostPort )
234
+ err := errors .Errorf ("failed to get listener with name %s for workload cluster serving on %s" , wclName , port )
226
235
m .log .Error (err , "Error resolving certificates" )
227
236
return nil , err
228
237
}
@@ -231,12 +240,12 @@ func (m *WorkloadClustersMux) getCertificate(info *tls.ClientHelloInfo) (*tls.Ce
231
240
// NOTE: the port forward call to etcd sets the server name to the name of the targeted etcd pod,
232
241
// which is also the name of the corresponding etcd member.
233
242
if wcl .etcdMembers .Has (info .ServerName ) {
234
- m .log .V (4 ).Info ("Using etcd serving certificate" , "listenerName" , wcl , "host" , hostPort , "etcdPod" , info .ServerName )
243
+ m .log .V (4 ).Info ("Using etcd serving certificate" , "listenerName" , wcl , "host" , port , "etcdPod" , info .ServerName )
235
244
return wcl .etcdServingCertificates [info .ServerName ], nil
236
245
}
237
246
238
247
// Otherwise we assume the request targets the API server.
239
- m .log .V (4 ).Info ("Using API server serving certificate" , "listenerName" , wcl , "host" , hostPort )
248
+ m .log .V (4 ).Info ("Using API server serving certificate" , "listenerName" , wcl , "host" , port )
240
249
return wcl .apiServerServingCertificate , nil
241
250
}
242
251
@@ -320,7 +329,7 @@ func (m *WorkloadClustersMux) initWorkloadClusterListenerWithPortLocked(wclName
320
329
// NOTE: it is required to add on both maps and keep them in sync
321
330
// In order to get the resourceGroupResolver to work.
322
331
m .workloadClusterListeners [wclName ] = wcl
323
- m .workloadClusterNameByHost [ wcl .HostPort ( )] = wclName
332
+ m .workloadClusterNameByPort [ fmt . Sprintf ( "%d" , wcl .Port () )] = wclName
324
333
325
334
m .log .Info ("Workload cluster listener created" , "listenerName" , wclName , "address" , wcl .Address ())
326
335
return wcl
@@ -432,9 +441,9 @@ func (m *WorkloadClustersMux) AddAPIServer(wclName, podName string, caCert *x509
432
441
return nil
433
442
}
434
443
435
- l , err := net .Listen ("tcp" , wcl .HostPort ( ))
444
+ l , err := net .Listen ("tcp" , fmt . Sprintf ( ":%d" , wcl .Port () ))
436
445
if err != nil {
437
- return errors .Wrapf (err , "failed to start WorkloadClusterListener %s, %s" , wclName , wcl .HostPort ( ))
446
+ return errors .Wrapf (err , "failed to start WorkloadClusterListener %s, %s" , wclName , fmt . Sprintf ( ":%d" , wcl .Port () ))
438
447
}
439
448
wcl .listener = l
440
449
@@ -603,7 +612,7 @@ func (m *WorkloadClustersMux) DeleteWorkloadClusterListener(wclName string) erro
603
612
}
604
613
605
614
delete (m .workloadClusterListeners , wclName )
606
- delete (m .workloadClusterNameByHost , wcl .HostPort ( ))
615
+ delete (m .workloadClusterNameByPort , fmt . Sprintf ( "%d" , wcl .Port () ))
607
616
608
617
m .log .Info ("Workload cluster listener deleted" , "listenerName" , wclName , "address" , wcl .Address ())
609
618
return nil
0 commit comments