22package cleaner
33
44import (
5+ "io/ioutil"
56 "os"
67 "path/filepath"
78 "strings"
@@ -15,22 +16,23 @@ import (
1516 "k8s.io/kubernetes/pkg/labels"
1617)
1718
18- func localDirs (gitHome string ) ([]string , error ) {
19- var ret []string
20- err := filepath .Walk (gitHome , func (path string , info os.FileInfo , err error ) error {
21- if err != nil {
22- return err
23- }
24- if info .IsDir () {
25- return filepath .SkipDir
26- }
27- ret = append (ret , filepath .Join (gitHome , path ))
28- return nil
29- })
19+ const (
20+ dotGitSuffix = ".git"
21+ )
3022
23+ func localDirs (gitHome string ) ([]string , error ) {
24+ fileInfos , err := ioutil .ReadDir (gitHome )
3125 if err != nil {
3226 return nil , err
3327 }
28+ var ret []string
29+ for _ , fileInfo := range fileInfos {
30+ nm := fileInfo .Name ()
31+ if len (nm ) <= 0 || nm == "." || ! fileInfo .IsDir () {
32+ continue
33+ }
34+ ret = append (ret , filepath .Join (gitHome , nm ))
35+ }
3436 return ret , nil
3537}
3638
@@ -56,20 +58,42 @@ func getDiff(namespaceList []api.Namespace, dirs []string) []string {
5658 return ret
5759}
5860
61+ func stripSuffixes (strs []string , suffix string ) []string {
62+ ret := make ([]string , len (strs ))
63+ for i , str := range strs {
64+ idx := strings .LastIndex (str , suffix )
65+ if idx >= 0 {
66+ ret [i ] = str [:idx ]
67+ }
68+ }
69+ return ret
70+ }
71+
5972// Run starts the deleted app cleaner. Every pollSleepDuration, it compares the result of nsLister.List with the directories in the top level of gitHome on the local file system. On any error, it uses log.Debug to output a human readable description of what happened.
6073func Run (gitHome string , nsLister k8s.NamespaceLister , repoLock sshd.RepositoryLock , pollSleepDuration time.Duration ) error {
6174 for {
6275 nsList , err := nsLister .List (labels .Everything (), fields .Everything ())
6376 if err != nil {
6477 log .Err ("Cleaner error listing namespaces (%s)" , err )
6578 continue
79+ } else {
80+ lst := make ([]string , len (nsList .Items ))
81+ for i , ns := range nsList .Items {
82+ lst [i ] = strings .ToLower (ns .Name )
83+ }
84+ log .Debug ("Cleaner found namespaces %s" , lst )
6685 }
6786
6887 gitDirs , err := localDirs (gitHome )
6988 if err != nil {
7089 log .Err ("Cleaner error listing local git directories (%s)" , err )
90+ continue
91+ } else {
92+ log .Debug ("Cleaner found local git directories in %s: %s" , gitHome , gitDirs )
7193 }
7294
95+ gitDirs = stripSuffixes (gitDirs , dotGitSuffix )
96+
7397 dirsToDelete := getDiff (nsList .Items , gitDirs )
7498 if len (dirsToDelete ) > 0 {
7599 log .Debug ("Cleaner found the following git directories to delete: %s" , dirsToDelete )
0 commit comments