Skip to content

Commit 0d4b81e

Browse files
committed
⚠️ Remove pkg/inject
This changeset brings in a few more breaking changes to remove the rest of the dependency injection code. The webhook code is now a bit simplified and more explicit in accepting a *runtime.Scheme. The logger is now also not configured in the Standalone mode. We should revisit in a future PR the webhook standalone configuration, and potentially always require a manager. From a quick look in GitHub projects, it seems most folks don't actually use these standalone webhooks, which makes sense because the Manager makes things easier, although there might be private code that's using them. The config loader was using InjectScheme before, although it really didn't need to. Signed-off-by: Vince Prignano <[email protected]>
1 parent d4a1690 commit 0d4b81e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+85
-1133
lines changed

.golangci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ issues:
8080
- Subprocess launch(ed with variable|ing should be audited)
8181
- (G204|G104|G307)
8282
- "ST1000: at least one file in a package should have a package comment"
83-
- "SA1019: \"sigs.k8s.io/controller-runtime/pkg/runtime/inject\""
84-
- "SA1019: inject.*"
8583
exclude-rules:
8684
- linters:
8785
- gosec

alias.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ var (
139139
// The logger, when used with controllers, can be expected to contain basic information about the object
140140
// that's being reconciled like:
141141
// - `reconciler group` and `reconciler kind` coming from the For(...) object passed in when building a controller.
142-
// - `name` and `namespace` injected from the reconciliation request.
142+
// - `name` and `namespace` from the reconciliation request.
143143
//
144144
// This is meant to be used with the context supplied in a struct that satisfies the Reconciler interface.
145145
LoggerFrom = log.FromContext

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ limitations under the License.
5252
//
5353
// Every controller and webhook is ultimately run by a Manager (pkg/manager). A
5454
// manager is responsible for running controllers and webhooks, and setting up
55-
// common dependencies (pkg/runtime/inject), like shared caches and clients, as
55+
// common dependencies, like shared caches and clients, as
5656
// well as managing leader election (pkg/leaderelection). Managers are
5757
// generally configured to gracefully shut down controllers on pod termination
5858
// by wiring up a signal handler (pkg/manager/signals).

example_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
// ReplicaSetReconciler.
3838
//
3939
// * Start the application.
40-
// TODO(pwittrock): Update this example when we have better dependency injection support.
4140
func Example() {
4241
var log = ctrl.Log.WithName("builder-examples")
4342

@@ -75,7 +74,6 @@ func Example() {
7574
// ReplicaSetReconciler.
7675
//
7776
// * Start the application.
78-
// TODO(pwittrock): Update this example when we have better dependency injection support.
7977
func Example_updateLeaderElectionDurations() {
8078
var log = ctrl.Log.WithName("builder-examples")
8179
leaseDuration := 100 * time.Second

examples/builtins/mutatingwebhook.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,3 @@ func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admiss
6060

6161
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod)
6262
}
63-
64-
// podAnnotator implements admission.DecoderInjector.
65-
// A decoder will be automatically injected.
66-
67-
// InjectDecoder injects the decoder.
68-
func (a *podAnnotator) InjectDecoder(d *admission.Decoder) error {
69-
a.decoder = d
70-
return nil
71-
}

examples/builtins/validatingwebhook.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,3 @@ func (v *podValidator) Handle(ctx context.Context, req admission.Request) admiss
6060

6161
return admission.Allowed("")
6262
}
63-
64-
// podValidator implements admission.DecoderInjector.
65-
// A decoder will be automatically injected.
66-
67-
// InjectDecoder injects the decoder.
68-
func (v *podValidator) InjectDecoder(d *admission.Decoder) error {
69-
v.decoder = d
70-
return nil
71-
}

pkg/builder/example_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,3 @@ func (a *ReplicaSetReconciler) Reconcile(ctx context.Context, req reconcile.Requ
155155

156156
return reconcile.Result{}, nil
157157
}
158-
159-
func (a *ReplicaSetReconciler) InjectClient(c client.Client) error {
160-
a.Client = c
161-
return nil
162-
}

pkg/builder/webhook.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() {
164164

165165
func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook {
166166
if defaulter := blder.withDefaulter; defaulter != nil {
167-
return admission.WithCustomDefaulter(blder.apiType, defaulter).WithRecoverPanic(blder.recoverPanic)
167+
return admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter).WithRecoverPanic(blder.recoverPanic)
168168
}
169169
if defaulter, ok := blder.apiType.(admission.Defaulter); ok {
170-
return admission.DefaultingWebhookFor(defaulter).WithRecoverPanic(blder.recoverPanic)
170+
return admission.DefaultingWebhookFor(blder.mgr.GetScheme(), defaulter).WithRecoverPanic(blder.recoverPanic)
171171
}
172172
log.Info(
173173
"skip registering a mutating webhook, object does not implement admission.Defaulter or WithDefaulter wasn't called",
@@ -194,10 +194,10 @@ func (blder *WebhookBuilder) registerValidatingWebhook() {
194194

195195
func (blder *WebhookBuilder) getValidatingWebhook() *admission.Webhook {
196196
if validator := blder.withValidator; validator != nil {
197-
return admission.WithCustomValidator(blder.apiType, validator).WithRecoverPanic(blder.recoverPanic)
197+
return admission.WithCustomValidator(blder.mgr.GetScheme(), blder.apiType, validator).WithRecoverPanic(blder.recoverPanic)
198198
}
199199
if validator, ok := blder.apiType.(admission.Validator); ok {
200-
return admission.ValidatingWebhookFor(validator).WithRecoverPanic(blder.recoverPanic)
200+
return admission.ValidatingWebhookFor(blder.mgr.GetScheme(), validator).WithRecoverPanic(blder.recoverPanic)
201201
}
202202
log.Info(
203203
"skip registering a validating webhook, object does not implement admission.Validator or WithValidator wasn't called",

pkg/builder/webhook_test.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ func runTests(admissionReviewVersion string) {
115115

116116
ctx, cancel := context.WithCancel(context.Background())
117117
cancel()
118-
// TODO: we may want to improve it to make it be able to inject dependencies,
119-
// but not always try to load certs and return not found error.
120118
err = svr.Start(ctx)
121119
if err != nil && !os.IsNotExist(err) {
122120
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -191,8 +189,6 @@ func runTests(admissionReviewVersion string) {
191189

192190
ctx, cancel := context.WithCancel(context.Background())
193191
cancel()
194-
// TODO: we may want to improve it to make it be able to inject dependencies,
195-
// but not always try to load certs and return not found error.
196192
err = svr.Start(ctx)
197193
if err != nil && !os.IsNotExist(err) {
198194
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -208,7 +204,7 @@ func runTests(admissionReviewVersion string) {
208204
By("sanity checking the response contains reasonable fields")
209205
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":false`))
210206
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":500`))
211-
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"message":"panic: injected panic [recovered]`))
207+
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"message":"panic: fake panic test [recovered]`))
212208
})
213209

214210
It("should scaffold a defaulting webhook with a custom defaulter", func() {
@@ -260,8 +256,6 @@ func runTests(admissionReviewVersion string) {
260256

261257
ctx, cancel := context.WithCancel(context.Background())
262258
cancel()
263-
// TODO: we may want to improve it to make it be able to inject dependencies,
264-
// but not always try to load certs and return not found error.
265259
err = svr.Start(ctx)
266260
if err != nil && !os.IsNotExist(err) {
267261
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -337,8 +331,6 @@ func runTests(admissionReviewVersion string) {
337331

338332
ctx, cancel := context.WithCancel(context.Background())
339333
cancel()
340-
// TODO: we may want to improve it to make it be able to inject dependencies,
341-
// but not always try to load certs and return not found error.
342334
err = svr.Start(ctx)
343335
if err != nil && !os.IsNotExist(err) {
344336
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -411,8 +403,6 @@ func runTests(admissionReviewVersion string) {
411403

412404
ctx, cancel := context.WithCancel(context.Background())
413405
cancel()
414-
// TODO: we may want to improve it to make it be able to inject dependencies,
415-
// but not always try to load certs and return not found error.
416406
err = svr.Start(ctx)
417407
if err != nil && !os.IsNotExist(err) {
418408
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -430,7 +420,7 @@ func runTests(admissionReviewVersion string) {
430420
By("sanity checking the response contains reasonable field")
431421
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":false`))
432422
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":500`))
433-
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"message":"panic: injected panic [recovered]`))
423+
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"message":"panic: fake panic test [recovered]`))
434424
})
435425

436426
It("should scaffold a validating webhook with a custom validator", func() {
@@ -463,12 +453,12 @@ func runTests(admissionReviewVersion string) {
463453
"kind":{
464454
"group":"foo.test.org",
465455
"version":"v1",
466-
"kind":"TestDefaulter"
456+
"kind":"TestValidator"
467457
},
468458
"resource":{
469459
"group":"foo.test.org",
470460
"version":"v1",
471-
"resource":"testdefaulter"
461+
"resource":"testvalidator"
472462
},
473463
"namespace":"default",
474464
"name":"foo",
@@ -484,8 +474,6 @@ func runTests(admissionReviewVersion string) {
484474

485475
ctx, cancel := context.WithCancel(context.Background())
486476
cancel()
487-
// TODO: we may want to improve it to make it be able to inject dependencies,
488-
// but not always try to load certs and return not found error.
489477
err = svr.Start(ctx)
490478
if err != nil && !os.IsNotExist(err) {
491479
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -511,7 +499,7 @@ func runTests(admissionReviewVersion string) {
511499
By("sanity checking the response contains reasonable field")
512500
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"allowed":false`))
513501
ExpectWithOffset(1, w.Body).To(ContainSubstring(`"code":403`))
514-
EventuallyWithOffset(1, logBuffer).Should(gbytes.Say(`"msg":"Validating object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testdefaulter"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"`))
502+
EventuallyWithOffset(1, logBuffer).Should(gbytes.Say(`"msg":"Validating object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testvalidator"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"`))
515503
})
516504

517505
It("should scaffold defaulting and validating webhooks if the type implements both Defaulter and Validator interfaces", func() {
@@ -558,8 +546,6 @@ func runTests(admissionReviewVersion string) {
558546

559547
ctx, cancel := context.WithCancel(context.Background())
560548
cancel()
561-
// TODO: we may want to improve it to make it be able to inject dependencies,
562-
// but not always try to load certs and return not found error.
563549
err = svr.Start(ctx)
564550
if err != nil && !os.IsNotExist(err) {
565551
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -636,8 +622,6 @@ func runTests(admissionReviewVersion string) {
636622
}`)
637623

638624
cancel()
639-
// TODO: we may want to improve it to make it be able to inject dependencies,
640-
// but not always try to load certs and return not found error.
641625
err = svr.Start(ctx)
642626
if err != nil && !os.IsNotExist(err) {
643627
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -724,7 +708,7 @@ func (*TestDefaulterList) DeepCopyObject() runtime.Object { return nil }
724708

725709
func (d *TestDefaulter) Default() {
726710
if d.Panic {
727-
panic("injected panic")
711+
panic("fake panic test")
728712
}
729713
if d.Replica < 2 {
730714
d.Replica = 2
@@ -767,7 +751,7 @@ var _ admission.Validator = &TestValidator{}
767751

768752
func (v *TestValidator) ValidateCreate() error {
769753
if v.Panic {
770-
panic("injected panic")
754+
panic("fake panic test")
771755
}
772756
if v.Replica < 0 {
773757
return errors.New("number of replica should be greater than or equal to 0")
@@ -777,7 +761,7 @@ func (v *TestValidator) ValidateCreate() error {
777761

778762
func (v *TestValidator) ValidateUpdate(old runtime.Object) error {
779763
if v.Panic {
780-
panic("injected panic")
764+
panic("fake panic test")
781765
}
782766
if v.Replica < 0 {
783767
return errors.New("number of replica should be greater than or equal to 0")
@@ -792,7 +776,7 @@ func (v *TestValidator) ValidateUpdate(old runtime.Object) error {
792776

793777
func (v *TestValidator) ValidateDelete() error {
794778
if v.Panic {
795-
panic("injected panic")
779+
panic("fake panic test")
796780
}
797781
if v.Replica > 0 {
798782
return errors.New("number of replica should be less than or equal to 0 to delete")

pkg/cluster/internal.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,8 @@ type cluster struct {
3434
// config is the rest.config used to talk to the apiserver. Required.
3535
config *rest.Config
3636

37-
// scheme is the scheme injected into Controllers, EventHandlers, Sources and Predicates. Defaults
38-
// to scheme.scheme.
3937
scheme *runtime.Scheme
40-
41-
cache cache.Cache
42-
43-
// TODO(directxman12): Provide an escape hatch to get individual indexers
44-
// client is the client injected into Controllers (and EventHandlers, Sources and Predicates).
38+
cache cache.Cache
4539
client client.Client
4640

4741
// apiReader is the reader that will make requests to the api server and not the cache.

pkg/config/config.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ func (d *DeferredFileLoader) OfKind(obj ControllerManagerConfiguration) *Deferre
8383
return d
8484
}
8585

86-
// InjectScheme will configure the scheme to be used for decoding the file.
87-
func (d *DeferredFileLoader) InjectScheme(scheme *runtime.Scheme) error {
88-
d.scheme = scheme
89-
return nil
90-
}
91-
9286
// loadFile is used from the mutex.Once to load the file.
9387
func (d *DeferredFileLoader) loadFile() {
9488
if d.scheme == nil {

pkg/config/example_test.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,10 @@ func ExampleFile() {
4444
}
4545

4646
// This example will load the file from a custom path.
47-
func ExampleDeferredFileLoader_atPath() {
47+
func ExampleFile_atPath() {
4848
loader := config.File().AtPath("/var/run/controller-runtime/config.yaml")
4949
if _, err := loader.Complete(); err != nil {
5050
fmt.Println("failed to load config")
5151
os.Exit(1)
5252
}
5353
}
54-
55-
// This example sets up loader with a custom scheme.
56-
func ExampleDeferredFileLoader_injectScheme() {
57-
loader := config.File()
58-
err := loader.InjectScheme(scheme)
59-
if err != nil {
60-
fmt.Println("failed to inject scheme")
61-
os.Exit(1)
62-
}
63-
64-
_, err = loader.Complete()
65-
if err != nil {
66-
fmt.Println("failed to load config")
67-
os.Exit(1)
68-
}
69-
}
70-
71-
// This example sets up the loader with a custom scheme and custom type.
72-
func ExampleDeferredFileLoader_ofKind() {
73-
loader := config.File().OfKind(&v1alpha1.CustomControllerManagerConfiguration{})
74-
err := loader.InjectScheme(scheme)
75-
if err != nil {
76-
fmt.Println("failed to inject scheme")
77-
os.Exit(1)
78-
}
79-
_, err = loader.Complete()
80-
if err != nil {
81-
fmt.Println("failed to load config")
82-
os.Exit(1)
83-
}
84-
}

pkg/internal/controller/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ var _ = Describe("controller", func() {
212212
It("should process events from source.Channel", func() {
213213
// channel to be closed when event is processed
214214
processed := make(chan struct{})
215-
// source channel to be injected
215+
// source channel
216216
ch := make(chan event.GenericEvent, 1)
217217

218218
ctx, cancel := context.WithCancel(context.TODO())

pkg/manager/internal.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import (
4646
"sigs.k8s.io/controller-runtime/pkg/internal/httpserver"
4747
intrec "sigs.k8s.io/controller-runtime/pkg/internal/recorder"
4848
"sigs.k8s.io/controller-runtime/pkg/metrics"
49-
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
5049
"sigs.k8s.io/controller-runtime/pkg/webhook"
5150
)
5251

@@ -191,28 +190,9 @@ func (cm *controllerManager) Add(r Runnable) error {
191190
}
192191

193192
func (cm *controllerManager) add(r Runnable) error {
194-
// Set dependencies on the object
195-
if err := cm.setFields(r); err != nil {
196-
return err
197-
}
198193
return cm.runnables.Add(r)
199194
}
200195

201-
// Deprecated: use the equivalent Options field to set a field. This method will be removed in v0.10.
202-
func (cm *controllerManager) setFields(i interface{}) error {
203-
if _, err := inject.SchemeInto(cm.cluster.GetScheme(), i); err != nil {
204-
return err
205-
}
206-
if _, err := inject.InjectorInto(cm.setFields, i); err != nil {
207-
return err
208-
}
209-
if _, err := inject.LoggerInto(cm.logger, i); err != nil {
210-
return err
211-
}
212-
213-
return nil
214-
}
215-
216196
// AddMetricsExtraHandler adds extra handler served on path to the http server that serves metrics.
217197
func (cm *controllerManager) AddMetricsExtraHandler(path string, handler http.Handler) error {
218198
cm.Lock()

0 commit comments

Comments
 (0)