|
| 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