-
Notifications
You must be signed in to change notification settings - Fork 5
/
data.go
96 lines (84 loc) · 2.3 KB
/
data.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
package data
import (
"context"
"encoding/json"
"io/fs"
"os"
"path/filepath"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
)
func isLessThan24hrs(t time.Time) bool {
return time.Since(t) < 24*time.Hour
}
func deleteOlderFile(fsWrapper afero.Fs, filePath string) (err error) {
err = fsWrapper.Remove(filePath)
if err != nil {
return
}
return
}
func readDataFromFile(fsWrapper afero.Fs, fileName string) (payload FalcoOutput, err error) {
data, err := afero.ReadFile(fsWrapper, fileName)
if err != nil {
return
}
err = json.Unmarshal(data, &payload)
if err != nil {
return
}
return
}
// Aggregate24hrsData return aggregated report for the past 24 hours
func Aggregate24hrsData(fsWrapper afero.Fs, dir string) (aggregatedData []FalcoOutput, err error) {
tmpfiles, err := os.ReadDir(dir)
if err != nil {
return
}
var fileStat fs.FileInfo
for _, file := range tmpfiles {
if file.Type().Perm().IsRegular() {
filename := filepath.Join(dir, file.Name())
fileStat, err = fsWrapper.Stat(filename)
if err != nil {
return
}
if isLessThan24hrs(fileStat.ModTime()) {
var output FalcoOutput
output, err = readDataFromFile(fsWrapper, filename)
// skip malformed files and continue
if err != nil {
logrus.Warnf("Error reading file: %v", err)
continue
}
aggregatedData = append(aggregatedData, output)
} else {
err = deleteOlderFile(fsWrapper, filename)
if err != nil {
return
}
}
}
}
return
}
// GetPodByPodName returns pod from the namespace and name provided.
func GetPodByPodName(ctx context.Context, dynamicClient dynamic.Interface, restMapper meta.RESTMapper, namespace, podname string) (*unstructured.Unstructured, error) {
fqKind := schema.FromAPIVersionAndKind("v1", "Pod")
mapping, err := restMapper.RESTMapping(fqKind.GroupKind(), fqKind.Version)
if err != nil {
logrus.Error(err, "Error retrieving mapping", "v1", "Pod")
return nil, err
}
pod, err := dynamicClient.Resource(mapping.Resource).Namespace(namespace).Get(ctx, podname, metav1.GetOptions{})
if err != nil {
return nil, err
}
return pod, nil
}