@@ -2,6 +2,8 @@ package controllerutil_test
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "math/rand"
5
7
6
8
. "github.com/onsi/ginkgo"
7
9
. "github.com/onsi/gomega"
@@ -98,17 +100,49 @@ var _ = Describe("Controllerutil", func() {
98
100
})
99
101
100
102
Describe ("CreateOrUpdate" , func () {
103
+ var deploy * appsv1.Deployment
104
+ var deplKey types.NamespacedName
105
+
106
+ BeforeEach (func () {
107
+ deploy = & appsv1.Deployment {
108
+ Spec : appsv1.DeploymentSpec {
109
+ Selector : & metav1.LabelSelector {
110
+ MatchLabels : map [string ]string {"foo" : "bar" },
111
+ },
112
+ Template : corev1.PodTemplateSpec {
113
+ ObjectMeta : metav1.ObjectMeta {
114
+ Labels : map [string ]string {
115
+ "foo" : "bar" ,
116
+ },
117
+ },
118
+ Spec : corev1.PodSpec {
119
+ Containers : []corev1.Container {
120
+ corev1.Container {
121
+ Name : "foo" ,
122
+ Image : "busybox" ,
123
+ },
124
+ },
125
+ },
126
+ },
127
+ },
128
+ }
101
129
102
- It ("creates a new object if one doesn't exists" , func () {
103
- deplKey := types.NamespacedName {Name : "test-create" , Namespace : "default" }
104
- depl := & appsv1.Deployment {}
130
+ deploy .Name = fmt .Sprintf ("deploy-%d" , rand .Int31 ())
131
+ deploy .Namespace = "default"
105
132
106
- op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deplKey , depl , createDeployment )
133
+ deplKey = types.NamespacedName {
134
+ Name : deploy .Name ,
135
+ Namespace : deploy .Namespace ,
136
+ }
137
+ })
138
+
139
+ It ("creates a new object if one doesn't exists" , func () {
140
+ op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deploy )
107
141
108
142
By ("returning OperationCreated" )
109
- Expect (op ).Should (BeEquivalentTo (controllerutil .OperationCreated ))
143
+ Expect (op ).To (BeEquivalentTo (controllerutil .OperationCreate ))
110
144
111
- By ("returning returning no error" )
145
+ By ("returning no error" )
112
146
Expect (err ).ShouldNot (HaveOccurred ())
113
147
114
148
By ("actually having the deployment created" )
@@ -117,22 +151,15 @@ var _ = Describe("Controllerutil", func() {
117
151
})
118
152
119
153
It ("update existing object" , func () {
120
- deplKey := types.NamespacedName {Name : "test-update" , Namespace : "default" }
121
- d , _ := createDeployment (& appsv1.Deployment {})
122
- depl := d .(* appsv1.Deployment )
123
- depl .Name = "test-update"
124
- depl .Namespace = "default"
125
-
126
154
var scale int32 = 2
155
+ Expect (c .Create (context .TODO (), deploy )).Should (Succeed ())
127
156
128
- Expect (c .Create (context .TODO (), depl )).Should (Succeed ())
129
-
130
- op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deplKey , & appsv1.Deployment {}, deploymentScaler (scale ))
157
+ op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deploy , deploymentScaler (scale ))
131
158
132
159
By ("returning OperationUpdated" )
133
- Expect (op ).Should (BeEquivalentTo (controllerutil .OperationUpdated ))
160
+ Expect (op ).Should (BeEquivalentTo (controllerutil .OperationUpdate ))
134
161
135
- By ("returning returning no error" )
162
+ By ("returning no error" )
136
163
Expect (err ).ShouldNot (HaveOccurred ())
137
164
138
165
By ("actually having the deployment scaled" )
@@ -142,21 +169,46 @@ var _ = Describe("Controllerutil", func() {
142
169
})
143
170
144
171
It ("updates only changed objects" , func () {
145
- deplKey := types.NamespacedName {Name : "test-idempotency" , Namespace : "default" }
146
- depl := & appsv1.Deployment {}
172
+ op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deploy )
147
173
148
- op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deplKey , depl , createDeployment )
149
- Expect (op ).Should (BeEquivalentTo (controllerutil .OperationCreated ))
174
+ Expect (op ).Should (BeEquivalentTo (controllerutil .OperationCreate ))
150
175
Expect (err ).ShouldNot (HaveOccurred ())
151
176
152
- op , err = controllerutil .CreateOrUpdate (context .TODO (), c , deplKey , depl , deploymentIdentity )
177
+ op , err = controllerutil .CreateOrUpdate (context .TODO (), c , deploy , deploymentIdentity )
153
178
154
179
By ("returning OperationNoop" )
155
180
Expect (op ).Should (BeEquivalentTo (controllerutil .OperationNoop ))
156
181
157
- By ("returning returning no error" )
182
+ By ("returning no error" )
158
183
Expect (err ).ShouldNot (HaveOccurred ())
159
184
})
185
+
186
+ It ("allows chaining transforms" , func () {
187
+ scaleToTwo := deploymentScaler (2 )
188
+ scaleToThree := deploymentScaler (3 )
189
+
190
+ op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deploy , scaleToTwo , scaleToThree )
191
+
192
+ Expect (op ).Should (BeEquivalentTo (controllerutil .OperationCreate ))
193
+ Expect (err ).ShouldNot (HaveOccurred ())
194
+
195
+ By ("applying the last scale" )
196
+ fetched := & appsv1.Deployment {}
197
+ Expect (c .Get (context .TODO (), deplKey , fetched )).Should (Succeed ())
198
+ Expect (* fetched .Spec .Replicas ).To (Equal (int32 (3 )))
199
+ })
200
+
201
+ It ("doesn't mutate the desired object" , func () {
202
+ scaleToTwo := deploymentScaler (2 )
203
+ scaleToThree := deploymentScaler (3 )
204
+
205
+ op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deploy , scaleToTwo , scaleToThree )
206
+
207
+ Expect (op ).Should (BeEquivalentTo (controllerutil .OperationCreate ))
208
+ Expect (err ).ShouldNot (HaveOccurred ())
209
+
210
+ Expect (deploy .Spec .Replicas ).To (BeNil ())
211
+ })
160
212
})
161
213
})
162
214
@@ -166,24 +218,16 @@ type errMetaObj struct {
166
218
metav1.ObjectMeta
167
219
}
168
220
169
- var createDeployment controllerutil.TransformFn = func (in runtime.Object ) (runtime.Object , error ) {
170
- out := in .(* appsv1.Deployment )
171
- out .Spec .Selector = & metav1.LabelSelector {MatchLabels : map [string ]string {"foo" : "bar" }}
172
- out .Spec .Template .ObjectMeta .Labels = map [string ]string {"foo" : "bar" }
173
- out .Spec .Template .Spec .Containers = []corev1.Container {corev1.Container {Name : "foo" , Image : "busybox" }}
174
- return out , nil
175
- }
176
-
177
- var deploymentIdentity controllerutil.TransformFn = func (in runtime.Object ) (runtime.Object , error ) {
178
- return in , nil
221
+ var deploymentIdentity controllerutil.ReconcileFn = func (obj , existing runtime.Object ) error {
222
+ existing .(* appsv1.Deployment ).DeepCopyInto (obj .(* appsv1.Deployment ))
223
+ return nil
179
224
}
180
225
181
- func deploymentScaler (replicas int32 ) controllerutil.TransformFn {
182
- fn := func (in runtime.Object ) (runtime.Object , error ) {
183
- d , _ := createDeployment (in )
184
- out := d .(* appsv1.Deployment )
226
+ func deploymentScaler (replicas int32 ) controllerutil.ReconcileFn {
227
+ fn := func (obj , existing runtime.Object ) error {
228
+ out := obj .(* appsv1.Deployment )
185
229
out .Spec .Replicas = & replicas
186
- return out , nil
230
+ return nil
187
231
}
188
232
return fn
189
233
}
0 commit comments