From bc69f45a9456dd9bd31211dcb93817f7dc59d270 Mon Sep 17 00:00:00 2001 From: Rahul Sharma Date: Tue, 17 Dec 2024 03:17:34 +0000 Subject: [PATCH] use single copy of instances --- cloud/linode/cloud.go | 6 +++-- cloud/linode/route_controller.go | 4 +-- cloud/linode/route_controller_test.go | 36 ++++++++++++++++++--------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/cloud/linode/cloud.go b/cloud/linode/cloud.go index e9ac261b..8699dc2a 100644 --- a/cloud/linode/cloud.go +++ b/cloud/linode/cloud.go @@ -97,7 +97,9 @@ func newCloud() (cloudprovider.Interface, error) { Options.VPCNames = Options.VPCName } - routes, err := newRoutes(linodeClient) + instances := newInstances(linodeClient) + + routes, err := newRoutes(linodeClient, instances) if err != nil { return nil, fmt.Errorf("routes client was not created successfully: %w", err) } @@ -123,7 +125,7 @@ func newCloud() (cloudprovider.Interface, error) { // create struct that satisfies cloudprovider.Interface lcloud := &linodeCloud{ client: linodeClient, - instances: newInstances(linodeClient), + instances: instances, loadbalancers: newLoadbalancers(linodeClient, region), routes: routes, } diff --git a/cloud/linode/route_controller.go b/cloud/linode/route_controller.go index 8b3bdf47..ed23f16a 100644 --- a/cloud/linode/route_controller.go +++ b/cloud/linode/route_controller.go @@ -62,7 +62,7 @@ type routes struct { routeCache *routeCache } -func newRoutes(client client.Client) (cloudprovider.Routes, error) { +func newRoutes(client client.Client, instances *instances) (cloudprovider.Routes, error) { timeout := 60 if raw, ok := os.LookupEnv("LINODE_ROUTES_CACHE_TTL_SECONDS"); ok { if t, _ := strconv.Atoi(raw); t > 0 { @@ -77,7 +77,7 @@ func newRoutes(client client.Client) (cloudprovider.Routes, error) { return &routes{ client: client, - instances: newInstances(client), + instances: instances, routeCache: &routeCache{ routes: make(map[int][]linodego.VPCIP, 0), ttl: time.Duration(timeout) * time.Second, diff --git a/cloud/linode/route_controller_test.go b/cloud/linode/route_controller_test.go index 6b2efc64..f8bb513f 100644 --- a/cloud/linode/route_controller_test.go +++ b/cloud/linode/route_controller_test.go @@ -33,7 +33,8 @@ func TestListRoutes(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), gomock.Any()).Times(1).Return([]linodego.Instance{}, nil) @@ -56,7 +57,8 @@ func TestListRoutes(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil) @@ -82,7 +84,8 @@ func TestListRoutes(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil) @@ -123,7 +126,8 @@ func TestListRoutes(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil) @@ -164,7 +168,8 @@ func TestListRoutes(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil) @@ -179,7 +184,8 @@ func TestListRoutes(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) vpcIP2 := "10.0.0.3" @@ -283,7 +289,8 @@ func TestCreateRoute(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil) @@ -315,7 +322,8 @@ func TestCreateRoute(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil) @@ -328,7 +336,8 @@ func TestCreateRoute(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{}, nil) @@ -370,7 +379,8 @@ func TestDeleteRoute(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{}, nil) @@ -400,7 +410,8 @@ func TestDeleteRoute(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil) @@ -431,7 +442,8 @@ func TestDeleteRoute(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() client := mocks.NewMockClient(ctrl) - routeController, err := newRoutes(client) + instances := newInstances(client) + routeController, err := newRoutes(client, instances) assert.NoError(t, err) client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil)