From e5a9976a936311ad2cc21ae8615043cd5f2f21b5 Mon Sep 17 00:00:00 2001 From: liwen wu Date: Tue, 15 May 2018 18:49:55 +0000 Subject: [PATCH] Add config option for number of ENIs get preallocated --- ipamd/ipamd.go | 40 +++++++++++++++++++++++++++++------- misc/cni_metrics_helper.yaml | 5 ++++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/ipamd/ipamd.go b/ipamd/ipamd.go index e15f30c285a..336b9b75265 100644 --- a/ipamd/ipamd.go +++ b/ipamd/ipamd.go @@ -15,6 +15,8 @@ package ipamd import ( "net" + "os" + "strconv" "strings" "time" @@ -40,6 +42,7 @@ const ( ipPoolMonitorInterval = 5 * time.Second maxRetryCheckENI = 5 eniAttachTime = 10 * time.Second + defaultPreAllocENIs = 1 ) var ( @@ -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() @@ -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 } @@ -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) } diff --git a/misc/cni_metrics_helper.yaml b/misc/cni_metrics_helper.yaml index e0c21fd1074..05fdcff4ef9 100644 --- a/misc/cni_metrics_helper.yaml +++ b/misc/cni_metrics_helper.yaml @@ -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"