diff --git a/ipamd/ipamd.go b/ipamd/ipamd.go index e7344b1f76..558c56aa2d 100644 --- a/ipamd/ipamd.go +++ b/ipamd/ipamd.go @@ -157,6 +157,8 @@ type IPAMContext struct { // It is initialized to 0 and it is set to current number of ENIs attached // when ipamd receives AttachmentLimitExceeded error maxENI int + warmENITarget int + warmIPTarget int primaryIP map[string]string lastNodeIPPoolAction time.Time lastDecreaseIPPool time.Time @@ -193,6 +195,10 @@ func New(k8sapiClient k8sapi.K8SAPIs, eniConfig *eniconfig.ENIConfigController) } c.awsClient = client + instanceMaxENIs, _ := c.awsClient.GetENILimit() + c.maxENI = getMaxENI(instanceMaxENIs) + c.warmENITarget = getWarmENITarget() + c.warmIPTarget = getWarmIPTarget() err = c.nodeInit() if err != nil { @@ -208,15 +214,13 @@ func (c *IPAMContext) nodeInit() error { log.Debugf("Start node init") - instanceMaxENIs, _ := c.awsClient.GetENILimit() - maxENIs := getMaxENI(instanceMaxENIs) - if maxENIs >= 1 { - enisMax.Set(float64(maxENIs)) + if c.maxENI >= 1 { + enisMax.Set(float64(c.maxENI)) } maxIPs, err := c.awsClient.GetENIipLimit() if err == nil { - ipMax.Set(float64(maxIPs * maxENIs)) + ipMax.Set(float64(maxIPs * c.maxENI)) } c.primaryIP = make(map[string]string) @@ -494,14 +498,12 @@ func (c *IPAMContext) increaseIPPool() { return } - instanceMaxENIs, err := c.awsClient.GetENILimit() - maxENIs := getMaxENI(instanceMaxENIs) - if maxENIs >= 1 { - enisMax.Set(float64(maxENIs)) + if c.maxENI >= 1 { + enisMax.Set(float64(c.maxENI)) } - if err == nil && maxENIs == c.dataStore.GetENIs() { - log.Debugf("Skipping increase IP pool due to max ENI already attached to the instance: %d", maxENIs) + if c.maxENI == c.dataStore.GetENIs() { + log.Debugf("Skipping increase IP pool due to max ENI already attached to the instance: %d", c.maxENI) return } if (c.maxENI > 0) && (c.maxENI == c.dataStore.GetENIs()) { @@ -782,29 +784,25 @@ func (c *IPAMContext) nodeIPPoolTooLow() bool { return short > 0 } - // If WARM-IP-TARGET not defined fallback using number of ENIs - warmENITarget := getWarmENITarget() total, used := c.dataStore.GetStats() logPoolStats(total, used, c.currentMaxAddrsPerENI, c.maxAddrsPerENI) available := total - used - return available < c.maxAddrsPerENI*warmENITarget + return available < c.maxAddrsPerENI*c.warmENITarget } // nodeIPPoolTooHigh returns true if IP pool is above high threshold func (c *IPAMContext) nodeIPPoolTooHigh() bool { - warmENITarget := getWarmENITarget() total, used := c.dataStore.GetStats() logPoolStats(total, used, c.currentMaxAddrsPerENI, c.maxAddrsPerENI) available := total - used - target := getWarmIPTarget() - if target != noWarmIPTarget { - return target > available + if c.warmIPTarget != noWarmIPTarget { + return c.warmIPTarget > available } - return available >= (warmENITarget + 1) * c.maxAddrsPerENI + return available >= (c.warmENITarget+1)*c.maxAddrsPerENI } func ipamdErrInc(fn string, err error) { @@ -942,8 +940,7 @@ func getWarmIPTarget() int { // ipTargetState determines the number of IPs `short` or `over` our WARM_IP_TARGET func (c *IPAMContext) ipTargetState() (short int, over int, enabled bool) { - target := getWarmIPTarget() - if target == noWarmIPTarget { + if c.warmIPTarget == noWarmIPTarget { // there is no WARM_IP_TARGET defined, fallback to use all IP addresses on ENI return 0, 0, false } @@ -952,10 +949,10 @@ func (c *IPAMContext) ipTargetState() (short int, over int, enabled bool) { available := total - assigned // short is greater than 0 when we have fewer available IPs than the warm IP target - short = max(target-available, 0) + short = max(c.warmIPTarget-available, 0) // over is the number of available IPs we have beyond the warm IP target - over = max(available-target, 0) + over = max(available-c.warmIPTarget, 0) log.Debugf("Current warm IP stats: target: %d, total: %d, assigned: %d, available: %d, short: %d, over %d", target, total, assigned, available, short, over) return short, over, true @@ -982,4 +979,4 @@ func min(x, y int) int { return y } return x -} \ No newline at end of file +}