Skip to content

Commit

Permalink
refactor conversion webhook (#654)
Browse files Browse the repository at this point in the history
refactor conversion webhook
  • Loading branch information
yalosev authored Sep 18, 2024
1 parent 22aa2b0 commit 44088bf
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 325 deletions.
1 change: 0 additions & 1 deletion pkg/app/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,4 @@ func DefineConversionWebhookFlags(cmd *kingpin.CmdClause) {
Default(ConversionWebhookSettings.ListenAddr).
Envar("CONVERSION_WEBHOOK_LISTEN_ADDRESS").
StringVar(&ConversionWebhookSettings.ListenAddr)

}
4 changes: 2 additions & 2 deletions pkg/hook/binding_context/binding_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package binding_context

import (
"encoding/json"

log "github.com/sirupsen/logrus"
v1 "k8s.io/api/admission/v1"
apixv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

. "github.com/flant/shell-operator/pkg/hook/types"
. "github.com/flant/shell-operator/pkg/kube_events_manager/types"
Expand All @@ -29,7 +29,7 @@ type BindingContext struct {
Objects []ObjectAndFilterResult
Snapshots map[string][]ObjectAndFilterResult
AdmissionReview *v1.AdmissionReview
ConversionReview map[string]interface{}
ConversionReview *apixv1.ConversionReview
FromVersion string
ToVersion string
}
Expand Down
47 changes: 18 additions & 29 deletions pkg/hook/controller/conversion_bindings_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
log "github.com/sirupsen/logrus"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

. "github.com/flant/shell-operator/pkg/hook/binding_context"
. "github.com/flant/shell-operator/pkg/hook/types"
Expand All @@ -19,18 +20,8 @@ type ConversionBindingToWebhookLink struct {
Group string
}

// ScheduleBindingsController handles schedule bindings for one hook.
type ConversionBindingsController interface {
WithBindings([]ConversionConfig)
WithWebhookManager(manager *conversion.WebhookManager)
EnableConversionBindings()
DisableConversionBindings()
CanHandleEvent(event conversion.Event, rule conversion.Rule) bool
HandleEvent(event conversion.Event, rule conversion.Rule) BindingExecutionInfo
}

// Controller holds validating bindings from one hook.
type conversionBindingsController struct {
// ConversionBindingsController holds validating bindings from one hook.
type ConversionBindingsController struct {
// crdName -> conversionRule id -> link
Links map[string]map[conversion.Rule]*ConversionBindingToWebhookLink

Expand All @@ -39,24 +30,22 @@ type conversionBindingsController struct {
webhookManager *conversion.WebhookManager
}

var _ ConversionBindingsController = &conversionBindingsController{}

// NewConversionBindingsController returns an implementation of ConversionBindingsController
var NewConversionBindingsController = func() *conversionBindingsController {
return &conversionBindingsController{
var NewConversionBindingsController = func() *ConversionBindingsController {
return &ConversionBindingsController{
Links: make(map[string]map[conversion.Rule]*ConversionBindingToWebhookLink),
}
}

func (c *conversionBindingsController) WithBindings(bindings []ConversionConfig) {
func (c *ConversionBindingsController) WithBindings(bindings []ConversionConfig) {
c.Bindings = bindings
}

func (c *conversionBindingsController) WithWebhookManager(mgr *conversion.WebhookManager) {
func (c *ConversionBindingsController) WithWebhookManager(mgr *conversion.WebhookManager) {
c.webhookManager = mgr
}

func (c *conversionBindingsController) EnableConversionBindings() {
func (c *ConversionBindingsController) EnableConversionBindings() {
// Setup links and inform webhookManager about webhooks.
for _, config := range c.Bindings {
if _, ok := c.Links[config.Webhook.CrdName]; !ok {
Expand All @@ -76,31 +65,31 @@ func (c *conversionBindingsController) EnableConversionBindings() {
}
}

func (c *conversionBindingsController) DisableConversionBindings() {
func (c *ConversionBindingsController) DisableConversionBindings() {
// TODO dynamic enable/disable conversion webhooks.
}

func (c *conversionBindingsController) CanHandleEvent(event conversion.Event, rule conversion.Rule) bool {
_, has := c.Links[event.CrdName]
func (c *ConversionBindingsController) CanHandleEvent(crdName string, request *v1.ConversionRequest, rule conversion.Rule) bool {
_, has := c.Links[crdName]
if !has {
return false
}
_, has = c.Links[event.CrdName][rule]
_, has = c.Links[crdName][rule]
return has
}

func (c *conversionBindingsController) HandleEvent(event conversion.Event, rule conversion.Rule) BindingExecutionInfo {
_, hasKey := c.Links[event.CrdName]
func (c *ConversionBindingsController) HandleEvent(crdName string, request *v1.ConversionRequest, rule conversion.Rule) BindingExecutionInfo {
_, hasKey := c.Links[crdName]
if !hasKey {
log.Errorf("Possible bug!!! No binding for conversion event for crd/%s", event.CrdName)
log.Errorf("Possible bug!!! No binding for conversion event for crd/%s", crdName)
return BindingExecutionInfo{
BindingContext: []BindingContext{},
AllowFailure: false,
}
}
link, has := c.Links[event.CrdName][rule]
link, has := c.Links[crdName][rule]
if !has {
log.Errorf("Possible bug!!! Event has an unknown conversion rule %s for crd/%s: no binding was registered", rule.String(), event.CrdName)
log.Errorf("Possible bug!!! Event has an unknown conversion rule %s for crd/%s: no binding was registered", rule.String(), crdName)
return BindingExecutionInfo{
BindingContext: []BindingContext{},
AllowFailure: false,
Expand All @@ -109,7 +98,7 @@ func (c *conversionBindingsController) HandleEvent(event conversion.Event, rule

bc := BindingContext{
Binding: link.BindingName,
ConversionReview: event.GetReview(),
ConversionReview: &v1.ConversionReview{Request: request},
FromVersion: link.FromVersion,
ToVersion: link.ToVersion,
}
Expand Down
Loading

0 comments on commit 44088bf

Please sign in to comment.