Skip to content

Commit 91bbdf9

Browse files
fix samples
1 parent ed2d282 commit 91bbdf9

File tree

17 files changed

+9828
-129
lines changed

17 files changed

+9828
-129
lines changed

docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
*/
2020

2121
package v1
22+
2223
/*
2324
*/
2425

@@ -61,7 +62,6 @@ import (
6162
the fields.
6263
*/
6364

64-
6565
// CronJobSpec defines the desired state of CronJob
6666
type CronJobSpec struct {
6767
//+kubebuilder:validation:MinLength=0
@@ -188,4 +188,5 @@ type CronJobList struct {
188188
func init() {
189189
SchemeBuilder.Register(&CronJob{}, &CronJobList{})
190190
}
191+
191192
//+kubebuilder:docs-gen:collapse=Root Object Definitions

docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Next, we'll setup a logger for the webhooks.
3737
*/
3838

3939
var cronjoblog = logf.Log.WithName("cronjob-resource")
40+
4041
/*
4142
Then, we set up the webhook with the manager.
4243
*/
@@ -63,14 +64,11 @@ A webhook will automatically be served that calls this defaulting.
6364
The `Default` method is expected to mutate the receiver, setting the defaults.
6465
*/
6566

66-
67-
68-
6967
var _ webhook.Defaulter = &CronJob{}
7068

7169
// Default implements webhook.Defaulter so a webhook will be registered for the type
7270
func (r *CronJob) Default() {
73-
cronjoblog.Info("default", "name", r.Name)
71+
cronjoblog.Info("default", "name", r.Name)
7472

7573
if r.Spec.ConcurrencyPolicy == "" {
7674
r.Spec.ConcurrencyPolicy = AllowConcurrent
@@ -116,24 +114,19 @@ Here, however, we just use the same shared validation for `ValidateCreate` and
116114
validate anything on deletion.
117115
*/
118116

119-
120-
121-
122117
var _ webhook.Validator = &CronJob{}
123118

124119
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
125120
func (r *CronJob) ValidateCreate() (admission.Warnings, error) {
126121
cronjoblog.Info("validate create", "name", r.Name)
127122

128-
129123
return nil, r.validateCronJob()
130124
}
131125

132126
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
133127
func (r *CronJob) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
134128
cronjoblog.Info("validate update", "name", r.Name)
135129

136-
137130
return nil, r.validateCronJob()
138131
}
139132

@@ -143,6 +136,8 @@ func (r *CronJob) ValidateDelete() (admission.Warnings, error) {
143136

144137
// TODO(user): fill in your validation logic upon object deletion.
145138
return nil, nil
139+
}
140+
146141
/*
147142
We validate the name and the spec of the CronJob.
148143
*/
@@ -217,4 +212,3 @@ func (r *CronJob) validateCronJobName() *field.Error {
217212
}
218213

219214
// +kubebuilder:docs-gen:collapse=Validate object name
220-
}

docs/book/src/cronjob-tutorial/testdata/project/api/v1/groupversion_info.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
// +kubebuilder:docs-gen:collapse=Apache License
17+
18+
/*
19+
First, we have some *package-level* markers that denote that there are
20+
Kubernetes objects in this package, and that this package represents the group
21+
`batch.tutorial.kubebuilder.io`. The `object` generator makes use of the
22+
former, while the latter is used by the CRD generator to generate the right
23+
metadata for the CRDs it creates from this package.
24+
*/
1625

1726
// Package v1 contains API Schema definitions for the batch v1 API group
1827
// +kubebuilder:object:generate=true
@@ -24,6 +33,13 @@ import (
2433
"sigs.k8s.io/controller-runtime/pkg/scheme"
2534
)
2635

36+
/*
37+
Then, we have the commonly useful variables that help us set up our Scheme.
38+
Since we need to use all the types in this package in our controller, it's
39+
helpful (and the convention) to have a convenient method to add all the types to
40+
some other `Scheme`. SchemeBuilder makes this easy for us.
41+
*/
42+
2743
var (
2844
// GroupVersion is group version used to register these objects
2945
GroupVersion = schema.GroupVersion{Group: "batch.tutorial.kubebuilder.io", Version: "v1"}

docs/book/src/cronjob-tutorial/testdata/project/api/v1/zz_generated.deepcopy.go

Lines changed: 33 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
// +kubebuilder:docs-gen:collapse=Apache License
1617

1718
package main
1819

@@ -36,6 +37,17 @@ import (
3637
//+kubebuilder:scaffold:imports
3738
)
3839

40+
// +kubebuilder:docs-gen:collapse=Imports
41+
42+
/*
43+
The first difference to notice is that kubebuilder has added the new API
44+
group's package (`batchv1`) to our scheme. This means that we can use those
45+
objects in our controller.
46+
47+
If we would be using any other CRD we would have to add their scheme the same way.
48+
Builtin types such as Job have their scheme added by `clientgoscheme`.
49+
*/
50+
3951
var (
4052
scheme = runtime.NewScheme()
4153
setupLog = ctrl.Log.WithName("setup")
@@ -48,7 +60,14 @@ func init() {
4860
//+kubebuilder:scaffold:scheme
4961
}
5062

63+
/*
64+
The other thing that's changed is that kubebuilder has added a block calling our
65+
CronJob controller's `SetupWithManager` method.
66+
*/
67+
5168
func main() {
69+
/*
70+
*/
5271
var metricsAddr string
5372
var enableLeaderElection bool
5473
var probeAddr string
@@ -89,16 +108,29 @@ func main() {
89108
os.Exit(1)
90109
}
91110

111+
// +kubebuilder:docs-gen:collapse=old stuff
112+
92113
if err = (&controller.CronJobReconciler{
93114
Client: mgr.GetClient(),
94115
Scheme: mgr.GetScheme(),
95116
}).SetupWithManager(mgr); err != nil {
96117
setupLog.Error(err, "unable to create controller", "controller", "CronJob")
97118
os.Exit(1)
98119
}
99-
if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
100-
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
101-
os.Exit(1)
120+
121+
/*
122+
We'll also set up webhooks for our type, which we'll talk about next.
123+
We just need to add them to the manager. Since we might want to run
124+
the webhooks separately, or not run them when testing our controller
125+
locally, we'll put them behind an environment variable.
126+
127+
We'll just make sure to set `ENABLE_WEBHOOKS=false` when we run locally.
128+
*/
129+
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
130+
if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
131+
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
132+
os.Exit(1)
133+
}
102134
}
103135
//+kubebuilder:scaffold:builder
104136

@@ -116,4 +148,5 @@ func main() {
116148
setupLog.Error(err, "problem running manager")
117149
os.Exit(1)
118150
}
151+
// +kubebuilder:docs-gen:collapse=old stuff
119152
}

0 commit comments

Comments
 (0)