Skip to content

Commit dedf53f

Browse files
committed
update
1 parent 511673d commit dedf53f

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

pkg/controllerutil/host_port_manager.go

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434

3535
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
3636
"github.com/apecloud/kubeblocks/pkg/constant"
37+
"github.com/apecloud/kubeblocks/pkg/kbagent"
3738
viper "github.com/apecloud/kubeblocks/pkg/viperx"
3839
)
3940

@@ -53,7 +54,10 @@ func GetPortManager(network *appsv1.ComponentNetwork) PortManager {
5354
if network == nil || !network.HostNetwork || len(network.HostPorts) == 0 {
5455
return defaultPortManager
5556
}
56-
return newDefinedPortManager(network.HostPorts)
57+
if defaultPortManager == nil {
58+
return newDefinedPortManager(nil, network.HostPorts)
59+
}
60+
return newDefinedPortManager(defaultPortManager.(*portManager), network.HostPorts)
5761
}
5862

5963
func InitDefaultHostPortManager(cli client.Client) error {
@@ -355,25 +359,35 @@ func (m *portManager) ReleaseByPrefix(prefix string) error {
355359
keys = append(keys, key)
356360
}
357361
}
358-
if err := m.delete(keys); err != nil {
359-
return err
362+
if len(keys) > 0 {
363+
return m.delete(keys)
360364
}
361365
return nil
362366
}
363367

364368
type definedPortManager struct {
365-
hostPorts map[string]int32
369+
defaultPortManager *portManager
370+
hostPorts map[string]int32
366371
}
367372

368-
func (m *definedPortManager) PortKey(_, _, _, portName string) string {
373+
func (m *definedPortManager) PortKey(clusterName, compName, containerName, portName string) string {
374+
if m.isKBAgentPortNNotDefined(containerName, portName) {
375+
return m.defaultPortManager.PortKey(clusterName, compName, containerName, portName)
376+
}
369377
return portName
370378
}
371379

372380
func (m *definedPortManager) GetPort(key string) (int32, error) {
381+
if m.isKBAgentPortNNotDefinedInKey(key) {
382+
return m.defaultPortManager.GetPort(key)
383+
}
373384
return m.hostPorts[key], nil
374385
}
375386

376-
func (m *definedPortManager) UsePort(_ string, _ int32) error {
387+
func (m *definedPortManager) UsePort(key string, port int32) error {
388+
if m.isKBAgentPortNNotDefinedInKey(key) {
389+
return m.defaultPortManager.UsePort(key, port)
390+
}
377391
return nil
378392
}
379393

@@ -382,18 +396,50 @@ func (m *definedPortManager) AllocatePort(key string) (int32, error) {
382396
if ok {
383397
return port, nil
384398
}
399+
if m.isKBAgentPortNNotDefinedInKey(key) {
400+
return m.defaultPortManager.AllocatePort(key)
401+
}
385402
return 0, fmt.Errorf("no available port")
386403

387404
}
388405

389-
func (m *definedPortManager) ReleaseByPrefix(_ string) error {
390-
return nil
406+
func (m *definedPortManager) ReleaseByPrefix(prefix string) error {
407+
if m.hasKBAgentPortDefined() {
408+
return nil
409+
}
410+
return m.defaultPortManager.ReleaseByPrefix(prefix)
391411
}
392412

393-
func newDefinedPortManager(hostPorts []appsv1.HostPort) *definedPortManager {
413+
func (m *definedPortManager) isKBAgentPort(containerName, portName string) bool {
414+
return containerName == kbagent.ContainerName && (portName == kbagent.DefaultHTTPPortName || portName == kbagent.DefaultStreamingPortName)
415+
}
416+
417+
func (m *definedPortManager) hasKBAgentPortDefined() bool {
418+
_, http := m.hostPorts[kbagent.DefaultHTTPPortName]
419+
_, stream := m.hostPorts[kbagent.DefaultStreamingPortName]
420+
return http && stream
421+
}
422+
423+
func (m *definedPortManager) isKBAgentPortNNotDefined(containerName, portName string) bool {
424+
_, defined := m.hostPorts[portName]
425+
return m.isKBAgentPort(containerName, portName) && !defined
426+
}
427+
428+
func (m *definedPortManager) isKBAgentPortNNotDefinedInKey(key string) bool {
429+
subs := strings.Split(key, "-")
430+
if len(subs) != 4 {
431+
return false
432+
}
433+
return m.isKBAgentPortNNotDefined(subs[2], subs[3])
434+
}
435+
436+
func newDefinedPortManager(defaultPortManager *portManager, hostPorts []appsv1.HostPort) *definedPortManager {
394437
hostPortsMap := make(map[string]int32)
395438
for _, hp := range hostPorts {
396439
hostPortsMap[hp.Name] = hp.Port
397440
}
398-
return &definedPortManager{hostPorts: hostPortsMap}
441+
return &definedPortManager{
442+
defaultPortManager: defaultPortManager,
443+
hostPorts: hostPortsMap,
444+
}
399445
}

0 commit comments

Comments
 (0)