-
Notifications
You must be signed in to change notification settings - Fork 187
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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) | ||
} | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.