Skip to content

Commit 07c81c2

Browse files
committed
refactor(kubernetes): use idiomatic error handling
1 parent 5b14719 commit 07c81c2

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"
@@ -95,7 +96,10 @@ func ContextFromIP(apiServer string) (*Cluster, *Context, error) {
9596
return nil, nil, err
9697
}
9798

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

@@ -106,7 +110,10 @@ func ContextFromIP(apiServer string) (*Cluster, *Context, error) {
106110
return nil, nil, err
107111
}
108112

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

@@ -128,7 +135,10 @@ func IPFromContext(name string) (ip string, err error) {
128135
return "", err
129136
}
130137

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

@@ -140,9 +150,10 @@ func IPFromContext(name string) (ip string, err error) {
140150
}
141151

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

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

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

186200
if i == nil {
187-
return errNotFound
201+
return ErrorNoMatch
188202
}
189203

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

0 commit comments

Comments
 (0)