-
Notifications
You must be signed in to change notification settings - Fork 6
/
projectinformer.go
169 lines (155 loc) · 5.5 KB
/
projectinformer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package appproject
import (
"context"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned"
applisters "github.com/argoproj/argo-cd/v2/pkg/client/listers/application/v1alpha1"
"github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"github.com/jannfis/argocd-agent/internal/informer"
)
type AppProjectInformer struct {
filterFunc func(proj *v1alpha1.AppProject) bool
namespaces []string
logger *logrus.Entry
addFunc func(proj *v1alpha1.AppProject)
updateFunc func(oldProj *v1alpha1.AppProject, newProj *v1alpha1.AppProject)
deleteFunc func(proj *v1alpha1.AppProject)
}
type AppProjectInformerOption func(pi *AppProjectInformer) error
// WithNamespaces restricts the informer to certain namespaces. If no namespace
// is set, or if multiple are set, the informer requires cluster-wide access to
// AppProjects. When one or more namespaces are set, only objects in these
// namespaces will be considered by the informer.
func WithNamespaces(namespaces ...string) AppProjectInformerOption {
return func(pi *AppProjectInformer) error {
pi.namespaces = namespaces
return nil
}
}
// WithListFilter sets a filter function for the add, update and delete events.
// This function will be called for each event, and if it returns false, the
// event's execution will not continue.
func WithListFilter(f func(proj *v1alpha1.AppProject) bool) AppProjectInformerOption {
return func(pi *AppProjectInformer) error {
pi.filterFunc = f
return nil
}
}
// WithAddFunc sets the function to be called when an AppProject is created
// on the cluster.
func WithAddFunc(f func(proj *v1alpha1.AppProject)) AppProjectInformerOption {
return func(pi *AppProjectInformer) error {
pi.addFunc = f
return nil
}
}
// WithUpdateFunc sets the function to be called when an AppProject is updated
// on the cluster.
func WithUpdateFunc(f func(oldProj *v1alpha1.AppProject, newProj *v1alpha1.AppProject)) AppProjectInformerOption {
return func(pi *AppProjectInformer) error {
pi.updateFunc = f
return nil
}
}
// WithDeleteFunc sets the function to be called when an AppProject is deleted
// on the cluster.
func WithDeleteFunc(f func(proj *v1alpha1.AppProject)) AppProjectInformerOption {
return func(pi *AppProjectInformer) error {
pi.deleteFunc = f
return nil
}
}
// WithLogger sets the logger to use with this AppProjectInformer
func WithLogger(l *logrus.Entry) AppProjectInformerOption {
return func(pi *AppProjectInformer) error {
pi.logger = l
return nil
}
}
// NewAppProjectInformer returns a new instance of a GenericInformer set up to
// handle AppProjects. It will be configured with the given options, using the
// given appclientset.
func NewAppProjectInformer(ctx context.Context, client appclientset.Interface, options ...AppProjectInformerOption) (*informer.GenericInformer, applisters.AppProjectLister, error) {
pi := &AppProjectInformer{
namespaces: make([]string, 0),
}
for _, o := range options {
err := o(pi)
if err != nil {
return nil, nil, err
}
}
if pi.logger == nil {
pi.logger = logrus.WithField("module", "AppProjectInformer")
}
i, err := informer.NewGenericInformer(&v1alpha1.AppProject{},
informer.WithListCallback(func(options v1.ListOptions, namespace string) (runtime.Object, error) {
projects, err := client.ArgoprojV1alpha1().AppProjects(namespace).List(ctx, options)
pi.logger.Debugf("Lister returned %d AppProjects", len(projects.Items))
if pi.filterFunc != nil {
newItems := make([]v1alpha1.AppProject, 0)
for _, p := range projects.Items {
if pi.filterFunc(&p) {
newItems = append(newItems, p)
}
}
pi.logger.Debugf("Lister has %d AppProjects after filtering", len(newItems))
projects.Items = newItems
}
return projects, err
}),
informer.WithNamespaces(pi.namespaces...),
informer.WithWatchCallback(func(options v1.ListOptions, namespace string) (watch.Interface, error) {
return client.ArgoprojV1alpha1().AppProjects(namespace).Watch(ctx, options)
}),
informer.WithAddCallback(func(obj interface{}) {
proj, ok := obj.(*v1alpha1.AppProject)
if !ok {
pi.logger.Errorf("Received add event for unknown type %T", obj)
return
}
pi.logger.Debugf("AppProject add event: %s", proj.Name)
if pi.addFunc != nil {
pi.addFunc(proj)
}
}),
informer.WithUpdateCallback(func(oldObj, newObj interface{}) {
oldProj, oldProjOk := oldObj.(*v1alpha1.AppProject)
newProj, newProjOk := newObj.(*v1alpha1.AppProject)
if !newProjOk || !oldProjOk {
pi.logger.Errorf("Received update event for unknown type old:%T new:%T", oldObj, newObj)
return
}
pi.logger.Debugf("AppProject update event: old:%s new:%s", oldProj.Name, newProj.Name)
if pi.updateFunc != nil {
pi.updateFunc(oldProj, newProj)
}
}),
informer.WithDeleteCallback(func(obj interface{}) {
proj, ok := obj.(*v1alpha1.AppProject)
if !ok {
pi.logger.Errorf("Received delete event for unknown type %T", obj)
return
}
pi.logger.Debugf("AppProject delete event: %s", proj.Name)
if pi.deleteFunc != nil {
pi.deleteFunc(proj)
}
}),
informer.WithFilterFunc(func(obj interface{}) bool {
if pi.filterFunc == nil {
return true
}
o, ok := obj.(*v1alpha1.AppProject)
if !ok {
pi.logger.Errorf("Failed type conversion for unknown type %T", obj)
return false
}
return pi.filterFunc(o)
}),
)
return i, applisters.NewAppProjectLister(i.Indexer()), err
}