Skip to content

Commit ede112f

Browse files
committed
feat: Create bastion security group only when bastion is enabled
1 parent 913ee94 commit ede112f

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

controllers/awscluster_controller.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,12 @@ import (
5757
"sigs.k8s.io/cluster-api/util/predicates"
5858
)
5959

60-
var (
61-
awsSecurityGroupRoles = []infrav1.SecurityGroupRole{
62-
infrav1.SecurityGroupBastion,
63-
infrav1.SecurityGroupAPIServerLB,
64-
infrav1.SecurityGroupLB,
65-
infrav1.SecurityGroupControlPlane,
66-
infrav1.SecurityGroupNode,
67-
}
68-
)
60+
var defaultAWSSecurityGroupRoles = []infrav1.SecurityGroupRole{
61+
infrav1.SecurityGroupAPIServerLB,
62+
infrav1.SecurityGroupLB,
63+
infrav1.SecurityGroupControlPlane,
64+
infrav1.SecurityGroupNode,
65+
}
6966

7067
// AWSClusterReconciler reconciles a AwsCluster object.
7168
type AWSClusterReconciler struct {
@@ -103,12 +100,24 @@ func (r *AWSClusterReconciler) getNetworkService(scope scope.ClusterScope) servi
103100
return network.NewService(&scope)
104101
}
105102

103+
// securityGroupRolesForCluster returns the security group roles determined by the cluster configuration.
104+
func securityGroupRolesForCluster(scope scope.ClusterScope) []infrav1.SecurityGroupRole {
105+
roles := []infrav1.SecurityGroupRole{}
106+
// Copy to ensure we do not modify the package-level variable.
107+
copy(roles, defaultAWSSecurityGroupRoles)
108+
109+
if scope.Bastion().Enabled {
110+
roles = append(roles, infrav1.SecurityGroupBastion)
111+
}
112+
return roles
113+
}
114+
106115
// getSecurityGroupService factory func is added for testing purpose so that we can inject mocked SecurityGroupService to the AWSClusterReconciler.
107116
func (r *AWSClusterReconciler) getSecurityGroupService(scope scope.ClusterScope) services.SecurityGroupInterface {
108117
if r.securityGroupFactory != nil {
109118
return r.securityGroupFactory(scope)
110119
}
111-
return securitygroup.NewService(&scope, awsSecurityGroupRoles)
120+
return securitygroup.NewService(&scope, securityGroupRolesForCluster(scope))
112121
}
113122

114123
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsclusters,verbs=get;list;watch;create;update;patch;delete

0 commit comments

Comments
 (0)