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