Skip to content

Commit

Permalink
Directly return after Create in Reconcile
Browse files Browse the repository at this point in the history
  • Loading branch information
xychu committed Jan 10, 2019
1 parent 985a292 commit 6ef4807
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
9 changes: 2 additions & 7 deletions pkg/scaffold/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,18 @@ func (r *Reconcile{{ .Resource.Kind }}) Reconcile(request reconcile.Request) (re
// TODO(user): Change this for the object type created by your controller
// Check if the Deployment already exists
found := &appsv1.Deployment{}
created := false
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
log.Info("Creating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
err = r.Create(context.TODO(), deploy)
if err != nil {
return reconcile.Result{}, err
} else {
created = true
}
return reconcile.Result{}, err
} else if err != nil {
return reconcile.Result{}, err
}
// TODO(user): Change this for the object type created by your controller
// Update the found object and write the result back if there are any changes
if !created && !reflect.DeepEqual(deploy.Spec, found.Spec) {
if !reflect.DeepEqual(deploy.Spec, found.Spec) {
found.Spec = deploy.Spec
log.Info("Updating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
err = r.Update(context.TODO(), found)
Expand Down
9 changes: 2 additions & 7 deletions test/project/pkg/controller/firstmate/firstmate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,18 @@ func (r *ReconcileFirstMate) Reconcile(request reconcile.Request) (reconcile.Res
// TODO(user): Change this for the object type created by your controller
// Check if the Deployment already exists
found := &appsv1.Deployment{}
created := false
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
log.Info("Creating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
err = r.Create(context.TODO(), deploy)
if err != nil {
return reconcile.Result{}, err
} else {
created = true
}
return reconcile.Result{}, err
} else if err != nil {
return reconcile.Result{}, err
}

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

0 comments on commit 6ef4807

Please sign in to comment.