Skip to content

Commit

Permalink
clustermesh: propagate context to Get/SetClusterConfig functions
Browse files Browse the repository at this point in the history
Let's propagate the context from the called to these helper functions,
rather than using context.Background().

Signed-off-by: Marco Iorio <marco.iorio@isovalent.com>
  • Loading branch information
giorio94 authored and julianwiedmann committed May 31, 2023
1 parent 37f0159 commit b46e154
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion clustermesh-apiserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func startServer(startCtx hive.HookContext, clientset k8sClient.Clientset, servi
ID: cfg.clusterID,
}

if err := clustermesh.SetClusterConfig(cfg.clusterName, &config, kvstore.Client()); err != nil {
if err := clustermesh.SetClusterConfig(context.Background(), cfg.clusterName, &config, kvstore.Client()); err != nil {
log.WithError(err).Fatal("Unable to set local cluster config on kvstore")
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/clustermesh/clustermesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ type Configuration struct {
ClusterSizeDependantInterval kvstore.ClusterSizeDependantIntervalFunc `optional:"true"`
}

func SetClusterConfig(clusterName string, config *cmtypes.CiliumClusterConfig, backend kvstore.BackendOperations) error {
func SetClusterConfig(ctx context.Context, clusterName string, config *cmtypes.CiliumClusterConfig, backend kvstore.BackendOperations) error {
key := path.Join(kvstore.ClusterConfigPrefix, clusterName)

val, err := json.Marshal(config)
if err != nil {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()

_, err = backend.UpdateIfDifferent(ctx, key, val, true)
Expand All @@ -85,10 +85,10 @@ func SetClusterConfig(clusterName string, config *cmtypes.CiliumClusterConfig, b
return nil
}

func GetClusterConfig(clusterName string, backend kvstore.BackendOperations) (*cmtypes.CiliumClusterConfig, error) {
func GetClusterConfig(ctx context.Context, clusterName string, backend kvstore.BackendOperations) (*cmtypes.CiliumClusterConfig, error) {
var config cmtypes.CiliumClusterConfig

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()

val, err := backend.Get(ctx, path.Join(kvstore.ClusterConfigPrefix, clusterName))
Expand Down
11 changes: 6 additions & 5 deletions pkg/clustermesh/clustermesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ func (o *testObserver) OnDelete(k store.NamedKey) {
}

func (s *ClusterMeshTestSuite) TestClusterMesh(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

kvstore.SetupDummy("etcd")
defer kvstore.Client().Close(context.TODO())
defer kvstore.Client().Close(ctx)

identity.InitWellKnownIdentities(&fakeConfig.Config{})
// The nils are only used by k8s CRD identities. We default to kvstore.
Expand All @@ -118,7 +121,7 @@ func (s *ClusterMeshTestSuite) TestClusterMesh(c *C) {
ID: uint32(i),
}

err = SetClusterConfig(name, &config, kvstore.Client())
err = SetClusterConfig(ctx, name, &config, kvstore.Client())
c.Assert(err, IsNil)
}

Expand All @@ -134,8 +137,6 @@ func (s *ClusterMeshTestSuite) TestClusterMesh(c *C) {
err = os.WriteFile(config3, etcdConfig, 0644)
c.Assert(err, IsNil)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ipc := ipcache.NewIPCache(&ipcache.Configuration{
Context: ctx,
})
Expand Down Expand Up @@ -163,7 +164,7 @@ func (s *ClusterMeshTestSuite) TestClusterMesh(c *C) {
for _, rc := range cm.clusters {
rc.mutex.RLock()
for _, name := range nodeNames {
err = rc.remoteNodes.UpdateLocalKeySync(context.TODO(), &testNode{Name: name, Cluster: rc.name})
err = rc.remoteNodes.UpdateLocalKeySync(ctx, &testNode{Name: name, Cluster: rc.name})
c.Assert(err, IsNil)
}
rc.mutex.RUnlock()
Expand Down
2 changes: 1 addition & 1 deletion pkg/clustermesh/remote_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (rc *remoteCluster) getClusterConfig(ctx context.Context, backend kvstore.B
rc.controllers.UpdateController(ctrlname, controller.ControllerParams{
DoFunc: func(ctx context.Context) error {
rc.getLogger().Debug("Retrieving cluster configuration from remote kvstore")
config, err := GetClusterConfig(rc.name, backend)
config, err := GetClusterConfig(ctx, rc.name, backend)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/clustermesh/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func (s *ClusterMeshServicesTestSuite) SetUpSuite(c *C) {
}

func (s *ClusterMeshServicesTestSuite) SetUpTest(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

kvstore.SetupDummy("etcd")

s.randomName = rand.RandomString()
Expand All @@ -77,7 +80,7 @@ func (s *ClusterMeshServicesTestSuite) SetUpTest(c *C) {
config := cmtypes.CiliumClusterConfig{
ID: uint32(i),
}
err := SetClusterConfig(cluster, &config, kvstore.Client())
err := SetClusterConfig(ctx, cluster, &config, kvstore.Client())
c.Assert(err, IsNil)
}

Expand All @@ -89,8 +92,6 @@ func (s *ClusterMeshServicesTestSuite) SetUpTest(c *C) {
err = os.WriteFile(config2, etcdConfig, 0644)
c.Assert(err, IsNil)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ipc := ipcache.NewIPCache(&ipcache.Configuration{
Context: ctx,
})
Expand Down

0 comments on commit b46e154

Please sign in to comment.