Skip to content

Commit da11db0

Browse files
committed
run-resourcewatch: Disable git automatic gc
By default, any git command can kick off a gc process which will continue to run after the original command completes. If this happens, all subsequent git commands will fail until the gc completes.
1 parent 1e509ff commit da11db0

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

pkg/resourcewatch/git/git_store.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func NewGitStorage(path string) (*GitStorage, error) {
5656
return storage, nil
5757
}
5858

59+
func (s *GitStorage) GC() error {
60+
return s.execGit("gc")
61+
}
62+
5963
// handle handles different operations on git
6064
func (s *GitStorage) handle(timestamp time.Time, gvr schema.GroupVersionResource, oldObj, obj *unstructured.Unstructured, delete bool) {
6165
filePath, content, err := decodeUnstructuredObject(gvr, obj)
@@ -102,7 +106,7 @@ func (s *GitStorage) handle(timestamp time.Time, gvr schema.GroupVersionResource
102106
klog.Infof("Calling write for %s", filePath)
103107
operation, err := s.write(filePath, content)
104108
if err != nil {
105-
klog.Warningf("Writing file content failed %q: %v", filePath, err)
109+
klog.Errorf("Writing file content failed %q: %v", filePath, err)
106110
return
107111
}
108112

@@ -219,12 +223,15 @@ func resourceFilename(gvr schema.GroupVersionResource, namespace, name string) s
219223
return filepath.Join("namespaces", namespace, groupStr, gvr.Resource, name+".yaml")
220224
}
221225

222-
func (s *GitStorage) exec(command string, args ...string) error {
223-
osCommand := exec.Command(command, args...)
226+
func (s *GitStorage) execGit(args ...string) error {
227+
// Disable automatic garbage collection to avoid racing with other processes.
228+
args = append([]string{"-c", "gc.auto=0"}, args...)
229+
230+
osCommand := exec.Command("git", args...)
224231
osCommand.Dir = s.path
225232
output, err := osCommand.CombinedOutput()
226233
if err != nil {
227-
klog.Errorf("Ran %s %v\n%v\n\n", command, args, string(output))
234+
klog.Errorf("Ran git %v\n%v\n\n", args, string(output))
228235
return err
229236
}
230237
return nil
@@ -234,11 +241,11 @@ func (s *GitStorage) commit(timestamp time.Time, path, author, commitMessage str
234241
authorString := fmt.Sprintf("%s <[email protected]>", author)
235242
dateString := timestamp.Format(time.RFC3339)
236243

237-
return s.exec("git", "commit", "--author", authorString, "--date", dateString, "-m", commitMessage)
244+
return s.execGit("commit", "--author", authorString, "--date", dateString, "-m", commitMessage)
238245
}
239246

240247
func (s *GitStorage) commitAdd(timestamp time.Time, path, author, ocCommand string) error {
241-
if err := s.exec("git", "add", path); err != nil {
248+
if err := s.execGit("add", path); err != nil {
242249
return err
243250
}
244251

@@ -252,7 +259,7 @@ func (s *GitStorage) commitAdd(timestamp time.Time, path, author, ocCommand stri
252259
}
253260

254261
func (s *GitStorage) commitModify(timestamp time.Time, path, author, ocCommand string) error {
255-
if err := s.exec("git", "add", path); err != nil {
262+
if err := s.execGit("add", path); err != nil {
256263
return err
257264
}
258265

@@ -266,7 +273,7 @@ func (s *GitStorage) commitModify(timestamp time.Time, path, author, ocCommand s
266273
}
267274

268275
func (s *GitStorage) commitRemove(timestamp time.Time, path, author, ocCommand string) error {
269-
if err := s.exec("git", "rm", path); err != nil {
276+
if err := s.execGit("rm", path); err != nil {
270277
return err
271278
}
272279

pkg/resourcewatch/git/sink.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ func Sink(log logr.Logger) (observe.ObservationSink, error) {
2323
for observation := range resourceC {
2424
gitWrite(gitStorage, observation)
2525
}
26+
27+
// We disable GC while we're writing, so run it after we're done.
28+
if err := gitStorage.GC(); err != nil {
29+
log.Error(err, "Failed to run git gc")
30+
}
2631
}()
2732
return finished
2833
}, nil

0 commit comments

Comments
 (0)