Skip to content

Commit 5ada764

Browse files
committed
Reuse Linode lookup logic + tests
1 parent 9a46472 commit 5ada764

2 files changed

Lines changed: 48 additions & 56 deletions

File tree

cloud/linode/instances.go

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7-
"strconv"
87

9-
"github.com/linode/linode-cloud-controller-manager/sentry"
108
"github.com/linode/linodego"
119
v1 "k8s.io/api/core/v1"
1210
"k8s.io/apimachinery/pkg/types"
@@ -55,49 +53,36 @@ func (i *instances) nodeAddresses(ctx context.Context, linode *linodego.Instance
5553
return addresses, nil
5654
}
5755

58-
func (i *instances) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) {
56+
func (i *instances) lookupLinode(ctx context.Context, node *v1.Node) (*linodego.Instance, error) {
5957
providerID := node.Spec.ProviderID
58+
nodeName := types.NodeName(node.Name)
6059

61-
ctx = sentry.SetHubOnContext(ctx)
62-
sentry.SetTag(ctx, "provider_id", providerID)
60+
if providerID != "" {
61+
id, err := parseProviderID(providerID)
62+
if err != nil {
63+
return nil, err
64+
}
6365

64-
id, err := parseProviderID(providerID)
65-
if err != nil {
66-
sentry.CaptureError(ctx, err)
67-
return false, err
66+
return linodeByID(ctx, i.client, id)
6867
}
6968

70-
sentry.SetTag(ctx, "linode_id", strconv.Itoa(id))
69+
return linodeByName(ctx, i.client, nodeName)
70+
}
7171

72-
_, err = linodeByID(ctx, i.client, id)
73-
if err != nil {
72+
func (i *instances) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) {
73+
if _, err := i.lookupLinode(ctx, node); err != nil {
7474
if apiError, ok := err.(*linodego.Error); ok && apiError.Code == http.StatusNotFound {
7575
return false, nil
7676
}
77-
sentry.CaptureError(ctx, err)
7877
return false, err
7978
}
8079

8180
return true, nil
8281
}
8382

8483
func (i *instances) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {
85-
providerID := node.Spec.ProviderID
86-
87-
ctx = sentry.SetHubOnContext(ctx)
88-
sentry.SetTag(ctx, "provider_id", providerID)
89-
90-
id, err := parseProviderID(providerID)
84+
instance, err := i.lookupLinode(ctx, node)
9185
if err != nil {
92-
sentry.CaptureError(ctx, err)
93-
return false, err
94-
}
95-
96-
sentry.SetTag(ctx, "linode_id", strconv.Itoa(id))
97-
98-
instance, err := linodeByID(ctx, i.client, id)
99-
if err != nil {
100-
sentry.CaptureError(ctx, err)
10186
return false, err
10287
}
10388

@@ -112,29 +97,9 @@ func (i *instances) InstanceShutdown(ctx context.Context, node *v1.Node) (bool,
11297
}
11398

11499
func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
115-
var (
116-
providerID = node.Spec.ProviderID
117-
nodeName = types.NodeName(node.Name)
118-
linode *linodego.Instance
119-
err error
120-
)
121-
122-
// TODO(okokes): abstract this away and reuse it in our other methods here
123-
if providerID != "" {
124-
id, err := parseProviderID(providerID)
125-
if err != nil {
126-
return nil, err
127-
}
128-
129-
linode, err = linodeByID(ctx, i.client, id)
130-
if err != nil {
131-
return nil, err
132-
}
133-
} else {
134-
linode, err = linodeByName(ctx, i.client, nodeName)
135-
if err != nil {
136-
return nil, err
137-
}
100+
linode, err := i.lookupLinode(ctx, node)
101+
if err != nil {
102+
return nil, err
138103
}
139104

140105
addresses, err := i.nodeAddresses(ctx, linode)
@@ -144,7 +109,7 @@ func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
144109

145110
// note that Zone is omitted as it's not a thing in Linode
146111
meta := &cloudprovider.InstanceMetadata{
147-
ProviderID: providerID,
112+
ProviderID: node.Spec.ProviderID, // TODO(okokes): this is circular... should we instead set it to a known prefix + linode.ID?
148113
NodeAddresses: addresses,
149114
InstanceType: linode.Type,
150115
Region: linode.Region,

cloud/linode/instances_test.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package linode
22

33
import (
44
"context"
5+
"errors"
56
"net/http"
67
"strconv"
78
"testing"
89

910
"github.com/golang/mock/gomock"
1011
"github.com/linode/linodego"
11-
"github.com/pkg/errors"
1212
"github.com/stretchr/testify/assert"
1313
v1 "k8s.io/api/core/v1"
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -25,7 +25,7 @@ func nodeWithName(name string) *v1.Node {
2525
return &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: name}}
2626
}
2727

28-
func TestInstanceExistsByProviderID(t *testing.T) {
28+
func TestInstanceExists(t *testing.T) {
2929
ctx := context.TODO()
3030
ctrl := gomock.NewController(t)
3131
defer ctrl.Finish()
@@ -43,7 +43,7 @@ func TestInstanceExistsByProviderID(t *testing.T) {
4343
assert.False(t, exists)
4444
})
4545

46-
t.Run("should return false if linode does not exist", func(t *testing.T) {
46+
t.Run("should return false if linode does not exist (by providerID)", func(t *testing.T) {
4747
node := nodeWithProviderID(providerIDPrefix + "123")
4848
client.EXPECT().GetInstance(gomock.Any(), 123).Times(1).Return(nil, &linodego.Error{
4949
Code: http.StatusNotFound,
@@ -54,7 +54,21 @@ func TestInstanceExistsByProviderID(t *testing.T) {
5454
assert.False(t, exists)
5555
})
5656

57-
t.Run("should return true if linode exists", func(t *testing.T) {
57+
t.Run("should return false if linode does not exist (by name)", func(t *testing.T) {
58+
name := "some-name"
59+
node := nodeWithName(name)
60+
notFound := &linodego.Error{
61+
Code: http.StatusNotFound,
62+
}
63+
filter := linodeFilterListOptions(name)
64+
client.EXPECT().ListInstances(gomock.Any(), filter).Times(1).Return(nil, notFound)
65+
66+
exists, err := instances.InstanceExists(ctx, node)
67+
assert.NoError(t, err)
68+
assert.False(t, exists)
69+
})
70+
71+
t.Run("should return true if linode exists (by provider)", func(t *testing.T) {
5872
node := nodeWithProviderID(providerIDPrefix + "123")
5973
client.EXPECT().GetInstance(gomock.Any(), 123).Times(1).Return(&linodego.Instance{
6074
ID: 123,
@@ -67,6 +81,19 @@ func TestInstanceExistsByProviderID(t *testing.T) {
6781
assert.NoError(t, err)
6882
assert.True(t, exists)
6983
})
84+
85+
t.Run("should return true if linode exists (by name)", func(t *testing.T) {
86+
name := "some-name"
87+
node := nodeWithName(name)
88+
89+
client.EXPECT().ListInstances(gomock.Any(), linodeFilterListOptions(name)).Times(1).Return([]linodego.Instance{
90+
{ID: 123, Label: name},
91+
}, nil)
92+
93+
exists, err := instances.InstanceExists(ctx, node)
94+
assert.NoError(t, err)
95+
assert.True(t, exists)
96+
})
7097
}
7198

7299
func TestNodeAddresses(t *testing.T) {

0 commit comments

Comments
 (0)