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

✨ enable conversion webhook in webhook builder #502

Closed
wants to merge 3 commits into from
Closed
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
25 changes: 0 additions & 25 deletions examples/conversion/pkg/apis/addtoscheme_jobs_v1.go

This file was deleted.

25 changes: 0 additions & 25 deletions examples/conversion/pkg/apis/addtoscheme_jobs_v2.go

This file was deleted.

32 changes: 0 additions & 32 deletions examples/conversion/pkg/apis/apis.go

This file was deleted.

22 changes: 0 additions & 22 deletions examples/conversion/pkg/apis/jobs/v1/doc.go

This file was deleted.

22 changes: 0 additions & 22 deletions examples/conversion/pkg/apis/jobs/v2/doc.go

This file was deleted.

4 changes: 4 additions & 0 deletions examples/crd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func main() {
scheme: mgr.GetScheme(),
})

err = ctrl.NewWebhookManagedBy(mgr).
For(&api.ChaosPod{}).
Complete()

if err != nil {
setupLog.Error(err, "unable to create controller")
os.Exit(1)
Expand Down
90 changes: 0 additions & 90 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 @@ -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 @@ -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