@@ -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,64 +100,91 @@ var _ = Describe("Controllerutil", func() {
98
100
})
99
101
100
102
Describe ("CreateOrUpdate" , func () {
103
+ var deploy * appsv1.Deployment
104
+ var deplSpec appsv1.DeploymentSpec
105
+ var deplKey types.NamespacedName
106
+
107
+ BeforeEach (func () {
108
+ deploy = & appsv1.Deployment {
109
+ ObjectMeta : metav1.ObjectMeta {
110
+ Name : fmt .Sprintf ("deploy-%d" , rand .Int31 ()),
111
+ Namespace : "default" ,
112
+ },
113
+ }
101
114
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 {}
115
+ deplSpec = appsv1.DeploymentSpec {
116
+ Selector : & metav1.LabelSelector {
117
+ MatchLabels : map [string ]string {"foo" : "bar" },
118
+ },
119
+ Template : corev1.PodTemplateSpec {
120
+ ObjectMeta : metav1.ObjectMeta {
121
+ Labels : map [string ]string {
122
+ "foo" : "bar" ,
123
+ },
124
+ },
125
+ Spec : corev1.PodSpec {
126
+ Containers : []corev1.Container {
127
+ corev1.Container {
128
+ Name : "busybox" ,
129
+ Image : "busybox" ,
130
+ },
131
+ },
132
+ },
133
+ },
134
+ }
135
+
136
+ deplKey = types.NamespacedName {
137
+ Name : deploy .Name ,
138
+ Namespace : deploy .Namespace ,
139
+ }
140
+ })
105
141
106
- op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deplKey , depl , createDeployment )
142
+ It ("creates a new object if one doesn't exists" , func () {
143
+ op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deploy , deploymentSpecr (deplSpec ))
107
144
108
145
By ("returning OperationCreated" )
109
- Expect (op ).Should (BeEquivalentTo (controllerutil .OperationCreated ))
146
+ Expect (op ).To (BeEquivalentTo (controllerutil .OperationCreate ))
110
147
111
- By ("returning returning no error" )
112
- Expect (err ).ShouldNot (HaveOccurred ())
148
+ By ("returning no error" )
149
+ Expect (err ).NotTo (HaveOccurred ())
113
150
114
151
By ("actually having the deployment created" )
115
152
fetched := & appsv1.Deployment {}
116
- Expect (c .Get (context .TODO (), deplKey , fetched )).Should (Succeed ())
153
+ Expect (c .Get (context .TODO (), deplKey , fetched )).To (Succeed ())
117
154
})
118
155
119
- 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
-
156
+ It ("updates existing object" , func () {
126
157
var scale int32 = 2
158
+ op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deploy , deploymentSpecr (deplSpec ))
159
+ Expect (err ).NotTo (HaveOccurred ())
160
+ Expect (op ).To (BeEquivalentTo (controllerutil .OperationCreate ))
127
161
128
- Expect (c .Create (context .TODO (), depl )).Should (Succeed ())
129
-
130
- op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deplKey , & appsv1.Deployment {}, deploymentScaler (scale ))
131
-
162
+ op , err = controllerutil .CreateOrUpdate (context .TODO (), c , deploy , deploymentScaler (scale ))
132
163
By ("returning OperationUpdated" )
133
- Expect (op ).Should (BeEquivalentTo (controllerutil .OperationUpdated ))
164
+ Expect (op ).To (BeEquivalentTo (controllerutil .OperationUpdate ))
134
165
135
- By ("returning returning no error" )
136
- Expect (err ).ShouldNot (HaveOccurred ())
166
+ By ("returning no error" )
167
+ Expect (err ).NotTo (HaveOccurred ())
137
168
138
169
By ("actually having the deployment scaled" )
139
170
fetched := & appsv1.Deployment {}
140
- Expect (c .Get (context .TODO (), deplKey , fetched )).Should (Succeed ())
171
+ Expect (c .Get (context .TODO (), deplKey , fetched )).To (Succeed ())
141
172
Expect (* fetched .Spec .Replicas ).To (Equal (scale ))
142
173
})
143
174
144
175
It ("updates only changed objects" , func () {
145
- deplKey := types.NamespacedName {Name : "test-idempotency" , Namespace : "default" }
146
- depl := & appsv1.Deployment {}
176
+ op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deploy , deploymentSpecr (deplSpec ))
147
177
148
- op , err := controllerutil .CreateOrUpdate (context .TODO (), c , deplKey , depl , createDeployment )
149
- Expect (op ).Should (BeEquivalentTo (controllerutil .OperationCreated ))
150
- Expect (err ).ShouldNot (HaveOccurred ())
178
+ Expect (op ).To (BeEquivalentTo (controllerutil .OperationCreate ))
179
+ Expect (err ).NotTo (HaveOccurred ())
151
180
152
- op , err = controllerutil .CreateOrUpdate (context .TODO (), c , deplKey , depl , deploymentIdentity )
181
+ op , err = controllerutil .CreateOrUpdate (context .TODO (), c , deploy , deploymentIdentity )
153
182
154
183
By ("returning OperationNoop" )
155
- Expect (op ).Should (BeEquivalentTo (controllerutil .OperationNoop ))
184
+ Expect (op ).To (BeEquivalentTo (controllerutil .OperationNoop ))
156
185
157
- By ("returning returning no error" )
158
- Expect (err ).ShouldNot (HaveOccurred ())
186
+ By ("returning no error" )
187
+ Expect (err ).NotTo (HaveOccurred ())
159
188
})
160
189
})
161
190
})
@@ -166,24 +195,23 @@ type errMetaObj struct {
166
195
metav1.ObjectMeta
167
196
}
168
197
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
198
+ func deploymentSpecr ( spec appsv1. DeploymentSpec ) controllerutil. ReconcileFn {
199
+ return func ( obj runtime. Object ) error {
200
+ deploy := obj .( * appsv1. Deployment )
201
+ deploy .Spec = spec
202
+ return nil
203
+ }
175
204
}
176
205
177
- var deploymentIdentity controllerutil.TransformFn = func (in runtime.Object ) (runtime. Object , error ) {
178
- return in , nil
206
+ var deploymentIdentity controllerutil.ReconcileFn = func (obj runtime.Object ) error {
207
+ return nil
179
208
}
180
209
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 )
185
- out .Spec .Replicas = & replicas
186
- return out , nil
210
+ func deploymentScaler (replicas int32 ) controllerutil.ReconcileFn {
211
+ fn := func (obj runtime.Object ) error {
212
+ deploy := obj .(* appsv1.Deployment )
213
+ deploy .Spec .Replicas = & replicas
214
+ return nil
187
215
}
188
216
return fn
189
217
}
0 commit comments