-
Notifications
You must be signed in to change notification settings - Fork 47
feat: use informer cache for Get method in Kubernetes backend #525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 18 commits
3512dba
7de1164
87fb311
c0b9baa
3a22c60
da53214
53100da
705aed2
4933a06
88a1c14
eee800d
2620360
3edf88a
df1199a
16a082d
93c90c9
9864fe1
66231cc
693ab36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
"github.com/sirupsen/logrus" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
@@ -57,6 +58,9 @@ type Informer[T runtime.Object] struct { | |
|
||
resType reflect.Type | ||
|
||
// groupResource is the group and resource of the watched objects. | ||
groupResource schema.GroupResource | ||
|
||
// logger is this informer's logger. | ||
logger *logrus.Entry | ||
|
||
|
@@ -109,6 +113,12 @@ func NewInformer[T runtime.Object](ctx context.Context, opts ...InformerOption[T | |
i := &Informer[T]{} | ||
var r T | ||
i.resType = reflect.TypeOf(r) | ||
|
||
i.groupResource = schema.GroupResource{ | ||
Group: "argoproj.io", | ||
Resource: "applications", | ||
} | ||
|
||
|
||
i.logger = logrus.NewEntry(logrus.StandardLogger()).WithFields(logrus.Fields{ | ||
"type": i.resType, | ||
"module": "Informer", | ||
|
@@ -287,3 +297,8 @@ func (i *Informer[T]) WaitForSync(ctx context.Context) error { | |
} | ||
return nil | ||
} | ||
|
||
// Lister returns a GenericLister that can be used to list and get cached resources. | ||
func (i *Informer[T]) Lister() cache.GenericLister { | ||
return cache.NewGenericLister(i.informer.GetIndexer(), i.groupResource) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -555,8 +555,14 @@ func (m *ApplicationManager) Delete(ctx context.Context, namespace string, incom | |
// be returned. | ||
func (m *ApplicationManager) update(ctx context.Context, upsert bool, incoming *v1alpha1.Application, updateFn updateTransformer, patchFn patchTransformer) (*v1alpha1.Application, error) { | ||
var updated *v1alpha1.Application | ||
|
||
if ctx == nil { | ||
ctx = context.Background() | ||
} | ||
ctxForUpdate := context.WithValue(ctx, backend.ForUpdateContextKey, true) | ||
|
||
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error { | ||
existing, ierr := m.applicationBackend.Get(ctx, incoming.Name, incoming.Namespace) | ||
existing, ierr := m.applicationBackend.Get(ctxForUpdate, incoming.Name, incoming.Namespace) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason why The issue arises when using the informer cache during updates. Although the informer is synchronized with the API server, there is a slight lag, meaning that the cache may return a stale In contrast, when Therefore, we use the informer cache for regular reads to optimize performance, and direct API reads during updates to ensure correctness. |
||
if ierr != nil { | ||
if errors.IsNotFound(ierr) && upsert { | ||
updated, ierr = m.Create(ctx, incoming) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please explain this construct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please take a look at my explanation below?