Skip to content

Commit adbc7af

Browse files
committed
Call MutateFn on CreateOrUpdate regardless of the operation
1 parent 6443f37 commit adbc7af

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

pkg/controller/controllerutil/controllerutil.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,23 @@ const ( // They should complete the sentence "Deployment default/foo has been ..
116116

117117
// CreateOrUpdate creates or updates the given object obj in the Kubernetes
118118
// cluster. The object's desired state should be reconciled with the existing
119-
// state using the passed in ReconcileFn. obj must be a struct pointer so that
119+
// state using the passed in MutateFn. obj is just a struct pointer so that
120120
// obj can be updated with the content returned by the Server.
121121
//
122+
// The MutateFn is called regardless of creating or updating an object.
123+
//
122124
// It returns the executed operation and an error.
123-
func CreateOrUpdate(ctx context.Context, c client.Client, obj runtime.Object, f MutateFn) (OperationResult, error) {
125+
func CreateOrUpdate(ctx context.Context, c client.Client, obj runtime.Object, f MutateFn) (OperationResult, error) { // nolint: gocyclo
124126
key, err := client.ObjectKeyFromObject(obj)
125127
if err != nil {
126128
return OperationResultNone, err
127129
}
128130

129131
if err := c.Get(ctx, key, obj); err != nil {
130132
if errors.IsNotFound(err) {
133+
if err := f(obj); err != nil {
134+
return OperationResultNone, err
135+
}
131136
if err := c.Create(ctx, obj); err != nil {
132137
return OperationResultNone, err
133138
}

pkg/controller/controllerutil/controllerutil_test.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ var _ = Describe("Controllerutil", func() {
151151
},
152152
}
153153

154-
deploy.Spec = deplSpec
155-
156154
deplKey = types.NamespacedName{
157155
Name: deploy.Name,
158156
Namespace: deploy.Namespace,
@@ -162,15 +160,20 @@ var _ = Describe("Controllerutil", func() {
162160
It("creates a new object if one doesn't exists", func() {
163161
op, err := controllerutil.CreateOrUpdate(context.TODO(), c, deploy, deploymentSpecr(deplSpec))
164162

165-
By("returning OperationResultCreated")
166-
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultCreated))
167-
168163
By("returning no error")
169164
Expect(err).NotTo(HaveOccurred())
170165

166+
By("returning OperationResultCreated")
167+
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultCreated))
168+
171169
By("actually having the deployment created")
172170
fetched := &appsv1.Deployment{}
173171
Expect(c.Get(context.TODO(), deplKey, fetched)).To(Succeed())
172+
173+
By("applying MutateFn")
174+
Expect(fetched.Spec.Template.Spec.Containers).To(HaveLen(1))
175+
Expect(fetched.Spec.Template.Spec.Containers[0].Name).To(Equal(deplSpec.Template.Spec.Containers[0].Name))
176+
Expect(fetched.Spec.Template.Spec.Containers[0].Image).To(Equal(deplSpec.Template.Spec.Containers[0].Image))
174177
})
175178

176179
It("updates existing object", func() {
@@ -180,12 +183,12 @@ var _ = Describe("Controllerutil", func() {
180183
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultCreated))
181184

182185
op, err = controllerutil.CreateOrUpdate(context.TODO(), c, deploy, deploymentScaler(scale))
183-
By("returning OperationResultUpdated")
184-
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultUpdated))
185-
186186
By("returning no error")
187187
Expect(err).NotTo(HaveOccurred())
188188

189+
By("returning OperationResultUpdated")
190+
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultUpdated))
191+
189192
By("actually having the deployment scaled")
190193
fetched := &appsv1.Deployment{}
191194
Expect(c.Get(context.TODO(), deplKey, fetched)).To(Succeed())
@@ -199,12 +202,11 @@ var _ = Describe("Controllerutil", func() {
199202
Expect(err).NotTo(HaveOccurred())
200203

201204
op, err = controllerutil.CreateOrUpdate(context.TODO(), c, deploy, deploymentIdentity)
205+
By("returning no error")
206+
Expect(err).NotTo(HaveOccurred())
202207

203208
By("returning OperationResultNone")
204209
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultNone))
205-
206-
By("returning no error")
207-
Expect(err).NotTo(HaveOccurred())
208210
})
209211

210212
It("errors when reconcile renames an object", func() {
@@ -215,11 +217,11 @@ var _ = Describe("Controllerutil", func() {
215217

216218
op, err = controllerutil.CreateOrUpdate(context.TODO(), c, deploy, deploymentRenamer)
217219

218-
By("returning OperationResultNone")
219-
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultNone))
220-
221220
By("returning error")
222221
Expect(err).To(HaveOccurred())
222+
223+
By("returning OperationResultNone")
224+
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultNone))
223225
})
224226

225227
It("errors when object namespace changes", func() {
@@ -230,11 +232,11 @@ var _ = Describe("Controllerutil", func() {
230232

231233
op, err = controllerutil.CreateOrUpdate(context.TODO(), c, deploy, deploymentNamespaceChanger)
232234

233-
By("returning OperationResultNone")
234-
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultNone))
235-
236235
By("returning error")
237236
Expect(err).To(HaveOccurred())
237+
238+
By("returning OperationResultNone")
239+
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultNone))
238240
})
239241

240242
It("aborts immediately if there was an error initially retrieving the object", func() {

0 commit comments

Comments
 (0)