@@ -29,6 +29,7 @@ import (
2929 . "github.com/onsi/gomega"
3030 "github.com/prometheus/client_golang/prometheus"
3131 dto "github.com/prometheus/client_model/go"
32+ "go.uber.org/goleak"
3233 appsv1 "k8s.io/api/apps/v1"
3334 corev1 "k8s.io/api/core/v1"
3435 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -1217,7 +1218,9 @@ var _ = Describe("controller", func() {
12171218 cfg , err := testenv .Start ()
12181219 Expect (err ).NotTo (HaveOccurred ())
12191220 m , err := manager .New (cfg , manager.Options {
1220- LeaderElection : true ,
1221+ LeaderElection : true ,
1222+ LeaderElectionID : "some-leader-election-id" ,
1223+ LeaderElectionNamespace : "default" ,
12211224 })
12221225 Expect (err ).NotTo (HaveOccurred ())
12231226
@@ -1316,6 +1319,50 @@ var _ = Describe("controller", func() {
13161319
13171320 Eventually (didWatchStart .Load ).Should (BeTrue (), "watch should be started if it is added after Warmup" )
13181321 })
1322+
1323+ DescribeTable ("should not leak goroutines when manager is stopped with warmup runnable" ,
1324+ func (leaderElection bool ) {
1325+ ctx , cancel := context .WithCancel (context .Background ())
1326+ defer cancel ()
1327+
1328+ ctrl .CacheSyncTimeout = time .Second
1329+
1330+ By ("Creating a manager" )
1331+ testenv = & envtest.Environment {}
1332+ cfg , err := testenv .Start ()
1333+ Expect (err ).NotTo (HaveOccurred ())
1334+ m , err := manager .New (cfg , manager.Options {
1335+ LeaderElection : leaderElection ,
1336+ LeaderElectionID : "some-leader-election-id" ,
1337+ LeaderElectionNamespace : "default" ,
1338+ })
1339+ Expect (err ).NotTo (HaveOccurred ())
1340+
1341+ ctrl .startWatches = []source.TypedSource [reconcile.Request ]{
1342+ source .Func (func (ctx context.Context , _ workqueue.TypedRateLimitingInterface [reconcile.Request ]) error {
1343+ <- ctx .Done ()
1344+ return nil
1345+ }),
1346+ }
1347+ Expect (m .Add (ctrl )).To (Succeed ())
1348+
1349+ // ignore needs to go after the testenv.Start() call to ignore the apiserver
1350+ // process
1351+ currentGRs := goleak .IgnoreCurrent ()
1352+ go func () {
1353+ defer GinkgoRecover ()
1354+ Expect (m .Start (ctx )).To (Succeed ())
1355+ }()
1356+
1357+ <- m .Elected ()
1358+ By ("stopping the manager via context" )
1359+ cancel ()
1360+
1361+ Eventually (func () error { return goleak .Find (currentGRs ) }).Should (Succeed ())
1362+ },
1363+ Entry ("manager with leader election enabled" , true ),
1364+ Entry ("manager without leader election enabled" , false ),
1365+ )
13191366 })
13201367
13211368 Describe ("Warmup with warmup disabled" , func () {
0 commit comments