Skip to content

Commit 4a98d3a

Browse files
committed
update example with better structured logging; log errors
1 parent dec71a1 commit 4a98d3a

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

commands/operator-sdk/cmd/test/local.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
101101
defer func() {
102102
err := os.Remove(tlConfig.namespacedManPath)
103103
if err != nil {
104-
log.Fatal("could not delete temporary namespace manifest file")
104+
log.Fatalf("could not delete temporary namespace manifest file: (%v)", err)
105105
}
106106
}()
107107
}
@@ -137,7 +137,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
137137
defer func() {
138138
err := os.Remove(tlConfig.globalManPath)
139139
if err != nil {
140-
log.Fatal("could not delete global manifest file")
140+
log.Fatalf("could not delete global manifest file: (%v)", err)
141141
}
142142
}()
143143
}

example/memcached-operator/memcached_controller.go.tmpl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ type ReconcileMemcached struct {
8787
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
8888
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
8989
func (r *ReconcileMemcached) Reconcile(request reconcile.Request) (reconcile.Result, error) {
90-
log.Info("Reconciling Memcached", "Request.Namespace", request.Namespace, "Requst.Name", request.Name)
90+
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Requst.Name", request.Name)
91+
reqLogger.Info("Reconciling Memcached")
9192

9293
// Fetch the Memcached instance
9394
memcached := &cachev1alpha1.Memcached{}
@@ -97,11 +98,11 @@ func (r *ReconcileMemcached) Reconcile(request reconcile.Request) (reconcile.Res
9798
// Request object not found, could have been deleted after reconcile request.
9899
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
99100
// Return and don't requeue
100-
log.Info("Memcached resource not found. Ignoring since object must be deleted", "Request.Namespace", request.Namespace, "Requst.Name", request.Name)
101+
reqLogger.Info("Memcached resource not found. Ignoring since object must be deleted")
101102
return reconcile.Result{}, nil
102103
}
103104
// Error reading the object - requeue the request.
104-
log.Error(err, "failed to get Memcached", "Request.Namespace", request.Namespace, "Requst.Name", request.Name)
105+
reqLogger.Error(err, "failed to get Memcached")
105106
return reconcile.Result{}, err
106107
}
107108

@@ -111,16 +112,16 @@ func (r *ReconcileMemcached) Reconcile(request reconcile.Request) (reconcile.Res
111112
if err != nil && errors.IsNotFound(err) {
112113
// Define a new deployment
113114
dep := r.deploymentForMemcached(memcached)
114-
log.Info("Creating a new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
115+
reqLogger.Info("Creating a new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
115116
err = r.client.Create(context.TODO(), dep)
116117
if err != nil {
117-
log.Error(err, "failed to create new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
118+
reqLogger.Error(err, "failed to create new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
118119
return reconcile.Result{}, err
119120
}
120121
// Deployment created successfully - return and requeue
121122
return reconcile.Result{Requeue: true}, nil
122123
} else if err != nil {
123-
log.Error(err, "failed to get Deployment")
124+
reqLogger.Error(err, "failed to get Deployment")
124125
return reconcile.Result{}, err
125126
}
126127

@@ -130,7 +131,7 @@ func (r *ReconcileMemcached) Reconcile(request reconcile.Request) (reconcile.Res
130131
found.Spec.Replicas = &size
131132
err = r.client.Update(context.TODO(), found)
132133
if err != nil {
133-
log.Error(err, "failed to update Deployment", "Deployment.Namespace", found.Namespace, "Deployment.Name", found.Name)
134+
reqLogger.Error(err, "failed to update Deployment", "Deployment.Namespace", found.Namespace, "Deployment.Name", found.Name)
134135
return reconcile.Result{}, err
135136
}
136137
// Spec updated - return and requeue
@@ -144,7 +145,7 @@ func (r *ReconcileMemcached) Reconcile(request reconcile.Request) (reconcile.Res
144145
listOps := &client.ListOptions{Namespace: memcached.Namespace, LabelSelector: labelSelector}
145146
err = r.client.List(context.TODO(), listOps, podList)
146147
if err != nil {
147-
log.Error(err, "failed to list pods", "Memcached.Namespace", memcached.Namespace, "Memcached.Name", memcached.Name)
148+
reqLogger.Error(err, "failed to list pods", "Memcached.Namespace", memcached.Namespace, "Memcached.Name", memcached.Name)
148149
return reconcile.Result{}, err
149150
}
150151
podNames := getPodNames(podList.Items)
@@ -154,7 +155,7 @@ func (r *ReconcileMemcached) Reconcile(request reconcile.Request) (reconcile.Res
154155
memcached.Status.Nodes = podNames
155156
err := r.client.Update(context.TODO(), memcached)
156157
if err != nil {
157-
log.Error(err, "failed to update Memcached status")
158+
reqLogger.Error(err, "failed to update Memcached status")
158159
return reconcile.Result{}, err
159160
}
160161
}

pkg/scaffold/controller_kind.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ type Reconcile{{ .Resource.Kind }} struct {
124124
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
125125
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
126126
func (r *Reconcile{{ .Resource.Kind }}) Reconcile(request reconcile.Request) (reconcile.Result, error) {
127-
log.Info("Reconciling {{ .Resource.Kind }}", "Request.Namespace", request.Namespace, "Request.Name", request.Name)
127+
reqLogger := reqLogger.WithValues("Request.Namespace", request.Namespace, "Requst.Name", request.Name)
128+
reqLogger.Info("Reconciling {{ .Resource.Kind }}")
128129
129130
// Fetch the {{ .Resource.Kind }} instance
130131
instance := &{{ .Resource.Group}}{{ .Resource.Version }}.{{ .Resource.Kind }}{}
@@ -152,7 +153,7 @@ func (r *Reconcile{{ .Resource.Kind }}) Reconcile(request reconcile.Request) (re
152153
found := &corev1.Pod{}
153154
err = r.client.Get(context.TODO(), types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}, found)
154155
if err != nil && errors.IsNotFound(err) {
155-
log.Info("Creating a new Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name)
156+
reqLogger.Info("Creating a new Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name)
156157
err = r.client.Create(context.TODO(), pod)
157158
if err != nil {
158159
return reconcile.Result{}, err
@@ -165,7 +166,7 @@ func (r *Reconcile{{ .Resource.Kind }}) Reconcile(request reconcile.Request) (re
165166
}
166167
167168
// Pod already exists - don't requeue
168-
log.Info("Skip reconcile: Pod already exists", "Pod.Namespace", found.Namespace, "Pod.Name", found.Name)
169+
reqLogger.Info("Skip reconcile: Pod already exists", "Pod.Namespace", found.Namespace, "Pod.Name", found.Name)
169170
return reconcile.Result{}, nil
170171
}
171172

pkg/scaffold/controller_kind_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ type ReconcileAppService struct {
121121
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
122122
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
123123
func (r *ReconcileAppService) Reconcile(request reconcile.Request) (reconcile.Result, error) {
124-
log.Info("Reconciling AppService", "Request.Namespace", request.Namespace, "Request.Name", request.Name)
124+
reqLogger := reqLogger.WithValues("Request.Namespace", request.Namespace, "Requst.Name", request.Name)
125+
reqLogger.Info("Reconciling AppService")
125126
126127
// Fetch the AppService instance
127128
instance := &appv1alpha1.AppService{}
@@ -149,7 +150,7 @@ func (r *ReconcileAppService) Reconcile(request reconcile.Request) (reconcile.Re
149150
found := &corev1.Pod{}
150151
err = r.client.Get(context.TODO(), types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}, found)
151152
if err != nil && errors.IsNotFound(err) {
152-
log.Info("Creating a new Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name)
153+
reqLogger.Info("Creating a new Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name)
153154
err = r.client.Create(context.TODO(), pod)
154155
if err != nil {
155156
return reconcile.Result{}, err
@@ -162,7 +163,7 @@ func (r *ReconcileAppService) Reconcile(request reconcile.Request) (reconcile.Re
162163
}
163164
164165
// Pod already exists - don't requeue
165-
log.Info("Skip reconcile: Pod already exists", "Pod.Namespace", found.Namespace, "Pod.Name", found.Name)
166+
reqLogger.Info("Skip reconcile: Pod already exists", "Pod.Namespace", found.Namespace, "Pod.Name", found.Name)
166167
return reconcile.Result{}, nil
167168
}
168169

0 commit comments

Comments
 (0)