Skip to content

Commit a802720

Browse files
author
Mengqi Yu
committed
🐛 fix a bug in create webhook command
1 parent 285b0bc commit a802720

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
lines changed

cmd/webhook_v2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ You need to implement the conversion.Hub and conversion.Convertible interfaces f
110110
"if set, scaffold the defaulting webhook")
111111
cmd.Flags().BoolVar(&o.validation, "programmatic-validation", false,
112112
"if set, scaffold the validating webhook")
113-
cmd.Flags().BoolVar(&o.validation, "conversion", false,
113+
cmd.Flags().BoolVar(&o.conversion, "conversion", false,
114114
"if set, scaffold the conversion webhook")
115115

116116
return cmd

generated_golden.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ scaffold_test_project() {
6464

6565
$kb init --project-version $version --domain testproject.org --license apache2 --owner "The Kubernetes authors"
6666
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
67-
$kb create webhook --group crew --version v1 --kind Captain --defaulting
67+
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation
6868
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
69-
$kb create webhook --group crew --version v1 --kind FirstMate --programmatic-validation
69+
$kb create webhook --group crew --version v1 --kind FirstMate --conversion
7070
# TODO(droot): Adding a second group is a valid test case and kubebuilder is expected to report an error in this case. It
7171
# doesn't do that currently so leaving it commented so that we can enable it later.
7272
# $kb create api --group ship --version v1beta1 --kind Frigate --example=false --controller=true --resource=true --make=false

testdata/project-v2/api/v1/captain_webhook.go

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1
1818

1919
import (
20+
"k8s.io/apimachinery/pkg/runtime"
2021
ctrl "sigs.k8s.io/controller-runtime"
2122
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
2223
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -43,3 +44,23 @@ func (r *Captain) Default() {
4344

4445
// TODO(user): fill in your defaulting logic.
4546
}
47+
48+
// +kubebuilder:webhook:path=/validate-crew-testproject-org-v1-captain,mutating=false,failurePolicy=fail,groups=crew.testproject.org,resources=captains,verbs=create;update,versions=v1,name=vcaptain.kb.io
49+
50+
var _ webhook.Validator = &Captain{}
51+
52+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
53+
func (r *Captain) ValidateCreate() error {
54+
captainlog.Info("validate create", "name", r.Name)
55+
56+
// TODO(user): fill in your validation logic upon object creation.
57+
return nil
58+
}
59+
60+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
61+
func (r *Captain) ValidateUpdate(old runtime.Object) error {
62+
captainlog.Info("validate update", "name", r.Name)
63+
64+
// TODO(user): fill in your validation logic upon object update.
65+
return nil
66+
}

testdata/project-v2/api/v1/firstmate_webhook.go

-22
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ limitations under the License.
1717
package v1
1818

1919
import (
20-
"k8s.io/apimachinery/pkg/runtime"
2120
ctrl "sigs.k8s.io/controller-runtime"
2221
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
23-
"sigs.k8s.io/controller-runtime/pkg/webhook"
2422
)
2523

2624
// log is for logging in this package.
@@ -33,23 +31,3 @@ func (r *FirstMate) SetupWebhookWithManager(mgr ctrl.Manager) error {
3331
}
3432

3533
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
36-
37-
// +kubebuilder:webhook:path=/validate-crew-testproject-org-v1-firstmate,mutating=false,failurePolicy=fail,groups=crew.testproject.org,resources=firstmates,verbs=create;update,versions=v1,name=vfirstmate.kb.io
38-
39-
var _ webhook.Validator = &FirstMate{}
40-
41-
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
42-
func (r *FirstMate) ValidateCreate() error {
43-
firstmatelog.Info("validate create", "name", r.Name)
44-
45-
// TODO(user): fill in your validation logic upon object creation.
46-
return nil
47-
}
48-
49-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
50-
func (r *FirstMate) ValidateUpdate(old runtime.Object) error {
51-
firstmatelog.Info("validate update", "name", r.Name)
52-
53-
// TODO(user): fill in your validation logic upon object update.
54-
return nil
55-
}

testdata/project-v2/config/webhook/manifests.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ webhooks:
3737
service:
3838
name: webhook-service
3939
namespace: system
40-
path: /validate-crew-testproject-org-v1-firstmate
40+
path: /validate-crew-testproject-org-v1-captain
4141
failurePolicy: Fail
42-
name: vfirstmate.kb.io
42+
name: vcaptain.kb.io
4343
rules:
4444
- apiGroups:
4545
- crew.testproject.org
@@ -49,4 +49,4 @@ webhooks:
4949
- CREATE
5050
- UPDATE
5151
resources:
52-
- firstmates
52+
- captains

0 commit comments

Comments
 (0)