Skip to content

Commit ddad34d

Browse files
authored
B #30: Allows to define VR HAProxy listener ports (#33)
Signed-off-by: Aleix Ramírez <aramirez@opennebula.io>
1 parent 27b7dde commit ddad34d

6 files changed

Lines changed: 48 additions & 8 deletions

File tree

api/v1beta1/onecluster_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ type ONEVirtualRouter struct {
5858
// +optional
5959
Replicas *int32 `json:"replicas,omitempty"`
6060

61+
// +optional
62+
ListenerPorts []int32 `json:"listenerPorts,omitempty"`
63+
6164
// +optional
6265
ExtraContext map[string]string `json:"extraContext,omitempty"`
6366
}

config/crd/bases/infrastructure.cluster.x-k8s.io_oneclusters.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ spec:
120120
type: integer
121121
templateName:
122122
type: string
123+
listenerPorts:
124+
items:
125+
format: int32
126+
type: integer
127+
type: array
123128
required:
124129
- templateName
125130
type: object

internal/cloud/machine.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ package cloud
1919
import (
2020
"encoding/base64"
2121
"fmt"
22+
"slices"
23+
"strconv"
2224
"strings"
2325

2426
infrav1 "github.com/OpenNebula/cluster-api-provider-opennebula/api/v1beta1"
2527

2628
goca "github.com/OpenNebula/one/src/oca/go/src/goca"
2729
goca_vm "github.com/OpenNebula/one/src/oca/go/src/goca/schemas/vm"
30+
goca_vm_keys "github.com/OpenNebula/one/src/oca/go/src/goca/schemas/vm/keys"
2831
)
2932

3033
type Machine struct {
@@ -143,12 +146,7 @@ func (m *Machine) FromTemplate(templateName string, userData *string, network *i
143146

144147
if router != nil {
145148
// Mark this machine as a Control-Plane backend in the VR (dynamic LB).
146-
update := goca_vm.NewTemplate()
147-
update.Add("ONEGATE_HAPROXY_LB0_IP", "<ETH0_EP0>")
148-
update.Add("ONEGATE_HAPROXY_LB0_PORT", "6443")
149-
update.Add("ONEGATE_HAPROXY_LB0_SERVER_HOST", m.Address4)
150-
update.Add("ONEGATE_HAPROXY_LB0_SERVER_PORT", "6443")
151-
149+
update := generateVMTemplateVRouterLBParams(router, m.Address4)
152150
if err := m.ctrl.VM(m.ID).Update(update.String(), 1); err != nil {
153151
return fmt.Errorf("Failed to update VM: %w", err)
154152
}
@@ -157,6 +155,28 @@ func (m *Machine) FromTemplate(templateName string, userData *string, network *i
157155
return nil
158156
}
159157

158+
func generateVMTemplateVRouterLBParams(router *infrav1.ONEVirtualRouter, serverAddress string) *goca_vm.Template {
159+
update := goca_vm.NewTemplate()
160+
if len(router.ListenerPorts) == 0 {
161+
//defaults to kubernetes api port load balancing
162+
update.Add("ONEGATE_HAPROXY_LB0_IP", "<ETH0_EP0>")
163+
update.Add("ONEGATE_HAPROXY_LB0_PORT", "6443")
164+
update.Add("ONEGATE_HAPROXY_LB0_SERVER_HOST", serverAddress)
165+
update.Add("ONEGATE_HAPROXY_LB0_SERVER_PORT", "6443")
166+
return update
167+
}
168+
169+
slices.Sort(router.ListenerPorts)
170+
for idx, port := range router.ListenerPorts {
171+
//NOTE: Pass ports as strings, as the template make pair method doesn't support int32 values
172+
update.Add(goca_vm_keys.Template(fmt.Sprintf("ONEGATE_HAPROXY_LB%d_IP", idx)), "<ETH0_EP0>")
173+
update.Add(goca_vm_keys.Template(fmt.Sprintf("ONEGATE_HAPROXY_LB%d_PORT", idx)), strconv.Itoa(int(port)))
174+
update.Add(goca_vm_keys.Template(fmt.Sprintf("ONEGATE_HAPROXY_LB%d_SERVER_HOST", idx)), serverAddress)
175+
update.Add(goca_vm_keys.Template(fmt.Sprintf("ONEGATE_HAPROXY_LB%d_SERVER_PORT", idx)), strconv.Itoa(int(port)))
176+
}
177+
return update
178+
}
179+
160180
func (m *Machine) Delete() error {
161181
if !m.Exists() {
162182
return nil

internal/cloud/router.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package cloud
1919
import (
2020
"fmt"
2121
"net"
22+
"slices"
23+
"strconv"
2224

2325
infrav1 "github.com/OpenNebula/cluster-api-provider-opennebula/api/v1beta1"
2426

@@ -141,8 +143,16 @@ func (r *Router) FromTemplate(virtualRouter *infrav1.ONEVirtualRouter, publicNet
141143
contextMap := map[string]string{}
142144
contextMap["ONEAPP_VNF_HAPROXY_ENABLED"] = "YES"
143145
contextMap["ONEAPP_VNF_HAPROXY_ONEGATE_ENABLED"] = "YES"
144-
contextMap["ONEAPP_VNF_HAPROXY_LB0_IP"] = "<ETH0_EP0>"
145-
contextMap["ONEAPP_VNF_HAPROXY_LB0_PORT"] = "6443"
146+
if len(virtualRouter.ListenerPorts) == 0 {
147+
//defaults to kubernets api port load balancing
148+
contextMap["ONEAPP_VNF_HAPROXY_LB0_IP"] = "<ETH0_EP0>"
149+
contextMap["ONEAPP_VNF_HAPROXY_LB0_PORT"] = "6443"
150+
}
151+
slices.Sort(virtualRouter.ListenerPorts)
152+
for idx, port := range virtualRouter.ListenerPorts {
153+
contextMap[fmt.Sprintf("ONEAPP_VNF_HAPROXY_LB%d_IP", idx)] = "<ETH0_EP0>"
154+
contextMap[fmt.Sprintf("ONEAPP_VNF_HAPROXY_LB%d_PORT", idx)] = strconv.Itoa(int(port))
155+
}
146156
updateContext(contextVec, &contextMap)
147157
if virtualRouter.ExtraContext != nil {
148158
updateContext(contextVec, &virtualRouter.ExtraContext)

kustomize/v1beta1/rke2-dev/cluster-template.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ spec:
119119
name: "${PRIVATE_NETWORK_NAME}"
120120
virtualRouter:
121121
templateName: "${ROUTER_TEMPLATE_NAME}"
122+
listenerPorts: [6443, 9345]
122123
extraContext: {}
123124
images:
124125
- imageName: "${ROUTER_TEMPLATE_NAME}"

kustomize/v1beta1/rke2/cluster-template.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ spec:
119119
name: "${PRIVATE_NETWORK_NAME}"
120120
virtualRouter:
121121
templateName: "${ROUTER_TEMPLATE_NAME}"
122+
listenerPorts: [6443, 9345]
122123
extraContext: {}
123124
---
124125
apiVersion: cluster.x-k8s.io/v1beta1

0 commit comments

Comments
 (0)