Skip to content

Commit 8aa6461

Browse files
authored
Merge pull request #4228 from alexander-demicev/ingressrules
Additional ingress rules for control plane
2 parents 19bac18 + 88d7502 commit 8aa6461

14 files changed

+625
-24
lines changed

api/v1beta1/awscluster_conversion.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
5050
}
5151
dst.Spec.Partition = restored.Spec.Partition
5252

53+
for role, sg := range restored.Status.Network.SecurityGroups {
54+
dst.Status.Network.SecurityGroups[role] = sg
55+
}
56+
5357
return nil
5458
}
5559

@@ -70,6 +74,7 @@ func restoreControlPlaneLoadBalancer(restored, dst *infrav2.AWSLoadBalancerSpec)
7074
dst.LoadBalancerType = restored.LoadBalancerType
7175
dst.DisableHostsRewrite = restored.DisableHostsRewrite
7276
dst.PreserveClientIP = restored.PreserveClientIP
77+
dst.AdditionalIngressRules = restored.AdditionalIngressRules
7378
}
7479

7580
// ConvertFrom converts the v1beta1 AWSCluster receiver to a v1beta1 AWSCluster.

api/v1beta1/conversion.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,7 @@ func Convert_v1beta2_LoadBalancer_To_v1beta1_ClassicELB(in *v1beta2.LoadBalancer
7878
out.SubnetIDs = in.SubnetIDs
7979
return nil
8080
}
81+
82+
func Convert_v1beta2_IngressRule_To_v1beta1_IngressRule(in *v1beta2.IngressRule, out *IngressRule, s conversion.Scope) error {
83+
return autoConvert_v1beta2_IngressRule_To_v1beta1_IngressRule(in, out, s)
84+
}

api/v1beta1/zz_generated.conversion.go

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

api/v1beta2/awscluster_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ type AWSLoadBalancerSpec struct {
208208
// +optional
209209
AdditionalSecurityGroups []string `json:"additionalSecurityGroups,omitempty"`
210210

211+
// AdditionalIngressRules sets the additional ingress rules for the control plane load balancer. If no source security group ids are specified, the
212+
// default control plane security group will be used.
213+
// +optional
214+
AdditionalIngressRules []IngressRule `json:"additionalIngressRules,omitempty"`
215+
211216
// LoadBalancerType sets the type for a load balancer. The default type is classic.
212217
// +kubebuilder:default=classic
213218
// +kubebuilder:validation:Enum:=classic;elb;alb;nlb

api/v1beta2/awscluster_webhook.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (r *AWSCluster) ValidateCreate() error {
5656
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
5757
allErrs = append(allErrs, r.Spec.S3Bucket.Validate()...)
5858
allErrs = append(allErrs, r.validateNetwork()...)
59+
allErrs = append(allErrs, r.validateAdditionalIngressRules()...)
5960

6061
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
6162
}
@@ -190,3 +191,19 @@ func (r *AWSCluster) validateNetwork() field.ErrorList {
190191
}
191192
return allErrs
192193
}
194+
195+
func (r *AWSCluster) validateAdditionalIngressRules() field.ErrorList {
196+
var allErrs field.ErrorList
197+
198+
if r.Spec.ControlPlaneLoadBalancer == nil {
199+
return allErrs
200+
}
201+
202+
for _, rule := range r.Spec.ControlPlaneLoadBalancer.AdditionalIngressRules {
203+
if (rule.CidrBlocks != nil || rule.IPv6CidrBlocks != nil) && (rule.SourceSecurityGroupIDs != nil || rule.SourceSecurityGroupRoles != nil) {
204+
allErrs = append(allErrs, field.Invalid(field.NewPath("additionalIngressRules"), r.Spec.ControlPlaneLoadBalancer.AdditionalIngressRules, "CIDR blocks and security group IDs or security group roles cannot be used together"))
205+
}
206+
}
207+
208+
return allErrs
209+
}

api/v1beta2/awscluster_webhook_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,90 @@ func TestAWSClusterValidateCreate(t *testing.T) {
251251
},
252252
wantErr: true,
253253
},
254+
{
255+
name: "rejects additional ingress rules with cidr block and source security group id",
256+
cluster: &AWSCluster{
257+
Spec: AWSClusterSpec{
258+
ControlPlaneLoadBalancer: &AWSLoadBalancerSpec{
259+
AdditionalIngressRules: []IngressRule{
260+
{
261+
Protocol: SecurityGroupProtocolTCP,
262+
CidrBlocks: []string{"test"},
263+
SourceSecurityGroupIDs: []string{"test"},
264+
},
265+
},
266+
},
267+
},
268+
},
269+
wantErr: true,
270+
},
271+
{
272+
name: "rejects additional ingress rules with cidr block and source security group id and role",
273+
cluster: &AWSCluster{
274+
Spec: AWSClusterSpec{
275+
ControlPlaneLoadBalancer: &AWSLoadBalancerSpec{
276+
AdditionalIngressRules: []IngressRule{
277+
{
278+
Protocol: SecurityGroupProtocolTCP,
279+
IPv6CidrBlocks: []string{"test"},
280+
SourceSecurityGroupIDs: []string{"test"},
281+
SourceSecurityGroupRoles: []SecurityGroupRole{SecurityGroupBastion},
282+
},
283+
},
284+
},
285+
},
286+
},
287+
wantErr: true,
288+
},
289+
{
290+
name: "accepts additional ingress rules with cidr block",
291+
cluster: &AWSCluster{
292+
Spec: AWSClusterSpec{
293+
ControlPlaneLoadBalancer: &AWSLoadBalancerSpec{
294+
AdditionalIngressRules: []IngressRule{
295+
{
296+
Protocol: SecurityGroupProtocolTCP,
297+
CidrBlocks: []string{"test"},
298+
},
299+
},
300+
},
301+
},
302+
},
303+
wantErr: false,
304+
},
305+
{
306+
name: "accepts additional ingress rules with source security group role",
307+
cluster: &AWSCluster{
308+
Spec: AWSClusterSpec{
309+
ControlPlaneLoadBalancer: &AWSLoadBalancerSpec{
310+
AdditionalIngressRules: []IngressRule{
311+
{
312+
Protocol: SecurityGroupProtocolTCP,
313+
SourceSecurityGroupRoles: []SecurityGroupRole{SecurityGroupBastion},
314+
},
315+
},
316+
},
317+
},
318+
},
319+
wantErr: false,
320+
},
321+
{
322+
name: "accepts additional ingress rules with source security group id and role",
323+
cluster: &AWSCluster{
324+
Spec: AWSClusterSpec{
325+
ControlPlaneLoadBalancer: &AWSLoadBalancerSpec{
326+
AdditionalIngressRules: []IngressRule{
327+
{
328+
Protocol: SecurityGroupProtocolTCP,
329+
SourceSecurityGroupIDs: []string{"test"},
330+
SourceSecurityGroupRoles: []SecurityGroupRole{SecurityGroupBastion},
331+
},
332+
},
333+
},
334+
},
335+
},
336+
wantErr: false,
337+
},
254338
}
255339
for _, tt := range tests {
256340
t.Run(tt.name, func(t *testing.T) {

api/v1beta2/network_types.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ type RouteTable struct {
462462
}
463463

464464
// SecurityGroupRole defines the unique role of a security group.
465+
// +kubebuilder:validation:Enum=bastion;node;controlplane;apiserver-lb;lb;node-eks-additional
465466
type SecurityGroupRole string
466467

467468
var (
@@ -530,10 +531,15 @@ var (
530531

531532
// IngressRule defines an AWS ingress rule for security groups.
532533
type IngressRule struct {
533-
Description string `json:"description"`
534-
Protocol SecurityGroupProtocol `json:"protocol"`
535-
FromPort int64 `json:"fromPort"`
536-
ToPort int64 `json:"toPort"`
534+
// Description provides extended information about the ingress rule.
535+
Description string `json:"description"`
536+
// Protocol is the protocol for the ingress rule. Accepted values are "-1" (all), "4" (IP in IP),"tcp", "udp", "icmp", and "58" (ICMPv6).
537+
// +kubebuilder:validation:Enum="-1";"4";tcp;udp;icmp;"58"
538+
Protocol SecurityGroupProtocol `json:"protocol"`
539+
// FromPort is the start of port range.
540+
FromPort int64 `json:"fromPort"`
541+
// ToPort is the end of port range.
542+
ToPort int64 `json:"toPort"`
537543

538544
// List of CIDR blocks to allow access from. Cannot be specified with SourceSecurityGroupID.
539545
// +optional
@@ -546,6 +552,11 @@ type IngressRule struct {
546552
// The security group id to allow access from. Cannot be specified with CidrBlocks.
547553
// +optional
548554
SourceSecurityGroupIDs []string `json:"sourceSecurityGroupIds,omitempty"`
555+
556+
// The security group role to allow access from. Cannot be specified with CidrBlocks.
557+
// The field will be combined with source security group IDs if specified.
558+
// +optional
559+
SourceSecurityGroupRoles []SecurityGroupRole `json:"sourceSecurityGroupRoles,omitempty"`
549560
}
550561

551562
// String returns a string representation of the ingress rule.

api/v1beta2/zz_generated.deepcopy.go

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

0 commit comments

Comments
 (0)