-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcleaner.go
More file actions
40 lines (34 loc) · 1.31 KB
/
cleaner.go
File metadata and controls
40 lines (34 loc) · 1.31 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
// 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 (
"log"
"k8s.io/kubernetes/pkg/api"
"github.com/deis/builder/pkg/k8s"
"github.com/deis/builder/pkg/sys"
)
const (
dotGitSuffix = ".git"
)
// Run starts the deleted app cleaner. This function listens to the Kubernetes event stream for events that indicate namespaces that were `DELETED`.
// If the namespace name matches a folder on the local filesystem, this func deletes that folder.
// Note that this function blocks until the watcher returned by `nsLister.Watch` is closed, so you should launch it in a goroutine.
func Run(gitHome string, nsLister k8s.NamespaceWatcher, fs sys.FS) error {
watcher, err := nsLister.Watch(nil, nil, "")
if err != nil {
log.Printf("unable to get watch events (%s)", err)
}
for {
event := <-watcher.ResultChan()
if event.Type == "DELETED" {
switch event.Object.(type) {
case (*api.Namespace):
namespace := event.Object.(*api.Namespace)
appToDelete := gitHome + "/" + namespace.ObjectMeta.Name + dotGitSuffix
if err := fs.RemoveAll(appToDelete); err != nil {
log.Printf("Cleaner error removing deleted app %s (%s)", appToDelete, err)
}
default:
}
}
}
}