Skip to content

Commit e8920a2

Browse files
authored
Merge pull request kubernetes-sigs#10245 from chrischdi/pr-test-inmemory-port-forward
🌱 test/inmemory: use port only to identify the wcl to make port-forward…
2 parents 878f800 + c45222a commit e8920a2

File tree

1 file changed

+23
-14
lines changed
  • test/infrastructure/inmemory/pkg/server

1 file changed

+23
-14
lines changed

test/infrastructure/inmemory/pkg/server/mux.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ type WorkloadClustersMux struct {
108108
debugServer http.Server
109109
muxServer http.Server
110110
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
113113

114114
lock sync.RWMutex
115115
log logr.Logger
@@ -131,7 +131,7 @@ func NewWorkloadClustersMux(manager inmemoryruntime.Manager, host string, opts .
131131
portIndex: options.MinPort,
132132
manager: manager,
133133
workloadClusterListeners: map[string]*WorkloadClusterListener{},
134-
workloadClusterNameByHost: map[string]string{},
134+
workloadClusterNameByPort: map[string]string{},
135135
log: log.Log,
136136
}
137137

@@ -169,7 +169,12 @@ func (m *WorkloadClustersMux) mixedHandler() http.Handler {
169169
resourceGroupResolver := func(host string) (string, error) {
170170
m.lock.RLock()
171171
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]
173178
if !ok {
174179
return "", errors.Errorf("failed to get workloadClusterListener for host %s", host)
175180
}
@@ -211,18 +216,22 @@ func (m *WorkloadClustersMux) getCertificate(info *tls.ClientHelloInfo) (*tls.Ce
211216
defer m.lock.RUnlock()
212217

213218
// 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]
216225
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)
218227
m.log.Error(err, "Error resolving certificates")
219228
return nil, err
220229
}
221230

222231
// Gets the listener config for the target workloadCluster.
223232
wcl, ok := m.workloadClusterListeners[wclName]
224233
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)
226235
m.log.Error(err, "Error resolving certificates")
227236
return nil, err
228237
}
@@ -231,12 +240,12 @@ func (m *WorkloadClustersMux) getCertificate(info *tls.ClientHelloInfo) (*tls.Ce
231240
// NOTE: the port forward call to etcd sets the server name to the name of the targeted etcd pod,
232241
// which is also the name of the corresponding etcd member.
233242
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)
235244
return wcl.etcdServingCertificates[info.ServerName], nil
236245
}
237246

238247
// 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)
240249
return wcl.apiServerServingCertificate, nil
241250
}
242251

@@ -320,7 +329,7 @@ func (m *WorkloadClustersMux) initWorkloadClusterListenerWithPortLocked(wclName
320329
// NOTE: it is required to add on both maps and keep them in sync
321330
// In order to get the resourceGroupResolver to work.
322331
m.workloadClusterListeners[wclName] = wcl
323-
m.workloadClusterNameByHost[wcl.HostPort()] = wclName
332+
m.workloadClusterNameByPort[fmt.Sprintf("%d", wcl.Port())] = wclName
324333

325334
m.log.Info("Workload cluster listener created", "listenerName", wclName, "address", wcl.Address())
326335
return wcl
@@ -432,9 +441,9 @@ func (m *WorkloadClustersMux) AddAPIServer(wclName, podName string, caCert *x509
432441
return nil
433442
}
434443

435-
l, err := net.Listen("tcp", wcl.HostPort())
444+
l, err := net.Listen("tcp", fmt.Sprintf(":%d", wcl.Port()))
436445
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()))
438447
}
439448
wcl.listener = l
440449

@@ -603,7 +612,7 @@ func (m *WorkloadClustersMux) DeleteWorkloadClusterListener(wclName string) erro
603612
}
604613

605614
delete(m.workloadClusterListeners, wclName)
606-
delete(m.workloadClusterNameByHost, wcl.HostPort())
615+
delete(m.workloadClusterNameByPort, fmt.Sprintf("%d", wcl.Port()))
607616

608617
m.log.Info("Workload cluster listener deleted", "listenerName", wclName, "address", wcl.Address())
609618
return nil

0 commit comments

Comments
 (0)