Skip to content

Commit 09ef555

Browse files
committed
feat: expose max allocatable IPs thru grpc server
1 parent 4c93424 commit 09ef555

7 files changed

Lines changed: 312 additions & 93 deletions

File tree

pkg/ipamd/ipamd.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ type IPAMContext struct {
224224
manageENIsNonScheduleable bool
225225
useSubnetDiscovery bool
226226
networkClient networkutils.NetworkAPIs
227+
maxIPs int
227228
maxIPsPerENI int
228229
maxENI int
229230
maxPrefixesPerENI int
@@ -2602,6 +2603,27 @@ func (c *IPAMContext) getPrefixesNeeded(networkCard int) int {
26022603
return toAllocate
26032604
}
26042605

2606+
// getMaxIPs returns the maximum number of ipv4 addresses allocatable given the context
2607+
func (c *IPAMContext) getMaxIPs() (int, error) {
2608+
if c.maxIPs <= 0 {
2609+
enisForPods, err := c.getMaxENI()
2610+
if err != nil {
2611+
return 0, err
2612+
}
2613+
if c.useCustomNetworking {
2614+
enisForPods = enisForPods - 1
2615+
}
2616+
2617+
ipv4Limit, _, err := c.GetIPv4Limit()
2618+
if err != nil {
2619+
return 0, err
2620+
}
2621+
c.maxIPs = enisForPods * ipv4Limit
2622+
}
2623+
2624+
return c.maxIPs, nil
2625+
}
2626+
26052627
func (c *IPAMContext) initENIAndIPLimits() (err error) {
26062628

26072629
nodeMaxENI, err := c.getMaxENI()

pkg/ipamd/ipamd_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,56 @@ func TestGetWarmIPTargetStateWithPDenabled(t *testing.T) {
13261326
assert.Equal(t, 0, over)
13271327
}
13281328

1329+
func TestGetMaxIPs(t *testing.T) {
1330+
m := setup(t)
1331+
defer m.ctrl.Finish()
1332+
1333+
for _, testCase := range []struct {
1334+
enablePrefixDelegation bool
1335+
useCustomNetworking bool
1336+
eniLimit int
1337+
eniIpv4Limit int
1338+
expectedMaxIps int
1339+
}{
1340+
{
1341+
// modeled for a t3.medium
1342+
eniLimit: 3,
1343+
eniIpv4Limit: 5,
1344+
expectedMaxIps: 15,
1345+
},
1346+
{
1347+
// modeled for a t3.medium
1348+
eniLimit: 3,
1349+
eniIpv4Limit: 5,
1350+
enablePrefixDelegation: true,
1351+
expectedMaxIps: 240,
1352+
},
1353+
{
1354+
// modeled for a t3.medium
1355+
eniLimit: 3,
1356+
eniIpv4Limit: 5,
1357+
useCustomNetworking: true,
1358+
expectedMaxIps: 10,
1359+
},
1360+
} {
1361+
mockContext := &IPAMContext{
1362+
awsClient: m.awsutils,
1363+
networkClient: m.network,
1364+
primaryIP: make(map[string]string),
1365+
terminating: int32(0),
1366+
enablePrefixDelegation: testCase.enablePrefixDelegation,
1367+
useCustomNetworking: testCase.useCustomNetworking,
1368+
}
1369+
m.awsutils.EXPECT().GetENILimit().Return(testCase.eniLimit)
1370+
m.awsutils.EXPECT().GetENIIPv4Limit().Return(testCase.eniIpv4Limit)
1371+
1372+
maxIps, err := mockContext.getMaxIPs()
1373+
1374+
assert.NoError(t, err)
1375+
assert.Equal(t, testCase.expectedMaxIps, maxIps)
1376+
}
1377+
}
1378+
13291379
func TestIPAMContext_nodeIPPoolTooLow(t *testing.T) {
13301380
m := setup(t)
13311381
defer m.ctrl.Finish()

pkg/ipamd/rpc_handler.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ func (s *server) GetNetworkPolicyConfigs(ctx context.Context, e *emptypb.Empty)
468468
return resp, nil
469469
}
470470

471+
func (s *server) GetAllocatableValues(ctx context.Context, e *emptypb.Empty) (*rpc.GetAllocatableValuesReply, error) {
472+
log.Infof("Received GetAllocatableValues request")
473+
474+
maxIPs, err := s.ipamContext.getMaxIPs()
475+
resp := &rpc.GetAllocatableValuesReply{
476+
MaxAllocatableIPs: int32(maxIPs),
477+
}
478+
479+
log.Infof("Send GetAllocatableValues: MaxAllocatableIPs: %d, err: %v", resp.MaxAllocatableIPs, err)
480+
return resp, err
481+
}
482+
471483
// RunRPCHandler handles request from gRPC
472484
func (c *IPAMContext) RunRPCHandler(version string) error {
473485
log.Infof("Serving RPC Handler version %s on %s", version, ipamdgRPCaddress)

rpc/mocks/rpc_mocks.go

Lines changed: 20 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)