Skip to content

⚠️ Propagate context.Context throughout the codebase #1116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion designs/move-cluster-specific-code-out-of-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func NewSecretMirrorReconciler(mgr manager.Manager, mirrorCluster cluster.Cluste

func main(){

mgr, err := manager.New(cfg1, manager.Options{})
mgr, err := manager.New(context.Background(), cfg1, manager.Options{})
if err != nil {
panic(err)
}
Expand Down
15 changes: 9 additions & 6 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
func Example() {
var log = controllers.Log.WithName("builder-examples")

manager, err := controllers.NewManager(controllers.GetConfigOrDie(), controllers.Options{})
manager, err := controllers.NewManager(context.Background(), controllers.GetConfigOrDie(), controllers.Options{})
if err != nil {
log.Error(err, "could not create manager")
os.Exit(1)
Expand Down Expand Up @@ -78,11 +78,14 @@ func Example_updateLeaderElectionDurations() {
leaseDuration := 100 * time.Second
renewDeadline := 80 * time.Second
retryPeriod := 20 * time.Second
manager, err := controllers.NewManager(controllers.GetConfigOrDie(), controllers.Options{
LeaseDuration: &leaseDuration,
RenewDeadline: &renewDeadline,
RetryPeriod: &retryPeriod,
})
manager, err := controllers.NewManager(
context.Background(),
controllers.GetConfigOrDie(),
controllers.Options{
LeaseDuration: &leaseDuration,
RenewDeadline: &renewDeadline,
RetryPeriod: &retryPeriod,
})
if err != nil {
log.Error(err, "could not create manager")
os.Exit(1)
Expand Down
3 changes: 2 additions & 1 deletion examples/builtins/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"context"
"os"

appsv1 "k8s.io/api/apps/v1"
Expand All @@ -42,7 +43,7 @@ func main() {

// Setup a Manager
entryLog.Info("setting up manager")
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
mgr, err := manager.New(context.Background(), config.GetConfigOrDie(), manager.Options{})
if err != nil {
entryLog.Error(err, "unable to set up overall controller manager")
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion examples/crd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
func main() {
ctrl.SetLogger(zap.New())

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
mgr, err := ctrl.NewManager(context.Background(), ctrl.GetConfigOrDie(), ctrl.Options{})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
Expand Down
26 changes: 13 additions & 13 deletions pkg/builder/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var _ = Describe("application", func() {
Describe("New", func() {
It("should return success if given valid objects", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

instance, err := ControllerManagedBy(m).
Expand All @@ -92,7 +92,7 @@ var _ = Describe("application", func() {

It("should return error if given two apiType objects in For function", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

instance, err := ControllerManagedBy(m).
Expand All @@ -106,7 +106,7 @@ var _ = Describe("application", func() {

It("should return an error if For function is not called", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

instance, err := ControllerManagedBy(m).
Expand All @@ -118,7 +118,7 @@ var _ = Describe("application", func() {

It("should return an error if there is no GVK for an object, and thus we can't default the controller name", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

By("creating a controller with a bad For type")
Expand All @@ -141,7 +141,7 @@ var _ = Describe("application", func() {
}

By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

instance, err := ControllerManagedBy(m).
Expand All @@ -164,7 +164,7 @@ var _ = Describe("application", func() {
}

By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

instance, err := ControllerManagedBy(m).
Expand All @@ -186,7 +186,7 @@ var _ = Describe("application", func() {
}

By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

instance, err := ControllerManagedBy(m).
Expand All @@ -209,7 +209,7 @@ var _ = Describe("application", func() {
}

By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

instance, err := ControllerManagedBy(m).
Expand All @@ -230,7 +230,7 @@ var _ = Describe("application", func() {
}

By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

instance, err := ControllerManagedBy(m).
Expand All @@ -244,7 +244,7 @@ var _ = Describe("application", func() {

It("should allow multiple controllers for the same kind", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

By("registering the type in the Scheme")
Expand Down Expand Up @@ -273,7 +273,7 @@ var _ = Describe("application", func() {

Describe("Start with ControllerManagedBy", func() {
It("should Reconcile Owns objects", func(done Done) {
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

bldr := ControllerManagedBy(m).
Expand All @@ -284,7 +284,7 @@ var _ = Describe("application", func() {
}, 10)

It("should Reconcile Watches objects", func(done Done) {
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

bldr := ControllerManagedBy(m).
Expand All @@ -299,7 +299,7 @@ var _ = Describe("application", func() {

Describe("Set custom predicates", func() {
It("should execute registered predicates only for assigned kind", func(done Done) {
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/builder/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ExampleBuilder() {

var log = logf.Log.WithName("builder-examples")

mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
mgr, err := manager.New(context.Background(), config.GetConfigOrDie(), manager.Options{})
if err != nil {
log.Error(err, "could not create manager")
os.Exit(1)
Expand Down
3 changes: 2 additions & 1 deletion pkg/builder/example_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package builder_test

import (
"context"
"os"

"sigs.k8s.io/controller-runtime/pkg/builder"
Expand All @@ -39,7 +40,7 @@ var _ admission.Validator = &examplegroup.ChaosPod{}
func ExampleWebhookBuilder() {
var log = logf.Log.WithName("webhookbuilder-example")

mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
mgr, err := manager.New(context.Background(), config.GetConfigOrDie(), manager.Options{})
if err != nil {
log.Error(err, "could not create manager")
os.Exit(1)
Expand Down
35 changes: 19 additions & 16 deletions pkg/builder/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package builder

import (
"context"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -49,7 +50,7 @@ var _ = Describe("webhook", func() {
Describe("New", func() {
It("should scaffold a defaulting webhook if the type implements the Defaulter interface", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

By("registering the type in the Scheme")
Expand Down Expand Up @@ -89,11 +90,11 @@ var _ = Describe("webhook", func() {
}
}`)

stopCh := make(chan struct{})
close(stopCh)
ctx, cancel := context.WithCancel(context.Background())
cancel()
// TODO: we may want to improve it to make it be able to inject dependencies,
// but not always try to load certs and return not found error.
err = svr.Start(stopCh)
err = svr.Start(ctx)
if err != nil && !os.IsNotExist(err) {
Expect(err).NotTo(HaveOccurred())
}
Expand Down Expand Up @@ -121,7 +122,7 @@ var _ = Describe("webhook", func() {

It("should scaffold a validating webhook if the type implements the Validator interface", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

By("registering the type in the Scheme")
Expand Down Expand Up @@ -163,11 +164,11 @@ var _ = Describe("webhook", func() {
}
}`)

stopCh := make(chan struct{})
close(stopCh)
ctx, cancel := context.WithCancel(context.Background())
cancel()
// TODO: we may want to improve it to make it be able to inject dependencies,
// but not always try to load certs and return not found error.
err = svr.Start(stopCh)
err = svr.Start(ctx)
if err != nil && !os.IsNotExist(err) {
Expect(err).NotTo(HaveOccurred())
}
Expand All @@ -194,7 +195,7 @@ var _ = Describe("webhook", func() {

It("should scaffold defaulting and validating webhooks if the type implements both Defaulter and Validator interfaces", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
m, err := manager.New(context.Background(), cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

By("registering the type in the Scheme")
Expand Down Expand Up @@ -234,11 +235,11 @@ var _ = Describe("webhook", func() {
}
}`)

stopCh := make(chan struct{})
close(stopCh)
ctx, cancel := context.WithCancel(context.Background())
cancel()
// TODO: we may want to improve it to make it be able to inject dependencies,
// but not always try to load certs and return not found error.
err = svr.Start(stopCh)
err = svr.Start(ctx)
if err != nil && !os.IsNotExist(err) {
Expect(err).NotTo(HaveOccurred())
}
Expand Down Expand Up @@ -269,7 +270,9 @@ var _ = Describe("webhook", func() {

It("should scaffold a validating webhook if the type implements the Validator interface to validate deletes", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
ctx, cancel := context.WithCancel(context.Background())

m, err := manager.New(ctx, cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

By("registering the type in the Scheme")
Expand Down Expand Up @@ -308,11 +311,11 @@ var _ = Describe("webhook", func() {
}
}
}`)
stopCh := make(chan struct{})
close(stopCh)

cancel()
// TODO: we may want to improve it to make it be able to inject dependencies,
// but not always try to load certs and return not found error.
err = svr.Start(stopCh)
err = svr.Start(ctx)
if err != nil && !os.IsNotExist(err) {
Expect(err).NotTo(HaveOccurred())
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ type Informers interface {
// of the underlying object.
GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind) (Informer, error)

// Start runs all the informers known to this cache until the given channel is closed.
// Start runs all the informers known to this cache until the context is closed.
// It blocks.
Start(stopCh <-chan struct{}) error
Start(ctx context.Context) error

// WaitForCacheSync waits for all the caches to sync. Returns false if it could not sync a cache.
WaitForCacheSync(stop <-chan struct{}) bool
WaitForCacheSync(ctx context.Context) bool

// Informers knows how to add indices to the caches (informers) that it manages.
client.FieldIndexer
Expand Down
Loading