Skip to content

Commit 36a5d5b

Browse files
committed
run-resourcewatch: Fix delete of non-observed resource
1 parent abb8cb3 commit 36a5d5b

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

pkg/resourcewatch/storage/git_store.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"path"
78
"path/filepath"
89
"strings"
910
"time"
@@ -81,6 +82,21 @@ func (s *GitStorage) handle(gvr schema.GroupVersionResource, oldObj, obj *unstru
8182
klog.Infof("Calling commitRemove for %s", filePath)
8283
// ignore error, we've already reported and we're not doing anything else.
8384
pollErr := wait.PollImmediate(1*time.Second, 15*time.Second, func() (bool, error) {
85+
86+
err := os.Remove(path.Join(s.path, filePath))
87+
if err != nil {
88+
// If the file doesn't exist it means we're deleting a file we haven't previously observed.
89+
// That's probably a collection bug.
90+
// Add it first before removing it.
91+
if os.IsNotExist(err) {
92+
klog.Info("Observed delete of file we haven't previously observed. Adding it first.")
93+
s.handle(gvr, nil, obj, false)
94+
s.handle(gvr, nil, obj, true)
95+
return true, nil
96+
}
97+
klog.Errorf("Error removing %q: %v", filePath, err)
98+
return false, err
99+
}
84100
if err := s.commitRemove(filePath, "unknown", ocCommand); err != nil {
85101
klog.Error(err)
86102
return false, nil
@@ -288,7 +304,8 @@ func (s *GitStorage) commitModify(path, author, ocCommand string) error {
288304
func (s *GitStorage) commitRemove(path, author, ocCommand string) error {
289305
authorString := fmt.Sprintf("%s <[email protected]>", author)
290306
commitMessage := fmt.Sprintf("removed %s", ocCommand)
291-
command := fmt.Sprintf(`rm %q && git rm %q && git commit --author=%q -m %q`, path, path, authorString, commitMessage)
307+
308+
command := fmt.Sprintf(`git rm %q && git commit --author=%q -m %q`, path, authorString, commitMessage)
292309

293310
osCommand := exec.Command("bash", "-e", "-c", command)
294311
osCommand.Dir = s.path

0 commit comments

Comments
 (0)