-
Notifications
You must be signed in to change notification settings - Fork 90
/
diskrecorder.go
213 lines (190 loc) · 5.39 KB
/
diskrecorder.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
package diskrecorder
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"k8s.io/klog/v2"
"github.com/openshift/insights-operator/pkg/insights/insightsclient"
"github.com/openshift/insights-operator/pkg/record"
)
type DiskRecorder struct {
basePath string
lastRecording time.Time
}
// New diskrecorder driver
func New(path string) *DiskRecorder {
return &DiskRecorder{basePath: path}
}
const archiveExtension = ".tar.gz"
// Save the records into the archive in the directory at d.basePath
func (d *DiskRecorder) Save(records record.MemoryRecords) (record.MemoryRecords, error) {
d.lastRecording = records[0].At.UTC()
name := fmt.Sprintf("insights-%s%s", d.lastRecording.Format("2006-01-02-150405"), archiveExtension)
path := filepath.Join(d.basePath, name)
return d.SaveAtPath(records, path)
}
// SaveAtPath the records into the archive at `path`
func (d *DiskRecorder) SaveAtPath(records record.MemoryRecords, path string) (record.MemoryRecords, error) {
if !strings.HasSuffix(path, archiveExtension) {
return nil, fmt.Errorf(`path should have suffix "%v"`, archiveExtension)
}
wrote := 0
start := time.Now()
defer func() {
if wrote > 0 {
klog.Infof("Wrote %d records to disk in %s", wrote, time.Since(start).Truncate(time.Millisecond))
}
}()
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0640)
if err != nil {
if os.IsExist(err) {
klog.Errorf("Tried to copy to %s which already exists", path)
return nil, err
}
return nil, fmt.Errorf("unable to create archive: %v", err)
}
defer f.Close()
completed := make([]record.MemoryRecord, 0, len(records))
defer func() {
wrote = len(completed)
}()
klog.Infof("Writing %d records to %s", len(records), path)
gw := gzip.NewWriter(f)
tw := tar.NewWriter(gw)
for _, r := range records {
if err := tw.WriteHeader(&tar.Header{
Name: r.Name,
ModTime: r.At,
Mode: int64(os.FileMode(0640).Perm()),
Size: int64(len(r.Data)),
Typeflag: tar.TypeReg,
}); err != nil {
return nil, fmt.Errorf("unable to write tar header: %v", err)
}
if _, err := tw.Write(r.Data); err != nil {
return nil, fmt.Errorf("unable to write tar entry: %v", err)
}
completed = append(completed, r)
}
if err := tw.Close(); err != nil {
return nil, fmt.Errorf("unable to close tar writer: %v", err)
}
if err := gw.Close(); err != nil {
return nil, fmt.Errorf("unable to close gzip writer: %v", err)
}
if err := f.Close(); err != nil {
return nil, fmt.Errorf("unable to close file: %v", err)
}
return completed, nil
}
// Prune the archives older than given time
func (d *DiskRecorder) Prune(olderThan time.Time) error {
files, err := os.ReadDir(d.basePath)
if err != nil {
return err
}
count := 0
var errors []string
for _, file := range files {
fileInfo, err := file.Info()
if err != nil {
continue
}
if isNotArchiveFile(fileInfo) {
continue
}
if fileInfo.ModTime().After(olderThan) {
continue
}
if err := os.Remove(filepath.Join(d.basePath, file.Name())); err != nil {
errors = append(errors, err.Error())
continue
}
count++
}
if len(errors) == 1 {
return fmt.Errorf("failed to delete expired file: %v", errors[0])
}
if len(errors) > 1 {
return fmt.Errorf("failed to delete %d expired files: %v", len(errors), errors[0])
}
if count > 0 {
klog.Infof("Deleted %d files older than %s", count, olderThan.UTC().Format(time.RFC3339))
}
return nil
}
// Summary implements summarizer interface to insights uploader
func (d *DiskRecorder) Summary(_ context.Context, since time.Time) (*insightsclient.Source, bool, error) {
files, err := os.ReadDir(d.basePath)
if err != nil {
return nil, false, err
}
if len(files) == 0 {
return nil, false, nil
}
recentFiles := make([]string, 0, len(files))
var fileInfo fs.FileInfo
for _, file := range files {
fileInfo, err = file.Info()
if err != nil {
return nil, false, err
}
if isNotArchiveFile(fileInfo) {
continue
}
if fileInfo.ModTime().Before(since) {
continue
}
recentFiles = append(recentFiles, file.Name())
}
if len(recentFiles) == 0 {
return nil, false, nil
}
lastFile := recentFiles[len(recentFiles)-1]
klog.Infof("Found files to send: %v", lastFile)
f, err := os.Open(filepath.Join(d.basePath, lastFile))
if err != nil {
return nil, false, nil
}
return &insightsclient.Source{Contents: f, CreationTime: d.lastRecording}, true, nil
}
func isNotArchiveFile(file os.FileInfo) bool {
return file.IsDir() || !strings.HasPrefix(file.Name(), "insights-") || !strings.HasSuffix(file.Name(), ".tar.gz")
}
// LastArchive tries to find the latest Insights archive. Returns an error
// when it can't read the base directory or when it can't open the last archive found.
func (d *DiskRecorder) LastArchive() (*insightsclient.Source, error) {
files, err := os.ReadDir(d.basePath)
if err != nil {
return nil, err
}
if len(files) == 0 {
return nil, nil
}
var lastTime time.Time
var lastArchive string
for _, file := range files {
fileInfo, err := file.Info() // nolint: govet
if err != nil {
return nil, err
}
if isNotArchiveFile(fileInfo) {
continue
}
if fileInfo.ModTime().After(lastTime) {
lastTime = fileInfo.ModTime()
lastArchive = file.Name()
}
}
f, err := os.Open(filepath.Join(d.basePath, lastArchive))
if err != nil {
return nil, err
}
return &insightsclient.Source{Contents: f, CreationTime: d.lastRecording}, nil
}