Skip to content

Commit 8cc0130

Browse files
authored
B #37: Set LB IDs to CP/VR ID to avoid LB conflicts (#38)
1 parent b166e36 commit 8cc0130

7 files changed

Lines changed: 104 additions & 40 deletions

File tree

internal/cloud/cleanup.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ type Cleanup struct {
2929
clusterName string
3030
}
3131

32-
func NewCleanup(cc *Clients, clusterName string) *Cleanup {
33-
return &Cleanup{
34-
ctrl: goca.NewController(cc.RPC2),
35-
clusterName: clusterName,
32+
func NewCleanup(clients *Clients, clusterName string) (*Cleanup, error) {
33+
if clients == nil {
34+
return nil, fmt.Errorf("clients reference is nil")
3635
}
36+
37+
return &Cleanup{ctrl: goca.NewController(clients.RPC2), clusterName: clusterName}, nil
3738
}
3839

3940
func (c *Cleanup) getVirtualRouterName() string {

internal/cloud/images.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ type Images struct {
2727
ctrl *goca.Controller
2828
}
2929

30-
func NewImages(cc *Clients) *Images {
31-
return &Images{ctrl: goca.NewController(cc.RPC2)}
30+
func NewImages(clients *Clients) (*Images, error) {
31+
if clients == nil {
32+
return nil, fmt.Errorf("clients reference is nil")
33+
}
34+
35+
return &Images{ctrl: goca.NewController(clients.RPC2)}, nil
3236
}
3337

3438
func (i *Images) CreateImage(imageName, imageContent string) error {

internal/cloud/machine.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4041
type 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-
6166
func (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)

internal/cloud/router.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,29 @@ type Router struct {
3636
FloatingIPs []string
3737
}
3838

39-
func NewRouter(cc *Clients, name string, maybeReplicas *int32) *Router {
40-
replicas := 1
41-
if maybeReplicas != nil {
42-
replicas = int(*maybeReplicas)
39+
type RouterOption func(*Router)
40+
41+
func WithRouterName(name string) RouterOption {
42+
return func(r *Router) {
43+
r.Name = name
44+
}
45+
}
46+
func WithRouterReplicas(replicas int) RouterOption {
47+
return func(r *Router) {
48+
r.Replicas = replicas
49+
}
50+
}
51+
52+
func NewRouter(clients *Clients, options ...RouterOption) (*Router, error) {
53+
if clients == nil {
54+
return nil, fmt.Errorf("clients reference is nil")
4355
}
44-
return &Router{
45-
ctrl: goca.NewController(cc.RPC2),
46-
ID: -1,
47-
Name: name,
48-
Replicas: replicas,
56+
57+
r := &Router{ctrl: goca.NewController(clients.RPC2), ID: -1, Replicas: 1}
58+
for _, option := range options {
59+
option(r)
4960
}
61+
return r, nil
5062
}
5163

5264
func (r *Router) Exists() bool {
@@ -79,7 +91,10 @@ func (r *Router) ByName(vrName string) error {
7991
return r.ByID(vrID)
8092
}
8193

82-
func (r *Router) FromTemplate(virtualRouter *infrav1.ONEVirtualRouter, publicNetwork, privateNetwork *infrav1.ONEVirtualNetwork) error {
94+
func (r *Router) FromTemplate(
95+
virtualRouter *infrav1.ONEVirtualRouter,
96+
publicNetwork, privateNetwork *infrav1.ONEVirtualNetwork) error {
97+
8398
if r.Exists() {
8499
return nil
85100
}

internal/cloud/templates.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ type Templates struct {
2727
clusterUID string
2828
}
2929

30-
func NewTemplates(cc *Clients, clusterUID string) *Templates {
31-
return &Templates{
32-
ctrl: goca.NewController(cc.RPC2),
33-
clusterUID: clusterUID,
30+
func NewTemplates(clients *Clients, clusterUID string) (*Templates, error) {
31+
if clients == nil {
32+
return nil, fmt.Errorf("clients reference is nil")
3433
}
34+
35+
return &Templates{ctrl: goca.NewController(clients.RPC2), clusterUID: clusterUID}, nil
3536
}
3637

3738
func (t *Templates) CreateTemplate(templateName, templateContent string) error {

internal/controller/onecluster_controller.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,35 @@ func (r *ONEClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
113113
if err != nil {
114114
return ctrl.Result{}, err
115115
}
116-
117116
if len(oneCluster.Spec.Images) > 0 {
118-
externalImages = cloud.NewImages(cloudClients)
117+
externalImages, err = cloud.NewImages(cloudClients)
118+
if err != nil {
119+
return ctrl.Result{}, fmt.Errorf("failed to initialize cloud images: %w", err)
120+
}
119121
}
120122
if len(oneCluster.Spec.Templates) > 0 {
121-
externalTemplates = cloud.NewTemplates(cloudClients, string(oneCluster.UID))
123+
externalTemplates, err = cloud.NewTemplates(cloudClients, string(oneCluster.UID))
124+
if err != nil {
125+
return ctrl.Result{}, fmt.Errorf("failed to initialize cloud templates: %w", err)
126+
}
122127
}
123128
if oneCluster.Spec.VirtualRouter != nil {
124-
vrName := fmt.Sprintf("%s-cp", oneCluster.Name)
125-
replicas := oneCluster.Spec.VirtualRouter.Replicas
126-
externalRouter = cloud.NewRouter(cloudClients, vrName, replicas)
127-
externalCleanup = cloud.NewCleanup(cloudClients, oneCluster.Name)
129+
routerOpts := []cloud.RouterOption{
130+
cloud.WithRouterName(fmt.Sprintf("%s-cp", oneCluster.Name)),
131+
}
132+
if oneCluster.Spec.VirtualRouter.Replicas != nil {
133+
routerOpts = append(routerOpts,
134+
cloud.WithRouterReplicas(int(*oneCluster.Spec.VirtualRouter.Replicas)),
135+
)
136+
}
137+
externalRouter, err = cloud.NewRouter(cloudClients, routerOpts...)
138+
if err != nil {
139+
return ctrl.Result{}, fmt.Errorf("failed to initialize cloud router: %w", err)
140+
}
141+
externalCleanup, err = cloud.NewCleanup(cloudClients, oneCluster.Name)
142+
if err != nil {
143+
return ctrl.Result{}, fmt.Errorf("failed to initialize cloud cleanup: %w", err)
144+
}
128145
}
129146
}
130147

internal/controller/onemachine_controller.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,20 @@ func (r *ONEMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
148148
if err != nil {
149149
return ctrl.Result{}, err
150150
}
151-
name := generateExternalMachineName(machine, oneMachine)
152-
externalMachine, err := cloud.NewMachine(cloudClients, cloud.WithMachineName(name))
151+
machineOpts := []cloud.MachineOption{
152+
cloud.WithMachineName(generateExternalMachineName(machine, oneMachine)),
153+
}
154+
if oneCluster.Spec.VirtualRouter != nil {
155+
externalRouter, err := cloud.NewRouter(cloudClients)
156+
if err != nil {
157+
return ctrl.Result{}, fmt.Errorf("failed to initialize cloud router: %w", err)
158+
}
159+
externalRouter.ByName(fmt.Sprintf("%s-cp", oneCluster.Name))
160+
if externalRouter.Exists() {
161+
machineOpts = append(machineOpts, cloud.WithMachineRouterID(externalRouter.ID))
162+
}
163+
}
164+
externalMachine, err := cloud.NewMachine(cloudClients, machineOpts...)
153165
if err != nil {
154166
return ctrl.Result{}, fmt.Errorf("failed to initialize cloud machine: %w", err)
155167
}

0 commit comments

Comments
 (0)