core: add context parameter to opcontroller
This commit adds context parameter to utilities in opcontroller to remove context.TODO use in opcontroller. By this, we can handle cancellation of reconcilers in a fine-grained way. Signed-off-by: Yuichiro Ueno <y1r.ueno@gmail.com>
This commit is contained in:
1 parent
1a6827bea5
commit
3fd86f83ae
26 files changed
+81
-79
No files matched your search
@@ -30,10 +30,10 @@ import (
|
||||
)
|
||||
|
||||
// UpdateCondition function will export each condition into the cluster custom resource
|
||||
func UpdateCondition(c *clusterd.Context, namespaceName types.NamespacedName, conditionType cephv1.ConditionType, status v1.ConditionStatus, reason cephv1.ConditionReason, message string) {
|
||||
func UpdateCondition(ctx context.Context, c *clusterd.Context, namespaceName types.NamespacedName, conditionType cephv1.ConditionType, status v1.ConditionStatus, reason cephv1.ConditionReason, message string) {
|
||||
// use client.Client unit test this more easily with updating statuses which must use the client
|
||||
cluster := &cephv1.CephCluster{}
|
||||
if err := c.Client.Get(context.TODO(), namespaceName, cluster); err != nil {
|
||||
if err := c.Client.Get(ctx, namespaceName, cluster); err != nil {
|
||||
logger.Errorf("failed to get cluster %v to update the conditions. %v", namespaceName, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -114,14 +114,14 @@ func canIgnoreHealthErrStatusInReconcile(cephCluster cephv1.CephCluster, control
|
||||
}
|
||||
|
||||
// IsReadyToReconcile determines if a controller is ready to reconcile or not
|
||||
func IsReadyToReconcile(c client.Client, clustercontext *clusterd.Context, namespacedName types.NamespacedName, controllerName string) (cephv1.CephCluster, bool, bool, reconcile.Result) {
|
||||
func IsReadyToReconcile(ctx context.Context, c client.Client, clustercontext *clusterd.Context, namespacedName types.NamespacedName, controllerName string) (cephv1.CephCluster, bool, bool, reconcile.Result) {
|
||||
cephClusterExists := false
|
||||
|
||||
// Running ceph commands won't work and the controller will keep re-queuing so I believe it's fine not to check
|
||||
// Make sure a CephCluster exists before doing anything
|
||||
var cephCluster cephv1.CephCluster
|
||||
clusterList := &cephv1.CephClusterList{}
|
||||
err := c.List(context.TODO(), clusterList, client.InNamespace(namespacedName.Namespace))
|
||||
err := c.List(ctx, clusterList, client.InNamespace(namespacedName.Namespace))
|
||||
if err != nil {
|
||||
logger.Errorf("%q: failed to fetch CephCluster %v", controllerName, err)
|
||||
return cephCluster, false, cephClusterExists, ImmediateRetryResult
|
||||
|
||||
@@ -52,7 +52,7 @@ func remove(list []string, s string) []string {
|
||||
|
||||
// AddFinalizerIfNotPresent adds a finalizer an object to avoid instant deletion
|
||||
// of the object without finalizing it.
|
||||
func AddFinalizerIfNotPresent(client client.Client, obj client.Object) error {
|
||||
func AddFinalizerIfNotPresent(ctx context.Context, client client.Client, obj client.Object) error {
|
||||
objectFinalizer := buildFinalizerName(obj.GetObjectKind().GroupVersionKind().Kind)
|
||||
|
||||
accessor, err := meta.Accessor(obj)
|
||||
@@ -65,7 +65,7 @@ func AddFinalizerIfNotPresent(client client.Client, obj client.Object) error {
|
||||
accessor.SetFinalizers(append(accessor.GetFinalizers(), objectFinalizer))
|
||||
|
||||
// Update CR with finalizer
|
||||
if err := client.Update(context.TODO(), obj); err != nil {
|
||||
if err := client.Update(ctx, obj); err != nil {
|
||||
return errors.Wrapf(err, "failed to add finalizer %q on %q", objectFinalizer, accessor.GetName())
|
||||
}
|
||||
}
|
||||
@@ -74,14 +74,14 @@ func AddFinalizerIfNotPresent(client client.Client, obj client.Object) error {
|
||||
}
|
||||
|
||||
// RemoveFinalizer removes a finalizer from an object
|
||||
func RemoveFinalizer(client client.Client, obj client.Object) error {
|
||||
func RemoveFinalizer(ctx context.Context, client client.Client, obj client.Object) error {
|
||||
finalizerName := buildFinalizerName(obj.GetObjectKind().GroupVersionKind().Kind)
|
||||
return RemoveFinalizerWithName(client, obj, finalizerName)
|
||||
return RemoveFinalizerWithName(ctx, client, obj, finalizerName)
|
||||
}
|
||||
|
||||
// RemoveFinalizerWithName removes finalizer passed as an argument from an object
|
||||
func RemoveFinalizerWithName(client client.Client, obj client.Object, finalizerName string) error {
|
||||
err := client.Get(context.TODO(), types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
|
||||
func RemoveFinalizerWithName(ctx context.Context, client client.Client, obj client.Object, finalizerName string) error {
|
||||
err := client.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get the latest version of the object")
|
||||
}
|
||||
@@ -93,7 +93,7 @@ func RemoveFinalizerWithName(client client.Client, obj client.Object, finalizerN
|
||||
if contains(accessor.GetFinalizers(), finalizerName) {
|
||||
logger.Infof("removing finalizer %q on %q", finalizerName, accessor.GetName())
|
||||
accessor.SetFinalizers(remove(accessor.GetFinalizers(), finalizerName))
|
||||
if err := client.Update(context.TODO(), obj); err != nil {
|
||||
if err := client.Update(ctx, obj); err != nil {
|
||||
return errors.Wrapf(err, "failed to remove finalizer %q on %q", finalizerName, accessor.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
||||
@@ -45,7 +46,7 @@ func TestAddFinalizerIfNotPresent(t *testing.T) {
|
||||
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build()
|
||||
|
||||
assert.Empty(t, fakeObject.Finalizers)
|
||||
err := AddFinalizerIfNotPresent(cl, fakeObject)
|
||||
err := AddFinalizerIfNotPresent(context.TODO(), cl, fakeObject)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, fakeObject.Finalizers)
|
||||
}
|
||||
@@ -72,7 +73,7 @@ func TestRemoveFinalizer(t *testing.T) {
|
||||
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build()
|
||||
|
||||
assert.NotEmpty(t, fakeObject.Finalizers)
|
||||
err := RemoveFinalizer(cl, fakeObject)
|
||||
err := RemoveFinalizer(context.TODO(), cl, fakeObject)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, fakeObject.Finalizers)
|
||||
}
|
||||
@@ -99,7 +100,7 @@ func TestRemoveFinalizerWithName(t *testing.T) {
|
||||
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build()
|
||||
|
||||
assert.NotEmpty(t, fakeObject.Finalizers)
|
||||
err := RemoveFinalizerWithName(cl, fakeObject, "cephblockpool.ceph.rook.io")
|
||||
err := RemoveFinalizerWithName(context.TODO(), cl, fakeObject, "cephblockpool.ceph.rook.io")
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, fakeObject.Finalizers)
|
||||
}
|
||||
@@ -33,7 +33,7 @@ import (
|
||||
// It is used to trigger a reconcile object Kind A when watching object Kind B
|
||||
// So we reconcile Kind A instead of Kind B
|
||||
// For instance, we watch for CephCluster CR changes but want to reconcile CephFilesystem based on a Spec change
|
||||
func ObjectToCRMapper(c client.Client, ro runtime.Object, scheme *runtime.Scheme) (handler.MapFunc, error) {
|
||||
func ObjectToCRMapper(ctx context.Context, c client.Client, ro runtime.Object, scheme *runtime.Scheme) (handler.MapFunc, error) {
|
||||
if _, ok := ro.(metav1.ListInterface); !ok {
|
||||
return nil, errors.Errorf("expected a metav1.ListInterface, got %T instead", ro)
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func ObjectToCRMapper(c client.Client, ro runtime.Object, scheme *runtime.Scheme
|
||||
return handler.MapFunc(func(o client.Object) []ctrl.Request {
|
||||
list := &unstructured.UnstructuredList{}
|
||||
list.SetGroupVersionKind(gvk)
|
||||
err := c.List(context.TODO(), list)
|
||||
err := c.List(ctx, list)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
@@ -61,7 +62,7 @@ func TestObjectToCRMapper(t *testing.T) {
|
||||
{NamespacedName: client.ObjectKey{Name: "my-pool", Namespace: "rook-ceph"}},
|
||||
}
|
||||
|
||||
handlerFunc, err := ObjectToCRMapper(cl, objects[0], s)
|
||||
handlerFunc, err := ObjectToCRMapper(context.TODO(), cl, objects[0], s)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, fakeRequest, handlerFunc(fs))
|
||||
}
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
)
|
||||
|
||||
// CreateOrUpdateObject updates an object with a given status
|
||||
func CreateOrUpdateObject(client client.Client, obj client.Object) error {
|
||||
func CreateOrUpdateObject(ctx context.Context, client client.Client, obj client.Object) error {
|
||||
accessor, err := meta.Accessor(obj)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get meta information of object")
|
||||
@@ -38,10 +38,10 @@ func CreateOrUpdateObject(client client.Client, obj client.Object) error {
|
||||
// Somehow meta.TypeAccessor returns an empty string for the type name so using reflection instead
|
||||
objType := reflect.TypeOf(obj)
|
||||
|
||||
err = client.Create(context.TODO(), obj)
|
||||
err = client.Create(ctx, obj)
|
||||
if err != nil {
|
||||
if kerrors.IsAlreadyExists(err) {
|
||||
err = client.Update(context.TODO(), obj)
|
||||
err = client.Update(ctx, obj)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to update ceph %q object %q", objType, objName)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user