@@ -34,30 +34,35 @@ type Machine struct {
3434 ctrl * goca.Controller
3535 ID int
3636 Name string
37+ RouterID int
3738 Address4 string
3839}
3940
4041type MachineOption func (* Machine )
4142
42- func NewMachine (clients * Clients , options ... MachineOption ) (* Machine , error ) {
43+ func WithMachineName (name string ) MachineOption {
44+ return func (m * Machine ) {
45+ m .Name = name
46+ }
47+ }
48+ func WithMachineRouterID (routerID int ) MachineOption {
49+ return func (m * Machine ) {
50+ m .RouterID = routerID
51+ }
52+ }
4353
54+ func NewMachine (clients * Clients , options ... MachineOption ) (* Machine , error ) {
4455 if clients == nil {
4556 return nil , fmt .Errorf ("clients reference is nil" )
4657 }
4758
48- m := & Machine {ctrl : goca .NewController (clients .RPC2 ), ID : - 1 }
59+ m := & Machine {ctrl : goca .NewController (clients .RPC2 ), ID : - 1 , RouterID : - 1 }
4960 for _ , option := range options {
5061 option (m )
5162 }
5263 return m , nil
5364}
5465
55- func WithMachineName (name string ) MachineOption {
56- return func (m * Machine ) {
57- m .Name = name
58- }
59- }
60-
6166func (m * Machine ) Exists () bool {
6267 return m .ID >= 0
6368}
@@ -88,7 +93,10 @@ func (m *Machine) ByName(vmName string) error {
8893 return m .ByID (vmID )
8994}
9095
91- func (m * Machine ) FromTemplate (templateName string , userData * string , network * infrav1.ONEVirtualNetwork , router * infrav1.ONEVirtualRouter ) error {
96+ func (m * Machine ) FromTemplate (
97+ templateName string , userData * string ,
98+ network * infrav1.ONEVirtualNetwork , router * infrav1.ONEVirtualRouter ) error {
99+
92100 if m .Exists () {
93101 return nil
94102 }
@@ -146,7 +154,7 @@ func (m *Machine) FromTemplate(templateName string, userData *string, network *i
146154
147155 if router != nil {
148156 // Mark this machine as a Control-Plane backend in the VR (dynamic LB).
149- update := generateVMTemplateVRouterLBParams (router , m .Address4 )
157+ update := generateVMTemplateVRouterLBParams (router , m .RouterID , m . Address4 )
150158 if err := m .ctrl .VM (m .ID ).Update (update .String (), 1 ); err != nil {
151159 return fmt .Errorf ("Failed to update VM: %w" , err )
152160 }
@@ -155,10 +163,13 @@ func (m *Machine) FromTemplate(templateName string, userData *string, network *i
155163 return nil
156164}
157165
158- func generateVMTemplateVRouterLBParams (router * infrav1.ONEVirtualRouter , serverAddress string ) * goca_vm.Template {
166+ func generateVMTemplateVRouterLBParams (router * infrav1.ONEVirtualRouter , routerID int , serverAddress string ) * goca_vm.Template {
159167 update := goca_vm .NewTemplate ()
160168 if len (router .ListenerPorts ) == 0 {
161169 //defaults to kubernetes api port load balancing
170+ if routerID >= 0 {
171+ update .Add ("ONEGATE_HAPROXY_LB0_ID" , routerID )
172+ }
162173 update .Add ("ONEGATE_HAPROXY_LB0_IP" , "<ETH0_EP0>" )
163174 update .Add ("ONEGATE_HAPROXY_LB0_PORT" , "6443" )
164175 update .Add ("ONEGATE_HAPROXY_LB0_SERVER_HOST" , serverAddress )
@@ -169,6 +180,9 @@ func generateVMTemplateVRouterLBParams(router *infrav1.ONEVirtualRouter, serverA
169180 slices .Sort (router .ListenerPorts )
170181 for idx , port := range router .ListenerPorts {
171182 //NOTE: Pass ports as strings, as the template make pair method doesn't support int32 values
183+ if routerID >= 0 {
184+ update .Add (goca_vm_keys .Template (fmt .Sprintf ("ONEGATE_HAPROXY_LB%d_ID" , idx )), routerID )
185+ }
172186 update .Add (goca_vm_keys .Template (fmt .Sprintf ("ONEGATE_HAPROXY_LB%d_IP" , idx )), "<ETH0_EP0>" )
173187 update .Add (goca_vm_keys .Template (fmt .Sprintf ("ONEGATE_HAPROXY_LB%d_PORT" , idx )), strconv .Itoa (int (port )))
174188 update .Add (goca_vm_keys .Template (fmt .Sprintf ("ONEGATE_HAPROXY_LB%d_SERVER_HOST" , idx )), serverAddress )
0 commit comments