Skip to content

Commit

Permalink
fix: forward context in pkg/counter (#919)
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Ilario <filario@redhat.com>
Co-authored-by: Alexey Kazakov <alkazako@redhat.com>
  • Loading branch information
filariow and alexeykazakov authored Nov 6, 2023
1 parent a8aa9be commit e28b4c8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion controllers/toolchainstatus/toolchainstatus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (r *Reconciler) restoredCheck(ctx context.Context, toolchainStatus *toolcha

// synchronizeWithCounter synchronizes the ToolchainStatus with the cached counter
func (r *Reconciler) synchronizeWithCounter(ctx context.Context, toolchainStatus *toolchainv1alpha1.ToolchainStatus) bool {
if err := counter.Synchronize(r.Client, toolchainStatus); err != nil {
if err := counter.Synchronize(ctx, r.Client, toolchainStatus); err != nil {
logger := log.FromContext(ctx)
logger.Error(err, "unable to synchronize with the counter")
return false
Expand Down
16 changes: 8 additions & 8 deletions pkg/counter/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ func GetCounts() (Counts, error) {
//
// If the cached counter is initialized and ToolchainStatus contains already some numbers
// then it updates the ToolchainStatus numbers with the one taken from the cached counter
func Synchronize(cl runtimeclient.Client, toolchainStatus *toolchainv1alpha1.ToolchainStatus) error {
func Synchronize(ctx context.Context, cl runtimeclient.Client, toolchainStatus *toolchainv1alpha1.ToolchainStatus) error {
cachedCounts.Lock()
defer cachedCounts.Unlock()

// initialize the cached counters (if needed)
if err := initialize(cl, toolchainStatus); err != nil {
if err := initialize(ctx, cl, toolchainStatus); err != nil {
return err
}

Expand Down Expand Up @@ -222,7 +222,7 @@ func indexOfMember(members []toolchainv1alpha1.Member, name string) int {
return -1
}

func initialize(cl runtimeclient.Client, toolchainStatus *toolchainv1alpha1.ToolchainStatus) error {
func initialize(ctx context.Context, cl runtimeclient.Client, toolchainStatus *toolchainv1alpha1.ToolchainStatus) error {
// skip if cached counters are already initialized
if cachedCounts.initialized {
return nil
Expand All @@ -238,26 +238,26 @@ func initialize(cl runtimeclient.Client, toolchainStatus *toolchainv1alpha1.Tool
if config.Metrics().ForceSynchronization() ||
!usersPerActivationAndDomainMetricKeyExists ||
!masterUserRecordsPerDomainMetricExists {
return initializeFromResources(cl, toolchainStatus.Namespace)
return initializeFromResources(ctx, cl, toolchainStatus.Namespace)
}
// otherwise, initialize the cached counters from the ToolchainStatus resource.
return initializeFromToolchainStatus(toolchainStatus)
}

// initialize the cached counters from the UserSignup and MasterUserRecord resources.
// this func lists all UserSignup and MasterUserRecord resources
func initializeFromResources(cl runtimeclient.Client, namespace string) error {
func initializeFromResources(ctx context.Context, cl runtimeclient.Client, namespace string) error {
log.Info("initializing counters from resources")
usersignups := &toolchainv1alpha1.UserSignupList{}
if err := cl.List(context.TODO(), usersignups, runtimeclient.InNamespace(namespace)); err != nil {
if err := cl.List(ctx, usersignups, runtimeclient.InNamespace(namespace)); err != nil {
return err
}
murs := &toolchainv1alpha1.MasterUserRecordList{}
if err := cl.List(context.TODO(), murs, runtimeclient.InNamespace(namespace)); err != nil {
if err := cl.List(ctx, murs, runtimeclient.InNamespace(namespace)); err != nil {
return err
}
spaces := &toolchainv1alpha1.SpaceList{}
if err := cl.List(context.TODO(), spaces, runtimeclient.InNamespace(namespace)); err != nil {
if err := cl.List(ctx, spaces, runtimeclient.InNamespace(namespace)); err != nil {
return err
}
reset()
Expand Down
6 changes: 3 additions & 3 deletions pkg/counter/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func TestShouldNotInitializeAgain(t *testing.T) {
require.NoError(t, err)

// when
err = counter.Synchronize(fakeClient, toolchainStatus)
err = counter.Synchronize(context.TODO(), fakeClient, toolchainStatus)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -505,15 +505,15 @@ func TestMultipleExecutionsInParallel(t *testing.T) {
go func() {
defer waitForFinished.Done()
latch.Wait()
err := counter.Synchronize(fakeClient, toolchainStatus)
err := counter.Synchronize(context.TODO(), fakeClient, toolchainStatus)
require.NoError(t, err)
}()
}

// when
latch.Done()
waitForFinished.Wait()
err := counter.Synchronize(fakeClient, toolchainStatus)
err := counter.Synchronize(context.TODO(), fakeClient, toolchainStatus)

// then
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion test/counter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"context"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -110,6 +111,6 @@ func InitializeCountersWithoutReset(t *testing.T, toolchainStatus *toolchainv1al

func initializeCounters(t *testing.T, cl *commontest.FakeClient, toolchainStatus *toolchainv1alpha1.ToolchainStatus) {
t.Logf("toolchainStatus members: %v", toolchainStatus.Status.Members)
err := counter.Synchronize(cl, toolchainStatus)
err := counter.Synchronize(context.TODO(), cl, toolchainStatus)
require.NoError(t, err)
}

0 comments on commit e28b4c8

Please sign in to comment.