Skip to content

Commit 3e3eac2

Browse files
author
Pushkar Acharya
committed
fix lint issues
1 parent 35c9c24 commit 3e3eac2

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

Diff for: internal/instances/instances.go

+25-16
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,10 @@ func (i *Instances) InstanceShutdownByProviderID(ctx context.Context, providerID
101101
defer responseBody.Body.Close()
102102
}
103103
if err != nil {
104-
if err == client.ErrInstanceNotFound {
105-
attempt, ok := i.nodeShutdown.Load(providerID)
106-
if !ok {
107-
attempt = 1
108-
i.nodeShutdown.Store(providerID, attempt)
109-
110-
return false, err
111-
}
112-
if attempt.(int) >= 3 {
113-
114-
return true, nil
115-
}
116-
intAttempt := attempt.(int)
117-
i.nodeShutdown.Store(providerID, (intAttempt + 1))
118-
119-
return false, err
104+
if errors.Is(err, client.ErrInstanceNotFound) {
105+
return i.handleInstanceNotFoundErr(providerID, err)
120106
}
107+
121108
return false, fmt.Errorf("failed to get instance by provider ID %s: %w", providerID, err)
122109
}
123110
if currInstance == nil || currInstance.State == "STATE_SHUTOFF" || currInstance.State == "STATE_SHUTDOWN" {
@@ -286,3 +273,25 @@ func getNodeAddress(currInstance *crusoeapi.InstanceV1Alpha5) ([]v1.NodeAddress,
286273

287274
return nodeAddress, nil
288275
}
276+
277+
func (i *Instances) handleInstanceNotFoundErr(providerID string, orignalErr error) (instanceShutdown bool, err error) {
278+
attempt, ok := i.nodeShutdown.Load(providerID)
279+
if !ok {
280+
i.nodeShutdown.Store(providerID, 1)
281+
282+
return false, orignalErr
283+
}
284+
intAttempt, ok := attempt.(int)
285+
if !ok {
286+
// store correct data in case to avoid conversion issues in next iteration
287+
i.nodeShutdown.Store(providerID, 1)
288+
289+
return false, orignalErr
290+
}
291+
if intAttempt >= 3 {
292+
return true, nil
293+
}
294+
i.nodeShutdown.Store(providerID, (intAttempt + 1))
295+
296+
return false, orignalErr
297+
}

Diff for: internal/instances/instances_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ func TestInstanceShutdownByProviderIDInstanceMissingFromCloudProvider(t *testing
116116
defer ctrl.Finish()
117117

118118
mockClient := mock_client.NewMockApiClient(ctrl)
119-
mockClient.EXPECT().GetInstanceByID(gomock.Any(), TESTInstanceID).Return(nil, nil, client.ErrInstanceNotFound).AnyTimes()
119+
mockClient.EXPECT().GetInstanceByID(gomock.Any(), TESTInstanceID).Return(nil,
120+
nil, client.ErrInstanceNotFound).AnyTimes()
120121
instanceService := instances.NewCrusoeInstances(mockClient)
121122

122123
// Instance shutdown by ID should return true on third attempt

0 commit comments

Comments
 (0)