Remove OnceAndForever util, create a Run() method on MinionController.

This commit is contained in:
Deyuan Deng 2014-10-14 18:45:09 -04:00
parent ec46e94dc2
commit 2bd88d4544
4 changed files with 24 additions and 35 deletions

View File

@ -18,10 +18,12 @@ package controller
import (
"fmt"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
)
@ -30,16 +32,30 @@ type MinionController struct {
matchRE string
staticResources *api.NodeResources
registry minion.Registry
period time.Duration
}
// NewMinionController returns a new minion controller to sync instances from cloudprovider.
func NewMinionController(cloud cloudprovider.Interface, matchRE string, staticResources *api.NodeResources, registry minion.Registry) (*MinionController, error) {
func NewMinionController(
cloud cloudprovider.Interface,
matchRE string,
staticResources *api.NodeResources,
registry minion.Registry,
period time.Duration) *MinionController {
return &MinionController{
cloud: cloud,
matchRE: matchRE,
staticResources: staticResources,
registry: registry,
}, nil
period: period,
}
}
// Run starts syncing instances from cloudprovider periodically.
func (s *MinionController) Run() {
// Call Sync() first to warm up minion registry.
s.Sync()
go util.Forever(func() { s.Sync() }, s.period)
}
// Sync syncs list of instances from cloudprovider to master etcd registry.

View File

@ -18,6 +18,7 @@ package controller
import (
"testing"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
@ -64,10 +65,7 @@ func TestSyncCreateMinion(t *testing.T) {
fakeCloud := fake_cloud.FakeCloud{
Machines: instances,
}
minionController, err := NewMinionController(&fakeCloud, ".*", nil, registry)
if err != nil {
t.Errorf("Unexpected error")
}
minionController := NewMinionController(&fakeCloud, ".*", nil, registry, time.Second)
minion, err := registry.GetMinion(ctx, "m3")
if minion != nil {
@ -112,10 +110,7 @@ func TestSyncDeleteMinion(t *testing.T) {
fakeCloud := fake_cloud.FakeCloud{
Machines: instances,
}
minionController, err := NewMinionController(&fakeCloud, ".*", nil, registry)
if err != nil {
t.Errorf("Unexpected error")
}
minionController := NewMinionController(&fakeCloud, ".*", nil, registry, time.Second)
minion, err := registry.GetMinion(ctx, "m3")
if minion == nil {
@ -150,12 +145,9 @@ func TestSyncMinionRegexp(t *testing.T) {
fakeCloud := fake_cloud.FakeCloud{
Machines: instances,
}
minionController, err := NewMinionController(&fakeCloud, "m[0-9]+", nil, registry)
if err != nil {
t.Errorf("Unexpected error")
}
minionController := NewMinionController(&fakeCloud, "m[0-9]+", nil, registry, time.Second)
err = minionController.Sync()
err := minionController.Sync()
if err != nil {
t.Errorf("unexpected error: %v", err)
}

View File

@ -40,7 +40,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
)
// Config is a structure used to configure a Master.
@ -119,12 +118,7 @@ func (m *Master) init(c *Config) {
if c.Cloud != nil && len(c.MinionRegexp) > 0 {
// TODO: Move minion controller to its own code.
minionController, err := cloudcontroller.NewMinionController(c.Cloud, c.MinionRegexp, &c.NodeResources, m.minionRegistry)
if err != nil {
glog.Errorf("Failed to initalize minion controller (%#v)", err)
}
// TODO: Create a Run() method on controller to invoke Sync().
go util.OnceAndForever(func() { minionController.Sync() }, c.MinionCacheTTL)
cloudcontroller.NewMinionController(c.Cloud, c.MinionRegexp, &c.NodeResources, m.minionRegistry, c.MinionCacheTTL).Run()
} else {
for _, minionID := range c.Minions {
m.minionRegistry.CreateMinion(nil, &api.Minion{

View File

@ -60,19 +60,6 @@ func Forever(f func(), period time.Duration) {
}
}
// OnceAndForever runs f first then loops forever running f every d. Catches any panics, and keeps going.
func OnceAndForever(f func(), period time.Duration) {
defer HandleCrash()
f()
for {
func() {
defer HandleCrash()
f()
}()
time.Sleep(period)
}
}
// EncodeJSON returns obj marshalled as a JSON string, ignoring any errors.
func EncodeJSON(obj interface{}) string {
data, _ := json.Marshal(obj)