|
4 | 4 | "fmt" |
5 | 5 | "os" |
6 | 6 | "os/exec" |
| 7 | + "path" |
7 | 8 | "path/filepath" |
8 | 9 | "strings" |
9 | 10 | "time" |
@@ -81,6 +82,21 @@ func (s *GitStorage) handle(gvr schema.GroupVersionResource, oldObj, obj *unstru |
81 | 82 | klog.Infof("Calling commitRemove for %s", filePath) |
82 | 83 | // ignore error, we've already reported and we're not doing anything else. |
83 | 84 | 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 | + } |
84 | 100 | if err := s.commitRemove(filePath, "unknown", ocCommand); err != nil { |
85 | 101 | klog.Error(err) |
86 | 102 | return false, nil |
@@ -288,7 +304,8 @@ func (s *GitStorage) commitModify(path, author, ocCommand string) error { |
288 | 304 | func (s *GitStorage) commitRemove(path, author, ocCommand string) error { |
289 | 305 | authorString := fmt. Sprintf( "%s <[email protected]>", author) |
290 | 306 | 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) |
292 | 309 |
|
293 | 310 | osCommand := exec.Command("bash", "-e", "-c", command) |
294 | 311 | osCommand.Dir = s.path |
|
0 commit comments