Skip to content

Commit 84daa82

Browse files
Added support for IPv6 CIDRs in security groups
1 parent 15dba89 commit 84daa82

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

internal/alb/sg/association.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ type associationController struct {
6262
}
6363

6464
type associationConfig struct {
65-
LbPorts []int64
66-
LbInboundCIDRs []string
67-
LbExternalSGs []string
68-
AdditionalTags map[string]string
65+
LbPorts []int64
66+
LbInboundCIDRs []string
67+
LbInboundV6CIDRs []string
68+
LbExternalSGs []string
69+
AdditionalTags map[string]string
6970
}
7071

7172
func (c *associationController) Reconcile(ctx context.Context, ingress *extensions.Ingress, lbInstance *elbv2.LoadBalancer, tgGroup tg.TargetGroupGroup) error {
@@ -144,11 +145,19 @@ func (c *associationController) reconcileLbSG(ctx context.Context, ingressKey ty
144145
Description: aws.String(fmt.Sprintf("Allow ingress on port %v from %v", port, cidr)),
145146
})
146147
}
148+
ipv6Ranges := make([]*ec2.Ipv6Range, 0, len(cfg.LbInboundV6CIDRs))
149+
for _, cidr := range cfg.LbInboundV6CIDRs {
150+
ipv6Ranges = append(ipv6Ranges, &ec2.Ipv6Range{
151+
CidrIpv6: aws.String(cidr),
152+
Description: aws.String(fmt.Sprintf("Allow ingress on port %v from %v", port, cidr)),
153+
})
154+
}
147155
permission := &ec2.IpPermission{
148156
IpProtocol: aws.String("tcp"),
149157
FromPort: aws.Int64(port),
150158
ToPort: aws.Int64(port),
151159
IpRanges: ipRanges,
160+
Ipv6Ranges: ipv6Ranges,
152161
}
153162
inboundPermissions = append(inboundPermissions, permission)
154163
}
@@ -255,10 +264,11 @@ func (c *associationController) buildAssociationConfig(ctx context.Context, ingr
255264
return associationConfig{}, err
256265
}
257266
return associationConfig{
258-
LbPorts: lbPorts,
259-
LbInboundCIDRs: ingressAnnos.LoadBalancer.InboundCidrs,
260-
LbExternalSGs: lbExternalSGs,
261-
AdditionalTags: ingressAnnos.Tags.LoadBalancer,
267+
LbPorts: lbPorts,
268+
LbInboundCIDRs: ingressAnnos.LoadBalancer.InboundCidrs,
269+
LbInboundV6CIDRs: ingressAnnos.LoadBalancer.InboundV6CIDRs,
270+
LbExternalSGs: lbExternalSGs,
271+
AdditionalTags: ingressAnnos.Tags.LoadBalancer,
262272
}, nil
263273
}
264274

internal/ingress/annotations/loadbalancer/main.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Config struct {
4242
WebACLId *string
4343

4444
InboundCidrs []string
45+
InboundV6CIDRs []string
4546
Ports []PortData
4647
SecurityGroups []string
4748
Subnets []string
@@ -102,7 +103,7 @@ func (lb loadBalancer) Parse(ing parser.AnnotationInterface) (interface{}, error
102103
securityGroups := parser.GetStringSliceAnnotation("security-groups", ing)
103104
subnets := parser.GetStringSliceAnnotation("subnets", ing)
104105

105-
cidrs, err := parseCidrs(ing)
106+
v4CIDRs, v6CIDRs, err := parseCidrs(ing)
106107
if err != nil {
107108
return nil, err
108109
}
@@ -112,9 +113,10 @@ func (lb loadBalancer) Parse(ing parser.AnnotationInterface) (interface{}, error
112113
Scheme: scheme,
113114
IPAddressType: ipAddressType,
114115

115-
Attributes: attributes,
116-
InboundCidrs: cidrs,
117-
Ports: ports,
116+
Attributes: attributes,
117+
InboundCidrs: v4CIDRs,
118+
InboundV6CIDRs: v6CIDRs,
119+
Ports: ports,
118120

119121
Subnets: subnets,
120122
SecurityGroups: securityGroups,
@@ -203,7 +205,7 @@ func parsePorts(ing parser.AnnotationInterface) ([]PortData, error) {
203205
return lps, nil
204206
}
205207

206-
func parseCidrs(ing parser.AnnotationInterface) (out []string, err error) {
208+
func parseCidrs(ing parser.AnnotationInterface) (v4CIDRs, v6CIDRs []string, err error) {
207209
cidrConfig := parser.GetStringSliceAnnotation("security-group-inbound-cidrs", ing)
208210
if len(cidrConfig) != 0 {
209211
glog.Warningf("`security-group-inbound-cidrs` annotation is deprecated, use `inbound-cidrs` instead")
@@ -214,18 +216,26 @@ func parseCidrs(ing parser.AnnotationInterface) (out []string, err error) {
214216
for _, inboundCidr := range cidrConfig {
215217
ip, _, err := net.ParseCIDR(inboundCidr)
216218
if err != nil {
217-
return out, err
219+
return v4CIDRs, v6CIDRs, err
218220
}
219221

220222
if ip.To4() == nil {
221-
return out, fmt.Errorf("CIDR must use an IPv4 address: %v", inboundCidr)
223+
v6CIDRs = append(v6CIDRs, inboundCidr)
224+
} else {
225+
v4CIDRs = append(v4CIDRs, inboundCidr)
222226
}
223-
out = append(out, inboundCidr)
224227
}
225-
if len(out) == 0 {
226-
out = append(out, "0.0.0.0/0")
228+
229+
if len(v4CIDRs) == 0 {
230+
v4CIDRs = append(v4CIDRs, "0.0.0.0/0")
227231
}
228-
return out, nil
232+
233+
addrType, _ := parser.GetStringAnnotation("ip-address-type", ing)
234+
if addrType != nil && *addrType == elbv2.IpAddressTypeDualstack && len(v6CIDRs) == 0 {
235+
v6CIDRs = append(v6CIDRs, "::/0")
236+
}
237+
238+
return v4CIDRs, v6CIDRs, nil
229239
}
230240

231241
func Dummy() *Config {

0 commit comments

Comments
 (0)