-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
generate_cronjob.go
638 lines (533 loc) · 18.8 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
/*
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 (
"fmt"
"os"
"os/exec"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
"sigs.k8s.io/kubebuilder/v3/test/e2e/utils"
)
type Sample struct {
ctx *utils.TestContext
}
func NewSample(binaryPath, samplePath string) Sample {
log.Infof("Generating the sample context of Cronjob...")
ctx := newSampleContext(binaryPath, samplePath, "GO111MODULE=on")
return Sample{&ctx}
}
func newSampleContext(binaryPath string, samplePath string, env ...string) utils.TestContext {
cmdContext := &utils.CmdContext{
Env: env,
Dir: samplePath,
}
testContext := utils.TestContext{
CmdContext: cmdContext,
BinaryName: binaryPath,
}
return testContext
}
// Prepare the Context for the sample project
func (sp *Sample) Prepare() {
log.Infof("destroying directory for sample project")
sp.ctx.Destroy()
log.Infof("refreshing tools and creating directory...")
err := sp.ctx.Prepare()
CheckError("creating directory for sample project", err)
}
func (sp *Sample) GenerateSampleProject() {
log.Infof("Initializing the project")
err := sp.ctx.Init(
"--plugins", "go/v4",
"--domain", "tutorial.kubebuilder.io",
"--repo", "tutorial.kubebuilder.io/project",
"--license", "apache2",
"--owner", "The Kubernetes authors",
)
CheckError("Initializing the project", err)
log.Infof("Adding a new config type")
err = sp.ctx.CreateAPI(
"--group", "batch",
"--version", "v1",
"--kind", "CronJob",
"--resource", "--controller",
)
CheckError("Creating the API", err)
log.Infof("Implementing admission webhook")
err = sp.ctx.CreateWebhook(
"--group", "batch",
"--version", "v1",
"--kind", "CronJob",
"--defaulting", "--programmatic-validation",
)
CheckError("Implementing admission webhook", err)
}
func (sp *Sample) UpdateTutorial() {
fmt.Println("TODO: update tutorial")
// 1. update specs
updateSpec(sp)
// 2. update webhook
updateWebhook(sp)
// 3. generate extra files
codeGen(sp)
// 4. compensate other intro in API
updateAPIStuff(sp)
// 5. update reconciliation and main.go
// 5.1 update controller
updateController(sp)
// 5.2 update main.go
updateMain(sp)
// 6. generate extra files
codeGen(sp)
// 7. update suite_test explanation
updateSuiteTest(sp)
// 8. uncomment kustomization
updateKustomization(sp)
// 9. add example
updateExample(sp)
// 10. add test
addControllerTest(sp)
}
func codeGen(sp *Sample) {
cmd := exec.Command("go", "get", "github.com/robfig/cron")
_, err := sp.ctx.Run(cmd)
CheckError("Failed to get package robfig/cron", err)
cmd = exec.Command("make", "build")
_, err = sp.ctx.Run(cmd)
CheckError("Failed to generate code in cronjob tutorial", err)
}
// insert code to fix docs
func updateSpec(sp *Sample) {
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
/*
*/`)
CheckError("fixing cronjob_types.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`package v1`,
`
/*
*/`)
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"`)
CheckError("fixing cronjob_types.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`to be serialized.`, CronjobSpecExplaination)
CheckError("fixing cronjob_types.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`type CronJobSpec struct {`, CronjobSpecStruct)
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"`+"`", "")
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)
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`)
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 {`+`
/*
*/`)
CheckError("fixing cronjob_types.go", err)
// fix lint
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`
}`, "")
CheckError("fixing cronjob_types.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`
}`, "")
CheckError("fixing cronjob_types.go", err)
}
func updateAPIStuff(sp *Sample) {
var err error
// fix groupversion_info
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/groupversion_info.go"),
`limitations under the License.
*/`, GroupversionIntro)
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)
CheckError("fixing groupversion_info.go", err)
}
func updateController(sp *Sample) {
var err error
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`limitations under the License.
*/`, ControllerIntro)
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)
CheckError("fixing cronjob_controller.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`Scheme *runtime.Scheme`, `
Clock`)
CheckError("fixing cronjob_controller.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
` Clock
}`, ControllerMockClock)
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)
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)
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)
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{}).`)
CheckError("fixing cronjob_controller.go", err)
}
func updateMain(sp *Sample) {
var err error
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`limitations under the License.
*/`,
`
// +kubebuilder:docs-gen:collapse=Apache License`)
CheckError("fixing main.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`//+kubebuilder:scaffold:imports
)`, MainBatch)
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.
*/`)
CheckError("fixing main.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`func main() {`, `
/*
*/`)
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`)
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)
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`)
CheckError("fixing main.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
` if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
os.Exit(1)
}`, `}`)
CheckError("fixing main.go", err)
}
func updateWebhook(sp *Sample) {
var err error
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`limitations under the License.
*/`,
`
// +kubebuilder:docs-gen:collapse=Apache License`)
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
// log is for logging in this package.
`, WebhookIntro)
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`var cronjoblog = logf.Log.WithName("cronjob-resource")`,
`
/*
Then, we set up the webhook with the manager.
*/`)
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!`, WebhookMarker)
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.`, "")
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`//+kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob.kb.io,admissionReviewVersions=v1`, "")
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`//+kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,sideEffects=None,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob.kb.io,admissionReviewVersions=v1`, "")
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`cronjoblog.Info("default", "name", r.Name)
// TODO(user): fill in your defaulting logic.
`, WebhookValidate)
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`func (r *CronJob) ValidateCreate() error {
cronjoblog.Info("validate create", "name", r.Name)
// TODO(user): fill in your validation logic upon object creation.
return nil
}`, `func (r *CronJob) ValidateCreate() error {
cronjoblog.Info("validate create", "name", r.Name)
return r.validateCronJob()
}`)
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`func (r *CronJob) ValidateUpdate(old runtime.Object) error {
cronjoblog.Info("validate update", "name", r.Name)
// TODO(user): fill in your validation logic upon object update.
return nil
}`, `func (r *CronJob) ValidateUpdate(old runtime.Object) error {
cronjoblog.Info("validate update", "name", r.Name)
return r.validateCronJob()
}`)
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`func (r *CronJob) ValidateDelete() error {
cronjoblog.Info("validate delete", "name", r.Name)
// TODO(user): fill in your validation logic upon object deletion.
return nil
}`, WebhookValidateSpec)
CheckError("fixing cronjob_webhook.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_webhook.go"),
`validate anything on deletion.
*/
}`, `validate anything on deletion.
*/`)
CheckError("fixing cronjob_webhook.go", err)
}
func updateSuiteTest(sp *Sample) {
var err error
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`limitations under the License.
*/`, SuiteTestIntro)
CheckError("fixing suite_test.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`import (`, `
"context"`)
CheckError("fixing suite_test.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
"path/filepath"
"testing"
`, `
ctrl "sigs.k8s.io/controller-runtime"
`)
CheckError("fixing suite_test.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
`, SuiteTestEnv)
CheckError("fixing suite_test.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
`, SuiteTestReadCRD)
CheckError("fixing suite_test.go", err)
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,
}
`, `
/*
Then, we start the envtest cluster.
*/`)
CheckError("fixing suite_test.go", 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)
CheckError("fixing suite_test.go", 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)
CheckError("fixing suite_test.go", err)
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
`, SuiteTestCleanup)
CheckError("fixing suite_test.go", err)
}
func updateKustomization(sp *Sample) {
var err error
// uncomment default/kustomization
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
`#- ../webhook`, `#`)
CheckError("fixing default/kustomization", err)
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
`#- ../certmanager`, `#`)
CheckError("fixing default/kustomization", err)
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
`#- manager_webhook_patch.yaml`, `#`)
CheckError("fixing default/kustomization", err)
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
`#- webhookcainjection`, `#`)
CheckError("fixing default/kustomization", err)
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
`#- ../prometheus`, `#`)
CheckError("fixing default/kustomization", err)
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
DefaultKustomization, `#`)
CheckError("fixing default/kustomization", err)
// uncomment crd/kustomization
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/crd/kustomization.yaml"),
`#- patches/webhook_in_cronjobs.yaml`, `#`)
CheckError("fixing crd/kustomization", err)
err = pluginutil.UncommentCode(
filepath.Join(sp.ctx.Dir, "config/crd/kustomization.yaml"),
`#- patches/cainjection_in_cronjobs.yaml`, `#`)
CheckError("fixing crd/kustomization", err)
}
func updateExample(sp *Sample) {
var err error
// samples/batch_v1_cronjob
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "config/samples/batch_v1_cronjob.yaml"),
`spec:`, CronjobSample)
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`, "")
CheckError("fixing samples/batch_v1_cronjob.yaml", err)
// update default/manager_auth_proxy_patch.yaml
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "config/default/manager_auth_proxy_patch.yaml"),
` template:
spec:`, ManagerAuthProxySample)
CheckError("fixing default/manager_auth_proxy_patch.yaml", err)
}
func addControllerTest(sp *Sample) {
var fs = afero.NewOsFs()
err := afero.WriteFile(fs, filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller_test.go"), []byte(ControllerTest), 0600)
CheckError("adding cronjob_controller_test", err)
}
// CheckError will exit with exit code 1 when err is not nil.
func CheckError(msg string, err error) {
if err != nil {
log.Errorf("error %s: %s", msg, err)
os.Exit(1)
}
}