@@ -37,21 +37,35 @@ const (
3737 toVersion = "v4.6.0"
3838
3939 // 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")`
4256)
4357
4458var _ = Describe ("kubebuilder" , func () {
4559 Context ("alpha update" , func () {
4660 var (
4761 mockProjectDir string
48- binFromVersionPath string
62+ pathBinFromVersion string
4963 kbc * utils.TestContext
5064 )
5165
5266 BeforeEach (func () {
5367 var err error
54- By ("setting up test context with current kubebuilder binary " )
68+ By ("setting up test context with binary build from source " )
5569 kbc , err = utils .NewTestContext (pluginutil .KubebuilderBinName , "GO111MODULE=on" )
5670 Expect (err ).NotTo (HaveOccurred ())
5771 Expect (kbc .Prepare ()).To (Succeed ())
@@ -61,20 +75,19 @@ var _ = Describe("kubebuilder", func() {
6175 Expect (err ).NotTo (HaveOccurred ())
6276
6377 By ("downloading kubebuilder v4.5.2 binary to isolated /tmp directory" )
64- binFromVersionPath , err = downloadKubebuilder ()
78+ pathBinFromVersion , err = downloadKubebuilder ()
6579 Expect (err ).NotTo (HaveOccurred ())
6680 })
6781
6882 AfterEach (func () {
6983 By ("cleaning up test artifacts" )
70-
7184 _ = os .RemoveAll (mockProjectDir )
72- _ = os .RemoveAll (filepath .Dir (binFromVersionPath ))
85+ _ = os .RemoveAll (filepath .Dir (pathBinFromVersion ))
7386
7487 // Clean up kubebuilder alpha update downloaded binaries
7588 binaryPatterns := []string {
76- binFromVersionPattern ,
77- binToVersionPattern ,
89+ pathBinFromVersion ,
90+ pathBinToVersion ,
7891 }
7992
8093 for _ , pattern := range binaryPatterns {
@@ -84,18 +97,16 @@ var _ = Describe("kubebuilder", func() {
8497 }
8598 }
8699
87- // Clean up TestContext
88- if kbc != nil {
89- kbc .Destroy ()
90- }
100+ _ = os .RemoveAll (kbc .Dir )
91101 })
92102
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 () {
94104 By ("creating mock project with kubebuilder v4.5.2" )
95- createMockProject (mockProjectDir , binFromVersionPath )
105+ createMockProject (mockProjectDir , pathBinFromVersion )
96106
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 )
99110
100111 By ("initializing git repository and committing mock project" )
101112 initializeGitRepo (mockProjectDir )
@@ -182,38 +193,36 @@ func createMockProject(projectDir, binaryPath string) {
182193 Expect (err ).NotTo (HaveOccurred ())
183194}
184195
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 ) {
186215 typesFile := filepath .Join (projectDir , "api" , "v1" , "testoperator_types.go" )
187- err := pluginutil .InsertCode (
216+ err := pluginutil .ReplaceInFile (
188217 typesFile ,
189218 "Foo string `json:\" foo,omitempty\" `" ,
190219 `
191220 // +kubebuilder:validation:Minimum=0
192221 // +kubebuilder:validation:Maximum=3
193222 // +kubebuilder:default=1
194- // Size is the size of the memcached deployment
195223 Size int32 ` + "`json:\" size,omitempty\" `" ,
196224 )
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" )
217226}
218227
219228func initializeGitRepo (projectDir string ) {
@@ -271,16 +280,18 @@ func runAlphaUpdate(projectDir string, kbc *utils.TestContext) {
271280}
272281
273282func validateCustomCodePreservation (projectDir string ) {
283+ By ("validating the API" )
274284 typesFile := filepath .Join (projectDir , "api" , "v1" , "testoperator_types.go" )
275285 content , err := os .ReadFile (typesFile )
276286 Expect (err ).NotTo (HaveOccurred ())
277287 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" ))
279291
292+ By ("validating the Controller" )
280293 controllerFile := filepath .Join (projectDir , "internal" , "controller" , "testoperator_controller.go" )
281294 content , err = os .ReadFile (controllerFile )
282295 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 ))
286297}
0 commit comments