Skip to content

Commit

Permalink
Moved all GetEnv's calls to init step
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyqsempai committed May 3, 2019
1 parent 0d3b925 commit 5ab3c51
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions ipamd/ipamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -982,4 +979,4 @@ func min(x, y int) int {
return y
}
return x
}
}

0 comments on commit 5ab3c51

Please sign in to comment.