Skip to content

Commit 8d40447

Browse files
authored
Merge pull request #700 from zoltan0907/master
🐛 Add SecurityGroup to Loadbalancerport when not using octavia
2 parents 4a15cdd + f2e8fac commit 8d40447

File tree

4 files changed

+98
-8
lines changed

4 files changed

+98
-8
lines changed

controllers/openstackcluster_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,18 +422,18 @@ func (r *OpenStackClusterReconciler) reconcileNetworkComponents(log logr.Logger,
422422
}
423423
}
424424

425+
err = networkingService.ReconcileSecurityGroups(clusterName, openStackCluster)
426+
if err != nil {
427+
return errors.Errorf("failed to reconcile security groups: %v", err)
428+
}
429+
425430
if openStackCluster.Spec.ManagedAPIServerLoadBalancer {
426431
err = loadBalancerService.ReconcileLoadBalancer(clusterName, openStackCluster)
427432
if err != nil {
428433
return errors.Errorf("failed to reconcile load balancer: %v", err)
429434
}
430435
}
431436

432-
err = networkingService.ReconcileSecurityGroups(clusterName, openStackCluster)
433-
if err != nil {
434-
return errors.Errorf("failed to reconcile security groups: %v", err)
435-
}
436-
437437
return nil
438438
}
439439

pkg/cloud/services/loadbalancer/loadbalancer.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ package loadbalancer
1919
import (
2020
"errors"
2121
"fmt"
22-
"github.com/go-logr/logr"
2322
"time"
2423

24+
"github.com/go-logr/logr"
25+
"k8s.io/apimachinery/pkg/util/wait"
26+
2527
"github.com/gophercloud/gophercloud"
2628
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners"
2729
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers"
2830
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors"
2931
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
3032
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
31-
"k8s.io/apimachinery/pkg/util/wait"
33+
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
34+
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
3235
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha3"
36+
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking"
3337
"sigs.k8s.io/cluster-api-provider-openstack/pkg/record"
3438
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
3539
"sigs.k8s.io/cluster-api/util"
@@ -61,6 +65,13 @@ func (s *Service) ReconcileLoadBalancer(clusterName string, openStackCluster *in
6165
return err
6266
}
6367

68+
if !openStackCluster.Spec.UseOctavia {
69+
err := s.assignNeutronLbaasAPISecGroup(clusterName, lb)
70+
if err != nil {
71+
return err
72+
}
73+
}
74+
6475
fp, err := getOrCreateFloatingIP(s.networkingClient, openStackCluster, openStackCluster.Spec.ControlPlaneEndpoint.Host)
6576
if err != nil {
6677
return err
@@ -166,6 +177,36 @@ func (s *Service) ReconcileLoadBalancer(clusterName string, openStackCluster *in
166177
return nil
167178
}
168179

180+
func (s *Service) assignNeutronLbaasAPISecGroup(clusterName string, lb *loadbalancers.LoadBalancer) error {
181+
neutronLbaasSecGroupName := networking.GetNeutronLBaasSecGroupName(clusterName)
182+
listOpts := groups.ListOpts{
183+
Name: neutronLbaasSecGroupName,
184+
}
185+
allPages, err := groups.List(s.networkingClient, listOpts).AllPages()
186+
if err != nil {
187+
return err
188+
}
189+
190+
neutronLbaasGroups, err := groups.ExtractGroups(allPages)
191+
if err != nil {
192+
return err
193+
}
194+
195+
if len(neutronLbaasGroups) != 1 {
196+
return fmt.Errorf("error found %v securitygroups with name %v", len(neutronLbaasGroups), neutronLbaasSecGroupName)
197+
}
198+
199+
updateOpts := ports.UpdateOpts{
200+
SecurityGroups: &[]string{neutronLbaasGroups[0].ID},
201+
}
202+
203+
_, err = ports.Update(s.networkingClient, lb.VipPortID, updateOpts).Extract()
204+
if err != nil {
205+
return err
206+
}
207+
return nil
208+
}
209+
169210
func (s *Service) ReconcileLoadBalancerMember(clusterName string, machine *clusterv1.Machine, openStackMachine *infrav1.OpenStackMachine, openStackCluster *infrav1.OpenStackCluster, ip string) error {
170211
if !util.IsControlPlaneMachine(machine) {
171212
return nil
@@ -186,7 +227,6 @@ func (s *Service) ReconcileLoadBalancerMember(clusterName string, machine *clust
186227

187228
lbID := openStackCluster.Status.Network.APIServerLoadBalancer.ID
188229
subnetID := openStackCluster.Status.Network.Subnet.ID
189-
190230
portList := []int{int(openStackCluster.Spec.ControlPlaneEndpoint.Port)}
191231
portList = append(portList, openStackCluster.Spec.APIServerLoadBalancerAdditionalPorts...)
192232
for _, port := range portList {

pkg/cloud/services/loadbalancer/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package loadbalancer
1818

1919
import (
2020
"fmt"
21+
2122
"github.com/go-logr/logr"
2223

2324
"github.com/gophercloud/gophercloud/openstack"

pkg/cloud/services/networking/securitygroups.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const (
3131
workerSuffix string = "worker"
3232
bastionSuffix string = "bastion"
3333
remoteGroupIDSelf string = "self"
34+
neutronLbaasSuffix string = "lbaas"
3435
)
3536

3637
var defaultRules = []infrav1.SecurityGroupRule{
@@ -74,6 +75,11 @@ func (s *Service) ReconcileSecurityGroups(clusterName string, openStackCluster *
7475
secGroupNames[bastionSuffix] = secBastionGroupName
7576
}
7677

78+
if openStackCluster.Spec.ManagedAPIServerLoadBalancer && !openStackCluster.Spec.UseOctavia {
79+
secLbaasGroupName := fmt.Sprintf("%s-cluster-%s-secgroup-%s", secGroupPrefix, clusterName, neutronLbaasSuffix)
80+
secGroupNames[neutronLbaasSuffix] = secLbaasGroupName
81+
}
82+
7783
//create security groups first, because desired rules use group ids.
7884
for _, v := range secGroupNames {
7985
if err := s.createSecurityGroupIfNotExists(openStackCluster, v); err != nil {
@@ -328,6 +334,44 @@ func (s *Service) generateDesiredSecGroups(secGroupNames map[string]string, open
328334
}
329335
}
330336

337+
if openStackCluster.Spec.ManagedAPIServerLoadBalancer && !openStackCluster.Spec.UseOctavia {
338+
neutronLbaasRules := append(
339+
[]infrav1.SecurityGroupRule{
340+
{
341+
Description: "Kubernetes API",
342+
Direction: "ingress",
343+
EtherType: "IPv4",
344+
PortRangeMin: 6443,
345+
PortRangeMax: 6443,
346+
Protocol: "tcp",
347+
RemoteIPPrefix: "0.0.0.0/0",
348+
},
349+
},
350+
defaultRules...,
351+
)
352+
if openStackCluster.Spec.APIServerLoadBalancerAdditionalPorts != nil {
353+
for _, value := range openStackCluster.Spec.APIServerLoadBalancerAdditionalPorts {
354+
neutronLbaasRules = append(neutronLbaasRules,
355+
[]infrav1.SecurityGroupRule{
356+
{
357+
Description: "APIServerLoadBalancerAdditionalPorts",
358+
Direction: "ingress",
359+
EtherType: "IPv4",
360+
PortRangeMin: value,
361+
PortRangeMax: value,
362+
Protocol: "tcp",
363+
RemoteIPPrefix: "0.0.0.0/0",
364+
},
365+
}...,
366+
)
367+
}
368+
}
369+
desiredSecGroups[neutronLbaasSuffix] = infrav1.SecurityGroup{
370+
Name: secGroupNames[neutronLbaasSuffix],
371+
Rules: neutronLbaasRules,
372+
}
373+
}
374+
331375
desiredSecGroups[controlPlaneSuffix] = infrav1.SecurityGroup{
332376
Name: secGroupNames[controlPlaneSuffix],
333377
Rules: controlPlaneRules,
@@ -531,3 +575,8 @@ func convertOSSecGroupRuleToConfigSecGroupRule(osSecGroupRule rules.SecGroupRule
531575
RemoteIPPrefix: osSecGroupRule.RemoteIPPrefix,
532576
}
533577
}
578+
579+
// GetNeutronLBaasSecGroupName export NeutronLBaasSecGroupName
580+
func GetNeutronLBaasSecGroupName(clusterName string) string {
581+
return fmt.Sprintf("%s-cluster-%s-secgroup-%s", secGroupPrefix, clusterName, neutronLbaasSuffix)
582+
}

0 commit comments

Comments
 (0)