@@ -36,21 +36,35 @@ const (
36
36
toVersion = "v4.6.0"
37
37
38
38
// Binary patterns for cleanup
39
- binFromVersionPattern = "/tmp/kubebuilder" + fromVersion + "-*"
40
- binToVersionPattern = "/tmp/kubebuilder" + toVersion + "-*"
39
+ binFromVersionPath = "/tmp/kubebuilder" + fromVersion + "-*"
40
+ pathBinToVersion = "/tmp/kubebuilder" + toVersion + "-*"
41
+
42
+ controllerImplementation = `// Fetch the TestOperator instance
43
+ testOperator := &webappv1.TestOperator{}
44
+ err := r.Get(ctx, req.NamespacedName, testOperator)
45
+ if err != nil {
46
+ if errors.IsNotFound(err) {
47
+ log.Info("testOperator resource not found. Ignoring since object must be deleted")
48
+ return ctrl.Result{}, nil
49
+ }
50
+ log.Error(err, "Failed to get testOperator")
51
+ return ctrl.Result{}, err
52
+ }
53
+
54
+ log.Info("testOperator reconciled")`
41
55
)
42
56
43
57
var _ = Describe ("kubebuilder" , func () {
44
58
Context ("alpha update" , func () {
45
59
var (
46
60
mockProjectDir string
47
- binFromVersionPath string
61
+ pathBinFromVersion string
48
62
kbc * utils.TestContext
49
63
)
50
64
51
65
BeforeEach (func () {
52
66
var err error
53
- By ("setting up test context with current kubebuilder binary " )
67
+ By ("setting up test context with binary build from source " )
54
68
kbc , err = utils .NewTestContext (pluginutil .KubebuilderBinName , "GO111MODULE=on" )
55
69
Expect (err ).NotTo (HaveOccurred ())
56
70
Expect (kbc .Prepare ()).To (Succeed ())
@@ -60,20 +74,19 @@ var _ = Describe("kubebuilder", func() {
60
74
Expect (err ).NotTo (HaveOccurred ())
61
75
62
76
By ("downloading kubebuilder v4.5.2 binary to isolated /tmp directory" )
63
- binFromVersionPath , err = downloadKubebuilder ()
77
+ pathBinFromVersion , err = downloadKubebuilder ()
64
78
Expect (err ).NotTo (HaveOccurred ())
65
79
})
66
80
67
81
AfterEach (func () {
68
82
By ("cleaning up test artifacts" )
69
-
70
83
_ = os .RemoveAll (mockProjectDir )
71
- _ = os .RemoveAll (filepath .Dir (binFromVersionPath ))
84
+ _ = os .RemoveAll (filepath .Dir (pathBinFromVersion ))
72
85
73
86
// Clean up kubebuilder alpha update downloaded binaries
74
87
binaryPatterns := []string {
75
- binFromVersionPattern ,
76
- binToVersionPattern ,
88
+ pathBinFromVersion ,
89
+ pathBinToVersion ,
77
90
}
78
91
79
92
for _ , pattern := range binaryPatterns {
@@ -83,18 +96,16 @@ var _ = Describe("kubebuilder", func() {
83
96
}
84
97
}
85
98
86
- // Clean up TestContext
87
- if kbc != nil {
88
- kbc .Destroy ()
89
- }
99
+ _ = os .RemoveAll (kbc .Dir )
90
100
})
91
101
92
- It ("should update project from v4.5.2 to v4.6.0 preserving custom code " , func () {
102
+ It ("should update project from v4.5.2 to v4.6.0 without conflicts " , func () {
93
103
By ("creating mock project with kubebuilder v4.5.2" )
94
- createMockProject (mockProjectDir , binFromVersionPath )
104
+ createMockProject (mockProjectDir , pathBinFromVersion )
95
105
96
- By ("injecting custom code in API and controller" )
97
- injectCustomCode (mockProjectDir )
106
+ By ("adding custom code in API and controller" )
107
+ updateAPI (mockProjectDir )
108
+ updateController (mockProjectDir )
98
109
99
110
By ("initializing git repository and committing mock project" )
100
111
initializeGitRepo (mockProjectDir )
@@ -181,38 +192,36 @@ func createMockProject(projectDir, binaryPath string) {
181
192
Expect (err ).NotTo (HaveOccurred ())
182
193
}
183
194
184
- func injectCustomCode (projectDir string ) {
195
+ func updateController (projectDir string ) {
196
+ controllerFile := filepath .Join (projectDir , "internal" , "controller" , "testoperator_controller.go" )
197
+
198
+ err := pluginutil .ReplaceInFile (
199
+ controllerFile ,
200
+ "_ = logf.FromContext(ctx)" ,
201
+ "log := logf.FromContext(ctx)" ,
202
+ )
203
+ Expect (err ).NotTo (HaveOccurred ())
204
+
205
+ err = pluginutil .ReplaceInFile (
206
+ controllerFile ,
207
+ "// TODO(user): your logic here" ,
208
+ controllerImplementation ,
209
+ )
210
+ Expect (err ).NotTo (HaveOccurred ())
211
+ }
212
+
213
+ func updateAPI (projectDir string ) {
185
214
typesFile := filepath .Join (projectDir , "api" , "v1" , "testoperator_types.go" )
186
- err := pluginutil .InsertCode (
215
+ err := pluginutil .ReplaceInFile (
187
216
typesFile ,
188
217
"Foo string `json:\" foo,omitempty\" `" ,
189
218
`
190
219
// +kubebuilder:validation:Minimum=0
191
220
// +kubebuilder:validation:Maximum=3
192
221
// +kubebuilder:default=1
193
- // Size is the size of the memcached deployment
194
222
Size int32 ` + "`json:\" size,omitempty\" `" ,
195
223
)
196
- Expect (err ).NotTo (HaveOccurred ())
197
- controllerFile := filepath .Join (projectDir , "internal" , "controller" , "testoperator_controller.go" )
198
- err = pluginutil .InsertCode (
199
- controllerFile ,
200
- "// TODO(user): your logic here" ,
201
- `// Custom reconciliation logic
202
- log := ctrl.LoggerFrom(ctx)
203
- log.Info("Reconciling TestOperator")
204
-
205
- // Fetch the TestOperator instance
206
- testOperator := &webappv1.TestOperator{}
207
- err := r.Get(ctx, req.NamespacedName, testOperator)
208
- if err != nil {
209
- return ctrl.Result{}, client.IgnoreNotFound(err)
210
- }
211
-
212
- // Custom logic: log the size field
213
- log.Info("TestOperator size", "size", testOperator.Spec.Size)` ,
214
- )
215
- Expect (err ).NotTo (HaveOccurred ())
224
+ Expect (err ).NotTo (HaveOccurred (), "Failed to update testoperator_types.go" )
216
225
}
217
226
218
227
func initializeGitRepo (projectDir string ) {
@@ -270,16 +279,18 @@ func runAlphaUpdate(projectDir string, kbc *utils.TestContext) {
270
279
}
271
280
272
281
func validateCustomCodePreservation (projectDir string ) {
282
+ By ("validating the API" )
273
283
typesFile := filepath .Join (projectDir , "api" , "v1" , "testoperator_types.go" )
274
284
content , err := os .ReadFile (typesFile )
275
285
Expect (err ).NotTo (HaveOccurred ())
276
286
Expect (string (content )).To (ContainSubstring ("Size int32 `json:\" size,omitempty\" `" ))
277
- Expect (string (content )).To (ContainSubstring ("Size is the size of the memcached deployment" ))
287
+ Expect (string (content )).To (ContainSubstring ("// +kubebuilder:validation:Minimum=0" ))
288
+ Expect (string (content )).To (ContainSubstring ("// +kubebuilder:validation:Maximum=3" ))
289
+ Expect (string (content )).To (ContainSubstring ("// +kubebuilder:default=1" ))
278
290
291
+ By ("validating the Controller" )
279
292
controllerFile := filepath .Join (projectDir , "internal" , "controller" , "testoperator_controller.go" )
280
293
content , err = os .ReadFile (controllerFile )
281
294
Expect (err ).NotTo (HaveOccurred ())
282
- Expect (string (content )).To (ContainSubstring ("Custom reconciliation logic" ))
283
- Expect (string (content )).To (ContainSubstring ("log.Info(\" Reconciling TestOperator\" )" ))
284
- Expect (string (content )).To (ContainSubstring ("log.Info(\" TestOperator size\" , \" size\" , testOperator.Spec.Size)" ))
295
+ Expect (string (content )).To (ContainSubstring (controllerImplementation ))
285
296
}
0 commit comments