Skip to content

Commit ab14dc5

Browse files
authored
Ensure consistent errors (#147)
1 parent 82f4dc9 commit ab14dc5

File tree

21 files changed

+138
-118
lines changed

21 files changed

+138
-118
lines changed

cmd/testrunner/crds/crds.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"path/filepath"
1010
"strings"
1111

12+
"github.com/pkg/errors"
13+
1214
"github.com/ghodss/yaml"
1315
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
1416
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
@@ -21,22 +23,22 @@ import (
2123
func EnsureCreation(config *rest.Config, deployDir string) error {
2224
apiextensionsClientSet, err := apiextensionsclientset.NewForConfig(config)
2325
if err != nil {
24-
return fmt.Errorf("error creating apiextensions client set: %v", err)
26+
return errors.Errorf("error creating apiextensions client set: %s", err)
2527
}
2628

2729
crdFilePaths, err := allCrds(deployDir)
2830
if err != nil {
29-
return fmt.Errorf("error walking deploy directory: %v", err)
31+
return errors.Errorf("error walking deploy directory: %s", err)
3032
}
3133

3234
for _, filePath := range crdFilePaths {
3335
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
3436
data, err := ioutil.ReadFile(filePath)
3537
if err != nil {
36-
return fmt.Errorf("error reading file: %v", err)
38+
return errors.Errorf("error reading file: %s", err)
3739
}
3840
if err := marshalCRDFromYAMLBytes(data, crd); err != nil {
39-
return fmt.Errorf("error converting yaml bytes to CRD: %v", err)
41+
return errors.Errorf("error converting yaml bytes to CRD: %s", err)
4042
}
4143
_, err = apiextensionsClientSet.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd)
4244

@@ -46,7 +48,7 @@ func EnsureCreation(config *rest.Config, deployDir string) error {
4648
}
4749

4850
if err != nil {
49-
return fmt.Errorf("error creating custom resource definition: %v", err)
51+
return errors.Errorf("error creating custom resource definition: %s", err)
5052
}
5153
}
5254
return nil

cmd/testrunner/main.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"path"
1111
"time"
1212

13+
"github.com/pkg/errors"
14+
1315
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/client"
1416

1517
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/pod"
@@ -19,8 +21,7 @@ import (
1921
appsv1 "k8s.io/api/apps/v1"
2022
corev1 "k8s.io/api/core/v1"
2123
rbacv1 "k8s.io/api/rbac/v1"
22-
"k8s.io/apimachinery/pkg/api/errors"
23-
apierrors "k8s.io/apimachinery/pkg/api/errors"
24+
apiErrors "k8s.io/apimachinery/pkg/api/errors"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
"k8s.io/apimachinery/pkg/runtime"
2627
"k8s.io/apimachinery/pkg/types"
@@ -70,53 +71,53 @@ func main() {
7071
func runCmd(f flags) error {
7172
config, err := rest.InClusterConfig()
7273
if err != nil {
73-
return fmt.Errorf("error retreiving kubernetes config: %v", err)
74+
return errors.Errorf("could not get kubernetes config: %s", err)
7475
}
7576

7677
k8s, err := k8sClient.New(config, k8sClient.Options{})
7778
if err != nil {
78-
return fmt.Errorf("error creating kubernetes client %v", err)
79+
return errors.Errorf("could not create client: %s", err)
7980
}
8081

8182
c := client.NewClient(k8s)
8283

8384
if err := ensureNamespace(f.namespace, c); err != nil {
84-
return fmt.Errorf("error ensuring namespace: %v", err)
85+
return errors.Errorf("could not create namespace: %s", err)
8586
}
8687

8788
fmt.Printf("Ensured namespace: %s\n", f.namespace)
8889

8990
if err := crds.EnsureCreation(config, f.deployDir); err != nil {
90-
return fmt.Errorf("error ensuring CRDs: %v", err)
91+
return errors.Errorf("could not ensure CRDs: %s", err)
9192
}
9293

9394
fmt.Println("Ensured CRDs")
9495
if err := deployOperator(f, c); err != nil {
95-
return fmt.Errorf("error deploying operator: %v", err)
96+
return errors.Errorf("could not deploy operator: %s", err)
9697
}
9798
fmt.Println("Successfully deployed the operator")
9899

99100
testToRun := "test/operator-sdk-test.yaml"
100101
if err := buildKubernetesResourceFromYamlFile(c, testToRun, &corev1.Pod{}, withNamespace(f.namespace), withTestImage(f.testImage), withTest(f.test), withEnvVar("PERFORM_CLEANUP", f.performCleanup)); err != nil {
101-
return fmt.Errorf("error deploying test: %v", err)
102+
return errors.Errorf("error deploying test: %s", err)
102103
}
103104

104105
nsName := types.NamespacedName{Name: "operator-sdk-test", Namespace: f.namespace}
105106

106107
fmt.Println("Waiting for pod to be ready...")
107108
testPod, err := pod.WaitForPhase(c, nsName, time.Second*5, time.Minute*5, corev1.PodRunning)
108109
if err != nil {
109-
return fmt.Errorf("error waiting for test pod to be created: %v", err)
110+
return errors.Errorf("error waiting for test pod to be created: %s", err)
110111
}
111112

112113
fmt.Println("Tailing pod logs...")
113114
if err := tailPodLogs(config, testPod); err != nil {
114-
return err
115+
return errors.Errorf("could not tail pod logs: %s", err)
115116
}
116117

117118
_, err = pod.WaitForPhase(c, nsName, time.Second*5, time.Minute, corev1.PodSucceeded)
118119
if err != nil {
119-
return fmt.Errorf("error waiting for test to finish: %v", err)
120+
return errors.Errorf("error waiting for test to finish: %s", err)
120121
}
121122

122123
fmt.Println("Test passed!")
@@ -126,19 +127,19 @@ func runCmd(f flags) error {
126127
func tailPodLogs(config *rest.Config, testPod corev1.Pod) error {
127128
clientset, err := kubernetes.NewForConfig(config)
128129
if err != nil {
129-
return fmt.Errorf("error getting clientset: %v", err)
130+
return errors.Errorf("could not get clientset: %s", err)
130131
}
131132

132133
if err := pod.GetLogs(os.Stdout, pod.CoreV1FollowStreamer(testPod, clientset.CoreV1())); err != nil {
133-
return fmt.Errorf("error tailing logs: %+v", err)
134+
return errors.Errorf("could not tail pod logs: %s", err)
134135
}
135136
return nil
136137
}
137138

138139
func ensureNamespace(ns string, client client.Client) error {
139140
err := client.Get(context.TODO(), types.NamespacedName{Name: ns}, &corev1.Namespace{})
140-
if err != nil && !errors.IsNotFound(err) {
141-
return fmt.Errorf("error creating namespace: %v", err)
141+
if err != nil && !apiErrors.IsNotFound(err) {
142+
return errors.Errorf("error creating namespace: %s", err)
142143
} else if err == nil {
143144
fmt.Printf("Namespace %s already exists!\n", ns)
144145
return nil
@@ -150,32 +151,32 @@ func ensureNamespace(ns string, client client.Client) error {
150151
},
151152
}
152153
if err := client.Create(context.TODO(), &newNamespace); err != nil {
153-
return fmt.Errorf("error creating namespace: %s", err)
154+
return errors.Errorf("error creating namespace: %s", err)
154155
}
155156
return nil
156157
}
157158

158159
func deployOperator(f flags, c client.Client) error {
159160
if err := buildKubernetesResourceFromYamlFile(c, path.Join(f.deployDir, "role.yaml"), &rbacv1.Role{}, withNamespace(f.namespace)); err != nil {
160-
return fmt.Errorf("error building operator role: %v", err)
161+
return errors.Errorf("error building operator role: %s", err)
161162
}
162163
fmt.Println("Successfully created the operator Role")
163164

164165
if err := buildKubernetesResourceFromYamlFile(c, path.Join(f.deployDir, "service_account.yaml"), &corev1.ServiceAccount{}, withNamespace(f.namespace)); err != nil {
165-
return fmt.Errorf("error building operator service account: %v", err)
166+
return errors.Errorf("error building operator service account: %s", err)
166167
}
167168
fmt.Println("Successfully created the operator Service Account")
168169

169170
if err := buildKubernetesResourceFromYamlFile(c, path.Join(f.deployDir, "role_binding.yaml"), &rbacv1.RoleBinding{}, withNamespace(f.namespace)); err != nil {
170-
return fmt.Errorf("error building operator role binding: %v", err)
171+
return errors.Errorf("error building operator role binding: %s", err)
171172
}
172173
fmt.Println("Successfully created the operator Role Binding")
173174
if err := buildKubernetesResourceFromYamlFile(c, path.Join(f.deployDir, "operator.yaml"),
174175
&appsv1.Deployment{},
175176
withNamespace(f.namespace),
176177
withOperatorImage(f.operatorImage),
177178
withVersionUpgradeHookImage(f.versionUpgradeHookImage)); err != nil {
178-
return fmt.Errorf("error building operator deployment: %v", err)
179+
return errors.Errorf("error building operator deployment: %s", err)
179180
}
180181
fmt.Println("Successfully created the operator Deployment")
181182
return nil
@@ -281,11 +282,11 @@ func withTest(test string) func(obj runtime.Object) {
281282
func buildKubernetesResourceFromYamlFile(c client.Client, yamlFilePath string, obj runtime.Object, options ...func(obj runtime.Object)) error {
282283
data, err := ioutil.ReadFile(yamlFilePath)
283284
if err != nil {
284-
return fmt.Errorf("error reading file: %v", err)
285+
return errors.Errorf("error reading file: %s", err)
285286
}
286287

287288
if err := marshalRuntimeObjectFromYAMLBytes(data, obj); err != nil {
288-
return fmt.Errorf("error converting yaml bytes to service account: %v", err)
289+
return errors.Errorf("error converting yaml bytes to service account: %s", err)
289290
}
290291

291292
for _, opt := range options {
@@ -307,10 +308,10 @@ func marshalRuntimeObjectFromYAMLBytes(bytes []byte, obj runtime.Object) error {
307308

308309
func createOrUpdate(c client.Client, obj runtime.Object) error {
309310
if err := c.Create(context.TODO(), obj); err != nil {
310-
if apierrors.IsAlreadyExists(err) {
311+
if apiErrors.IsAlreadyExists(err) {
311312
return c.Update(context.TODO(), obj)
312313
}
313-
return fmt.Errorf("error creating %s in kubernetes: %v", obj.GetObjectKind(), err)
314+
return errors.Errorf("error creating %s in kubernetes: %s", obj.GetObjectKind(), err)
314315
}
315316
return nil
316317
}

cmd/versionhook/main.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package main
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"io"
87
"io/ioutil"
98
"os"
109
"strings"
1110
"time"
1211

12+
"github.com/pkg/errors"
13+
1314
"github.com/mongodb/mongodb-kubernetes-operator/pkg/agenthealth"
1415
"go.uber.org/zap"
1516
corev1 "k8s.io/api/core/v1"
@@ -108,7 +109,7 @@ func waitForAgentHealthStatus() (agenthealth.Health, error) {
108109

109110
status, ok := health.Healthiness[getHostname()]
110111
if !ok {
111-
return agenthealth.Health{}, fmt.Errorf("couldn't find status for hostname %s", getHostname())
112+
return agenthealth.Health{}, errors.Errorf("couldn't find status for hostname %s", getHostname())
112113
}
113114

114115
// We determine if the file has been updated by checking if the process is not in goal state.
@@ -117,7 +118,7 @@ func waitForAgentHealthStatus() (agenthealth.Health, error) {
117118
return health, nil
118119
}
119120
}
120-
return agenthealth.Health{}, fmt.Errorf("agenth health status not ready after waiting %s", pollingDuration.String())
121+
return agenthealth.Health{}, errors.Errorf("agent health status not ready after waiting %s", pollingDuration.String())
121122

122123
}
123124

@@ -126,13 +127,13 @@ func waitForAgentHealthStatus() (agenthealth.Health, error) {
126127
func getAgentHealthStatus() (agenthealth.Health, error) {
127128
f, err := os.Open(os.Getenv(agentStatusFilePathEnv))
128129
if err != nil {
129-
return agenthealth.Health{}, fmt.Errorf("error opening file: %s", err)
130+
return agenthealth.Health{}, errors.Errorf("could not open file: %s", err)
130131
}
131132
defer f.Close()
132133

133134
h, err := readAgentHealthStatus(f)
134135
if err != nil {
135-
return agenthealth.Health{}, fmt.Errorf("error reading health status: %s", err)
136+
return agenthealth.Health{}, errors.Errorf("could not read health status file: %s", err)
136137
}
137138
return h, err
138139
}
@@ -159,7 +160,7 @@ func getHostname() string {
159160
func shouldDeletePod(health agenthealth.Health) (bool, error) {
160161
status, ok := health.ProcessPlans[getHostname()]
161162
if !ok {
162-
return false, fmt.Errorf("hostname %s was not in the process plans", getHostname())
163+
return false, errors.Errorf("hostname %s was not in the process plans", getHostname())
163164
}
164165
return isWaitingToBeDeleted(status), nil
165166
}
@@ -186,15 +187,15 @@ func isWaitingToBeDeleted(healthStatus agenthealth.MmsDirectorStatus) bool {
186187
func deletePod() error {
187188
thisPod, err := getThisPod()
188189
if err != nil {
189-
return fmt.Errorf("error getting this pod: %s", err)
190+
return errors.Errorf("could not get pod: %s", err)
190191
}
191192
k8sClient, err := inClusterClient()
192193
if err != nil {
193-
return fmt.Errorf("error getting client: %s", err)
194+
return errors.Errorf("could not get client: %s", err)
194195
}
195196

196197
if err := k8sClient.Delete(context.TODO(), &thisPod); err != nil {
197-
return fmt.Errorf("error deleting pod: %s", err)
198+
return errors.Errorf("could not delete pod: %s", err)
198199
}
199200
return nil
200201
}
@@ -203,12 +204,12 @@ func deletePod() error {
203204
func getThisPod() (corev1.Pod, error) {
204205
podName := getHostname()
205206
if podName == "" {
206-
return corev1.Pod{}, fmt.Errorf("environment variable HOSTNAME was not present")
207+
return corev1.Pod{}, errors.Errorf("environment variable HOSTNAME was not present")
207208
}
208209

209210
ns, err := getNamespace()
210211
if err != nil {
211-
return corev1.Pod{}, fmt.Errorf("error reading namespace: %+v", err)
212+
return corev1.Pod{}, errors.Errorf("could not read namespace: %s", err)
212213
}
213214

214215
return corev1.Pod{
@@ -222,12 +223,12 @@ func getThisPod() (corev1.Pod, error) {
222223
func inClusterClient() (client.Client, error) {
223224
config, err := rest.InClusterConfig()
224225
if err != nil {
225-
return nil, fmt.Errorf("error getting cluster config: %+v", err)
226+
return nil, errors.Errorf("could not get cluster config: %%s", err)
226227
}
227228

228229
k8sClient, err := client.New(config, client.Options{})
229230
if err != nil {
230-
return nil, fmt.Errorf("error creating client: %+v", err)
231+
return nil, errors.Errorf("could not create client: %s", err)
231232
}
232233
return k8sClient, nil
233234
}

0 commit comments

Comments
 (0)