Skip to content

Commit 6dcb234

Browse files
committed
Skip update when instance is created
1 parent 6d2f5e5 commit 6dcb234

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

pkg/scaffold/controller/controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,23 @@ 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
251252
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
252253
if err != nil && errors.IsNotFound(err) {
253254
log.Info("Creating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
254255
err = r.Create(context.TODO(), deploy)
255256
if err != nil {
256257
return reconcile.Result{}, err
258+
} else {
259+
created = true
257260
}
258261
} else if err != nil {
259262
return reconcile.Result{}, err
260263
}
261264
262265
// TODO(user): Change this for the object type created by your controller
263266
// Update the found object and write the result back if there are any changes
264-
if !reflect.DeepEqual(deploy.Spec, found.Spec) {
267+
if !created && !reflect.DeepEqual(deploy.Spec, found.Spec) {
265268
found.Spec = deploy.Spec
266269
log.Info("Updating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
267270
err = r.Update(context.TODO(), found)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,23 @@ 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
147148
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
148149
if err != nil && errors.IsNotFound(err) {
149150
log.Info("Creating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
150151
err = r.Create(context.TODO(), deploy)
151152
if err != nil {
152153
return reconcile.Result{}, err
154+
} else {
155+
created = true
153156
}
154157
} else if err != nil {
155158
return reconcile.Result{}, err
156159
}
157160

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

0 commit comments

Comments
 (0)