Skip to content

Commit

Permalink
fix webhooks no longer self-register decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
joejulian committed Oct 4, 2023
1 parent 7fa4fae commit a794d05
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 89 deletions.
13 changes: 11 additions & 2 deletions src/go/k8s/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

clusterredpandacomv1alpha1 "github.com/redpanda-data/redpanda/src/go/k8s/api/cluster.redpanda.com/v1alpha1"
redpandav1alpha1 "github.com/redpanda-data/redpanda/src/go/k8s/api/redpanda/v1alpha1"
Expand Down Expand Up @@ -289,8 +290,16 @@ func main() {
os.Exit(1)
}
hookServer := mgr.GetWebhookServer()
hookServer.Register("/mutate-redpanda-vectorized-io-v1alpha1-console", &webhook.Admission{Handler: &redpandawebhooks.ConsoleDefaulter{Client: mgr.GetClient()}})
hookServer.Register("/validate-redpanda-vectorized-io-v1alpha1-console", &webhook.Admission{Handler: &redpandawebhooks.ConsoleValidator{Client: mgr.GetClient()}})
hookServer.Register("/mutate-redpanda-vectorized-io-v1alpha1-console", &webhook.Admission{
Handler: &redpandawebhooks.ConsoleDefaulter{
Client: mgr.GetClient(),
Decoder: admission.NewDecoder(scheme),
}})
hookServer.Register("/validate-redpanda-vectorized-io-v1alpha1-console", &webhook.Admission{
Handler: &redpandawebhooks.ConsoleValidator{
Client: mgr.GetClient(),
Decoder: admission.NewDecoder(scheme),
}})
}
case OperatorV2Mode:
ctrl.Log.Info("running in v2", "mode", OperatorV2Mode, "namespace", namespace)
Expand Down
26 changes: 4 additions & 22 deletions src/go/k8s/webhooks/redpanda/console_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var validHostnameSegment = regexp.MustCompile(`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0
// ConsoleValidator validates Consoles
type ConsoleValidator struct {
Client client.Client
decoder *admission.Decoder
Decoder *admission.Decoder
}

// Handle processes admission for Console
Expand All @@ -37,7 +37,7 @@ func (v *ConsoleValidator) Handle(
) admission.Response {
console := &vectorizedv1alpha1.Console{}

err := v.decoder.Decode(req, console)
err := v.Decoder.Decode(req, console)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
Expand Down Expand Up @@ -97,21 +97,12 @@ func (v *ConsoleValidator) Handle(
return admission.Allowed("")
}

// ConsoleValidator implements admission.DecoderInjector.
// A decoder will be automatically injected.

// InjectDecoder injects the decoder.
func (v *ConsoleValidator) InjectDecoder(d *admission.Decoder) error {
v.decoder = d
return nil
}

// +kubebuilder:webhook:path=/mutate-redpanda-vectorized-io-v1alpha1-console,mutating=true,failurePolicy=fail,sideEffects=None,groups="redpanda.vectorized.io",resources=consoles,verbs=create;update,versions=v1alpha1,name=mconsole.kb.io,admissionReviewVersions=v1

// ConsoleDefaulter mutates Consoles
type ConsoleDefaulter struct {
Client client.Client
decoder *admission.Decoder
Decoder *admission.Decoder
}

// Handle processes admission for Console
Expand All @@ -121,7 +112,7 @@ func (m *ConsoleDefaulter) Handle(
) admission.Response {
console := &vectorizedv1alpha1.Console{}

err := m.decoder.Decode(req, console)
err := m.Decoder.Decode(req, console)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
Expand Down Expand Up @@ -168,12 +159,3 @@ func (m *ConsoleDefaulter) Default(
response := admission.PatchResponseFromRaw(original, current)
return &response, nil
}

// ConsoleDefaulter implements admission.DecoderInjector.
// A decoder will be automatically injected.

// InjectDecoder injects the decoder.
func (m *ConsoleDefaulter) InjectDecoder(d *admission.Decoder) error {
m.decoder = d
return nil
}
Loading

0 comments on commit a794d05

Please sign in to comment.