Skip to content

Commit 1bf3b8e

Browse files
committed
Minor improvements to godoc, code style in builder pkg
1 parent b1d6919 commit 1bf3b8e

File tree

3 files changed

+50
-50
lines changed

3 files changed

+50
-50
lines changed

pkg/builder/controller.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ import (
4141
var newController = controller.New
4242
var getGvk = apiutil.GVKForObject
4343

44-
// project represents other forms that the we can use to
44+
// project represents other forms that we can use to
4545
// send/receive a given resource (metadata-only, unstructured, etc).
4646
type objectProjection int
4747

4848
const (
4949
// projectAsNormal doesn't change the object from the form given.
5050
projectAsNormal objectProjection = iota
51-
// projectAsMetadata turns this into an metadata-only watch.
51+
// projectAsMetadata turns this into a metadata-only watch.
5252
projectAsMetadata
5353
)
5454

@@ -69,7 +69,7 @@ func ControllerManagedBy(m manager.Manager) *Builder {
6969
return &Builder{mgr: m}
7070
}
7171

72-
// ForInput represents the information set by For method.
72+
// ForInput represents the information set by the For method.
7373
type ForInput struct {
7474
object client.Object
7575
predicates []predicate.Predicate
@@ -124,7 +124,7 @@ func (blder *Builder) Owns(object client.Object, opts ...OwnsOption) *Builder {
124124
// WatchesInput represents the information set by Watches method.
125125
type WatchesInput struct {
126126
src source.Source
127-
eventhandler handler.EventHandler
127+
eventHandler handler.EventHandler
128128
predicates []predicate.Predicate
129129
objectProjection objectProjection
130130
}
@@ -133,16 +133,16 @@ type WatchesInput struct {
133133
// update events by *reconciling the object* with the given EventHandler.
134134
//
135135
// This is the equivalent of calling
136-
// WatchesRawSource(source.Kind(scheme, object), eventhandler, opts...).
137-
func (blder *Builder) Watches(object client.Object, eventhandler handler.EventHandler, opts ...WatchesOption) *Builder {
136+
// WatchesRawSource(source.Kind(cache, object), eventHandler, opts...).
137+
func (blder *Builder) Watches(object client.Object, eventHandler handler.EventHandler, opts ...WatchesOption) *Builder {
138138
src := source.Kind(blder.mgr.GetCache(), object)
139-
return blder.WatchesRawSource(src, eventhandler, opts...)
139+
return blder.WatchesRawSource(src, eventHandler, opts...)
140140
}
141141

142142
// WatchesMetadata is the same as Watches, but forces the internal cache to only watch PartialObjectMetadata.
143143
//
144144
// This is useful when watching lots of objects, really big objects, or objects for which you only know
145-
// the GVK, but not the structure. You'll need to pass metav1.PartialObjectMetadata to the client
145+
// the GVK, but not the structure. You'll need to pass metav1.PartialObjectMetadata to the client
146146
// when fetching objects in your reconciler, otherwise you'll end up with a duplicate structured or unstructured cache.
147147
//
148148
// When watching a resource with metadata only, for example the v1.Pod, you should not Get and List using the v1.Pod type.
@@ -166,18 +166,18 @@ func (blder *Builder) Watches(object client.Object, eventhandler handler.EventHa
166166
// In the first case, controller-runtime will create another cache for the
167167
// concrete type on top of the metadata cache; this increases memory
168168
// consumption and leads to race conditions as caches are not in sync.
169-
func (blder *Builder) WatchesMetadata(object client.Object, eventhandler handler.EventHandler, opts ...WatchesOption) *Builder {
169+
func (blder *Builder) WatchesMetadata(object client.Object, eventHandler handler.EventHandler, opts ...WatchesOption) *Builder {
170170
opts = append(opts, OnlyMetadata)
171-
return blder.Watches(object, eventhandler, opts...)
171+
return blder.Watches(object, eventHandler, opts...)
172172
}
173173

174174
// WatchesRawSource exposes the lower-level ControllerManagedBy Watches functions through the builder.
175175
// Specified predicates are registered only for given source.
176176
//
177177
// STOP! Consider using For(...), Owns(...), Watches(...), WatchesMetadata(...) instead.
178-
// This method is only exposed for more advanced use cases, most users should use higher level functions.
179-
func (blder *Builder) WatchesRawSource(src source.Source, eventhandler handler.EventHandler, opts ...WatchesOption) *Builder {
180-
input := WatchesInput{src: src, eventhandler: eventhandler}
178+
// This method is only exposed for more advanced use cases, most users should use one of the higher level functions.
179+
func (blder *Builder) WatchesRawSource(src source.Source, eventHandler handler.EventHandler, opts ...WatchesOption) *Builder {
180+
input := WatchesInput{src: src, eventHandler: eventHandler}
181181
for _, opt := range opts {
182182
opt.ApplyToWatches(&input)
183183
}
@@ -187,15 +187,15 @@ func (blder *Builder) WatchesRawSource(src source.Source, eventhandler handler.E
187187
}
188188

189189
// WithEventFilter sets the event filters, to filter which create/update/delete/generic events eventually
190-
// trigger reconciliations. For example, filtering on whether the resource version has changed.
190+
// trigger reconciliations. For example, filtering on whether the resource version has changed.
191191
// Given predicate is added for all watched objects.
192192
// Defaults to the empty list.
193193
func (blder *Builder) WithEventFilter(p predicate.Predicate) *Builder {
194194
blder.globalPredicates = append(blder.globalPredicates, p)
195195
return blder
196196
}
197197

198-
// WithOptions overrides the controller options use in doController. Defaults to empty.
198+
// WithOptions overrides the controller options used in doController. Defaults to empty.
199199
func (blder *Builder) WithOptions(options controller.Options) *Builder {
200200
blder.ctrlOptions = options
201201
return blder
@@ -207,7 +207,7 @@ func (blder *Builder) WithLogConstructor(logConstructor func(*reconcile.Request)
207207
return blder
208208
}
209209

210-
// Named sets the name of the controller to the given name. The name shows up
210+
// Named sets the name of the controller to the given name. The name shows up
211211
// in metrics, among other things, and thus should be a prometheus compatible name
212212
// (underscores and alphanumeric characters only).
213213
//
@@ -299,8 +299,7 @@ func (blder *Builder) doWatch() error {
299299
blder.forInput.object,
300300
opts...,
301301
)
302-
allPredicates := append([]predicate.Predicate(nil), blder.globalPredicates...)
303-
allPredicates = append(allPredicates, own.predicates...)
302+
allPredicates := append(blder.globalPredicates, own.predicates...)
304303
if err := blder.ctrl.Watch(src, hdler, allPredicates...); err != nil {
305304
return err
306305
}
@@ -311,19 +310,16 @@ func (blder *Builder) doWatch() error {
311310
return errors.New("there are no watches configured, controller will never get triggered. Use For(), Owns() or Watches() to set them up")
312311
}
313312
for _, w := range blder.watchesInput {
314-
allPredicates := append([]predicate.Predicate(nil), blder.globalPredicates...)
315-
allPredicates = append(allPredicates, w.predicates...)
316-
317313
// If the source of this watch is of type Kind, project it.
318-
if srckind, ok := w.src.(*internalsource.Kind); ok {
319-
typeForSrc, err := blder.project(srckind.Type, w.objectProjection)
314+
if srcKind, ok := w.src.(*internalsource.Kind); ok {
315+
typeForSrc, err := blder.project(srcKind.Type, w.objectProjection)
320316
if err != nil {
321317
return err
322318
}
323-
srckind.Type = typeForSrc
319+
srcKind.Type = typeForSrc
324320
}
325-
326-
if err := blder.ctrl.Watch(w.src, w.eventhandler, allPredicates...); err != nil {
321+
allPredicates := append(blder.globalPredicates, w.predicates...)
322+
if err := blder.ctrl.Watch(w.src, w.eventHandler, allPredicates...); err != nil {
327323
return err
328324
}
329325
}
@@ -344,12 +340,15 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
344340
globalOpts := blder.mgr.GetControllerOptions()
345341

346342
ctrlOptions := blder.ctrlOptions
343+
if ctrlOptions.Reconciler != nil && r != nil {
344+
return errors.New("reconciler was set via WithOptions() and via Build() or Complete()")
345+
}
347346
if ctrlOptions.Reconciler == nil {
348347
ctrlOptions.Reconciler = r
349348
}
350349

351350
// Retrieve the GVK from the object we're reconciling
352-
// to prepopulate logger information, and to optionally generate a default name.
351+
// to pre-populate logger information, and to optionally generate a default name.
353352
var gvk schema.GroupVersionKind
354353
hasGVK := blder.forInput.object != nil
355354
if hasGVK {

pkg/builder/options.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type ForOption interface {
2828
ApplyToFor(*ForInput)
2929
}
3030

31-
// OwnsOption is some configuration that modifies options for a owns request.
31+
// OwnsOption is some configuration that modifies options for an owns request.
3232
type OwnsOption interface {
3333
// ApplyToOwns applies this configuration to the given owns input.
3434
ApplyToOwns(*OwnsInput)
@@ -79,8 +79,8 @@ var _ WatchesOption = &Predicates{}
7979

8080
// {{{ For & Owns Dual-Type options
8181

82-
// asProjection configures the projection (currently only metadata) on the input.
83-
// Currently only metadata is supported. We might want to expand
82+
// projectAs configures the projection on the input.
83+
// Currently only OnlyMetadata is supported. We might want to expand
8484
// this to arbitrary non-special local projections in the future.
8585
type projectAs objectProjection
8686

@@ -101,9 +101,9 @@ func (p projectAs) ApplyToWatches(opts *WatchesInput) {
101101

102102
var (
103103
// OnlyMetadata tells the controller to *only* cache metadata, and to watch
104-
// the API server in metadata-only form. This is useful when watching
104+
// the API server in metadata-only form. This is useful when watching
105105
// lots of objects, really big objects, or objects for which you only know
106-
// the GVK, but not the structure. You'll need to pass
106+
// the GVK, but not the structure. You'll need to pass
107107
// metav1.PartialObjectMetadata to the client when fetching objects in your
108108
// reconciler, otherwise you'll end up with a duplicate structured or
109109
// unstructured cache.

pkg/builder/webhook.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ import (
3636

3737
// WebhookBuilder builds a Webhook.
3838
type WebhookBuilder struct {
39-
apiType runtime.Object
40-
withDefaulter admission.CustomDefaulter
41-
withValidator admission.CustomValidator
42-
gvk schema.GroupVersionKind
43-
mgr manager.Manager
44-
config *rest.Config
45-
recoverPanic bool
46-
logConstructor func(base logr.Logger, req *admission.Request) logr.Logger
39+
apiType runtime.Object
40+
customDefaulter admission.CustomDefaulter
41+
customValidator admission.CustomValidator
42+
gvk schema.GroupVersionKind
43+
mgr manager.Manager
44+
config *rest.Config
45+
recoverPanic bool
46+
logConstructor func(base logr.Logger, req *admission.Request) logr.Logger
4747
}
4848

49-
// WebhookManagedBy allows inform its manager.Manager.
49+
// WebhookManagedBy returns a new webhook builder.
5050
func WebhookManagedBy(m manager.Manager) *WebhookBuilder {
5151
return &WebhookBuilder{mgr: m}
5252
}
@@ -61,15 +61,15 @@ func (blder *WebhookBuilder) For(apiType runtime.Object) *WebhookBuilder {
6161
return blder
6262
}
6363

64-
// WithDefaulter takes a admission.WithDefaulter interface, a MutatingWebhook will be wired for this type.
64+
// WithDefaulter takes an admission.CustomDefaulter interface, a MutatingWebhook will be wired for this type.
6565
func (blder *WebhookBuilder) WithDefaulter(defaulter admission.CustomDefaulter) *WebhookBuilder {
66-
blder.withDefaulter = defaulter
66+
blder.customDefaulter = defaulter
6767
return blder
6868
}
6969

70-
// WithValidator takes a admission.WithValidator interface, a ValidatingWebhook will be wired for this type.
70+
// WithValidator takes a admission.CustomValidator interface, a ValidatingWebhook will be wired for this type.
7171
func (blder *WebhookBuilder) WithValidator(validator admission.CustomValidator) *WebhookBuilder {
72-
blder.withValidator = validator
72+
blder.customValidator = validator
7373
return blder
7474
}
7575

@@ -79,7 +79,7 @@ func (blder *WebhookBuilder) WithLogConstructor(logConstructor func(base logr.Lo
7979
return blder
8080
}
8181

82-
// RecoverPanic indicates whether the panic caused by webhook should be recovered.
82+
// RecoverPanic indicates whether panics caused by the webhook should be recovered.
8383
func (blder *WebhookBuilder) RecoverPanic() *WebhookBuilder {
8484
blder.recoverPanic = true
8585
return blder
@@ -129,12 +129,12 @@ func (blder *WebhookBuilder) registerWebhooks() error {
129129
return err
130130
}
131131

132-
// Create webhook(s) for each type
133132
blder.gvk, err = apiutil.GVKForObject(typ, blder.mgr.GetScheme())
134133
if err != nil {
135134
return err
136135
}
137136

137+
// Register webhook(s) for type
138138
blder.registerDefaultingWebhook()
139139
blder.registerValidatingWebhook()
140140

@@ -145,7 +145,7 @@ func (blder *WebhookBuilder) registerWebhooks() error {
145145
return nil
146146
}
147147

148-
// registerDefaultingWebhook registers a defaulting webhook if th.
148+
// registerDefaultingWebhook registers a defaulting webhook if necessary.
149149
func (blder *WebhookBuilder) registerDefaultingWebhook() {
150150
mwh := blder.getDefaultingWebhook()
151151
if mwh != nil {
@@ -164,7 +164,7 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() {
164164
}
165165

166166
func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook {
167-
if defaulter := blder.withDefaulter; defaulter != nil {
167+
if defaulter := blder.customDefaulter; defaulter != nil {
168168
return admission.WithCustomDefaulter(blder.mgr.GetScheme(), blder.apiType, defaulter).WithRecoverPanic(blder.recoverPanic)
169169
}
170170
if defaulter, ok := blder.apiType.(admission.Defaulter); ok {
@@ -176,6 +176,7 @@ func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook {
176176
return nil
177177
}
178178

179+
// registerValidatingWebhook registers a validating webhook if necessary.
179180
func (blder *WebhookBuilder) registerValidatingWebhook() {
180181
vwh := blder.getValidatingWebhook()
181182
if vwh != nil {
@@ -194,7 +195,7 @@ func (blder *WebhookBuilder) registerValidatingWebhook() {
194195
}
195196

196197
func (blder *WebhookBuilder) getValidatingWebhook() *admission.Webhook {
197-
if validator := blder.withValidator; validator != nil {
198+
if validator := blder.customValidator; validator != nil {
198199
return admission.WithCustomValidator(blder.mgr.GetScheme(), blder.apiType, validator).WithRecoverPanic(blder.recoverPanic)
199200
}
200201
if validator, ok := blder.apiType.(admission.Validator); ok {

0 commit comments

Comments
 (0)