Skip to content

Commit 4cd9da7

Browse files
committed
Implement InstanceShutdown
1 parent 90eb4f1 commit 4cd9da7

2 files changed

Lines changed: 44 additions & 18 deletions

File tree

cloud/linode/instances.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,33 @@ func (i *instances) InstanceExists(ctx context.Context, node *v1.Node) (bool, er
237237
}
238238

239239
func (i *instances) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {
240-
return false, nil // TODO(PR): fix
240+
providerID := node.Spec.ProviderID
241+
242+
ctx = sentry.SetHubOnContext(ctx)
243+
sentry.SetTag(ctx, "provider_id", providerID)
244+
245+
id, err := parseProviderID(providerID)
246+
if err != nil {
247+
sentry.CaptureError(ctx, err)
248+
return false, err
249+
}
250+
251+
sentry.SetTag(ctx, "linode_id", strconv.Itoa(id))
252+
253+
instance, err := linodeByID(ctx, i.client, id)
254+
if err != nil {
255+
sentry.CaptureError(ctx, err)
256+
return false, err
257+
}
258+
259+
// An instance is considered to be "shutdown" when it is
260+
// in the process of shutting down, or already offline.
261+
if instance.Status == linodego.InstanceOffline ||
262+
instance.Status == linodego.InstanceShuttingDown {
263+
return true, nil
264+
}
265+
266+
return false, nil
241267
}
242268

243269
func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {

cloud/linode/instances_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import (
1515
cloudprovider "k8s.io/cloud-provider"
1616
)
1717

18+
func nodeWithProviderID(providerID string) *v1.Node {
19+
return &v1.Node{Spec: v1.NodeSpec{
20+
ProviderID: providerID,
21+
}}
22+
}
23+
1824
func TestInstanceExistsByProviderID(t *testing.T) {
1925
ctx := context.TODO()
2026
ctrl := gomock.NewController(t)
@@ -24,9 +30,7 @@ func TestInstanceExistsByProviderID(t *testing.T) {
2430
instances := newInstances(client)
2531

2632
t.Run("should propagate generic api error", func(t *testing.T) {
27-
node := &v1.Node{Spec: v1.NodeSpec{
28-
ProviderID: providerIDPrefix + "123",
29-
}}
33+
node := nodeWithProviderID(providerIDPrefix + "123")
3034
expectedErr := errors.New("some error")
3135
client.EXPECT().GetInstance(gomock.Any(), 123).Times(1).Return(nil, expectedErr)
3236

@@ -36,9 +40,7 @@ func TestInstanceExistsByProviderID(t *testing.T) {
3640
})
3741

3842
t.Run("should return false if linode does not exist", func(t *testing.T) {
39-
node := &v1.Node{Spec: v1.NodeSpec{
40-
ProviderID: providerIDPrefix + "123",
41-
}}
43+
node := nodeWithProviderID(providerIDPrefix + "123")
4244
client.EXPECT().GetInstance(gomock.Any(), 123).Times(1).Return(nil, &linodego.Error{
4345
Code: http.StatusNotFound,
4446
})
@@ -49,9 +51,7 @@ func TestInstanceExistsByProviderID(t *testing.T) {
4951
})
5052

5153
t.Run("should return true if linode exists", func(t *testing.T) {
52-
node := &v1.Node{Spec: v1.NodeSpec{
53-
ProviderID: providerIDPrefix + "123",
54-
}}
54+
node := nodeWithProviderID(providerIDPrefix + "123")
5555
client.EXPECT().GetInstance(gomock.Any(), 123).Times(1).Return(&linodego.Instance{
5656
ID: 123,
5757
Label: "mock",
@@ -329,45 +329,45 @@ func TestInstanceShutdownByProviderID(t *testing.T) {
329329

330330
t.Run("fails when instance not found", func(t *testing.T) {
331331
id := 12345
332-
providerID := providerIDPrefix + strconv.Itoa(id)
332+
node := nodeWithProviderID(providerIDPrefix + strconv.Itoa(id))
333333
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(nil, linodego.Error{Code: http.StatusNotFound})
334-
shutdown, err := instances.InstanceShutdownByProviderID(ctx, providerID)
334+
shutdown, err := instances.InstanceShutdown(ctx, node)
335335

336336
assert.Error(t, err)
337337
assert.False(t, shutdown)
338338
})
339339

340340
t.Run("returns true when instance is shut down", func(t *testing.T) {
341341
id := 12345
342-
providerID := providerIDPrefix + strconv.Itoa(id)
342+
node := nodeWithProviderID(providerIDPrefix + strconv.Itoa(id))
343343
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(&linodego.Instance{
344344
ID: id, Label: "offline-linode", Status: linodego.InstanceOffline,
345345
}, nil)
346-
shutdown, err := instances.InstanceShutdownByProviderID(ctx, providerID)
346+
shutdown, err := instances.InstanceShutdown(ctx, node)
347347

348348
assert.NoError(t, err)
349349
assert.True(t, shutdown)
350350
})
351351

352352
t.Run("returns true when instance is shutting down", func(t *testing.T) {
353353
id := 12345
354-
providerID := providerIDPrefix + strconv.Itoa(id)
354+
node := nodeWithProviderID(providerIDPrefix + strconv.Itoa(id))
355355
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(&linodego.Instance{
356356
ID: id, Label: "shutting-down-linode", Status: linodego.InstanceShuttingDown,
357357
}, nil)
358-
shutdown, err := instances.InstanceShutdownByProviderID(ctx, providerID)
358+
shutdown, err := instances.InstanceShutdown(ctx, node)
359359

360360
assert.NoError(t, err)
361361
assert.True(t, shutdown)
362362
})
363363

364364
t.Run("returns false when instance is running", func(t *testing.T) {
365365
id := 12345
366-
providerID := providerIDPrefix + strconv.Itoa(id)
366+
node := nodeWithProviderID(providerIDPrefix + strconv.Itoa(id))
367367
client.EXPECT().GetInstance(gomock.Any(), id).Times(1).Return(&linodego.Instance{
368368
ID: id, Label: "running-linode", Status: linodego.InstanceRunning,
369369
}, nil)
370-
shutdown, err := instances.InstanceShutdownByProviderID(ctx, providerID)
370+
shutdown, err := instances.InstanceShutdown(ctx, node)
371371

372372
assert.NoError(t, err)
373373
assert.False(t, shutdown)

0 commit comments

Comments
 (0)