mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Remove OnceAndForever util, create a Run() method on MinionController.
This commit is contained in:
parent
ec46e94dc2
commit
2bd88d4544
@ -18,10 +18,12 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,16 +32,30 @@ type MinionController struct {
|
|||||||
matchRE string
|
matchRE string
|
||||||
staticResources *api.NodeResources
|
staticResources *api.NodeResources
|
||||||
registry minion.Registry
|
registry minion.Registry
|
||||||
|
period time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMinionController returns a new minion controller to sync instances from cloudprovider.
|
// 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{
|
return &MinionController{
|
||||||
cloud: cloud,
|
cloud: cloud,
|
||||||
matchRE: matchRE,
|
matchRE: matchRE,
|
||||||
staticResources: staticResources,
|
staticResources: staticResources,
|
||||||
registry: registry,
|
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.
|
// Sync syncs list of instances from cloudprovider to master etcd registry.
|
||||||
|
@ -18,6 +18,7 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
||||||
@ -64,10 +65,7 @@ func TestSyncCreateMinion(t *testing.T) {
|
|||||||
fakeCloud := fake_cloud.FakeCloud{
|
fakeCloud := fake_cloud.FakeCloud{
|
||||||
Machines: instances,
|
Machines: instances,
|
||||||
}
|
}
|
||||||
minionController, err := NewMinionController(&fakeCloud, ".*", nil, registry)
|
minionController := NewMinionController(&fakeCloud, ".*", nil, registry, time.Second)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error")
|
|
||||||
}
|
|
||||||
|
|
||||||
minion, err := registry.GetMinion(ctx, "m3")
|
minion, err := registry.GetMinion(ctx, "m3")
|
||||||
if minion != nil {
|
if minion != nil {
|
||||||
@ -112,10 +110,7 @@ func TestSyncDeleteMinion(t *testing.T) {
|
|||||||
fakeCloud := fake_cloud.FakeCloud{
|
fakeCloud := fake_cloud.FakeCloud{
|
||||||
Machines: instances,
|
Machines: instances,
|
||||||
}
|
}
|
||||||
minionController, err := NewMinionController(&fakeCloud, ".*", nil, registry)
|
minionController := NewMinionController(&fakeCloud, ".*", nil, registry, time.Second)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error")
|
|
||||||
}
|
|
||||||
|
|
||||||
minion, err := registry.GetMinion(ctx, "m3")
|
minion, err := registry.GetMinion(ctx, "m3")
|
||||||
if minion == nil {
|
if minion == nil {
|
||||||
@ -150,12 +145,9 @@ func TestSyncMinionRegexp(t *testing.T) {
|
|||||||
fakeCloud := fake_cloud.FakeCloud{
|
fakeCloud := fake_cloud.FakeCloud{
|
||||||
Machines: instances,
|
Machines: instances,
|
||||||
}
|
}
|
||||||
minionController, err := NewMinionController(&fakeCloud, "m[0-9]+", nil, registry)
|
minionController := NewMinionController(&fakeCloud, "m[0-9]+", nil, registry, time.Second)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = minionController.Sync()
|
err := minionController.Sync()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,6 @@ import (
|
|||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
"github.com/golang/glog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config is a structure used to configure a Master.
|
// 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 {
|
if c.Cloud != nil && len(c.MinionRegexp) > 0 {
|
||||||
// TODO: Move minion controller to its own code.
|
// TODO: Move minion controller to its own code.
|
||||||
minionController, err := cloudcontroller.NewMinionController(c.Cloud, c.MinionRegexp, &c.NodeResources, m.minionRegistry)
|
cloudcontroller.NewMinionController(c.Cloud, c.MinionRegexp, &c.NodeResources, m.minionRegistry, c.MinionCacheTTL).Run()
|
||||||
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)
|
|
||||||
} else {
|
} else {
|
||||||
for _, minionID := range c.Minions {
|
for _, minionID := range c.Minions {
|
||||||
m.minionRegistry.CreateMinion(nil, &api.Minion{
|
m.minionRegistry.CreateMinion(nil, &api.Minion{
|
||||||
|
@ -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.
|
// EncodeJSON returns obj marshalled as a JSON string, ignoring any errors.
|
||||||
func EncodeJSON(obj interface{}) string {
|
func EncodeJSON(obj interface{}) string {
|
||||||
data, _ := json.Marshal(obj)
|
data, _ := json.Marshal(obj)
|
||||||
|
Loading…
Reference in New Issue
Block a user