@@ -19,12 +19,15 @@ package cloud
1919import (
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
3033type 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+
160180func (m * Machine ) Delete () error {
161181 if ! m .Exists () {
162182 return nil
0 commit comments