Skip to content

Commit 728317b

Browse files
committed
Implement ConditionalController in builder
1 parent 9eb6176 commit 728317b

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

pkg/builder/controller.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ package builder
1919
import (
2020
"fmt"
2121
"strings"
22+
"time"
2223

2324
"github.com/go-logr/logr"
2425
"k8s.io/apimachinery/pkg/runtime"
2526
"k8s.io/apimachinery/pkg/runtime/schema"
27+
"k8s.io/client-go/discovery"
2628
"k8s.io/client-go/rest"
2729
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2830
"sigs.k8s.io/controller-runtime/pkg/controller"
2931
"sigs.k8s.io/controller-runtime/pkg/handler"
32+
intctrl "sigs.k8s.io/controller-runtime/pkg/internal/controller"
3033
"sigs.k8s.io/controller-runtime/pkg/manager"
3134
"sigs.k8s.io/controller-runtime/pkg/predicate"
3235
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -57,9 +60,11 @@ func ControllerManagedBy(m manager.Manager) *Builder {
5760

5861
// ForInput represents the information set by For method.
5962
type ForInput struct {
60-
object runtime.Object
61-
predicates []predicate.Predicate
62-
err error
63+
object runtime.Object
64+
predicates []predicate.Predicate
65+
err error
66+
conditionallyRun bool
67+
waitTime time.Duration
6368
}
6469

6570
// For defines the type of Object being *reconciled*, and configures the ControllerManagedBy to respond to create / delete /
@@ -252,7 +257,34 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
252257
}
253258
ctrlOptions.Log = ctrlOptions.Log.WithValues("reconcilerGroup", gvk.Group, "reconcilerKind", gvk.Kind)
254259

255-
// Build the controller and return.
256-
blder.ctrl, err = newController(blder.getControllerName(gvk), blder.mgr, ctrlOptions)
257-
return err
260+
// Build the base controller
261+
baseController, err := controller.NewUnmanaged(blder.getControllerName(gvk), blder.mgr, ctrlOptions)
262+
if err != nil {
263+
return err
264+
}
265+
266+
// Set the builder controller to either the base controller or wrapped as a ConditionalController.
267+
var ctrl controller.Controller
268+
if blder.forInput.conditionallyRun {
269+
dc, err := discovery.NewDiscoveryClientForConfig(blder.mgr.GetConfig())
270+
if err != nil {
271+
return err
272+
}
273+
ic := baseController.(*intctrl.Controller)
274+
ic.SaveWatches()
275+
ctrl = &controller.ConditionalController{
276+
Cache: blder.mgr.GetCache(),
277+
ConditionalOn: blder.forInput.object,
278+
Controller: ic,
279+
DiscoveryClient: dc,
280+
Scheme: blder.mgr.GetScheme(),
281+
WaitTime: blder.forInput.waitTime,
282+
}
283+
284+
} else {
285+
ctrl = baseController
286+
}
287+
blder.ctrl = ctrl
288+
289+
return blder.mgr.Add(ctrl)
258290
}

pkg/builder/options.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ limitations under the License.
1717
package builder
1818

1919
import (
20+
"time"
21+
2022
"sigs.k8s.io/controller-runtime/pkg/predicate"
2123
)
2224

25+
const defaultWaitTime = time.Minute
26+
2327
// {{{ "Functional" Option Interfaces
2428

2529
// ForOption is some configuration that modifies options for a For request.
@@ -75,4 +79,23 @@ var _ ForOption = &Predicates{}
7579
var _ OwnsOption = &Predicates{}
7680
var _ WatchesOption = &Predicates{}
7781

82+
// ConditionallyRun runs the controller
83+
// condtionally on the existence of the forInput object
84+
// in the cluster's discovery doc, letting you start a
85+
// controller manager for a CRD not yet installed on the cluster.
86+
type ConditionallyRun struct {
87+
WaitTime time.Duration
88+
}
89+
90+
// ApplyToFor applies this configuration to the give forInput options,
91+
// setting the waitTime to the default wait time if it is unset.
92+
func (w ConditionallyRun) ApplyToFor(opts *ForInput) {
93+
opts.conditionallyRun = true
94+
if w.WaitTime == time.Duration(0) {
95+
opts.waitTime = defaultWaitTime
96+
} else {
97+
opts.waitTime = w.WaitTime
98+
}
99+
}
100+
78101
// }}}

0 commit comments

Comments
 (0)