From 0af437a30916eb081c70c031e96322ea06217497 Mon Sep 17 00:00:00 2001 From: Jayanth Varavani <1111446+jayanthvn@users.noreply.github.com> Date: Thu, 1 Jul 2021 18:06:58 -0700 Subject: [PATCH] non-nitro instances init issues (#1526) * non-nitro init issues * fix formatting --- pkg/awsutils/awsutils.go | 13 +++++++++---- pkg/awsutils/mocks/awsutils_mocks.go | 26 ++++++++++++-------------- pkg/ipamd/ipamd.go | 3 ++- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/pkg/awsutils/awsutils.go b/pkg/awsutils/awsutils.go index b60f6dbfff..4b2bb7ff87 100644 --- a/pkg/awsutils/awsutils.go +++ b/pkg/awsutils/awsutils.go @@ -179,6 +179,9 @@ type APIs interface { //GetInstanceType returns the EC2 instance type GetInstanceType() string + + //Update cached prefix delegation flag + InitCachedPrefixDelegation(bool) } // EC2InstanceMetadataCache caches instance metadata @@ -339,7 +342,7 @@ func (i instrumentedIMDS) GetMetadataWithContext(ctx context.Context, p string) } // New creates an EC2InstanceMetadataCache -func New(useCustomNetworking, enableIpv4PrefixDelegation bool) (*EC2InstanceMetadataCache, error) { +func New(useCustomNetworking bool) (*EC2InstanceMetadataCache, error) { //ctx is passed to initWithEC2Metadata func to cancel spawned go-routines when tests are run ctx := context.Background() @@ -365,9 +368,6 @@ func New(useCustomNetworking, enableIpv4PrefixDelegation bool) (*EC2InstanceMeta cache.useCustomNetworking = useCustomNetworking log.Infof("Custom networking enabled %v", cache.useCustomNetworking) - cache.enableIpv4PrefixDelegation = enableIpv4PrefixDelegation - log.Infof("Prefix Delegation enabled %v", cache.enableIpv4PrefixDelegation) - awsCfg := aws.NewConfig().WithRegion(region) sess = sess.Copy(awsCfg) @@ -384,6 +384,11 @@ func New(useCustomNetworking, enableIpv4PrefixDelegation bool) (*EC2InstanceMeta return cache, nil } +func (cache *EC2InstanceMetadataCache) InitCachedPrefixDelegation(enableIpv4PrefixDelegation bool) { + cache.enableIpv4PrefixDelegation = enableIpv4PrefixDelegation + log.Infof("Prefix Delegation enabled %v", cache.enableIpv4PrefixDelegation) +} + // InitWithEC2metadata initializes the EC2InstanceMetadataCache with the data retrieved from EC2 metadata service func (cache *EC2InstanceMetadataCache) initWithEC2Metadata(ctx context.Context) error { var err error diff --git a/pkg/awsutils/mocks/awsutils_mocks.go b/pkg/awsutils/mocks/awsutils_mocks.go index cda31a3627..f2c69f3f1a 100644 --- a/pkg/awsutils/mocks/awsutils_mocks.go +++ b/pkg/awsutils/mocks/awsutils_mocks.go @@ -93,20 +93,6 @@ func (mr *MockAPIsMockRecorder) AllocIPAddresses(arg0, arg1 interface{}) *gomock return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AllocIPAddresses", reflect.TypeOf((*MockAPIs)(nil).AllocIPAddresses), arg0, arg1) } -// DeallocCidrs mocks base method -func (m *MockAPIs) DeallocCidrs(arg0 string, arg1 []string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DeallocCidrs", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// DeallocCidrs indicates an expected call of DeallocCidrs -func (mr *MockAPIsMockRecorder) DeallocCidrs(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeallocCidrs", reflect.TypeOf((*MockAPIs)(nil).DeallocCidrs), arg0, arg1) -} - // DeallocIPAddresses mocks base method func (m *MockAPIs) DeallocIPAddresses(arg0 string, arg1 []string) error { m.ctrl.T.Helper() @@ -325,6 +311,18 @@ func (mr *MockAPIsMockRecorder) GetVPCIPv4CIDRs() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetVPCIPv4CIDRs", reflect.TypeOf((*MockAPIs)(nil).GetVPCIPv4CIDRs)) } +// InitCachedPrefixDelegation mocks base method +func (m *MockAPIs) InitCachedPrefixDelegation(arg0 bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "InitCachedPrefixDelegation", arg0) +} + +// InitCachedPrefixDelegation indicates an expected call of InitCachedPrefixDelegation +func (mr *MockAPIsMockRecorder) InitCachedPrefixDelegation(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InitCachedPrefixDelegation", reflect.TypeOf((*MockAPIs)(nil).InitCachedPrefixDelegation), arg0) +} + // IsCNIUnmanagedENI mocks base method func (m *MockAPIs) IsCNIUnmanagedENI(arg0 string) bool { m.ctrl.T.Helper() diff --git a/pkg/ipamd/ipamd.go b/pkg/ipamd/ipamd.go index 7aefbaa9fc..8d8da5b8ee 100644 --- a/pkg/ipamd/ipamd.go +++ b/pkg/ipamd/ipamd.go @@ -304,7 +304,7 @@ func New(rawK8SClient client.Client, cachedK8SClient client.Client) (*IPAMContex c.useCustomNetworking = UseCustomNetworkCfg() c.enableIpv4PrefixDelegation = useIpv4PrefixDelegation() - client, err := awsutils.New(c.useCustomNetworking, c.enableIpv4PrefixDelegation) + client, err := awsutils.New(c.useCustomNetworking) if err != nil { return nil, errors.Wrap(err, "ipamd: can not initialize with AWS SDK interface") } @@ -329,6 +329,7 @@ func New(rawK8SClient client.Client, cachedK8SClient client.Client) (*IPAMContex log.Warnf("Prefix delegation is not supported on non-nitro instance %s hence falling back to default (secondary IP) mode", c.awsClient.GetInstanceType()) c.enableIpv4PrefixDelegation = false } + c.awsClient.InitCachedPrefixDelegation(c.enableIpv4PrefixDelegation) c.myNodeName = os.Getenv("MY_NODE_NAME") checkpointer := datastore.NewJSONFile(dsBackingStorePath()) c.dataStore = datastore.NewDataStore(log, checkpointer, c.enableIpv4PrefixDelegation)