Skip to content

Commit 927c09e

Browse files
committed
⚠️ Remove pkg/inject
Signed-off-by: Vince Prignano <[email protected]>
1 parent d4a1690 commit 927c09e

Some content is hidden

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

48 files changed

+152
-1090
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/main.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
appsv1 "k8s.io/api/apps/v1"
2323
corev1 "k8s.io/api/core/v1"
2424
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
25+
"sigs.k8s.io/controller-runtime/pkg/builder"
2526
"sigs.k8s.io/controller-runtime/pkg/client/config"
2627
"sigs.k8s.io/controller-runtime/pkg/controller"
2728
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -30,7 +31,6 @@ import (
3031
"sigs.k8s.io/controller-runtime/pkg/manager"
3132
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
3233
"sigs.k8s.io/controller-runtime/pkg/source"
33-
"sigs.k8s.io/controller-runtime/pkg/webhook"
3434
)
3535

3636
func init() {
@@ -71,13 +71,14 @@ func main() {
7171
os.Exit(1)
7272
}
7373

74-
// Setup webhooks
75-
entryLog.Info("setting up webhook server")
76-
hookServer := mgr.GetWebhookServer()
77-
78-
entryLog.Info("registering webhooks to the webhook server")
79-
hookServer.Register("/mutate-v1-pod", &webhook.Admission{Handler: &podAnnotator{Client: mgr.GetClient()}})
80-
hookServer.Register("/validate-v1-pod", &webhook.Admission{Handler: &podValidator{Client: mgr.GetClient()}})
74+
if err := builder.WebhookManagedBy(mgr).
75+
For(&corev1.Pod{}).
76+
WithDefaulter(&podAnnotator{}).
77+
WithValidator(&podValidator{}).
78+
Complete(); err != nil {
79+
entryLog.Error(err, "unable to create webhook", "webhook", "Pod")
80+
os.Exit(1)
81+
}
8182

8283
entryLog.Info("starting manager")
8384
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {

examples/builtins/mutatingwebhook.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,31 @@ package main
1818

1919
import (
2020
"context"
21-
"encoding/json"
22-
"net/http"
21+
"fmt"
2322

2423
corev1 "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/runtime"
2525

26-
"sigs.k8s.io/controller-runtime/pkg/client"
2726
logf "sigs.k8s.io/controller-runtime/pkg/log"
28-
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2927
)
3028

3129
// +kubebuilder:webhook:path=/mutate-v1-pod,mutating=true,failurePolicy=fail,groups="",resources=pods,verbs=create;update,versions=v1,name=mpod.kb.io
3230

3331
// podAnnotator annotates Pods
34-
type podAnnotator struct {
35-
Client client.Client
36-
decoder *admission.Decoder
37-
}
32+
type podAnnotator struct{}
3833

39-
// podAnnotator adds an annotation to every incoming pods.
40-
func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admission.Response {
41-
// set up a convenient log object so we don't have to type request over and over again
34+
func (a *podAnnotator) Default(ctx context.Context, obj runtime.Object) error {
4235
log := logf.FromContext(ctx)
43-
44-
pod := &corev1.Pod{}
45-
err := a.decoder.Decode(req, pod)
46-
if err != nil {
47-
return admission.Errored(http.StatusBadRequest, err)
36+
pod, ok := obj.(*corev1.Pod)
37+
if !ok {
38+
return fmt.Errorf("expected a Pod but got a %T", obj)
4839
}
4940

5041
if pod.Annotations == nil {
5142
pod.Annotations = map[string]string{}
5243
}
5344
pod.Annotations["example-mutating-admission-webhook"] = "foo"
45+
log.Info("Annotated Pod")
5446

55-
marshaledPod, err := json.Marshal(pod)
56-
if err != nil {
57-
return admission.Errored(http.StatusInternalServerError, err)
58-
}
59-
log.Info("Annotating Pod")
60-
61-
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod)
62-
}
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
7047
return nil
7148
}

examples/builtins/validatingwebhook.go

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,47 @@ package main
1919
import (
2020
"context"
2121
"fmt"
22-
"net/http"
2322

2423
corev1 "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/runtime"
2525

26-
"sigs.k8s.io/controller-runtime/pkg/client"
2726
logf "sigs.k8s.io/controller-runtime/pkg/log"
28-
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2927
)
3028

3129
// +kubebuilder:webhook:path=/validate-v1-pod,mutating=false,failurePolicy=fail,groups="",resources=pods,verbs=create;update,versions=v1,name=vpod.kb.io
3230

3331
// podValidator validates Pods
34-
type podValidator struct {
35-
Client client.Client
36-
decoder *admission.Decoder
37-
}
32+
type podValidator struct{}
3833

39-
// podValidator admits a pod if a specific annotation exists.
40-
func (v *podValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
41-
// set up a convenient log object so we don't have to type request over and over again
34+
// validate admits a pod if a specific annotation exists.
35+
func (v *podValidator) validate(ctx context.Context, obj runtime.Object) error {
4236
log := logf.FromContext(ctx)
43-
44-
pod := &corev1.Pod{}
45-
err := v.decoder.Decode(req, pod)
46-
if err != nil {
47-
return admission.Errored(http.StatusBadRequest, err)
37+
pod, ok := obj.(*corev1.Pod)
38+
if !ok {
39+
return fmt.Errorf("expected a Pod but got a %T", obj)
4840
}
4941

5042
log.Info("Validating Pod")
51-
5243
key := "example-mutating-admission-webhook"
5344
anno, found := pod.Annotations[key]
5445
if !found {
55-
return admission.Denied(fmt.Sprintf("missing annotation %s", key))
46+
return fmt.Errorf("missing annotation %s", key)
5647
}
5748
if anno != "foo" {
58-
return admission.Denied(fmt.Sprintf("annotation %s did not have value %q", key, "foo"))
49+
return fmt.Errorf("annotation %s did not have value %q", key, "foo")
5950
}
6051

61-
return admission.Allowed("")
52+
return nil
53+
}
54+
55+
func (v *podValidator) ValidateCreate(ctx context.Context, obj runtime.Object) error {
56+
return v.validate(ctx, obj)
6257
}
6358

64-
// podValidator implements admission.DecoderInjector.
65-
// A decoder will be automatically injected.
59+
func (v *podValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) error {
60+
return v.validate(ctx, newObj)
61+
}
6662

67-
// InjectDecoder injects the decoder.
68-
func (v *podValidator) InjectDecoder(d *admission.Decoder) error {
69-
v.decoder = d
70-
return nil
63+
func (v *podValidator) ValidateDelete(ctx context.Context, obj runtime.Object) error {
64+
return v.validate(ctx, obj)
7165
}

pkg/builder/example_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ func ExampleBuilder() {
107107
ControllerManagedBy(mgr). // Create the ControllerManagedBy
108108
For(&appsv1.ReplicaSet{}). // ReplicaSet is the Application API
109109
Owns(&corev1.Pod{}). // ReplicaSet owns Pods created by it
110-
Complete(&ReplicaSetReconciler{})
110+
Complete(&ReplicaSetReconciler{
111+
Client: mgr.GetClient(),
112+
})
111113
if err != nil {
112114
log.Error(err, "could not create controller")
113115
os.Exit(1)
@@ -155,8 +157,3 @@ func (a *ReplicaSetReconciler) Reconcile(ctx context.Context, req reconcile.Requ
155157

156158
return reconcile.Result{}, nil
157159
}
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",

0 commit comments

Comments
 (0)