Skip to content

Commit 511673d

Browse files
committed
map for container ports to host ports
1 parent d0e11c6 commit 511673d

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

controllers/apps/component/component_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ func (r *ComponentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
151151
&componentMonitorContainerTransformer{},
152152
// allocate ports for host-network component
153153
&componentHostNetworkTransformer{},
154+
// map for container ports to host ports
155+
&componentHostPortTransformer{},
154156
// handle component services
155157
&componentServiceTransformer{},
156158
// handle component system accounts
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright (C) 2022-2025 ApeCloud Co., Ltd
3+
4+
This file is part of KubeBlocks project
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU Affero General Public License for more details.
15+
16+
You should have received a copy of the GNU Affero General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package component
21+
22+
import (
23+
"github.com/apecloud/kubeblocks/pkg/controller/graph"
24+
)
25+
26+
type componentHostPortTransformer struct{}
27+
28+
var _ graph.Transformer = &componentHostPortTransformer{}
29+
30+
func (t *componentHostPortTransformer) Transform(ctx graph.TransformContext, dag *graph.DAG) error {
31+
transCtx, _ := ctx.(*componentTransformContext)
32+
if isCompDeleting(transCtx.ComponentOrig) {
33+
return nil
34+
}
35+
36+
synthesizedComp := transCtx.SynthesizeComponent
37+
if synthesizedComp == nil ||
38+
synthesizedComp.PodSpec.HostNetwork ||
39+
synthesizedComp.Network == nil ||
40+
synthesizedComp.Network.HostNetwork {
41+
return nil
42+
}
43+
44+
ports := map[string]int32{}
45+
for _, hostPort := range synthesizedComp.Network.HostPorts {
46+
ports[hostPort.Name] = hostPort.Port
47+
}
48+
if len(ports) > 0 {
49+
for i, c := range synthesizedComp.PodSpec.Containers {
50+
for j, p := range c.Ports {
51+
if hostPort, ok := ports[p.Name]; ok {
52+
synthesizedComp.PodSpec.Containers[i].Ports[j].HostPort = hostPort
53+
}
54+
}
55+
}
56+
}
57+
return nil
58+
}

0 commit comments

Comments
 (0)