Skip to content

Commit d809156

Browse files
committed
feat: use informr cache in KubernetesBackend Get method
Signed-off-by: yeonsoo <[email protected]>
1 parent f462434 commit d809156

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

internal/backend/kubernetes/application/kubernetes.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
appclientset "github.com/argoproj/argo-cd/v3/pkg/client/clientset/versioned"
3131
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3232
"k8s.io/apimachinery/pkg/types"
33+
"k8s.io/client-go/tools/cache"
3334
)
3435

3536
var _ backend.Application = &KubernetesBackend{}
@@ -43,18 +44,24 @@ type KubernetesBackend struct {
4344
appClient appclientset.Interface
4445
// appInformer is used to watch for change events for Argo CD Application resources on the cluster
4546
appInformer informer.InformerInterface
47+
// appLister is used to list Argo CD Application resources from the cache
48+
appLister cache.GenericLister
4649
// namespace is not currently read, is not guaranteed to be non-empty, and is not guaranteed to contain the source of Argo CD Application CRs in all cases
4750
namespace string
4851
usePatch bool
4952
}
5053

5154
func NewKubernetesBackend(appClient appclientset.Interface, namespace string, appInformer informer.InformerInterface, usePatch bool) *KubernetesBackend {
52-
return &KubernetesBackend{
55+
be := &KubernetesBackend{
5356
appClient: appClient,
5457
appInformer: appInformer,
5558
usePatch: usePatch,
5659
namespace: namespace,
5760
}
61+
if specificInformer, ok := appInformer.(*informer.Informer[*v1alpha1.Application]); ok {
62+
be.appLister = specificInformer.Lister()
63+
}
64+
return be
5865
}
5966

6067
func (be *KubernetesBackend) List(ctx context.Context, selector backend.ApplicationSelector) ([]v1alpha1.Application, error) {
@@ -82,7 +89,15 @@ func (be *KubernetesBackend) Create(ctx context.Context, app *v1alpha1.Applicati
8289
}
8390

8491
func (be *KubernetesBackend) Get(ctx context.Context, name string, namespace string) (*v1alpha1.Application, error) {
85-
return be.appClient.ArgoprojV1alpha1().Applications(namespace).Get(ctx, name, v1.GetOptions{})
92+
obj, err := be.appLister.ByNamespace(namespace).Get(name)
93+
if err != nil {
94+
return nil, err
95+
}
96+
app, ok := obj.(*v1alpha1.Application)
97+
if !ok {
98+
return nil, fmt.Errorf("object is not an Application: %T", obj)
99+
}
100+
return app, nil
86101
}
87102

88103
func (be *KubernetesBackend) Delete(ctx context.Context, name string, namespace string, deletionPropagation *backend.DeletionPropagation) error {

0 commit comments

Comments
 (0)