-
Notifications
You must be signed in to change notification settings - Fork 29
/
objectkind.go
415 lines (375 loc) · 12.6 KB
/
objectkind.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package configuration
import (
"fmt"
"time"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
corev1 "k8s.io/api/core/v1"
helmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
sourcev1beta2 "github.com/fluxcd/source-controller/api/v1beta2"
clusterreflectorv1alpha1 "github.com/weaveworks/cluster-reflector-controller/api/v1alpha1"
gitopssets "github.com/weaveworks/gitopssets-controller/api/v1alpha1"
capiv1 "github.com/weaveworks/templates-controller/apis/capi/v1alpha2"
gapiv1 "github.com/weaveworks/templates-controller/apis/gitops/v1alpha2"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// FilterFunc can be used to only retain relevant objects.
// For example, we may want to keep Events, but only Events from a particular source.
type FilterFunc func(obj client.Object) bool
type ObjectCategory string
const (
CategoryAutomation ObjectCategory = "automation"
CategorySource ObjectCategory = "source"
CategoryEvent ObjectCategory = "event"
CategoryGitopsSet ObjectCategory = "gitopsset"
CategoryTemplate ObjectCategory = "template"
CategoryRBAC ObjectCategory = "rbac"
CategoryClusterDiscovery ObjectCategory = "clusterdiscovery"
)
// ObjectKind is the main structur for a object that explorer is able to manage. It includes all the configuration and
// behaviour that is required for both collection and querying.
type ObjectKind struct {
// Gvk is the GroupVersionKind of the objectKind
Gvk schema.GroupVersionKind `json:"groupVersionKind"`
// NewClientObjectFunc is a function that returns a new kuberentes object for the objectKind.
NewClientObjectFunc func() client.Object `json:"-"`
// AddToSchemeFunc is a function that adds the objectKind to the kubernetes scheme.
AddToSchemeFunc func(*runtime.Scheme) error `json:"-"`
// RetentionPolicy is a function to define retention for objects of this objectkind. For example for event to be retained for 24 hours.
RetentionPolicy RetentionPolicy `json:"-"`
// FilterFunc is a function to filter objects of this objectkind. For example to only retain events from a particular source.
FilterFunc FilterFunc
// StatusFunc is a function to get the status of an object of this objectkind. It allows to customise status resolution by objectkind.
StatusFunc func(obj client.Object) ObjectStatus
// MessageFunc is a function to get the message of an object of this objectkind. It allows to customise message resolution by objectkind.
MessageFunc func(obj client.Object) string
// Labels defines a list of labels that you are interested to collect and query for the object kind. For example, templates, defines templateType as label.
Labels []string
// Category defines the category of the objectkind. It allows to group objectkinds in the UI.
Category ObjectCategory
// HumanReadableLabelKeys is a map of label keys to human readable names. It allows to customise the label names in the UI.
// Values should be dash case: template-type, some-value, etc.
HumanReadableLabelKeys map[string]string
}
type ObjectStatus string
const (
Success ObjectStatus = "Success"
Failed ObjectStatus = "Failed"
NoStatus ObjectStatus = "-"
)
func (ok ObjectKind) String() string {
return ok.Gvk.String()
}
func (o ObjectKind) Validate() error {
if o.Gvk.Kind == "" {
return fmt.Errorf("missing gvk")
}
if o.NewClientObjectFunc == nil {
return fmt.Errorf("missing client func")
}
if o.AddToSchemeFunc == nil {
return fmt.Errorf("missing add to scheme func")
}
if o.StatusFunc == nil {
return fmt.Errorf("missing status func")
}
if o.MessageFunc == nil {
return fmt.Errorf("missing message func")
}
if o.Category == "" {
return fmt.Errorf("missing category")
}
return nil
}
type FluxObject interface {
client.Object
GetConditions() []metav1.Condition
}
var (
HelmReleaseObjectKind = ObjectKind{
Gvk: helmv2beta1.GroupVersion.WithKind(helmv2beta1.HelmReleaseKind),
NewClientObjectFunc: func() client.Object {
return &helmv2beta1.HelmRelease{}
},
AddToSchemeFunc: helmv2beta1.AddToScheme,
StatusFunc: defaultFluxObjectStatusFunc,
MessageFunc: defaultFluxObjectMessageFunc,
Category: CategoryAutomation,
}
KustomizationObjectKind = ObjectKind{
Gvk: kustomizev1.GroupVersion.WithKind(kustomizev1.KustomizationKind),
NewClientObjectFunc: func() client.Object {
return &kustomizev1.Kustomization{}
},
AddToSchemeFunc: kustomizev1.AddToScheme,
StatusFunc: defaultFluxObjectStatusFunc,
MessageFunc: defaultFluxObjectMessageFunc,
Category: CategoryAutomation,
}
HelmRepositoryObjectKind = ObjectKind{
Gvk: sourcev1beta2.GroupVersion.WithKind(sourcev1beta2.HelmRepositoryKind),
NewClientObjectFunc: func() client.Object {
return &sourcev1beta2.HelmRepository{}
},
AddToSchemeFunc: sourcev1.AddToScheme,
StatusFunc: defaultFluxObjectStatusFunc,
MessageFunc: defaultFluxObjectMessageFunc,
Category: CategorySource,
}
HelmChartObjectKind = ObjectKind{
Gvk: sourcev1beta2.GroupVersion.WithKind(sourcev1beta2.HelmChartKind),
NewClientObjectFunc: func() client.Object {
return &sourcev1beta2.HelmChart{}
},
AddToSchemeFunc: sourcev1.AddToScheme,
StatusFunc: defaultFluxObjectStatusFunc,
MessageFunc: defaultFluxObjectMessageFunc,
Category: CategorySource,
}
GitRepositoryObjectKind = ObjectKind{
Gvk: sourcev1.GroupVersion.WithKind(sourcev1.GitRepositoryKind),
NewClientObjectFunc: func() client.Object {
return &sourcev1.GitRepository{}
},
AddToSchemeFunc: sourcev1.AddToScheme,
StatusFunc: defaultFluxObjectStatusFunc,
MessageFunc: defaultFluxObjectMessageFunc,
Category: CategorySource,
}
OCIRepositoryObjectKind = ObjectKind{
Gvk: sourcev1beta2.GroupVersion.WithKind(sourcev1beta2.OCIRepositoryKind),
NewClientObjectFunc: func() client.Object {
return &sourcev1beta2.OCIRepository{}
},
AddToSchemeFunc: sourcev1beta2.AddToScheme,
StatusFunc: defaultFluxObjectStatusFunc,
MessageFunc: defaultFluxObjectMessageFunc,
Category: CategorySource,
}
BucketObjectKind = ObjectKind{
Gvk: sourcev1beta2.GroupVersion.WithKind(sourcev1beta2.BucketKind),
NewClientObjectFunc: func() client.Object {
return &sourcev1beta2.Bucket{}
},
AddToSchemeFunc: sourcev1beta2.AddToScheme,
StatusFunc: defaultFluxObjectStatusFunc,
MessageFunc: defaultFluxObjectMessageFunc,
Category: CategorySource,
}
RoleObjectKind = ObjectKind{
Gvk: rbacv1.SchemeGroupVersion.WithKind("Role"),
NewClientObjectFunc: func() client.Object {
return &rbacv1.Role{}
},
AddToSchemeFunc: rbacv1.AddToScheme,
Category: CategoryRBAC,
}
ClusterRoleObjectKind = ObjectKind{
Gvk: rbacv1.SchemeGroupVersion.WithKind("ClusterRole"),
NewClientObjectFunc: func() client.Object {
return &rbacv1.ClusterRole{}
},
AddToSchemeFunc: rbacv1.AddToScheme,
Category: CategoryRBAC,
}
RoleBindingObjectKind = ObjectKind{
Gvk: rbacv1.SchemeGroupVersion.WithKind("RoleBinding"),
NewClientObjectFunc: func() client.Object {
return &rbacv1.RoleBinding{}
},
AddToSchemeFunc: rbacv1.AddToScheme,
Category: CategoryRBAC,
}
ClusterRoleBindingObjectKind = ObjectKind{
Gvk: rbacv1.SchemeGroupVersion.WithKind("ClusterRoleBinding"),
NewClientObjectFunc: func() client.Object {
return &rbacv1.ClusterRoleBinding{}
},
AddToSchemeFunc: rbacv1.AddToScheme,
Category: CategoryRBAC,
}
PolicyAgentAuditEventObjectKind = ObjectKind{
Gvk: corev1.SchemeGroupVersion.WithKind("Event"),
NewClientObjectFunc: func() client.Object {
return &corev1.Event{}
},
AddToSchemeFunc: corev1.AddToScheme,
FilterFunc: func(obj client.Object) bool {
e, ok := obj.(*corev1.Event)
if !ok {
return false
}
return e.Labels["pac.weave.works/type"] == "Audit" && e.Source.Component == "policy-agent"
},
RetentionPolicy: RetentionPolicy(24 * time.Hour),
StatusFunc: func(obj client.Object) ObjectStatus {
e, ok := obj.(*corev1.Event)
if !ok {
return NoStatus
}
if e.Type == "Normal" {
return Success
}
return Failed
},
MessageFunc: func(obj client.Object) string {
e, ok := obj.(*corev1.Event)
if !ok {
return ""
}
return e.Message
},
Category: CategoryEvent,
}
GitOpsSetsObjectKind = ObjectKind{
Gvk: gitopssets.GroupVersion.WithKind("GitOpsSet"),
NewClientObjectFunc: func() client.Object {
return &gitopssets.GitOpsSet{}
},
AddToSchemeFunc: gitopssets.AddToScheme,
StatusFunc: defaultFluxObjectStatusFunc,
MessageFunc: defaultFluxObjectMessageFunc,
Category: CategoryGitopsSet,
}
GitopsTemplateObjectKind = ObjectKind{
Gvk: gapiv1.GroupVersion.WithKind(gapiv1.Kind),
NewClientObjectFunc: func() client.Object {
return &gapiv1.GitOpsTemplate{}
},
AddToSchemeFunc: gapiv1.AddToScheme,
StatusFunc: func(obj client.Object) ObjectStatus {
return NoStatus
},
MessageFunc: func(obj client.Object) string {
e, ok := obj.(*gapiv1.GitOpsTemplate)
if !ok {
return ""
}
return e.Spec.Description
},
Labels: []string{
"weave.works/template-type",
},
Category: CategoryTemplate,
HumanReadableLabelKeys: map[string]string{
"weave.works/template-type": "template-type",
},
}
CapiTemplateObjectKind = ObjectKind{
Gvk: capiv1.GroupVersion.WithKind(capiv1.Kind),
NewClientObjectFunc: func() client.Object {
return &capiv1.CAPITemplate{}
},
AddToSchemeFunc: capiv1.AddToScheme,
StatusFunc: func(obj client.Object) ObjectStatus {
return NoStatus
},
MessageFunc: func(obj client.Object) string {
e, ok := obj.(*capiv1.CAPITemplate)
if !ok {
return ""
}
return e.Spec.Description
},
Category: CategoryTemplate,
}
AutomatedClusterDiscoveryKind = ObjectKind{
Gvk: clusterreflectorv1alpha1.GroupVersion.WithKind("AutomatedClusterDiscovery"),
NewClientObjectFunc: func() client.Object {
return &clusterreflectorv1alpha1.AutomatedClusterDiscovery{}
},
AddToSchemeFunc: clusterreflectorv1alpha1.AddToScheme,
StatusFunc: defaultFluxObjectStatusFunc,
MessageFunc: defaultFluxObjectMessageFunc,
Category: CategoryClusterDiscovery,
}
)
// SupportedObjectKinds list with the default supported Object resources to query.
var SupportedObjectKinds = []ObjectKind{
HelmReleaseObjectKind,
KustomizationObjectKind,
HelmRepositoryObjectKind,
HelmChartObjectKind,
GitRepositoryObjectKind,
OCIRepositoryObjectKind,
BucketObjectKind,
PolicyAgentAuditEventObjectKind,
GitOpsSetsObjectKind,
GitopsTemplateObjectKind,
CapiTemplateObjectKind,
AutomatedClusterDiscoveryKind,
}
// SupportedRbacKinds list with the default supported RBAC resources.
var SupportedRbacKinds = []ObjectKind{
RoleObjectKind,
ClusterRoleObjectKind,
RoleBindingObjectKind,
ClusterRoleBindingObjectKind,
}
// defaultFluxObjectStatusFunc is the default status function for Flux objects.
// Flux objects all report status via the Conditions field, so we can standardize on that.
func defaultFluxObjectStatusFunc(obj client.Object) ObjectStatus {
fo, err := ToFluxObject(obj)
if err != nil {
return Failed
}
for _, c := range fo.GetConditions() {
if ObjectStatus(c.Type) == NoStatus {
return NoStatus
}
if c.Type == "Ready" || c.Type == "Available" {
if c.Status == "True" {
return Success
}
return Failed
}
}
return Failed
}
func defaultFluxObjectMessageFunc(obj client.Object) string {
fo, err := ToFluxObject(obj)
if err != nil {
return ""
}
for _, c := range fo.GetConditions() {
// Generally, the Ready message has the most useful error message
if c.Type == "Ready" || c.Type == "Available" {
return c.Message
}
}
return ""
}
type AutomatedClusterDiscoveryAdaptor struct {
client.Object
}
func (a *AutomatedClusterDiscoveryAdaptor) GetConditions() []metav1.Condition {
acd := a.Object.(*clusterreflectorv1alpha1.AutomatedClusterDiscovery)
return acd.Status.Conditions
}
func ToFluxObject(obj client.Object) (FluxObject, error) {
switch t := obj.(type) {
case *helmv2beta1.HelmRelease:
return t, nil
case *kustomizev1.Kustomization:
return t, nil
case *sourcev1beta2.HelmRepository:
return t, nil
case *sourcev1beta2.HelmChart:
return t, nil
case *sourcev1beta2.Bucket:
return t, nil
case *sourcev1.GitRepository:
return t, nil
case *sourcev1beta2.OCIRepository:
return t, nil
case *gitopssets.GitOpsSet:
return t, nil
case *clusterreflectorv1alpha1.AutomatedClusterDiscovery:
return &AutomatedClusterDiscoveryAdaptor{Object: t}, nil
}
return nil, fmt.Errorf("unknown object type: %T", obj)
}