-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbilling_event.go
More file actions
139 lines (123 loc) · 4.24 KB
/
billing_event.go
File metadata and controls
139 lines (123 loc) · 4.24 KB
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
/*
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lib
import (
"context"
"strings"
api "go.bytebuilders.dev/audit/api/v1"
core "k8s.io/api/core/v1"
kmapi "kmodules.xyz/client-go/api/v1"
"kmodules.xyz/client-go/discovery"
corev1alpha1 "kmodules.xyz/resource-metadata/apis/core/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type BillingEventCreator struct {
Mapper discovery.ResourceMapper
ClusterMetadata *kmapi.ClusterMetadata
ClientBilling bool
NamespaceLister client.Reader
PodLister client.Reader
PVCLister client.Reader
}
func (p *BillingEventCreator) CreateEvent(obj client.Object) (*api.Event, error) {
rid, err := p.Mapper.ResourceIDForGVK(obj.GetObjectKind().GroupVersionKind())
if err != nil {
return nil, err
}
res, err := corev1alpha1.ToGenericResource(obj, rid, p.ClusterMetadata)
if err != nil {
return nil, err
}
if p.NamespaceLister != nil {
var ns core.Namespace
err = p.NamespaceLister.Get(context.TODO(), client.ObjectKey{Name: obj.GetNamespace()}, &ns)
if err != nil {
return nil, err
}
res.Spec.Namespace = &corev1alpha1.NamespaceInfo{
UID: ns.UID,
Name: ns.Name,
CreationTimestamp: ns.CreationTimestamp,
}
res.Spec.Namespace.SkipBilling = ns.Annotations[kmapi.AceSkipBillingKey] == "true"
if ns.Labels[kmapi.ClientOrgKey] == "true" {
res.Spec.Namespace.AceOrgID = ns.Annotations[kmapi.AceOrgIDKey]
orgMetadata := map[string]string{}
for k, v := range ns.Annotations {
if after, found := strings.CutPrefix(k, kmapi.ClientKeyPrefix); found {
orgMetadata[after] = v
}
}
res.Spec.Namespace.AceOrgMetadata = orgMetadata
}
}
if p.ClientBilling {
if r, ok := obj.(Resource); ok {
var podList core.PodList
err = p.PodLister.List(context.TODO(), &podList, client.InNamespace(obj.GetNamespace()), client.MatchingLabels(r.OffshootSelectors()))
if err != nil {
return nil, err
}
podresources := make([]corev1alpha1.ComputeResource, 0, len(podList.Items))
for _, pod := range podList.Items {
pr := corev1alpha1.ComputeResource{
UID: pod.UID,
Name: pod.Name,
CreationTimestamp: pod.CreationTimestamp,
Containers: make([]corev1alpha1.ContainerResource, 0, len(pod.Spec.Containers)),
InitContainers: make([]corev1alpha1.ContainerResource, 0, len(pod.Spec.Containers)),
}
for _, c := range pod.Spec.Containers {
pr.Containers = append(pr.Containers, corev1alpha1.ContainerResource{
Name: c.Name,
Resource: c.Resources,
RestartPolicy: c.RestartPolicy,
})
}
for _, c := range pod.Spec.InitContainers {
pr.InitContainers = append(pr.InitContainers, corev1alpha1.ContainerResource{
Name: c.Name,
Resource: c.Resources,
RestartPolicy: c.RestartPolicy,
})
}
podresources = append(podresources, pr)
}
res.Spec.Pods = podresources
var pvcList core.PersistentVolumeClaimList
err = p.PVCLister.List(context.TODO(), &pvcList, client.InNamespace(obj.GetNamespace()), client.MatchingLabels(r.OffshootSelectors()))
if err != nil {
return nil, err
}
pvcresources := make([]corev1alpha1.StorageResource, 0, len(podList.Items))
for _, pvc := range pvcList.Items {
if pvc.Status.Phase == core.ClaimBound {
pvcresources = append(pvcresources, corev1alpha1.StorageResource{
UID: pvc.UID,
Name: pvc.Name,
CreationTimestamp: pvc.CreationTimestamp,
Resources: pvc.Spec.Resources,
})
}
}
res.Spec.Storage = pvcresources
}
}
return &api.Event{
Resource: res,
ResourceID: *rid,
}, nil
}
type Resource interface {
OffshootSelectors() map[string]string
}