Skip to content

Commit 9f781a2

Browse files
committed
feat: expose max allocatable IPs thru grpc server
1 parent c9d6f7b commit 9f781a2

7 files changed

Lines changed: 310 additions & 145 deletions

File tree

go.mod

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,3 @@ require (
177177
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
178178
sigs.k8s.io/yaml v1.4.0 // indirect
179179
)
180-
181-
replace gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 => gopkg.in/yaml.v3 v3.0.1
182-
183-
// Version of go-cose v1.2.0 and v1.2.1 have been deprecated in favor v1.1.0
184-
replace github.com/veraison/go-cose => github.com/veraison/go-cose v1.1.0

pkg/ipamd/ipamd.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,23 @@ func (c *IPAMContext) getPrefixesNeeded() int {
23472347
return toAllocate
23482348
}
23492349

2350+
// getMaxIPs returns the maximum number of ipv4 addresses allocatable given the context
2351+
func (c *IPAMContext) getMaxIPs() (int, error) {
2352+
enisForPods, err := c.getMaxENI()
2353+
if err != nil {
2354+
return 0, err
2355+
}
2356+
if c.useCustomNetworking {
2357+
enisForPods = enisForPods - 1
2358+
}
2359+
2360+
ipv4Limit, _, err := c.GetIPv4Limit()
2361+
if err != nil {
2362+
return 0, err
2363+
}
2364+
return enisForPods * ipv4Limit, nil
2365+
}
2366+
23502367
func (c *IPAMContext) initENIAndIPLimits() (err error) {
23512368
if c.enableIPv4 {
23522369
nodeMaxENI, err := c.getMaxENI()

pkg/ipamd/ipamd_test.go

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

1268+
func TestGetMaxIPs(t *testing.T) {
1269+
m := setup(t)
1270+
defer m.ctrl.Finish()
1271+
1272+
for _, testCase := range []struct {
1273+
enablePrefixDelegation bool
1274+
useCustomNetworking bool
1275+
eniLimit int
1276+
eniIpv4Limit int
1277+
expectedMaxIps int
1278+
}{
1279+
{
1280+
// modeled for a t3.medium
1281+
eniLimit: 3,
1282+
eniIpv4Limit: 5,
1283+
expectedMaxIps: 15,
1284+
},
1285+
{
1286+
// modeled for a t3.medium
1287+
eniLimit: 3,
1288+
eniIpv4Limit: 5,
1289+
enablePrefixDelegation: true,
1290+
expectedMaxIps: 240,
1291+
},
1292+
{
1293+
// modeled for a t3.medium
1294+
eniLimit: 3,
1295+
eniIpv4Limit: 5,
1296+
useCustomNetworking: true,
1297+
expectedMaxIps: 10,
1298+
},
1299+
} {
1300+
mockContext := &IPAMContext{
1301+
awsClient: m.awsutils,
1302+
networkClient: m.network,
1303+
primaryIP: make(map[string]string),
1304+
terminating: int32(0),
1305+
enablePrefixDelegation: testCase.enablePrefixDelegation,
1306+
useCustomNetworking: testCase.useCustomNetworking,
1307+
}
1308+
m.awsutils.EXPECT().GetENILimit().Return(testCase.eniLimit)
1309+
m.awsutils.EXPECT().GetENIIPv4Limit().Return(testCase.eniIpv4Limit)
1310+
1311+
maxIps, err := mockContext.getMaxIPs()
1312+
1313+
assert.NoError(t, err)
1314+
assert.Equal(t, testCase.expectedMaxIps, maxIps)
1315+
}
1316+
}
1317+
12681318
func TestIPAMContext_nodeIPPoolTooLow(t *testing.T) {
12691319
m := setup(t)
12701320
defer m.ctrl.Finish()

pkg/ipamd/rpc_handler.go

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

337+
func (s *server) GetMaxAllocatableIPs(ctx context.Context, e *emptypb.Empty) (*rpc.GetMaxAllocatableIPsReply, error) {
338+
log.Infof("Received GetMaxAllocatableIPs request")
339+
340+
maxIPs, err := s.ipamContext.getMaxIPs()
341+
resp := &rpc.GetMaxAllocatableIPsReply{
342+
MaxAllocatableIPs: int32(maxIPs),
343+
}
344+
345+
log.Infof("Send GetMaxAllocatableIPs: MaxAllocatableIPs: %d, err: %v", resp.MaxAllocatableIPs, err)
346+
return resp, err
347+
}
348+
337349
// RunRPCHandler handles request from gRPC
338350
func (c *IPAMContext) RunRPCHandler(version string) error {
339351
log.Infof("Serving RPC Handler version %s on %s", version, ipamdgRPCaddress)

rpc/mocks/rpc_mocks.go

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

0 commit comments

Comments
 (0)