Skip to content

Commit 2a14111

Browse files
committed
Allow setting controller name, fix default
This switches the default controller name in the builder to just lower(<kind>) which fixes any issues that arrise from it showing up in the deprecated metrics (which use the workqueue name as part of the metric name). It also allows overriding name in case of two controllers reconciling the same type using the builder.
1 parent 38483b2 commit 2a14111

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

pkg/builder/build.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Builder struct {
5050
watchRequest []watchRequest
5151
config *rest.Config
5252
ctrl controller.Controller
53+
name string
5354
}
5455

5556
// SimpleController returns a new Builder.
@@ -131,6 +132,16 @@ func (blder *Builder) WithEventFilter(p predicate.Predicate) *Builder {
131132
return blder
132133
}
133134

135+
// Named sets the name of the controller to the given name. The name shows up
136+
// in metrics, among other things, and thus should be a prometheus compatible name
137+
// (underscores and alphanumeric characters only).
138+
//
139+
// By default, controllers are named using the lowercase version of their kind.
140+
func (blder *Builder) Named(name string) *Builder {
141+
blder.name = name
142+
return blder
143+
}
144+
134145
// Complete builds the Application ControllerManagedBy.
135146
func (blder *Builder) Complete(r reconcile.Reconciler) error {
136147
_, err := blder.Build(r)
@@ -227,12 +238,14 @@ func (blder *Builder) doManager() error {
227238
}
228239

229240
func (blder *Builder) getControllerName() (string, error) {
241+
if blder.name != "" {
242+
return blder.name, nil
243+
}
230244
gvk, err := getGvk(blder.apiType, blder.mgr.GetScheme())
231245
if err != nil {
232246
return "", err
233247
}
234-
name := fmt.Sprintf("%s-application", strings.ToLower(gvk.Kind))
235-
return name, nil
248+
return strings.ToLower(gvk.Kind), nil
236249
}
237250

238251
func (blder *Builder) doController(r reconcile.Reconciler) error {

0 commit comments

Comments
 (0)