Skip to content

Commit 981c9ee

Browse files
author
Mengqi Yu
committed
address comments
1 parent 49dca2b commit 981c9ee

File tree

7 files changed

+27
-84
lines changed

7 files changed

+27
-84
lines changed

pkg/builder/build.go

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ package builder
1919
import (
2020
"fmt"
2121
"strings"
22-
"sync"
2322

2423
"k8s.io/apimachinery/pkg/runtime"
25-
"k8s.io/apimachinery/pkg/runtime/schema"
2624
"k8s.io/client-go/rest"
2725
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2826
"sigs.k8s.io/controller-runtime/pkg/client/config"
@@ -32,7 +30,6 @@ import (
3230
"sigs.k8s.io/controller-runtime/pkg/predicate"
3331
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3432
"sigs.k8s.io/controller-runtime/pkg/source"
35-
"sigs.k8s.io/controller-runtime/pkg/webhook"
3633
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3734
)
3835

@@ -44,14 +41,13 @@ var getGvk = apiutil.GVKForObject
4441

4542
// Builder builds a Controller.
4643
type Builder struct {
47-
apiType runtime.Object
48-
mgr manager.Manager
49-
predicates []predicate.Predicate
50-
managedObjects []runtime.Object
51-
watchRequest []watchRequest
52-
config *rest.Config
53-
ctrl controller.Controller
54-
addServerToManagerOnce sync.Once
44+
apiType runtime.Object
45+
mgr manager.Manager
46+
predicates []predicate.Predicate
47+
managedObjects []runtime.Object
48+
watchRequest []watchRequest
49+
config *rest.Config
50+
ctrl controller.Controller
5551
}
5652

5753
// SimpleController returns a new Builder.
@@ -242,29 +238,22 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
242238
}
243239

244240
func (blder *Builder) doWebhook() error {
245-
if blder.mgr.GetScheme() == nil {
246-
return fmt.Errorf("scheme must be provided")
247-
}
248-
249-
svr := blder.mgr.GetWebhookServer()
250-
if svr == nil {
251-
svr = &webhook.Server{}
252-
}
253-
254241
// Create a webhook for each type
255242
gvk, err := apiutil.GVKForObject(blder.apiType, blder.mgr.GetScheme())
256243
if err != nil {
257244
return err
258245
}
259246

260-
partialPath := generatePath(gvk)
247+
partialPath := strings.Replace(gvk.Group, ".", "-", -1) + "-" +
248+
gvk.Version + "-" + strings.ToLower(gvk.Kind)
249+
261250
defaulter, isDefaulter := blder.apiType.(admission.Defaulter)
262251
if isDefaulter {
263252
mwh := admission.DefaultingWebhookFor(defaulter)
264253
if mwh != nil {
265254
path := "/mutate-" + partialPath
266255
log.Info("registering a mutating webhook", "path", path)
267-
svr.Register(path, mwh)
256+
blder.mgr.GetWebhookServer().Register(path, mwh)
268257
}
269258
}
270259

@@ -274,26 +263,9 @@ func (blder *Builder) doWebhook() error {
274263
if vwh != nil {
275264
path := "/validate-" + partialPath
276265
log.Info("registering a validating webhook", "path", path)
277-
svr.Register(path, vwh)
266+
blder.mgr.GetWebhookServer().Register(path, vwh)
278267
}
279268
}
280269

281-
if isDefaulter || isValidator {
282-
blder.addServerToManagerOnce.Do(func() {
283-
err = blder.mgr.Add(svr)
284-
})
285-
}
286270
return err
287271
}
288-
289-
func generatePath(gvk schema.GroupVersionKind) string {
290-
var pathItems []string
291-
splittedGroup := strings.Split(gvk.Group, ".")
292-
if len(splittedGroup) > 0 && len(splittedGroup[0]) > 0 {
293-
pathItems = append(pathItems, splittedGroup[0])
294-
}
295-
if len(gvk.Kind) > 0 {
296-
pathItems = append(pathItems, strings.ToLower(gvk.Kind))
297-
}
298-
return strings.Join(pathItems, "-")
299-
}

pkg/manager/internal.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ func (cm *controllerManager) GetRESTMapper() meta.RESTMapper {
171171
}
172172

173173
func (cm *controllerManager) GetWebhookServer() *webhook.Server {
174+
if cm.webhookServer == nil {
175+
cm.webhookServer = &webhook.Server{}
176+
if err := cm.Add(cm.webhookServer); err != nil {
177+
panic("unable to add webhookServer to the controller manager")
178+
}
179+
}
174180
return cm.webhookServer
175181
}
176182

pkg/webhook/admission/decode.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,7 @@ func NewDecoder(scheme *runtime.Scheme) (*Decoder, error) {
3737
// Decode decodes the inlined object in the AdmissionRequest into the passed-in runtime.Object.
3838
// If you want decode the OldObject in the AdmissionRequest, use DecodeRaw.
3939
func (d *Decoder) Decode(req Request, into runtime.Object) error {
40-
// NB(directxman12): there's a bug/weird interaction between decoders and
41-
// the API server where the API server doesn't send a GVK on the embedded
42-
// objects, which means the unstructured decoder refuses to decode. It
43-
// also means we can't pass the unstructured directly in, since it'll try
44-
// and call unstructured's special Unmarshal implementation, which calls
45-
// back into that same decoder :-/
46-
// See kubernetes/kubernetes#74373.
47-
if unstructuredInto, isUnstructured := into.(*unstructured.Unstructured); isUnstructured {
48-
// unmarshal into unstructured's underlying object to avoid calling the decoder
49-
if err := json.Unmarshal(req.Object.Raw, &unstructuredInto.Object); err != nil {
50-
return err
51-
}
52-
53-
return nil
54-
}
55-
56-
deserializer := d.codecs.UniversalDeserializer()
57-
return runtime.DecodeInto(deserializer, req.AdmissionRequest.Object.Raw, into)
40+
return d.DecodeRaw(req.Object, into)
5841
}
5942

6043
// DecodeRaw decodes a RawExtension object into the passed-in runtime.Object.

pkg/webhook/admission/defaulter.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"net/http"
2323

2424
"k8s.io/apimachinery/pkg/runtime"
25-
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
2625
)
2726

2827
// Defaulter defines functions for setting defaults on resources
@@ -40,18 +39,9 @@ func DefaultingWebhookFor(defaulter Defaulter) *Webhook {
4039

4140
type mutatingHandler struct {
4241
defaulter Defaulter
43-
scheme *runtime.Scheme
4442
decoder *Decoder
4543
}
4644

47-
var _ inject.Scheme = &mutatingHandler{}
48-
49-
// InjectScheme injects the scheme into a mutatingHandler.
50-
func (h *mutatingHandler) InjectScheme(s *runtime.Scheme) error {
51-
h.scheme = s
52-
return nil
53-
}
54-
5545
var _ DecoderInjector = &mutatingHandler{}
5646

5747
// InjectDecoder injects the decoder into a mutatingHandler.
@@ -63,7 +53,7 @@ func (h *mutatingHandler) InjectDecoder(d *Decoder) error {
6353
// Handle handles admission requests.
6454
func (h *mutatingHandler) Handle(ctx context.Context, req Request) Response {
6555
if h.defaulter == nil {
66-
return Allowed("")
56+
panic("defaulter should never be nil")
6757
}
6858

6959
// Get the object in the request

pkg/webhook/admission/validator.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"k8s.io/api/admission/v1beta1"
2424
"k8s.io/apimachinery/pkg/runtime"
25-
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
2625
)
2726

2827
// Validator defines functions for validating an operation
@@ -41,18 +40,9 @@ func ValidatingWebhookFor(validator Validator) *Webhook {
4140

4241
type validatingHandler struct {
4342
validator Validator
44-
scheme *runtime.Scheme
4543
decoder *Decoder
4644
}
4745

48-
var _ inject.Scheme = &validatingHandler{}
49-
50-
// InjectScheme injects the scheme into a validatingHandler
51-
func (h *validatingHandler) InjectScheme(s *runtime.Scheme) error {
52-
h.scheme = s
53-
return nil
54-
}
55-
5646
var _ DecoderInjector = &validatingHandler{}
5747

5848
// InjectDecoder injects the decoder into a validatingHandler.
@@ -64,7 +54,7 @@ func (h *validatingHandler) InjectDecoder(d *Decoder) error {
6454
// Handle handles admission requests.
6555
func (h *validatingHandler) Handle(ctx context.Context, req Request) Response {
6656
if h.validator == nil {
67-
return Allowed("")
57+
panic("validator should never be nil")
6858
}
6959

7060
// Get the object in the request

pkg/webhook/alias.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ import (
2323

2424
// define some aliases for common bits of the webhook functionality
2525

26+
// Defaulter defines functions for setting defaults on resources
27+
type Defaulter = admission.Defaulter
28+
29+
// Validator defines functions for validating an operation
30+
type Validator = admission.Validator
31+
2632
// AdmissionRequest defines the input for an admission handler.
2733
// It contains information to identify the object in
2834
// question (group, version, kind, resource, subresource,

pkg/webhook/server.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"sync"
2828
"time"
2929

30-
"k8s.io/apimachinery/pkg/runtime"
3130
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3231
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/metrics"
3332
)
@@ -58,9 +57,6 @@ type Server struct {
5857
// the user is responsible to mount the secret to the this location for the server to consume.
5958
CertDir string
6059

61-
// Scheme knows the mapping between go struct types and GVKs.
62-
Scheme *runtime.Scheme
63-
6460
// TODO(directxman12): should we make the mux configurable?
6561

6662
// webhookMux is the multiplexer that handles different webhooks.

0 commit comments

Comments
 (0)