Skip to content

Commit

Permalink
Cleaned up stray contexts in test expectations (#910)
Browse files Browse the repository at this point in the history
* Cleaned up stay contexts in test expectations

* PR comments
  • Loading branch information
ellistarn authored Dec 3, 2021
1 parent a859cad commit a2031aa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func main() {

// Set up controller runtime controller
cloudProvider := registry.NewCloudProvider(ctx, cloudprovider.Options{ClientSet: clientSet})
manager := controllers.NewManagerOrDie(config, controllerruntime.Options{
manager := controllers.NewManagerOrDie(ctx, config, controllerruntime.Options{
Logger: zapr.NewLogger(logging.FromContext(ctx).Desugar()),
LeaderElection: true,
LeaderElectionID: "karpenter-leader-election",
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ type GenericControllerManager struct {
}

// NewManagerOrDie instantiates a controller manager or panics
func NewManagerOrDie(config *rest.Config, options controllerruntime.Options) Manager {
func NewManagerOrDie(ctx context.Context, config *rest.Config, options controllerruntime.Options) Manager {
newManager, err := controllerruntime.NewManager(config, options)
if err != nil {
panic(fmt.Sprintf("Failed to create controller newManager, %s", err.Error()))
}
if err := newManager.GetFieldIndexer().IndexField(context.Background(), &v1.Pod{}, "spec.nodeName", podSchedulingIndex); err != nil {
if err := newManager.GetFieldIndexer().IndexField(ctx, &v1.Pod{}, "spec.nodeName", podSchedulingIndex); err != nil {
panic(fmt.Sprintf("Failed to setup pod indexer, %s", err.Error()))
}
return &GenericControllerManager{Manager: newManager}
Expand Down
16 changes: 8 additions & 8 deletions pkg/test/expectations/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ const (

func ExpectPodExists(ctx context.Context, c client.Client, name string, namespace string) *v1.Pod {
pod := &v1.Pod{}
Expect(c.Get(context.Background(), client.ObjectKey{Name: name, Namespace: namespace}, pod)).To(Succeed())
Expect(c.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, pod)).To(Succeed())
return pod
}

func ExpectNodeExists(ctx context.Context, c client.Client, name string) *v1.Node {
node := &v1.Node{}
Expect(c.Get(context.Background(), client.ObjectKey{Name: name}, node)).To(Succeed())
Expect(c.Get(ctx, client.ObjectKey{Name: name}, node)).To(Succeed())
return node
}

func ExpectNotFound(ctx context.Context, c client.Client, objects ...client.Object) {
for _, object := range objects {
Eventually(func() bool {
return errors.IsNotFound(c.Get(context.Background(), types.NamespacedName{Name: object.GetName(), Namespace: object.GetNamespace()}, object))
return errors.IsNotFound(c.Get(ctx, types.NamespacedName{Name: object.GetName(), Namespace: object.GetNamespace()}, object))
}, ReconcilerPropagationTime, RequestInterval).Should(BeTrue(), func() string {
return fmt.Sprintf("expected %s to be deleted, but it still exists", object.GetSelfLink())
})
Expand All @@ -77,22 +77,22 @@ func ExpectNotScheduled(ctx context.Context, c client.Client, pod *v1.Pod) {
func ExpectApplied(ctx context.Context, c client.Client, objects ...client.Object) {
for _, object := range objects {
if object.GetResourceVersion() == "" {
Expect(c.Create(context.Background(), object)).To(Succeed())
Expect(c.Create(ctx, object)).To(Succeed())
} else {
Expect(c.Update(context.Background(), object)).To(Succeed())
Expect(c.Update(ctx, object)).To(Succeed())
}
}
}

func ExpectStatusUpdated(ctx context.Context, c client.Client, objects ...client.Object) {
for _, object := range objects {
Expect(c.Status().Update(context.Background(), object)).To(Succeed())
Expect(c.Status().Update(ctx, object)).To(Succeed())
}
}

func ExpectCreated(ctx context.Context, c client.Client, objects ...client.Object) {
for _, object := range objects {
Expect(c.Create(context.Background(), object)).To(Succeed())
Expect(c.Create(ctx, object)).To(Succeed())
}
}

Expand All @@ -101,7 +101,7 @@ func ExpectCreatedWithStatus(ctx context.Context, c client.Client, objects ...cl
// Preserve a copy of the status, which is overriden by create
status := object.DeepCopyObject().(client.Object)
ExpectApplied(ctx, c, object)
Expect(c.Status().Update(context.Background(), status)).To(Succeed())
Expect(c.Status().Update(ctx, status)).To(Succeed())
}
}

Expand Down

0 comments on commit a2031aa

Please sign in to comment.