Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lib/billing_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ limitations under the License.
package lib

import (
"context"

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"
Expand All @@ -28,6 +31,9 @@ import (
type BillingEventCreator struct {
Mapper discovery.ResourceMapper
ClusterMetadata *kmapi.ClusterMetadata
ClientBilling bool
PodLister client.Reader
PVCLister client.Reader
}

func (p *BillingEventCreator) CreateEvent(obj client.Object) (*api.Event, error) {
Expand All @@ -41,8 +47,66 @@ func (p *BillingEventCreator) CreateEvent(obj client.Object) (*api.Event, error)
return nil, err
}

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{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need pod uid to track auto scaling pods.

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need uid to track pvc with same name.

Copy link
Member

@ArnobKumarSaha ArnobKumarSaha Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never do pvc deletion from operators. If someone delete it manually, thats seperate thing. So, may be only pod uid is enough

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
}
136 changes: 136 additions & 0 deletions lib/lister.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
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"
"reflect"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
corelisters "k8s.io/client-go/listers/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type PodReader struct {
delegate corelisters.PodLister
}

var _ client.Reader = &PodReader{}

func NewPodReader(delegate corelisters.PodLister) PodReader {
return PodReader{delegate: delegate}
}

func (r PodReader) Get(ctx context.Context, key client.ObjectKey, obj client.Object, _ ...client.GetOption) error {
pod, err := r.delegate.Pods(key.Namespace).Get(key.Name)
if err != nil {
return err
}
assign(obj, pod)
return nil
}

func (r PodReader) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
var o client.ListOptions
o.ApplyOptions(opts)

var ls labels.Selector
if o.LabelSelector != nil {
ls = o.LabelSelector
} else if o.Raw != nil && o.Raw.LabelSelector != "" {
var err error
ls, err = labels.Parse(o.Raw.LabelSelector)
if err != nil {
return err
}
}

pods, err := r.delegate.Pods(o.Namespace).List(ls)
if err != nil {
return err
}

podList := core.PodList{
Items: make([]core.Pod, 0, len(pods)),
}
for _, pod := range pods {
podList.Items = append(podList.Items, *pod)
}
assign(list, podList)

return nil
}

type PersistentVolumeClaimReader struct {
delegate corelisters.PersistentVolumeClaimLister
}

var _ client.Reader = &PersistentVolumeClaimReader{}

func NewPersistentVolumeClaimReader(delegate corelisters.PersistentVolumeClaimLister) PersistentVolumeClaimReader {
return PersistentVolumeClaimReader{delegate: delegate}
}

func (r PersistentVolumeClaimReader) Get(ctx context.Context, key client.ObjectKey, obj client.Object, _ ...client.GetOption) error {
pod, err := r.delegate.PersistentVolumeClaims(key.Namespace).Get(key.Name)
if err != nil {
return err
}
assign(obj, pod)

return nil
}

func (r PersistentVolumeClaimReader) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
var o client.ListOptions
o.ApplyOptions(opts)

var ls labels.Selector
if o.LabelSelector != nil {
ls = o.LabelSelector
} else if o.Raw != nil && o.Raw.LabelSelector != "" {
var err error
ls, err = labels.Parse(o.Raw.LabelSelector)
if err != nil {
return err
}
}

pvcs, err := r.delegate.PersistentVolumeClaims(o.Namespace).List(ls)
if err != nil {
return err
}

pvcList := core.PersistentVolumeClaimList{
Items: make([]core.PersistentVolumeClaim, 0, len(pvcs)),
}
for _, pvc := range pvcs {
pvcList.Items = append(pvcList.Items, *pvc)
}
assign(list, pvcList)

return nil
}

func assign(target, src any) {
srcValue := reflect.ValueOf(src)
if srcValue.Kind() == reflect.Pointer {
srcValue = srcValue.Elem()
}
reflect.ValueOf(target).Elem().Set(srcValue)
}
Loading