Skip to content

Commit 8a563a0

Browse files
authored
VPC: Extend support for SG's (#1989)
Add support to reconcile SecurityGroups and SecurityGroupRules for VPC extended Infrastructure support. Related: #1896
1 parent 622fcf4 commit 8a563a0

File tree

6 files changed

+626
-6
lines changed

6 files changed

+626
-6
lines changed

cloud/scope/powervs_cluster.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,8 +1615,10 @@ func (s *PowerVSClusterScope) validateVPCSecurityGroup(securityGroup infrav1beta
16151615
}
16161616
} else {
16171617
securityGroupDet, err = s.IBMVPCClient.GetSecurityGroupByName(*securityGroup.Name)
1618-
if err != nil && err.Error() != vpc.SecurityGroupByNameNotFound(*securityGroup.Name).Error() {
1619-
return nil, nil, err
1618+
if err != nil {
1619+
if _, ok := err.(*vpc.SecurityGroupByNameNotFound); !ok {
1620+
return nil, nil, err
1621+
}
16201622
}
16211623
if securityGroupDet == nil {
16221624
return nil, nil, nil

cloud/scope/vpc_cluster.go

Lines changed: 578 additions & 1 deletion
Large diffs are not rendered by default.

controllers/ibmvpccluster_controller.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,19 @@ func (r *IBMVPCClusterReconciler) reconcileCluster(clusterScope *scope.VPCCluste
274274
clusterScope.Info("Reconciliation of VPC Subnets complete")
275275
conditions.MarkTrue(clusterScope.IBMVPCCluster, infrav1beta2.VPCSubnetReadyCondition)
276276

277+
// Reconcile the cluster's Security Groups (and Security Group Rules)
278+
clusterScope.Info("Reconciling Security Groups")
279+
if requeue, err := clusterScope.ReconcileSecurityGroups(); err != nil {
280+
clusterScope.Error(err, "failed to reconcile Security Groups")
281+
conditions.MarkFalse(clusterScope.IBMVPCCluster, infrav1beta2.VPCSecurityGroupReadyCondition, infrav1beta2.VPCSecurityGroupReconciliationFailedReason, capiv1beta1.ConditionSeverityError, "%s", err.Error())
282+
return reconcile.Result{}, err
283+
} else if requeue {
284+
clusterScope.Info("Security Groups creation is pending, requeueing")
285+
return reconcile.Result{RequeueAfter: 15 * time.Second}, nil
286+
}
287+
clusterScope.Info("Reconciliation of Security Groups complete")
288+
conditions.MarkTrue(clusterScope.IBMVPCCluster, infrav1beta2.VPCSecurityGroupReadyCondition)
289+
277290
// TODO(cjschaef): add remaining resource reconciliation.
278291

279292
// Mark cluster as ready.

pkg/cloud/services/vpc/mock/vpc_generated.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cloud/services/vpc/service.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ import (
2626
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
2727
)
2828

29-
// SecurityGroupByNameNotFound returns an appropriate error when security group by name not found.
30-
var SecurityGroupByNameNotFound = func(name string) error { return fmt.Errorf("failed to find security group by name '%s'", name) }
29+
// SecurityGroupByNameNotFound represents an error when security group is not found by name.
30+
type SecurityGroupByNameNotFound struct {
31+
Name string
32+
}
33+
34+
func (s *SecurityGroupByNameNotFound) Error() string {
35+
return fmt.Sprintf("failed to find security group by name: %s", s.Name)
36+
}
3137

3238
// Service holds the VPC Service specific information.
3339
type Service struct {
@@ -472,14 +478,19 @@ func (s *Service) GetSecurityGroupByName(name string) (*vpcv1.SecurityGroup, err
472478
}
473479
}
474480

475-
return nil, SecurityGroupByNameNotFound(name)
481+
return nil, &SecurityGroupByNameNotFound{Name: name}
476482
}
477483

478484
// GetSecurityGroupRule gets a specific security group rule.
479485
func (s *Service) GetSecurityGroupRule(options *vpcv1.GetSecurityGroupRuleOptions) (vpcv1.SecurityGroupRuleIntf, *core.DetailedResponse, error) {
480486
return s.vpcService.GetSecurityGroupRule(options)
481487
}
482488

489+
// ListSecurityGroupRules returns a list of security group rules.
490+
func (s *Service) ListSecurityGroupRules(options *vpcv1.ListSecurityGroupRulesOptions) (*vpcv1.SecurityGroupRuleCollection, *core.DetailedResponse, error) {
491+
return s.vpcService.ListSecurityGroupRules(options)
492+
}
493+
483494
// GetVPCZonesByRegion gets the VPC availability zones for a specific IBM Cloud region.
484495
func (s *Service) GetVPCZonesByRegion(region string) ([]string, error) {
485496
zones := make([]string, 0)

pkg/cloud/services/vpc/vpc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@ type Vpc interface {
6969
GetSecurityGroup(options *vpcv1.GetSecurityGroupOptions) (*vpcv1.SecurityGroup, *core.DetailedResponse, error)
7070
GetSecurityGroupByName(name string) (*vpcv1.SecurityGroup, error)
7171
GetSecurityGroupRule(options *vpcv1.GetSecurityGroupRuleOptions) (vpcv1.SecurityGroupRuleIntf, *core.DetailedResponse, error)
72+
ListSecurityGroupRules(options *vpcv1.ListSecurityGroupRulesOptions) (*vpcv1.SecurityGroupRuleCollection, *core.DetailedResponse, error)
7273
GetVPCZonesByRegion(region string) ([]string, error)
7374
}

0 commit comments

Comments
 (0)