@@ -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() {
7071func 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 {
126127func 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
138139func 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
158159func 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) {
281282func 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
308309func 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}
0 commit comments