Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: support kbagent multi ports allocate hostnetwork port #8565

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions controllers/apps/transformer_component_hostnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package apps

import (
"fmt"

corev1 "k8s.io/api/core/v1"

"github.com/apecloud/kubeblocks/pkg/controller/component"
Expand Down Expand Up @@ -51,47 +53,67 @@ func (t *componentHostNetworkTransformer) Transform(ctx graph.TransformContext,
}

func allocateHostPorts(synthesizedComp *component.SynthesizedComponent) (map[string]map[string]int32, error) {
// container -> portName -> allocateRequired
ports := map[string]map[string]bool{}
for _, c := range synthesizedComp.HostNetwork.ContainerPorts {
containerPorts := map[string]bool{}

for _, c := range synthesizedComp.PodSpec.Containers {
ports[c.Name] = make(map[string]bool)
for _, p := range c.Ports {
containerPorts[p] = true
ports[c.Name][p.Name] = false
}
}

for _, c := range synthesizedComp.HostNetwork.ContainerPorts {
if containerPorts, exists := ports[c.Container]; exists {
for _, p := range c.Ports {
containerPorts[p] = true
}
}
ports[c.Container] = containerPorts
}

pm := intctrlutil.GetPortManager()
needAllocate := func(c string, p string) bool {
containerPorts, ok := ports[c]
if !ok {
return false
needAllocate := func(containerName string, portName string) bool {
if containerPorts, ok := ports[containerName]; ok {
return containerPorts[portName]
}
return containerPorts[p]
return false
}

return allocateHostPortsWithFunc(pm, synthesizedComp, needAllocate)
}

func allocateHostPortsWithFunc(pm *intctrlutil.PortManager, synthesizedComp *component.SynthesizedComponent,
needAllocate func(string, string) bool) (map[string]map[string]int32, error) {
// container -> portName -> port
ports := map[string]map[string]int32{}
insert := func(c, pk string, pv int32) {
if _, ok := ports[c]; !ok {
ports[c] = map[string]int32{}

insert := func(containerName, portName string, portValue int32) {
if _, ok := ports[containerName]; !ok {
ports[containerName] = map[string]int32{}
}
ports[c][pk] = pv
ports[containerName][portName] = portValue
}
for _, c := range synthesizedComp.PodSpec.Containers {
for _, p := range c.Ports {
portKey := intctrlutil.BuildHostPortName(synthesizedComp.ClusterName, synthesizedComp.Name, c.Name, p.Name)
if needAllocate(c.Name, p.Name) {
port, err := pm.AllocatePort(portKey)

for _, container := range synthesizedComp.PodSpec.Containers {
for _, port := range container.Ports {
portKey := intctrlutil.BuildHostPortName(
synthesizedComp.ClusterName,
synthesizedComp.Name,
container.Name,
port.Name,
)

if needAllocate(container.Name, port.Name) {
allocatedPort, err := pm.AllocatePort(portKey)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to allocate port for container %s, port %s: %v",
container.Name, port.Name, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order of params is incorrect.

}
insert(c.Name, p.Name, port)
insert(container.Name, port.Name, allocatedPort)
} else {
if err := pm.UsePort(portKey, p.ContainerPort); err != nil {
return nil, err
if err := pm.UsePort(portKey, port.ContainerPort); err != nil {
return nil, fmt.Errorf("failed to use port for container %s, port %s: %v",
container.Name, port.Name, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

}
}
}
Expand Down
Loading