-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcleaner.go
More file actions
96 lines (82 loc) · 2.68 KB
/
cleaner.go
File metadata and controls
96 lines (82 loc) · 2.68 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
91
92
93
94
95
96
// 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/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
}
// getDisjunction gets the items that are in namespaceList and not in dirs or vice versa
func getDisjunction(namespaceList []api.Namespace, dirs []string) []string {
var ret []string
namespacesSet := make(map[string]struct{})
dirsSet := make(map[string]struct{})
// create sets of the namespaces and dirs
for _, ns := range namespaceList {
lowerName := strings.ToLower(ns.Name)
namespacesSet[lowerName] = struct{}{}
}
for _, dir := range dirs {
lowerName := strings.ToLower(dir)
dirsSet[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)
}
}
// get namespaces not in the dirs set
for _, ns := range namespaceList {
lowerName := strings.ToLower(ns.Name)
if _, ok := dirsSet[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.
// TODO: locking mechanism on repositories. Nobody should be able to push to a repo while one is being deleted
func Run(gitHome string, nsLister k8s.NamespaceLister, 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)
}
disjunctions := getDisjunction(nsList.Items, gitDirs)
for _, disj := range disjunctions {
if err := os.RemoveAll(disj); err != nil {
log.Debug("Cleaner error removing deleted app %s (%s)", disj, err)
}
}
time.Sleep(pollSleepDuration)
}
}