-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcleaner.go
More file actions
90 lines (78 loc) · 2.64 KB
/
cleaner.go
File metadata and controls
90 lines (78 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Package cleaner is a background process that compares the kubernetes namespace list with the folders in the local git home directory, deleting what's not in the namespace list
package cleaner
import (
"os"
"path/filepath"
"strings"
"time"
"github.com/deis/builder/pkg/k8s"
"github.com/deis/builder/pkg/sshd"
"github.com/deis/pkg/log"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
)
func localDirs(gitHome string) ([]string, error) {
var ret []string
err := filepath.Walk(gitHome, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return filepath.SkipDir
}
ret = append(ret, filepath.Join(gitHome, path))
return nil
})
if err != nil {
return nil, err
}
return ret, nil
}
// getDiff gets the directories that are not in namespaceList
func getDiff(namespaceList []api.Namespace, dirs []string) []string {
var ret []string
// create a set of lowercase namespace names
namespacesSet := make(map[string]struct{})
for _, ns := range namespaceList {
lowerName := strings.ToLower(ns.Name)
namespacesSet[lowerName] = struct{}{}
}
// get dirs not in the namespaces set
for _, dir := range dirs {
lowerName := strings.ToLower(dir)
if _, ok := namespacesSet[lowerName]; !ok {
ret = append(ret, lowerName)
}
}
return ret
}
// 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.
func Run(gitHome string, nsLister k8s.NamespaceLister, repoLock sshd.RepositoryLock, pollSleepDuration time.Duration) error {
for {
nsList, err := nsLister.List(labels.Everything(), fields.Everything())
if err != nil {
log.Debug("Cleaner error listing namespaces (%s)", err)
continue
}
gitDirs, err := localDirs(gitHome)
if err != nil {
log.Debug("Cleaner error listing local git directories (%s)", err)
}
dirsToDelete := getDiff(nsList.Items, gitDirs)
for _, dirToDelete := range dirsToDelete {
if err := repoLock.Lock(dirToDelete, time.Duration(0)); err != nil {
log.Debug("Cleaner error locking repository %s for deletion (%s)", dirToDelete, err)
continue
}
if err := os.RemoveAll(dirToDelete); err != nil {
log.Debug("Cleaner error removing deleted app %s (%s)", dirToDelete, err)
}
if err := repoLock.Unlock(dirToDelete, time.Duration(0)); err != nil {
log.Debug("Cleaner error unlocking repository %s for deletion (%s)", dirToDelete, err)
continue
}
}
time.Sleep(pollSleepDuration)
}
}