Skip to content

Instantly share code, notes, and snippets.

@smothiki
Last active March 9, 2016 23:15
Show Gist options
  • Save smothiki/cfdd9a4def87eb25dfb2 to your computer and use it in GitHub Desktop.
Save smothiki/cfdd9a4def87eb25dfb2 to your computer and use it in GitHub Desktop.
// 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 (
"fmt"
"log"
"os"
"k8s.io/kubernetes/pkg/api"
"github.com/deis/builder/pkg/k8s"
)
const (
dotGitSuffix = ".git"
)
// 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 messages to output a human readable description of what happened.
func Run(gitHome string, nsLister k8s.NamespaceWatcher) error {
fmt.Println("here1.1")
watcher, err := nsLister.Watch(nil, nil, "")
if err != nil {
fmt.Println("here1.1")
log.Printf("unable to get watch events (%s)", err)
}
fmt.Println("here1.2")
for {
fmt.Println("here1.3")
event := <-watcher.ResultChan()
fmt.Println(event)
if event.Type == "DELETED" {
namespace := event.Object.(*api.Namespace)
appToDelete := gitHome + "/" + namespace.ObjectMeta.Name + dotGitSuffix
if err := os.RemoveAll(appToDelete); err != nil {
log.Printf("Cleaner error removing deleted app %s (%s)", appToDelete, err)
}
}
}
}
package cleaner
import (
"fmt"
"sync"
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/watch"
)
type namespace struct {
}
// Watch returns a watch.Interface that watches the requested namespaces.
func (r *namespace) Watch(label labels.Selector, field fields.Selector, resourceversion string) (watch.Interface, error) {
nst := watch.NewFake()
nst.Add(&api.Namespace{ObjectMeta: api.ObjectMeta{Name: "dir1"}})
nst.Modify(&api.Namespace{ObjectMeta: api.ObjectMeta{Name: "dir1"}})
nst.Delete(&api.Namespace{ObjectMeta: api.ObjectMeta{Name: "dir1"}})
nst.Stop()
return nst, nil
}
func (r *namespace) IsAnAPIObject() {}
func TestRun(t *testing.T) {
ns := &namespace{}
// dirhome, _ := os.Getwd()
dirhome := "/Users/smothiki/deisworld/src/github.com/deis/builder"
dirhome = dirhome + "/testdata"
fmt.Println("here1")
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
Run(dirhome, ns)
wg.Done()
}()
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment