@@ -22,16 +22,17 @@ import (
22
22
23
23
log "github.com/sirupsen/logrus"
24
24
"github.com/spf13/afero"
25
-
26
25
hackutils "sigs.k8s.io/kubebuilder/v4/hack/docs/utils"
27
26
pluginutil "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
28
27
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
29
28
)
30
29
30
+ // Sample define the sample which will be scaffolded
31
31
type Sample struct {
32
32
ctx * utils.TestContext
33
33
}
34
34
35
+ // NewSample create a new instance of the cronjob sample and configure the KB CLI that will be used
35
36
func NewSample (binaryPath , samplePath string ) Sample {
36
37
log .Infof ("Generating the sample context of Cronjob..." )
37
38
ctx := hackutils .NewSampleContext (binaryPath , samplePath , "GO111MODULE=on" )
@@ -49,6 +50,7 @@ func (sp *Sample) Prepare() {
49
50
hackutils .CheckError ("creating directory for sample project" , err )
50
51
}
51
52
53
+ // GenerateSampleProject will generate the sample
52
54
func (sp * Sample ) GenerateSampleProject () {
53
55
log .Infof ("Initializing the cronjob project" )
54
56
@@ -79,6 +81,7 @@ func (sp *Sample) GenerateSampleProject() {
79
81
hackutils .CheckError ("Implementing admission webhook" , err )
80
82
}
81
83
84
+ // UpdateTutorial the cronjob tutorial with the scaffold changes
82
85
func (sp * Sample ) UpdateTutorial () {
83
86
log .Println ("Update tutorial with cronjob code" )
84
87
// 1. update specs
@@ -168,12 +171,12 @@ func (sp *Sample) updateSpec() {
168
171
169
172
err = pluginutil .InsertCode (
170
173
filepath .Join (sp .ctx .Dir , "api/v1/cronjob_types.go" ),
171
- `to be serialized.` , CronjobSpecExplaination )
174
+ `to be serialized.` , cronjobSpecExplaination )
172
175
hackutils .CheckError ("fixing cronjob_types.go" , err )
173
176
174
177
err = pluginutil .InsertCode (
175
178
filepath .Join (sp .ctx .Dir , "api/v1/cronjob_types.go" ),
176
- `type CronJobSpec struct {` , CronjobSpecStruct )
179
+ `type CronJobSpec struct {` , cronjobSpecStruct )
177
180
hackutils .CheckError ("fixing cronjob_types.go" , err )
178
181
179
182
err = pluginutil .ReplaceInFile (
@@ -187,7 +190,7 @@ func (sp *Sample) updateSpec() {
187
190
188
191
err = pluginutil .InsertCode (
189
192
filepath .Join (sp .ctx .Dir , "api/v1/cronjob_types.go" ),
190
- `// Important: Run "make" to regenerate code after modifying this file` , CronjobList )
193
+ `// Important: Run "make" to regenerate code after modifying this file` , cronjobList )
191
194
hackutils .CheckError ("fixing cronjob_types.go" , err )
192
195
193
196
err = pluginutil .InsertCode (
@@ -229,13 +232,13 @@ func (sp *Sample) updateAPIStuff() {
229
232
err = pluginutil .InsertCode (
230
233
filepath .Join (sp .ctx .Dir , "api/v1/groupversion_info.go" ),
231
234
`limitations under the License.
232
- */` , GroupversionIntro )
235
+ */` , groupVersionIntro )
233
236
hackutils .CheckError ("fixing groupversion_info.go" , err )
234
237
235
238
err = pluginutil .InsertCode (
236
239
filepath .Join (sp .ctx .Dir , "api/v1/groupversion_info.go" ),
237
240
` "sigs.k8s.io/controller-runtime/pkg/scheme"
238
- )` , GroupversionSchema )
241
+ )` , groupVersionSchema )
239
242
hackutils .CheckError ("fixing groupversion_info.go" , err )
240
243
}
241
244
@@ -244,7 +247,7 @@ func (sp *Sample) updateController() {
244
247
err = pluginutil .InsertCode (
245
248
filepath .Join (sp .ctx .Dir , "internal/controller/cronjob_controller.go" ),
246
249
`limitations under the License.
247
- */` , ControllerIntro )
250
+ */` , controllerIntro )
248
251
hackutils .CheckError ("fixing cronjob_controller.go" , err )
249
252
250
253
err = pluginutil .ReplaceInFile (
@@ -258,7 +261,7 @@ func (sp *Sample) updateController() {
258
261
"sigs.k8s.io/controller-runtime/pkg/log"
259
262
260
263
batchv1 "tutorial.kubebuilder.io/project/api/v1"
261
- )` , ControllerImport )
264
+ )` , controllerImport )
262
265
hackutils .CheckError ("fixing cronjob_controller.go" , err )
263
266
264
267
err = pluginutil .InsertCode (
@@ -270,12 +273,12 @@ func (sp *Sample) updateController() {
270
273
err = pluginutil .InsertCode (
271
274
filepath .Join (sp .ctx .Dir , "internal/controller/cronjob_controller.go" ),
272
275
` Clock
273
- }` , ControllerMockClock )
276
+ }` , controllerMockClock )
274
277
hackutils .CheckError ("fixing cronjob_controller.go" , err )
275
278
276
279
err = pluginutil .InsertCode (
277
280
filepath .Join (sp .ctx .Dir , "internal/controller/cronjob_controller.go" ),
278
- `// +kubebuilder:rbac:groups=batch.tutorial.kubebuilder.io,resources=cronjobs/finalizers,verbs=update` , ControllerReconcile )
281
+ `// +kubebuilder:rbac:groups=batch.tutorial.kubebuilder.io,resources=cronjobs/finalizers,verbs=update` , controllerReconcile )
279
282
hackutils .CheckError ("fixing cronjob_controller.go" , err )
280
283
281
284
err = pluginutil .ReplaceInFile (
@@ -285,12 +288,12 @@ func (sp *Sample) updateController() {
285
288
// TODO(user): your logic here
286
289
287
290
return ctrl.Result{}, nil
288
- }` , ControllerReconcileLogic )
291
+ }` , controllerReconcileLogic )
289
292
hackutils .CheckError ("fixing cronjob_controller.go" , err )
290
293
291
294
err = pluginutil .InsertCode (
292
295
filepath .Join (sp .ctx .Dir , "internal/controller/cronjob_controller.go" ),
293
- `SetupWithManager(mgr ctrl.Manager) error {` , ControllerSetupWithManager )
296
+ `SetupWithManager(mgr ctrl.Manager) error {` , controllerSetupWithManager )
294
297
hackutils .CheckError ("fixing cronjob_controller.go" , err )
295
298
296
299
err = pluginutil .InsertCode (
@@ -313,7 +316,7 @@ func (sp *Sample) updateMain() {
313
316
err = pluginutil .InsertCode (
314
317
filepath .Join (sp .ctx .Dir , "cmd/main.go" ),
315
318
`// +kubebuilder:scaffold:imports
316
- )` , MainBatch )
319
+ )` , mainBatch )
317
320
hackutils .CheckError ("fixing main.go" , err )
318
321
319
322
err = pluginutil .InsertCode (
@@ -347,7 +350,7 @@ CronJob controller's`+" `"+`SetupWithManager`+"`"+` method.
347
350
filepath .Join (sp .ctx .Dir , "cmd/main.go" ),
348
351
`setupLog.Error(err, "unable to create controller", "controller", "CronJob")
349
352
os.Exit(1)
350
- }` , MainEnableWebhook )
353
+ }` , mainEnableWebhook )
351
354
hackutils .CheckError ("fixing main.go" , err )
352
355
353
356
err = pluginutil .InsertCode (
@@ -512,7 +515,7 @@ func (sp *Sample) updateSuiteTest() {
512
515
err = pluginutil .InsertCode (
513
516
filepath .Join (sp .ctx .Dir , "internal/controller/suite_test.go" ),
514
517
`limitations under the License.
515
- */` , SuiteTestIntro )
518
+ */` , suiteTestIntro )
516
519
hackutils .CheckError ("updating suite_test.go to add license intro" , err )
517
520
518
521
err = pluginutil .InsertCode (
@@ -534,13 +537,13 @@ var (
534
537
cfg *rest.Config
535
538
k8sClient client.Client
536
539
)
537
- ` , SuiteTestEnv )
540
+ ` , suiteTestEnv )
538
541
hackutils .CheckError ("updating suite_test.go to add more variables" , err )
539
542
540
543
err = pluginutil .InsertCode (
541
544
filepath .Join (sp .ctx .Dir , "internal/controller/suite_test.go" ),
542
545
`ctx, cancel = context.WithCancel(context.TODO())
543
- ` , SuiteTestReadCRD )
546
+ ` , suiteTestReadCRD )
544
547
hackutils .CheckError ("updating suite_test.go to add text about CRD" , err )
545
548
546
549
err = pluginutil .InsertCode (
@@ -559,7 +562,7 @@ var (
559
562
Expect(err).NotTo(HaveOccurred())
560
563
561
564
// +kubebuilder:scaffold:scheme
562
- ` , SuiteTestAddSchema )
565
+ ` , suiteTestAddSchema )
563
566
hackutils .CheckError ("updating suite_test.go to add schema" , err )
564
567
565
568
err = pluginutil .InsertCode (
@@ -568,7 +571,7 @@ var (
568
571
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
569
572
Expect(err).NotTo(HaveOccurred())
570
573
Expect(k8sClient).NotTo(BeNil())
571
- ` , SuiteTestDescription )
574
+ ` , suiteTestDescription )
572
575
hackutils .CheckError ("updating suite_test.go for test description" , err )
573
576
574
577
err = pluginutil .ReplaceInFile (
@@ -580,7 +583,7 @@ var _ = AfterSuite(func() {
580
583
err := testEnv.Stop()
581
584
Expect(err).NotTo(HaveOccurred())
582
585
})
583
- ` , SuiteTestCleanup )
586
+ ` , suiteTestCleanup )
584
587
hackutils .CheckError ("updating suite_test.go to cleanup tests" , err )
585
588
}
586
589
@@ -623,7 +626,7 @@ func (sp *Sample) updateExample() {
623
626
// samples/batch_v1_cronjob
624
627
err = pluginutil .InsertCode (
625
628
filepath .Join (sp .ctx .Dir , "config/samples/batch_v1_cronjob.yaml" ),
626
- `spec:` , CronjobSample )
629
+ `spec:` , cronjobSample )
627
630
hackutils .CheckError ("fixing samples/batch_v1_cronjob.yaml" , err )
628
631
629
632
err = pluginutil .ReplaceInFile (
@@ -634,6 +637,6 @@ func (sp *Sample) updateExample() {
634
637
635
638
func (sp * Sample ) addControllerTest () {
636
639
var fs = afero .NewOsFs ()
637
- err := afero .WriteFile (fs , filepath .Join (sp .ctx .Dir , "internal/controller/cronjob_controller_test.go" ), []byte (ControllerTest ), 0600 )
640
+ err := afero .WriteFile (fs , filepath .Join (sp .ctx .Dir , "internal/controller/cronjob_controller_test.go" ), []byte (controllerTest ), 0600 )
638
641
hackutils .CheckError ("adding cronjob_controller_test" , err )
639
642
}
0 commit comments