Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ split the webhook builder out as a separate builder. #497

Merged
merged 2 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ var (
// NewControllerManagedBy returns a new controller builder that will be started by the provided Manager
NewControllerManagedBy = builder.ControllerManagedBy

// NewWebhookManagedBy returns a new webhook builder that will be started by the provided Manager
NewWebhookManagedBy = builder.WebhookManagedBy

// NewManager returns a new Manager for creating Controllers.
NewManager = manager.New

Expand Down
9 changes: 8 additions & 1 deletion examples/crd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,19 @@ func main() {
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
})

if err != nil {
setupLog.Error(err, "unable to create controller")
os.Exit(1)
}

err = ctrl.NewWebhookManagedBy(mgr).
For(&api.ChaosPod{}).
Complete()
if err != nil {
setupLog.Error(err, "unable to create webhook")
os.Exit(1)
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e h1:n/3MEhJQjQxrO
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273 h1:agujYaXJSxSo18YNX3jzl+4G6Bstwt+kqv47GS12uL0=
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand Down Expand Up @@ -88,7 +87,7 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 h1:+DCIGbF/swA92ohVg0//6X2IVY3KZs6p9mix0ziNYJM=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
gomodules.xyz/jsonpatch/v2 v2.0.0 h1:lHNQverf0+Gm1TbSbVIDWVXOhZ2FpZopxRqpr2uIjs4=
gomodules.xyz/jsonpatch/v2 v2.0.0 h1:OyHbl+7IOECpPKfVK42oFr6N7+Y2dR+Jsb/IiDV3hOo=
gomodules.xyz/jsonpatch/v2 v2.0.0/go.mod h1:IhYNNY4jnS53ZnfE4PAmpKtDpTCj1JFXc+3mwe7XcUU=
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down
94 changes: 2 additions & 92 deletions pkg/builder/build.go → pkg/builder/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ package builder

import (
"fmt"
"net/http"
"net/url"
"strings"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand All @@ -33,8 +30,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/controller-runtime/pkg/webhook/conversion"
)

// Supporting mocking out functions for testing
Expand Down Expand Up @@ -71,8 +66,6 @@ func ControllerManagedBy(m manager.Manager) *Builder {
// update events by *reconciling the object*.
// This is the equivalent of calling
// Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{})
// If the passed in object has implemented the admission.Defaulter interface, a MutatingWebhook will be wired for this type.
// If the passed in object has implemented the admission.Validator interface, a ValidatingWebhook will be wired for this type.
//
// Deprecated: Use For
func (blder *Builder) ForType(apiType runtime.Object) *Builder {
Expand All @@ -83,8 +76,6 @@ func (blder *Builder) ForType(apiType runtime.Object) *Builder {
// update events by *reconciling the object*.
// This is the equivalent of calling
// Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{})
// If the passed in object has implemented the admission.Defaulter interface, a MutatingWebhook will be wired for this type.
// If the passed in object has implemented the admission.Validator interface, a ValidatingWebhook will be wired for this type.
func (blder *Builder) For(apiType runtime.Object) *Builder {
blder.apiType = apiType
return blder
Expand Down Expand Up @@ -159,7 +150,7 @@ func (blder *Builder) Build(r reconcile.Reconciler) (manager.Manager, error) {
}

// Set the Config
if err := blder.doConfig(); err != nil {
if err := blder.loadRestConfig(); err != nil {
return nil, err
}

Expand All @@ -173,11 +164,6 @@ func (blder *Builder) Build(r reconcile.Reconciler) (manager.Manager, error) {
return nil, err
}

// Set the Webook if needed
if err := blder.doWebhook(); err != nil {
return nil, err
}

// Set the Watch
if err := blder.doWatch(); err != nil {
return nil, err
Expand Down Expand Up @@ -217,7 +203,7 @@ func (blder *Builder) doWatch() error {
return nil
}

func (blder *Builder) doConfig() error {
func (blder *Builder) loadRestConfig() error {
if blder.config != nil {
return nil
}
Expand Down Expand Up @@ -258,79 +244,3 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
blder.ctrl, err = newController(name, blder.mgr, controller.Options{Reconciler: r})
return err
}

func (blder *Builder) doWebhook() error {
// Create a webhook for each type
gvk, err := apiutil.GVKForObject(blder.apiType, blder.mgr.GetScheme())
if err != nil {
return err
}

// TODO: When the conversion webhook lands, we need to handle all registered versions of a given group-kind.
// A potential workflow for defaulting webhook
// 1) a bespoke (non-hub) version comes in
// 2) convert it to the hub version
// 3) do defaulting
// 4) convert it back to the same bespoke version
// 5) calculate the JSON patch
//
// A potential workflow for validating webhook
// 1) a bespoke (non-hub) version comes in
// 2) convert it to the hub version
// 3) do validation
if defaulter, isDefaulter := blder.apiType.(admission.Defaulter); isDefaulter {
mwh := admission.DefaultingWebhookFor(defaulter)
if mwh != nil {
path := generateMutatePath(gvk)

// Checking if the path is already registered.
// If so, just skip it.
if !blder.isAlreadyHandled(path) {
log.Info("Registering a mutating webhook",
"GVK", gvk,
"path", path)
blder.mgr.GetWebhookServer().Register(path, mwh)
}
}
}

if validator, isValidator := blder.apiType.(admission.Validator); isValidator {
vwh := admission.ValidatingWebhookFor(validator)
if vwh != nil {
path := generateValidatePath(gvk)

// Checking if the path is already registered.
// If so, just skip it.
if !blder.isAlreadyHandled(path) {
log.Info("Registering a validating webhook",
"GVK", gvk,
"path", path)
blder.mgr.GetWebhookServer().Register(path, vwh)
}
}
}

err = conversion.CheckConvertibility(blder.mgr.GetScheme(), blder.apiType)
if err != nil {
log.Error(err, "conversion check failed", "GVK", gvk)
}
return nil
}

func (blder *Builder) isAlreadyHandled(path string) bool {
h, p := blder.mgr.GetWebhookServer().WebhookMux.Handler(&http.Request{URL: &url.URL{Path: path}})
if p == path && h != nil {
return true
}
return false
}

func generateMutatePath(gvk schema.GroupVersionKind) string {
return "/mutate-" + strings.Replace(gvk.Group, ".", "-", -1) + "-" +
gvk.Version + "-" + strings.ToLower(gvk.Kind)
}

func generateValidatePath(gvk schema.GroupVersionKind) string {
return "/validate-" + strings.Replace(gvk.Group, ".", "-", -1) + "-" +
gvk.Version + "-" + strings.ToLower(gvk.Kind)
}
Loading