Skip to content

Commit 37381a3

Browse files
authored
Merge pull request #1232 from luckyganesh/master
Add support private ipv4 addresses annotation for NLB
2 parents 83eae93 + e5ba2d2 commit 37381a3

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

pkg/providers/v1/aws.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ const ServiceAnnotationLoadBalancerTargetGroupAttributes = "service.beta.kuberne
229229
// static IP addresses for the NLB. Only supported on elbv2 (NLB)
230230
const ServiceAnnotationLoadBalancerEIPAllocations = "service.beta.kubernetes.io/aws-load-balancer-eip-allocations"
231231

232+
// ServiceAnnotationLoadBalancerPrivateIPv4Addresses is the annotation used on the
233+
// service to specify a comma separated list of Private IPv4 addresses to use as
234+
// static IP addresses for the NLB. Only supported on elbv2 (NLB)
235+
const ServiceAnnotationLoadBalancerPrivateIPv4Addresses = "service.beta.kubernetes.io/aws-load-balancer-private-ipv4-addresses"
236+
232237
// ServiceAnnotationLoadBalancerTargetNodeLabels is the annotation used on the service
233238
// to specify a comma-separated list of key-value pairs which will be used to select
234239
// the target nodes for the load balancer

pkg/providers/v1/aws_loadbalancer.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,17 @@ func (c *Cloud) ensureLoadBalancerv2(ctx context.Context, namespacedName types.N
177177
}
178178
}
179179

180+
var privateIPv4Addresses []string
181+
if privateIPList, present := annotations[ServiceAnnotationLoadBalancerPrivateIPv4Addresses]; present {
182+
privateIPv4Addresses = strings.Split(privateIPList, ",")
183+
if len(privateIPv4Addresses) != len(discoveredSubnetIDs) {
184+
return nil, fmt.Errorf("error creating load balancer: Must have same number of Private IPv4Addresses (%d) and SubnetIDs (%d)", len(privateIPv4Addresses), len(discoveredSubnetIDs))
185+
}
186+
}
187+
180188
// We are supposed to specify one subnet per AZ.
181189
// TODO: What happens if we have more than one subnet per AZ?
182-
createRequest.SubnetMappings = createSubnetMappings(discoveredSubnetIDs, allocationIDs)
190+
createRequest.SubnetMappings = createSubnetMappings(discoveredSubnetIDs, allocationIDs, privateIPv4Addresses)
183191

184192
// Enable provisioning NLB with security groups when enabled.
185193
createRequest.SecurityGroups = securityGroups
@@ -1469,14 +1477,17 @@ func elbListenersAreEqual(actual, expected elbtypes.Listener) bool {
14691477
return true
14701478
}
14711479

1472-
func createSubnetMappings(subnetIDs []string, allocationIDs []string) []elbv2types.SubnetMapping {
1480+
func createSubnetMappings(subnetIDs []string, allocationIDs []string, privateIPv4Addresses []string) []elbv2types.SubnetMapping {
14731481
response := []elbv2types.SubnetMapping{}
14741482

14751483
for index, id := range subnetIDs {
14761484
sm := elbv2types.SubnetMapping{SubnetId: aws.String(id)}
14771485
if len(allocationIDs) > 0 {
14781486
sm.AllocationId = aws.String(allocationIDs[index])
14791487
}
1488+
if len(privateIPv4Addresses) > 0 {
1489+
sm.PrivateIPv4Address = aws.String(privateIPv4Addresses[index])
1490+
}
14801491
response = append(response, sm)
14811492
}
14821493

pkg/providers/v1/aws_loadbalancer_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,70 @@ func TestCloud_buildTargetGroupAttributes(t *testing.T) {
13121312
}
13131313
}
13141314

1315+
func TestCreateSubnetMappings(t *testing.T) {
1316+
tests := []struct {
1317+
name string
1318+
subnetIDs []string
1319+
allocationIDs []string
1320+
privateIPv4Addresses []string
1321+
expectedSubnetMappings []elbv2types.SubnetMapping
1322+
}{
1323+
{
1324+
name: "Add allocation ids",
1325+
subnetIDs: []string{"subnet-1234", "subnet-3456"},
1326+
allocationIDs: []string{"eipalloc-2345", "eipalloc-4567"},
1327+
privateIPv4Addresses: []string{},
1328+
expectedSubnetMappings: []elbv2types.SubnetMapping{
1329+
{
1330+
SubnetId: aws.String("subnet-1234"),
1331+
AllocationId: aws.String("eipalloc-2345"),
1332+
},
1333+
{
1334+
SubnetId: aws.String("subnet-3456"),
1335+
AllocationId: aws.String("eipalloc-4567"),
1336+
},
1337+
},
1338+
},
1339+
{
1340+
name: "Add Private ip address",
1341+
subnetIDs: []string{"subnet-1234", "subnet-3456"},
1342+
allocationIDs: []string{},
1343+
privateIPv4Addresses: []string{"10.1.2.3", "10.2.3.4"},
1344+
expectedSubnetMappings: []elbv2types.SubnetMapping{
1345+
{
1346+
SubnetId: aws.String("subnet-1234"),
1347+
PrivateIPv4Address: aws.String("10.1.2.3"),
1348+
},
1349+
{
1350+
SubnetId: aws.String("subnet-3456"),
1351+
PrivateIPv4Address: aws.String("10.2.3.4"),
1352+
},
1353+
},
1354+
},
1355+
{
1356+
name: "No private ips and allocation ids",
1357+
subnetIDs: []string{"subnet-1234", "subnet-3456"},
1358+
allocationIDs: []string{},
1359+
privateIPv4Addresses: []string{},
1360+
expectedSubnetMappings: []elbv2types.SubnetMapping{
1361+
{
1362+
SubnetId: aws.String("subnet-1234"),
1363+
},
1364+
{
1365+
SubnetId: aws.String("subnet-3456"),
1366+
},
1367+
},
1368+
},
1369+
}
1370+
1371+
for _, tt := range tests {
1372+
t.Run(tt.name, func(t *testing.T) {
1373+
actualSubnetMappings := createSubnetMappings(tt.subnetIDs, tt.allocationIDs, tt.privateIPv4Addresses)
1374+
assert.Equal(t, tt.expectedSubnetMappings, actualSubnetMappings)
1375+
})
1376+
}
1377+
}
1378+
13151379
// Unit test generated by Cursor AI
13161380
func TestGetKeyValuePropertiesFromAnnotation_TargetGroupAttributes(t *testing.T) {
13171381
tests := []struct {

0 commit comments

Comments
 (0)