Skip to content

Commit

Permalink
Replace plugins by webhook (kubernetes-retired#16)
Browse files Browse the repository at this point in the history
* Replace ServiceBinding plugin by webhook

* Replace Broker plugins by webhook

* Adjust webhooks to multi validation handlers
  • Loading branch information
Piotr Jasiak authored and mszostok committed Jun 11, 2019
1 parent 6ba56a3 commit 63516bb
Show file tree
Hide file tree
Showing 22 changed files with 1,753 additions and 35 deletions.
8 changes: 4 additions & 4 deletions charts/catalog/templates/crds.yaml
Expand Up @@ -55,14 +55,14 @@ spec:
- "secretRef"
properties:
secretRef:
type: string
type: object
bearer:
type: object
required:
- "secretRef"
properties:
secretRef:
type: string
type: object

---

Expand Down Expand Up @@ -123,14 +123,14 @@ spec:
- "secretRef"
properties:
secretRef:
type: string
type: object
bearer:
type: object
required:
- "secretRef"
properties:
secretRef:
type: string
type: object

---

Expand Down
3 changes: 3 additions & 0 deletions charts/catalog/templates/rbac.yaml
Expand Up @@ -172,6 +172,9 @@ items:
- apiGroups: ["servicecatalog.k8s.io"]
resources: ["serviceinstances","servicebindings"]
verbs: ["get","list","watch"]
- apiGroups: ["authorization.k8s.io"]
resources: ["subjectaccessreviews"]
verbs: ["get","list","create"]
{{- if not .Values.namespacedServiceBrokerDisabled }}
- apiGroups: ["servicecatalog.k8s.io"]
resources: ["serviceclasses"]
Expand Down
39 changes: 39 additions & 0 deletions charts/catalog/templates/webhook-register.yaml
Expand Up @@ -133,6 +133,45 @@ webhooks:
apiGroups: ["servicecatalog.k8s.io"]
apiVersions: ["v1beta1"]
resources: ["serviceinstances"]
- name: validating.clusterservicebrokers.servicecatalog.k8s.io
clientConfig:
caBundle: {{ b64enc $ca.Cert }}
service:
name: {{ template "fullname" . }}-webhook
namespace: "{{ .Release.Namespace }}"
path: "/validating-clusterservicebrokers"
failurePolicy: Fail
rules:
- operations: [ "CREATE", "UPDATE" ]
apiGroups: ["servicecatalog.k8s.io"]
apiVersions: ["v1beta1"]
resources: ["clusterservicebrokers"]
- name: validating.servicebindings.servicecatalog.k8s.io
clientConfig:
caBundle: {{ b64enc $ca.Cert }}
service:
name: {{ template "fullname" . }}-webhook
namespace: "{{ .Release.Namespace }}"
path: "/validating-servicebindings"
failurePolicy: Fail
rules:
- operations: [ "CREATE", "UPDATE" ]
apiGroups: ["servicecatalog.k8s.io"]
apiVersions: ["v1beta1"]
resources: ["servicebindings"]
- name: validating.servicebrokers.servicecatalog.k8s.io
clientConfig:
caBundle: {{ b64enc $ca.Cert }}
service:
name: {{ template "fullname" . }}-webhook
namespace: "{{ .Release.Namespace }}"
path: "/validating-servicebrokers"
failurePolicy: Fail
rules:
- operations: [ "CREATE", "UPDATE" ]
apiGroups: ["servicecatalog.k8s.io"]
apiVersions: ["v1beta1"]
resources: ["servicebrokers"]
---
apiVersion: v1
kind: Secret
Expand Down
2 changes: 1 addition & 1 deletion charts/catalog/values.yaml
@@ -1,6 +1,6 @@
# Default values for Service Catalog
# service-catalog image to use
image: eu.gcr.io/kyma-project/develop/service-catalog/service-catalog-amd64:crd-0.0.3
image: eu.gcr.io/kyma-project/develop/service-catalog/service-catalog-amd64:crd-0.0.5
# imagePullPolicy for the service-catalog; valid values are "IfNotPresent",
# "Never", and "Always"
imagePullPolicy: Always
Expand Down
8 changes: 7 additions & 1 deletion cmd/webhook/server/webhook.go
Expand Up @@ -31,6 +31,9 @@ import (
simutation "github.com/kubernetes-incubator/service-catalog/pkg/webhook/servicecatalog/serviceinstance/mutation"
spmutation "github.com/kubernetes-incubator/service-catalog/pkg/webhook/servicecatalog/serviceplan/mutation"

csbrvalidation "github.com/kubernetes-incubator/service-catalog/pkg/webhook/servicecatalog/clusterservicebroker/validation"
sbvalidation "github.com/kubernetes-incubator/service-catalog/pkg/webhook/servicecatalog/servicebinding/validation"
sbrvalidation "github.com/kubernetes-incubator/service-catalog/pkg/webhook/servicecatalog/servicebroker/validation"
sivalidation "github.com/kubernetes-incubator/service-catalog/pkg/webhook/servicecatalog/serviceinstance/validation"

"github.com/pkg/errors"
Expand Down Expand Up @@ -81,7 +84,10 @@ func run(opts *WebhookServerOptions, stopCh <-chan struct{}) error {
"/mutating-serviceplans": &spmutation.CreateUpdateHandler{},
"/mutating-serviceinstances": simutation.New(),

"/validating-serviceinstances": sivalidation.NewAdmissionHandler(),
"/validating-clusterservicebrokers": csbrvalidation.NewAdmissionHandler(),
"/validating-servicebindings": sbvalidation.NewAdmissionHandler(),
"/validating-servicebrokers": sbrvalidation.NewAdmissionHandler(),
"/validating-serviceinstances": sivalidation.NewAdmissionHandler(),
}

for path, handler := range webhooks {
Expand Down
14 changes: 13 additions & 1 deletion pkg/apis/servicecatalog/v1beta1/types.go
Expand Up @@ -1496,5 +1496,17 @@ type RemoveKeyTransform struct {
func init() {
// SchemaBuilder is used to map go structs to GroupVersionKinds.
// Solution suggested by the Kubebuilder book: https://book.kubebuilder.io/basics/simple_resource.html - "Scaffolded Boilerplate" section
SchemeBuilderRuntime.Register(&ServiceBinding{}, &ServiceInstance{}, &ClusterServiceClassList{}, &ServiceClassList{}, &ServiceClass{}, &ClusterServiceClass{}, &ClusterServicePlanList{}, &ServicePlanList{}, &ServicePlan{}, &ClusterServicePlan{})
SchemeBuilderRuntime.Register(
&ServiceBinding{},
&ServiceInstance{},
&ClusterServiceClass{},
&ClusterServiceClassList{},
&ServiceBroker{},
&ClusterServiceBroker{},
&ServiceClass{},
&ServiceClassList{},
&ServicePlan{},
&ServicePlanList{},
&ClusterServicePlan{},
&ClusterServicePlanList{})
}
144 changes: 144 additions & 0 deletions pkg/webhook/servicecatalog/clusterservicebroker/validation/handler.go
@@ -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
}

0 comments on commit 63516bb

Please sign in to comment.