Lossy map functions

オープン
#1,996 コメント 34 件 リアクション 6 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
go
領域
backend

調査の方向性

まず、controller-runtime における MapFunc と EnqueueRequestsFromMapFunc の定義、およびその周辺の EventHandler の動作を読んでください。リンクされた使用例と、フォールトトレラントなマッピング設計について合意された議論を確認してください。プロジェクトがマッピングエラーに対する方針を決定し、それに対応する実装とテストを備えていることが完了の条件であり、提案された回避策だけでは不十分です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Go
スター
3k
フォーク
1.3k
平均マージ
1日 7時間
マージ済み PR(30日)
48

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

kubernetes-sigs/controller-runtime のほかの issue

kubernetes-sigs/controller-runtime の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。