Skip to content

Commit bfd7ba5

Browse files
author
Aaron Schlesinger
committed
fix(pkg/cleaner): fix localDirs and strip git suffixes
1 parent d1e300e commit bfd7ba5

2 files changed

Lines changed: 77 additions & 17 deletions

File tree

pkg/cleaner/cleaner.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package cleaner
33

44
import (
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.
6073
func 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)

pkg/cleaner/cleaner_test.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,46 @@ func TestLocalDirs(t *testing.T) {
2626
pkgDir, err := filepath.Abs(wd + "/..")
2727
assert.NoErr(t, err)
2828
lDirs, err := localDirs(pkgDir)
29-
for _, dir := range lDirs {
30-
rel, err := filepath.Rel(pkgDir, dir)
31-
assert.NoErr(t, err)
32-
spl := strings.Split(rel, "/")
33-
assert.Equal(t, len(spl), 1, "directory depth")
29+
assert.NoErr(t, err)
30+
31+
expectedPackages := map[string]int{
32+
pkgDir + "/cleaner": 1,
33+
pkgDir + "/conf": 1,
34+
pkgDir + "/controller": 1,
35+
pkgDir + "/env": 1,
36+
pkgDir + "/git": 1,
37+
pkgDir + "/gitreceive": 1,
38+
pkgDir + "/healthsrv": 1,
39+
pkgDir + "/k8s": 1,
40+
pkgDir + "/sshd": 1,
41+
}
42+
43+
actualPackages := map[string]int{}
44+
for _, lDir := range lDirs {
45+
actualPackages[lDir]++
46+
}
47+
assert.Equal(t, len(actualPackages), len(expectedPackages), "number of packages")
48+
for actualPackageName, actualNum := range actualPackages {
49+
if actualNum != 1 {
50+
t.Errorf("found %d %s packages", actualNum, actualPackageName)
51+
continue
52+
}
53+
expectedNum, ok := expectedPackages[actualPackageName]
54+
if !ok {
55+
t.Errorf("found unexpected package %s", actualPackageName)
56+
continue
57+
}
58+
if actualNum != expectedNum {
59+
t.Errorf("found %d %s packages, expected %d", actualNum, actualPackageName, expectedNum)
60+
continue
61+
}
62+
}
63+
}
64+
65+
func TestStripSuffixes(t *testing.T) {
66+
strs := []string{"a.git", "b.git", "c.git"}
67+
newStrs := stripSuffixes(strs, dotGitSuffix)
68+
for _, str := range newStrs {
69+
assert.False(t, strings.HasSuffix(str, dotGitSuffix), "string %s has suffix %s", str, dotGitSuffix)
3470
}
3571
}

0 commit comments

Comments
 (0)