Skip to content

Commit 6ef4807

Browse files
committed
Directly return after Create in Reconcile
1 parent 985a292 commit 6ef4807

File tree

2 files changed

+4
-14
lines changed

2 files changed

+4
-14
lines changed

pkg/scaffold/controller/controller.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,23 +248,18 @@ func (r *Reconcile{{ .Resource.Kind }}) Reconcile(request reconcile.Request) (re
248248
// TODO(user): Change this for the object type created by your controller
249249
// Check if the Deployment already exists
250250
found := &appsv1.Deployment{}
251-
created := false
252251
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
253252
if err != nil && errors.IsNotFound(err) {
254253
log.Info("Creating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
255254
err = r.Create(context.TODO(), deploy)
256-
if err != nil {
257-
return reconcile.Result{}, err
258-
} else {
259-
created = true
260-
}
255+
return reconcile.Result{}, err
261256
} else if err != nil {
262257
return reconcile.Result{}, err
263258
}
264259
265260
// TODO(user): Change this for the object type created by your controller
266261
// Update the found object and write the result back if there are any changes
267-
if !created && !reflect.DeepEqual(deploy.Spec, found.Spec) {
262+
if !reflect.DeepEqual(deploy.Spec, found.Spec) {
268263
found.Spec = deploy.Spec
269264
log.Info("Updating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
270265
err = r.Update(context.TODO(), found)

test/project/pkg/controller/firstmate/firstmate_controller.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,18 @@ func (r *ReconcileFirstMate) Reconcile(request reconcile.Request) (reconcile.Res
144144
// TODO(user): Change this for the object type created by your controller
145145
// Check if the Deployment already exists
146146
found := &appsv1.Deployment{}
147-
created := false
148147
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
149148
if err != nil && errors.IsNotFound(err) {
150149
log.Info("Creating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
151150
err = r.Create(context.TODO(), deploy)
152-
if err != nil {
153-
return reconcile.Result{}, err
154-
} else {
155-
created = true
156-
}
151+
return reconcile.Result{}, err
157152
} else if err != nil {
158153
return reconcile.Result{}, err
159154
}
160155

161156
// TODO(user): Change this for the object type created by your controller
162157
// Update the found object and write the result back if there are any changes
163-
if !created && !reflect.DeepEqual(deploy.Spec, found.Spec) {
158+
if !reflect.DeepEqual(deploy.Spec, found.Spec) {
164159
found.Spec = deploy.Spec
165160
log.Info("Updating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
166161
err = r.Update(context.TODO(), found)

0 commit comments

Comments
 (0)