Skip to content

Commit

Permalink
Add config option for number of ENIs get preallocated
Browse files Browse the repository at this point in the history
  • Loading branch information
liwenwu-amazon committed May 15, 2018
1 parent d69852b commit e5a9976
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
40 changes: 33 additions & 7 deletions ipamd/ipamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package ipamd

import (
"net"
"os"
"strconv"
"strings"
"time"

Expand All @@ -40,6 +42,7 @@ const (
ipPoolMonitorInterval = 5 * time.Second
maxRetryCheckENI = 5
eniAttachTime = 10 * time.Second
defaultPreAllocENIs = 1
)

var (
Expand Down Expand Up @@ -117,6 +120,8 @@ func New() (*IPAMContext, error) {

//TODO need to break this function down(comments from CR)
func (c *IPAMContext) nodeInit() error {
ipamdActionsInprogress.WithLabelValues("nodeInit").Add(float64(1))
defer ipamdActionsInprogress.WithLabelValues("nodeInit").Sub(float64(1))
maxENIs, err := c.awsClient.GetENILimit()
enisMax.Set(float64(maxENIs))
enis, err := c.awsClient.GetAttachedENIs()
Expand Down Expand Up @@ -260,6 +265,10 @@ func (c *IPAMContext) increaseIPPool() {
return
}
if (c.maxENI > 0) && (c.maxENI == c.dataStore.GetENIs()) {
if c.maxENI < maxENIs {
errString := "desired: " + strconv.FormatInt(int64(maxENIs), 10) + "current: " + strconv.FormatInt(int64(c.maxENI), 10)
ipamdErrInc("unExpectedMaxENIAttached", errors.New(errString))
}
log.Debugf("Skipping increase IPPOOL due to max ENI already attached to the instance : %d", c.maxENI)
return
}
Expand Down Expand Up @@ -400,23 +409,40 @@ func (c *IPAMContext) waitENIAttached(eni string) (awsutils.ENIMetadata, error)
}
}

func getPreAllocENILimit() int {
limitStr, found := os.LookupEnv("PRE_ALLOC_ENIS")

if !found {
return defaultPreAllocENIs
}

if limit, err := strconv.Atoi(limitStr); err == nil {
if limit < 0 {
return defaultPreAllocENIs
}
return limit
}
return defaultPreAllocENIs
}

//nodeIPPoolTooLow returns true if IP pool is below low threshhold
func (c *IPAMContext) nodeIPPoolTooLow() bool {
preAllocENIs := getPreAllocENILimit()
total, used := c.dataStore.GetStats()
log.Debugf("IP pool stats: total=%d, used=%d, c.currentMaxAddrsPerENI =%d, c.maxAddrsPerENI = %d",
total, used, c.currentMaxAddrsPerENI, c.maxAddrsPerENI)
log.Debugf("IP pool stats: total=%d, used=%d, c.currentMaxAddrsPerENI =%d, c.maxAddrsPerENI = %d, preAllocENIs = %d",
total, used, c.currentMaxAddrsPerENI, c.maxAddrsPerENI, preAllocENIs)

return ((total - used) <= c.currentMaxAddrsPerENI)
return ((total - used) <= c.currentMaxAddrsPerENI*preAllocENIs)
}

// NodeIPPoolTooHigh returns true if IP pool is above high threshhold
func (c *IPAMContext) nodeIPPoolTooHigh() bool {
preAllocENIs := getPreAllocENILimit()
total, used := c.dataStore.GetStats()
log.Debugf("IP pool stats: total=%d, used=%d, c.currentMaxAddrsPerENI =%d, c.maxAddrsPerENI = %d, preAllocENIs",
total, used, c.currentMaxAddrsPerENI, c.maxAddrsPerENI, preAllocENIs)

log.Debugf("IP pool stats: total=%d, used=%d, c.currentMaxAddrsPerENI =%d, c.maxAddrsPerENI = %d",
total, used, c.currentMaxAddrsPerENI, c.maxAddrsPerENI)

return (total-used > 2*c.currentMaxAddrsPerENI)
return (total-used > (preAllocENIs+1)*c.currentMaxAddrsPerENI)

}

Expand Down
5 changes: 4 additions & 1 deletion misc/cni_metrics_helper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ spec:
spec:
serviceAccountName: cni-metrics-helper
containers:
- image: 694065802095.dkr.ecr.us-west-2.amazonaws.com/cni-metrics-helper:0.1.0
- image: 694065802095.dkr.ecr.us-west-2.amazonaws.com/cni-metrics-helper:0.1.1
imagePullPolicy: Always
name: cni-metrics-helper
env:
- name: USE_CLOUDWATCH
value: "no"

0 comments on commit e5a9976

Please sign in to comment.