cluster: simplify the new cluster process

This commit is contained in:
Hongchao Deng 2016-08-31 21:29:04 -07:00
Родитель 23f1f8c809
Коммит c2c287a42b
2 изменённых файлов: 16 добавлений и 16 удалений

Просмотреть файл

@ -19,7 +19,6 @@ import (
type clusterEventType string
const (
eventNewCluster clusterEventType = "Add"
eventDeleteCluster clusterEventType = "Delete"
eventReconcile clusterEventType = "Reconcile"
eventModifyCluster clusterEventType = "Modify"
@ -51,7 +50,15 @@ type Cluster struct {
backupDir string
}
func New(kclient *unversioned.Client, name string, spec *Spec) *Cluster {
func New(c *unversioned.Client, name string, spec *Spec) *Cluster {
return new(c, name, spec, true)
}
func Restore(c *unversioned.Client, name string, spec *Spec) *Cluster {
return new(c, name, spec, false)
}
func new(kclient *unversioned.Client, name string, spec *Spec, isNewCluster bool) *Cluster {
c := &Cluster{
kclient: kclient,
name: name,
@ -61,14 +68,13 @@ func New(kclient *unversioned.Client, name string, spec *Spec) *Cluster {
}
go c.run()
return c
}
if isNewCluster {
if err := c.startSeedMember(spec); err != nil {
panic(err)
}
}
func (c *Cluster) Init(spec *Spec) {
c.send(&clusterEvent{
typ: eventNewCluster,
spec: *spec,
})
return c
}
func (c *Cluster) Delete() {
@ -90,10 +96,6 @@ func (c *Cluster) run() {
select {
case event := <-c.eventCh:
switch event.typ {
case eventNewCluster:
if err := c.startSeedMember(&event.spec); err != nil {
panic(err)
}
case eventReconcile:
if err := c.reconcile(event.running); err != nil {
panic(err)

Просмотреть файл

@ -74,8 +74,6 @@ func (c *Controller) Run() {
switch event.Type {
case "ADDED":
nc := cluster.New(c.kclient, clusterName, &event.Object.Spec)
// TODO: combine init into New. Different fresh new and recovered new.
nc.Init(&event.Object.Spec)
c.clusters[clusterName] = nc
backup := event.Object.Spec.Backup
@ -109,7 +107,7 @@ func (c *Controller) findAllClusters() (string, error) {
return "", err
}
for _, item := range list.Items {
nc := cluster.New(c.kclient, item.Name, &item.Spec)
nc := cluster.Restore(c.kclient, item.Name, &item.Spec)
c.clusters[item.Name] = nc
backup := item.Spec.Backup