forked from kubernetes-sigs/kubebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_cronjob.go
684 lines (573 loc) · 23.3 KB
/
generate_cronjob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cronjob
import (
"os/exec"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
hackutils "sigs.k8s.io/kubebuilder/v4/hack/docs/utils"
pluginutil "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
)
// Sample define the sample which will be scaffolded
type Sample struct {
ctx *utils.TestContext
}
// NewSample create a new instance of the cronjob sample and configure the KB CLI that will be used
func NewSample(binaryPath, samplePath string) Sample {
log.Infof("Generating the sample context of Cronjob...")
ctx := hackutils.NewSampleContext(binaryPath, samplePath, "GO111MODULE=on")
return Sample{&ctx}
}
// Prepare the Context for the sample project
func (sp *Sample) Prepare() {
log.Infof("destroying directory for cronjob sample project")
sp.ctx.Destroy()
log.Infof("refreshing tools and creating directory...")
err := sp.ctx.Prepare()
hackutils.CheckError("creating directory for sample project", err)
}
// GenerateSampleProject will generate the sample
func (sp *Sample) GenerateSampleProject() {
log.Infof("Initializing the cronjob project")
err := sp.ctx.Init(
"--domain", "tutorial.kubebuilder.io",
"--repo", "tutorial.kubebuilder.io/project",
"--license", "apache2",
"--owner", "The Kubernetes authors",
)
hackutils.CheckError("Initializing the cronjob project", err)
log.Infof("Adding a new config type")
err = sp.ctx.CreateAPI(
"--group", "batch",
"--version", "v1",
"--kind", "CronJob",
"--resource", "--controller",
)
hackutils.CheckError("Creating the API", err)
log.Infof("Implementing admission webhook")
err = sp.ctx.CreateWebhook(
"--group", "batch",
"--version", "v1",
"--kind", "CronJob",
"--defaulting", "--programmatic-validation",
)
hackutils.CheckError("Implementing admission webhook", err)
}
// UpdateTutorial the cronjob tutorial with the scaffold changes
func (sp *Sample) UpdateTutorial() {
log.Println("Update tutorial with cronjob code")
// 1. update specs
sp.updateSpec()
// 2. update webhook
sp.updateWebhook()
// 3. update webhookTests
sp.updateWebhookTests()
// 4. update makefile
sp.updateMakefile()
// 5. generate extra files
cmd := exec.Command("go", "mod", "tidy")
_, err := sp.ctx.Run(cmd)
hackutils.CheckError("Failed to run go mod tidy for cronjob tutorial", err)
cmd = exec.Command("go", "get", "github.com/robfig/cron")
_, err = sp.ctx.Run(cmd)
hackutils.CheckError("Failed to get package robfig/cron", err)
cmd = exec.Command("make", "generate", "manifests")
_, err = sp.ctx.Run(cmd)
hackutils.CheckError("run make generate and manifests", err)
// 6. compensate other intro in API
sp.updateAPIStuff()
// 7. update reconciliation and main.go
// 7.1 update controller
sp.updateController()
// 7.2 update main.go
sp.updateMain()
// 8. generate extra files
cmd = exec.Command("make", "generate", "manifests")
_, err = sp.ctx.Run(cmd)
hackutils.CheckError("run make generate and manifests", err)
// 9. update suite_test explanation
sp.updateSuiteTest()
// 10. uncomment kustomization
sp.updateKustomization()
// 11. add example
sp.updateExample()
// 12. add test
sp.addControllerTest()
// 13. update e2e tests
sp.updateE2E()
}
// CodeGen is a noop for this sample, just to make generation of all samples
// more efficient. We may want to refactor `UpdateTutorial` some day to take
// advantage of a separate call, but it is not necessary.
func (sp *Sample) CodeGen() {
cmd := exec.Command("make", "all")
_, err := sp.ctx.Run(cmd)
hackutils.CheckError("Failed to run make all for cronjob tutorial", err)
cmd = exec.Command("make", "build-installer")
_, err = sp.ctx.Run(cmd)
hackutils.CheckError("Failed to run make build-installer for cronjob tutorial", err)
err = sp.ctx.EditHelmPlugin()
hackutils.CheckError("Failed to enable helm plugin", err)
}
// insert code to fix docs
func (sp *Sample) updateSpec() {
var err error
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`limitations under the License.
*/`,
`
// +kubebuilder:docs-gen:collapse=Apache License
/*
*/`)
hackutils.CheckError("fixing cronjob_types.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`package v1`,
`
/*
*/`)
hackutils.CheckError("fixing cronjob_types.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`import (`,
`
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"`)
hackutils.CheckError("fixing cronjob_types.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`to be serialized.`, cronjobSpecExplaination)
hackutils.CheckError("fixing cronjob_types.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`type CronJobSpec struct {`, cronjobSpecStruct)
hackutils.CheckError("fixing cronjob_types.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
// Foo is an example field of CronJob. Edit cronjob_types.go to remove/update
Foo string`+" `"+`json:"foo,omitempty"`+"`", "")
hackutils.CheckError("fixing cronjob_types.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`// Important: Run "make" to regenerate code after modifying this file`, cronjobList)
hackutils.CheckError("fixing cronjob_types.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`SchemeBuilder.Register(&CronJob{}, &CronJobList{})
}`, `
// +kubebuilder:docs-gen:collapse=Root Object Definitions`)
hackutils.CheckError("fixing cronjob_types.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`// CronJob is the Schema for the cronjobs API.
type CronJob struct {`, `// CronJob is the Schema for the cronjobs API.
type CronJob struct {`+`
/*
*/`)
hackutils.CheckError("fixing cronjob_types.go", err)
// fix lint
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`
}`, "")
hackutils.CheckError("fixing cronjob_types.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`
}`, "")
hackutils.CheckError("fixing cronjob_types.go", err)
}
func (sp *Sample) updateAPIStuff() {
var err error
// fix groupversion_info
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/groupversion_info.go"),
`limitations under the License.
*/`, groupVersionIntro)
hackutils.CheckError("fixing groupversion_info.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/groupversion_info.go"),
` "sigs.k8s.io/controller-runtime/pkg/scheme"
)`, groupVersionSchema)
hackutils.CheckError("fixing groupversion_info.go", err)
}
func (sp *Sample) updateController() {
var err error
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`limitations under the License.
*/`, controllerIntro)
hackutils.CheckError("fixing cronjob_controller.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`import (
"context"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
batchv1 "tutorial.kubebuilder.io/project/api/v1"
)`, controllerImport)
hackutils.CheckError("fixing cronjob_controller.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`Scheme *runtime.Scheme`, `
Clock`)
hackutils.CheckError("fixing cronjob_controller.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
` Clock
}`, controllerMockClock)
hackutils.CheckError("fixing cronjob_controller.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`// +kubebuilder:rbac:groups=batch.tutorial.kubebuilder.io,resources=cronjobs/finalizers,verbs=update`, controllerReconcile)
hackutils.CheckError("fixing cronjob_controller.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.1/pkg/reconcile`, skipGoCycloLint)
hackutils.CheckError("fixing cronjob_controller.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
` _ = log.FromContext(ctx)
// TODO(user): your logic here
return ctrl.Result{}, nil
}`, controllerReconcileLogic)
hackutils.CheckError("fixing cronjob_controller.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`SetupWithManager(mgr ctrl.Manager) error {`, controllerSetupWithManager)
hackutils.CheckError("fixing cronjob_controller.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`For(&batchv1.CronJob{}).`, `
Owns(&kbatch.Job{}).`)
hackutils.CheckError("fixing cronjob_controller.go", err)
}
func (sp *Sample) updateMain() {
var err error
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`limitations under the License.
*/`,
`
// +kubebuilder:docs-gen:collapse=Apache License`)
hackutils.CheckError("fixing main.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`// +kubebuilder:scaffold:imports
)`, mainBatch)
hackutils.CheckError("fixing main.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`// +kubebuilder:scaffold:scheme
}`, `
/*
The other thing that's changed is that kubebuilder has added a block calling our
CronJob controller's`+" `"+`SetupWithManager`+"`"+` method.
*/`)
hackutils.CheckError("fixing main.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`func main() {`, `
/*
*/`)
hackutils.CheckError("fixing main.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}`, `
// +kubebuilder:docs-gen:collapse=old stuff`)
hackutils.CheckError("fixing main.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`setupLog.Error(err, "unable to create controller", "controller", "CronJob")
os.Exit(1)
}`, mainEnableWebhook)
hackutils.CheckError("fixing main.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`setupLog.Error(err, "problem running manager")
os.Exit(1)
}`, `
// +kubebuilder:docs-gen:collapse=old stuff`)
hackutils.CheckError("fixing main.go", err)
}
func (sp *Sample) updateMakefile() {
const originalManifestTarget = `.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
`
const changedManifestTarget = `.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
# Note that the option maxDescLen=0 was added in the default scaffold in order to sort out the issue
# Too long: must have at most 262144 bytes. By using kubectl apply to create / update resources an annotation
# is created by K8s API to store the latest version of the resource ( kubectl.kubernetes.io/last-applied-configuration).
# However, it has a size limit and if the CRD is too big with so many long descriptions as this one it will cause the failure.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:maxDescLen=0 webhook paths="./..." output:crd:artifacts:config=config/crd/bases
`
err := pluginutil.ReplaceInFile(filepath.Join(sp.ctx.Dir, "Makefile"), originalManifestTarget, changedManifestTarget)
hackutils.CheckError("updating makefile to use maxDescLen=0 in make manifest target", err)
}
func (sp *Sample) updateWebhookTests() {
file := filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook_test.go")
err := pluginutil.ReplaceInFile(file,
webhookTestCreateDefaultingFragment,
webhookTestCreateDefaultingReplaceFragment)
hackutils.CheckError("replace create defaulting test", err)
err = pluginutil.ReplaceInFile(file,
webhookTestingValidatingTodoFragment,
webhookTestingValidatingExampleFragment)
hackutils.CheckError("replace validating defaulting test", err)
err = pluginutil.ReplaceInFile(file,
webhookTestsVars,
webhookTestsConstants)
hackutils.CheckError("replace before each webhook test ", err)
err = pluginutil.ReplaceInFile(file,
webhookTestsBeforeEachOriginal,
webhookTestsBeforeEachChanged)
hackutils.CheckError("replace before each webhook test ", err)
}
func (sp *Sample) updateWebhook() {
var err error
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`limitations under the License.
*/`,
`
// +kubebuilder:docs-gen:collapse=Apache License`)
hackutils.CheckError("fixing cronjob_webhook.go by adding collapse", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`import (
"context"
"fmt"`,
`
"github.com/robfig/cron"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
validationutils "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"`,
)
hackutils.CheckError("add extra imports to cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`batchv1 "tutorial.kubebuilder.io/project/api/v1"
)
// nolint:unused
// log is for logging in this package.
`, webhookIntro)
hackutils.CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`var cronjoblog = logf.Log.WithName("cronjob-resource")`,
`
/*
Then, we set up the webhook with the manager.
*/`)
hackutils.CheckError("fixing cronjob_webhook.go by setting webhook with manager comment", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!`, webhooksNoticeMarker)
hackutils.CheckError("fixing cronjob_webhook.go by replacing note about path attribute", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.`, explanationValidateCRD)
hackutils.CheckError("fixing cronjob_webhook.go by replacing note about path attribute", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.`, "")
hackutils.CheckError("fixing cronjob_webhook.go by replace TODO to change verbs", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`// TODO(user): Add more fields as needed for defaulting`, fragmentForDefaultFields)
hackutils.CheckError("fixing cronjob_webhook.go by replacing TODO in Defaulter", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`WithDefaulter(&CronJobCustomDefaulter{}).`,
`WithDefaulter(&CronJobCustomDefaulter{
DefaultConcurrencyPolicy: batchv1.AllowConcurrent,
DefaultSuspend: false,
DefaultSuccessfulJobsHistoryLimit: 3,
DefaultFailedJobsHistoryLimit: 1,
}).`)
hackutils.CheckError("replacing WithDefaulter call in cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`// TODO(user): fill in your defaulting logic.
return nil
}`, webhookDefaultingSettings)
hackutils.CheckError("fixing cronjob_webhook.go by adding logic", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`// TODO(user): fill in your validation logic upon object creation.
return nil, nil`,
`return nil, validateCronJob(cronjob)`)
hackutils.CheckError("fixing cronjob_webhook.go by fill in your validation", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`// TODO(user): fill in your validation logic upon object update.
return nil, nil`,
`return nil, validateCronJob(cronjob)`)
hackutils.CheckError("fixing cronjob_webhook.go by adding validation logic upon object update", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
`// Default implements webhook.CustomDefaulter so a webhook will be registered for the Kind CronJob.`,
customInterfaceDefaultInfo)
hackutils.CheckError("fixing cronjob_webhook.go by adding validation logic upon object update", err)
err = pluginutil.AppendCodeAtTheEnd(
filepath.Join(sp.ctx.Dir, "internal/webhook/v1/cronjob_webhook.go"),
webhookValidateSpecMethods)
hackutils.CheckError("adding validation spec methods at the end", err)
}
func (sp *Sample) updateSuiteTest() {
var err error
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`limitations under the License.
*/`, suiteTestIntro)
hackutils.CheckError("updating suite_test.go to add license intro", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
"testing"
`, `
ctrl "sigs.k8s.io/controller-runtime"
`)
hackutils.CheckError("updating suite_test.go to add ctrl import", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client
)
`, suiteTestEnv)
hackutils.CheckError("updating suite_test.go to add more variables", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
err = batchv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
// +kubebuilder:scaffold:scheme
`, suiteTestAddSchema)
hackutils.CheckError("updating suite_test.go to add schema", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`testEnv.BinaryAssetsDirectory = getFirstFoundEnvTestBinaryDir()
}`, `
/*
Then, we start the envtest cluster.
*/`)
hackutils.CheckError("updating suite_test.go to add text to show where envtest cluster start", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
`, suiteTestClient)
hackutils.CheckError("updating suite_test.go to add text about test client", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
`, suiteTestDescription)
hackutils.CheckError("updating suite_test.go for test description", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
var _ = AfterSuite(func() {
By("tearing down the test environment")
cancel()
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
`, suiteTestCleanup)
hackutils.CheckError("updating suite_test.go to cleanup tests", err)
}
func (sp *Sample) updateKustomization() {
var err error
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
`#- ../certmanager`, `#`)
hackutils.CheckError("fixing default/kustomization", err)
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
`#- ../prometheus`, `#`)
hackutils.CheckError("fixing default/kustomization", err)
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
`#- path: cert_metrics_manager_patch.yaml
# target:
# kind: Deployment`, `#`)
hackutils.CheckError("enabling cert_metrics_manager_patch.yaml", err)
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/prometheus/kustomization.yaml"),
`#patches:
# - path: monitor_tls_patch.yaml
# target:
# kind: ServiceMonitor`, `#`)
hackutils.CheckError("enabling monitor tls patch", err)
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
certManagerForMetricsAndWebhooks, `#`)
hackutils.CheckError("fixing default/kustomization", err)
}
func (sp *Sample) updateExample() {
var err error
// samples/batch_v1_cronjob
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "config/samples/batch_v1_cronjob.yaml"),
`spec:`, cronjobSample)
hackutils.CheckError("fixing samples/batch_v1_cronjob.yaml", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "config/samples/batch_v1_cronjob.yaml"),
`# TODO(user): Add fields here`, "")
hackutils.CheckError("fixing samples/batch_v1_cronjob.yaml", err)
}
func (sp *Sample) addControllerTest() {
var fs = afero.NewOsFs()
err := afero.WriteFile(fs, filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller_test.go"), []byte(controllerTest), 0600)
hackutils.CheckError("adding cronjob_controller_test", err)
}
func (sp *Sample) updateE2E() {
cronjobE2ESuite := filepath.Join(sp.ctx.Dir, "test", "e2e", "e2e_suite_test.go")
cronjobE2ETest := filepath.Join(sp.ctx.Dir, "test", "e2e", "e2e_test.go")
var err error
err = pluginutil.InsertCode(cronjobE2ESuite, `isCertManagerAlreadyInstalled = false`, isPrometheusInstalledVar)
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go", err)
err = pluginutil.InsertCode(cronjobE2ESuite, `var _ = BeforeSuite(func() {`, beforeSuitePrometheus)
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go", err)
err = pluginutil.InsertCode(cronjobE2ESuite,
`// The tests-e2e are intended to run on a temporary cluster that is created and destroyed for testing.`,
checkPrometheusInstalled)
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go", err)
err = pluginutil.InsertCode(cronjobE2ESuite, `var _ = AfterSuite(func() {`, afterSuitePrometheus)
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go", err)
err = pluginutil.InsertCode(cronjobE2ETest, `Expect(err).NotTo(HaveOccurred(), "Metrics service should exist")`, serviceMonitorE2e)
hackutils.CheckError("fixing test/e2e/e2e_test.go", err)
}