forked from kubernetes-retired/service-catalog
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace plugins by webhook (kubernetes-retired#16)
* Replace ServiceBinding plugin by webhook * Replace Broker plugins by webhook * Adjust webhooks to multi validation handlers
- Loading branch information
Showing
22 changed files
with
1,753 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
pkg/webhook/servicecatalog/clusterservicebroker/validation/handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package validation | ||
|
||
import ( | ||
"context" | ||
sc "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1" | ||
"github.com/kubernetes-incubator/service-catalog/pkg/webhookutil" | ||
admissionTypes "k8s.io/api/admission/v1beta1" | ||
"net/http" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/runtime/inject" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
// Validator is used to implement new validation logic | ||
type Validator interface { | ||
Validate(context.Context, admission.Request, *sc.ClusterServiceBroker, *webhookutil.TracedLogger) *webhookutil.WebhookError | ||
} | ||
|
||
// AdmissionHandler handles ClusterServiceBroker validation | ||
type AdmissionHandler struct { | ||
decoder *admission.Decoder | ||
|
||
CreateValidators []Validator | ||
UpdateValidators []Validator | ||
} | ||
|
||
var _ admission.Handler = &AdmissionHandler{} | ||
var _ admission.DecoderInjector = &AdmissionHandler{} | ||
var _ inject.Client = &AdmissionHandler{} | ||
|
||
// NewAdmissionHandler creates new AdmissionHandler and initializes validators list | ||
func NewAdmissionHandler() *AdmissionHandler { | ||
return &AdmissionHandler{ | ||
CreateValidators: []Validator{&AccessToBroker{}}, | ||
UpdateValidators: []Validator{&AccessToBroker{}}, | ||
} | ||
} | ||
|
||
// Handle handles admission requests. | ||
func (h *AdmissionHandler) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
traced := webhookutil.NewTracedLogger(req.UID) | ||
traced.Infof("Start handling validation operation: %s for %s", req.Operation, req.Kind.Kind) | ||
|
||
csb := &sc.ClusterServiceBroker{} | ||
if err := webhookutil.MatchKinds(csb, req.Kind); err != nil { | ||
traced.Errorf("Error matching kinds: %v", err) | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
if err := h.decoder.Decode(req, csb); err != nil { | ||
traced.Errorf("Could not decode request object: %v", err) | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
traced.Infof("start validation process for %s: %s/%s", csb.Kind, csb.Namespace, csb.Name) | ||
|
||
var err *webhookutil.WebhookError | ||
|
||
switch req.Operation { | ||
case admissionTypes.Create: | ||
for _, v := range h.CreateValidators { | ||
err = v.Validate(ctx, req, csb, traced) | ||
if err != nil { | ||
break | ||
} | ||
} | ||
case admissionTypes.Update: | ||
for _, v := range h.UpdateValidators { | ||
err = v.Validate(ctx, req, csb, traced) | ||
if err != nil { | ||
break | ||
} | ||
} | ||
default: | ||
traced.Infof("ClusterServiceBroker validation wehbook does not support action %q", req.Operation) | ||
return admission.Allowed("action not taken") | ||
} | ||
|
||
if err != nil { | ||
switch err.Code() { | ||
case http.StatusForbidden: | ||
return admission.Denied(err.Error()) | ||
default: | ||
return admission.Errored(err.Code(), err) | ||
} | ||
} | ||
|
||
traced.Infof("Completed successfully validation operation: %s for %s: %q", req.Operation, req.Kind.Kind, req.Name) | ||
return admission.Allowed("ClusterServiceBroker AdmissionHandler successful") | ||
} | ||
|
||
// InjectDecoder injects the decoder into the handlers | ||
func (h *AdmissionHandler) InjectDecoder(d *admission.Decoder) error { | ||
h.decoder = d | ||
|
||
for _, v := range h.CreateValidators { | ||
_, err := admission.InjectDecoderInto(d, v) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
for _, v := range h.UpdateValidators { | ||
_, err := admission.InjectDecoderInto(d, v) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// InjectClient injects the client into the handlers | ||
func (h *AdmissionHandler) InjectClient(c client.Client) error { | ||
for _, v := range h.CreateValidators { | ||
_, err := inject.ClientInto(c, v) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
for _, v := range h.UpdateValidators { | ||
_, err := inject.ClientInto(c, v) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.