Skip to content
Permalink
Browse files

🐛 fix a bug in create webhook command

  • Loading branch information...
mengqiy committed Jul 8, 2019
1 parent 285b0bc commit a8027202e33c257bc5e6f36ad2e5f5ba01a39cf2
@@ -110,7 +110,7 @@ You need to implement the conversion.Hub and conversion.Convertible interfaces f
"if set, scaffold the defaulting webhook")
cmd.Flags().BoolVar(&o.validation, "programmatic-validation", false,
"if set, scaffold the validating webhook")
cmd.Flags().BoolVar(&o.validation, "conversion", false,
cmd.Flags().BoolVar(&o.conversion, "conversion", false,
"if set, scaffold the conversion webhook")

return cmd
@@ -64,9 +64,9 @@ scaffold_test_project() {

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

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

// TODO(user): fill in your defaulting logic.
}

// +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

var _ webhook.Validator = &Captain{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Captain) ValidateCreate() error {
captainlog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Captain) ValidateUpdate(old runtime.Object) error {
captainlog.Info("validate update", "name", r.Name)

// TODO(user): fill in your validation logic upon object update.
return nil
}
@@ -17,10 +17,8 @@ limitations under the License.
package v1

import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

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

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

// +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

var _ webhook.Validator = &FirstMate{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *FirstMate) ValidateCreate() error {
firstmatelog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *FirstMate) ValidateUpdate(old runtime.Object) error {
firstmatelog.Info("validate update", "name", r.Name)

// TODO(user): fill in your validation logic upon object update.
return nil
}
@@ -37,9 +37,9 @@ webhooks:
service:
name: webhook-service
namespace: system
path: /validate-crew-testproject-org-v1-firstmate
path: /validate-crew-testproject-org-v1-captain
failurePolicy: Fail
name: vfirstmate.kb.io
name: vcaptain.kb.io
rules:
- apiGroups:
- crew.testproject.org
@@ -49,4 +49,4 @@ webhooks:
- CREATE
- UPDATE
resources:
- firstmates
- captains

0 comments on commit a802720

Please sign in to comment.
You can’t perform that action at this time.