Skip to content

Commit 46599a3

Browse files
committed
refactor(kubernetes): use idiomatic error handling
1 parent 2bf22c8 commit 46599a3

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

pkg/kubernetes/client/context.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io/ioutil"
89
"os"
@@ -94,7 +95,10 @@ func ContextFromIP(apiServer string) (*Cluster, *Context, error) {
9495
return nil, nil, err
9596
}
9697

97-
if err := find(clusters, "cluster.server", apiServer, &cluster, ErrorNoCluster(apiServer)); err != nil {
98+
err = find(clusters, "cluster.server", apiServer, &cluster)
99+
if err == ErrorNoMatch {
100+
return nil, nil, ErrorNoCluster(apiServer)
101+
} else if err != nil {
98102
return nil, nil, err
99103
}
100104

@@ -105,7 +109,10 @@ func ContextFromIP(apiServer string) (*Cluster, *Context, error) {
105109
return nil, nil, err
106110
}
107111

108-
if err := find(contexts, "context.cluster", cluster.Name, &context, ErrorNoContext(cluster.Name)); err != nil {
112+
err = find(contexts, "context.cluster", cluster.Name, &context)
113+
if err == ErrorNoMatch {
114+
return nil, nil, ErrorNoContext(cluster.Name)
115+
} else if err != nil {
109116
return nil, nil, err
110117
}
111118

@@ -127,7 +134,10 @@ func IPFromContext(name string) (ip string, err error) {
127134
return "", err
128135
}
129136

130-
if err := find(contexts, "name", name, &context, ErrorNoContext(name)); err != nil {
137+
err = find(contexts, "name", name, &context)
138+
if err == ErrorNoMatch {
139+
return "", ErrorNoContext(name)
140+
} else if err != nil {
131141
return "", err
132142
}
133143

@@ -139,9 +149,10 @@ func IPFromContext(name string) (ip string, err error) {
139149
}
140150

141151
clusterName := context.Context.Cluster
142-
if err := find(clusters, "name", clusterName, &cluster,
143-
fmt.Errorf("no cluster named `%s` as required by context `%s` was found. Please check your $KUBECONFIG", clusterName, name),
144-
); err != nil {
152+
err = find(clusters, "name", clusterName, &cluster)
153+
if err == ErrorNoMatch {
154+
return "", fmt.Errorf("no cluster named `%s` as required by context `%s` was found. Please check your $KUBECONFIG", clusterName, name)
155+
} else if err != nil {
145156
return "", err
146157
}
147158

@@ -160,9 +171,12 @@ func tryMSISlice(v *objx.Value, what string) ([]map[string]interface{}, error) {
160171
return data, nil
161172
}
162173

174+
// ErrorNoMatch occurs when no item matched had the expected value
175+
var ErrorNoMatch error = errors.New("no matches found")
176+
163177
// find attempts to find an object in list whose prop equals expected.
164178
// If found, the value is unmarshalled to ptr, otherwise errNotFound is returned.
165-
func find(list []map[string]interface{}, prop string, expected string, ptr interface{}, errNotFound error) error {
179+
func find(list []map[string]interface{}, prop string, expected string, ptr interface{}) error {
166180
var findErr error
167181
i := funk.Find(list, func(x map[string]interface{}) bool {
168182
if findErr != nil {
@@ -183,7 +197,7 @@ func find(list []map[string]interface{}, prop string, expected string, ptr inter
183197
}
184198

185199
if i == nil {
186-
return errNotFound
200+
return ErrorNoMatch
187201
}
188202

189203
o := objx.New(i).MustJSON()

0 commit comments

Comments
 (0)