Lossy map functions

Abierto
#1,996 34 comentarios 6 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
go
Área
backend

Línea de trabajo

Comienza leyendo las definiciones de MapFunc y EnqueueRequestsFromMapFunc y el comportamiento circundante de EventHandler en controller-runtime. Revisa los ejemplos de uso enlazados y la discusión sobre un diseño acordado y tolerante a fallos para el mapeo. Se considera terminado cuando el proyecto tiene un enfoque decidido para los errores de mapeo, junto con la implementación y las pruebas correspondientes, en lugar de solo una solución alternativa propuesta.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

lifecycle/frozen
Preface

This relates to a conversation I brought up at the community meeting on 2022-09-08, @camilamacedo86 and @jmrodri suggested I create an issue so we can discuss it further.

Background

Currently in controller-runtime we have the following tools:

type MapFunc func(client.Object) []reconcile.Request

func EnqueueRequestsFromMapFunc(fn MapFunc) EventHandler {
	return &enqueueRequestsFromMapFunc{
		toRequests: fn,
	}
}

With these it's possible to express a Watch() on one source.Kind which enqueues objects of another kind by some arbitrary relation:

ctrl.NewControllerManagedBy(mgr).Watches(
	&source.Kind{Type: &customapi.Tree},
	handler.EnqueueRequestsFromMapFunc(r.mapFruitsFromTree),
)

Now ideally you use metav1.ObjectReference when dealing with relationships between objects, however it can sometimes be the case that you need to use the API client or some other utility that may produce an error to map the related objects, e.g.:

func (r *myReconciler) mapFruitsFromTree(tree client.Object) []reconcile.Request {
	var fruits customapi.FruitList
	if err := r.client.List(context.Background(), &fruits); err != nil {
		r.log.WithError(err).Error("error listing fruits")
		return nil
	}

	var req []reconcile.Request
	for _, fruit := range fruits.Items {
		if isRelated(tree, fruit) {
			req = append(req, reconcile.Request{
				NamespacedName: types.NamespacedName{
					Namespace: fruit.Namespace,
					Name:      fruit.Name,
				},
			})
		}
	}

	return req
}

In these cases it would appear there is effectively no provisions to handle error conditions.

This kind of implementation and use case is not uncommon. I was able to find several examples of notable open source projects which are using it this way:

Problem

The EnqueueRequestsFromMapFunc generator for EventHandlers has no concept of handling errors that may occur when making the mapping. As such in the example above and all the links provided for projects which are doing this that process is currently "lossy": that is to say, it appears the machinery could effectively end up dropping an enqueued object from the queue if a failure occurred during the mapping process.

For a specific example using the Tree and Fruit sample above: if the object listing produces an error (e.g. performed at a time when the API server is having trouble and cache can't be used) this failure means that the Tree object is consumed from the queue but the related objects don't get enqueued. This would cause the resource state to become "stuck" and this wont heal without adding some other mechanism to re-queue it, or some unrelated action to re-trigger it.

Exploring Solutions

The purpose of this issue is to ask for community guidance on an existing or new provision to support this kind of mapping in a manner that is fault tolerant and can heal from an error.

Some of the workarounds that I have conceived of are:

  • using object references as mentioned above (wont work in all cases)
  • some fallback process which would actively re-enqueue all relevant objects (messy, costly)
  • watching a custom backup source.Channel where objects are re-enqueued in an implementation-specific manner (clunky)

So far I think the third option there might be the most effective strategy with what we have today, but ideally what I would like to see is the ability to cleanly trigger a re-queue from within the mapping function as part of the function return. I would very much appreciate community feedback on this.

Lenguaje dominante
Go
Estrellas
3k
Forks
1.3k
Merge medio
1 d 7 h
PR fusionados (30 d)
48

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de kubernetes-sigs/controller-runtime

Todos los issues de kubernetes-sigs/controller-runtime

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.