Skip to content

Commit ea57ee9

Browse files
committed
Use sentinels instead of error types
1 parent f7a98bf commit ea57ee9

2 files changed

Lines changed: 8 additions & 15 deletions

File tree

internal/controller/eviction_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controller
1919

2020
import (
2121
"context"
22+
"errors"
2223
"fmt"
2324
"math/rand"
2425
"net/http"
@@ -331,7 +332,7 @@ func (r *EvictionReconciler) handleFinalizer(ctx context.Context, eviction *kvmv
331332
if controllerutil.RemoveFinalizer(eviction, evictionFinalizerName) {
332333
err := r.enableHypervisorService(ctx, eviction)
333334
if err != nil {
334-
if _, ok := err.(*openstack.NoHypervisorError); ok {
335+
if errors.Is(err, openstack.ErrNoHypervisor) {
335336
log := logger.FromContext(ctx)
336337
log.Info("Can't enable host, it is gone")
337338
} else {

internal/openstack/hypervisor.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package openstack
1919

2020
import (
2121
"context"
22+
"fmt"
2223
"net/http"
2324

2425
"github.com/gophercloud/gophercloud/v2"
@@ -61,17 +62,8 @@ type HyperVisorsDetails struct {
6162
Hypervisors []Hypervisor `json:"hypervisors"`
6263
}
6364

64-
type NoHypervisorError struct{}
65-
66-
func (*NoHypervisorError) Error() string {
67-
return "no hypervisor found"
68-
}
69-
70-
type MultipleHypervisorsError struct{}
71-
72-
func (*MultipleHypervisorsError) Error() string {
73-
return "multiple hypervisors found"
74-
}
65+
var ErrNoHypervisor = fmt.Errorf("no hypervisor found")
66+
var ErrMultipleHypervisors = fmt.Errorf("multiple hypervisors found")
7567

7668
func GetHypervisorByName(ctx context.Context, sc *gophercloud.ServiceClient, hypervisorHostnamePattern string, withServers bool) (*Hypervisor, error) {
7769
listOpts := hypervisors.ListOpts{
@@ -82,7 +74,7 @@ func GetHypervisorByName(ctx context.Context, sc *gophercloud.ServiceClient, hyp
8274
pages, err := hypervisors.List(sc, listOpts).AllPages(ctx)
8375
if err != nil {
8476
if gophercloud.ResponseCodeIs(err, http.StatusNotFound) {
85-
return nil, &NoHypervisorError{}
77+
return nil, ErrNoHypervisor
8678
}
8779
return nil, err
8880
}
@@ -94,9 +86,9 @@ func GetHypervisorByName(ctx context.Context, sc *gophercloud.ServiceClient, hyp
9486
}
9587

9688
if len(h.Hypervisors) == 0 {
97-
return nil, &NoHypervisorError{}
89+
return nil, ErrNoHypervisor
9890
} else if len(h.Hypervisors) > 1 {
99-
return nil, &MultipleHypervisorsError{}
91+
return nil, ErrMultipleHypervisors
10092
}
10193

10294
return &h.Hypervisors[0], nil

0 commit comments

Comments
 (0)