Skip to content

Commit

Permalink
Merge pull request #7022 from markturansky/ns_controller_indempotent
Browse files Browse the repository at this point in the history
Made NamespaceManager.Run idempotent and added graceful shutdown
  • Loading branch information
derekwaynecarr committed Apr 21, 2015
2 parents b944049 + 13a38f3 commit 74d6f30
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pkg/namespace/namespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,24 @@ func NewNamespaceManager(kubeClient client.Interface, resyncPeriod time.Duration
)

return &NamespaceManager{
controller: controller,
StopEverything: make(chan struct{}),
controller: controller,
}
}

// Run begins observing the system. It starts a goroutine and returns immediately.
func (nm *NamespaceManager) Run() {
go nm.controller.Run(nm.StopEverything)
if nm.StopEverything == nil {
nm.StopEverything = make(chan struct{})
go nm.controller.Run(nm.StopEverything)
}
}

// Stop gracefully shutsdown this controller
func (nm *NamespaceManager) Stop() {
if nm.StopEverything != nil {
close(nm.StopEverything)
nm.StopEverything = nil
}
}

// finalized returns true if the spec.finalizers is empty list
Expand Down
24 changes: 24 additions & 0 deletions pkg/namespace/namespace_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package namespace

import (
"testing"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
Expand Down Expand Up @@ -131,3 +133,25 @@ func TestSyncNamespaceThatIsActive(t *testing.T) {
t.Errorf("Expected no action from controller, but got: %v", actionSet)
}
}

func TestRunStop(t *testing.T) {
o := testclient.NewObjects(api.Scheme)
client := &testclient.Fake{ReactFn: testclient.ObjectReaction(o, latest.RESTMapper)}
nsMgr := NewNamespaceManager(client, 1*time.Second)

if nsMgr.StopEverything != nil {
t.Errorf("Non-running manager should not have a stop channel. Got %v", nsMgr.StopEverything)
}

nsMgr.Run()

if nsMgr.StopEverything == nil {
t.Errorf("Running manager should have a stop channel. Got nil")
}

nsMgr.Stop()

if nsMgr.StopEverything != nil {
t.Errorf("Non-running manager should not have a stop channel. Got %v", nsMgr.StopEverything)
}
}

0 comments on commit 74d6f30

Please sign in to comment.