Use interfaces

This commit is contained in:
erthalion
2018-06-13 14:43:31 +02:00
parent 2081fc47f1
commit 627bb51a0c
4 changed files with 137 additions and 112 deletions
+27 -30
View File
@@ -339,13 +339,12 @@ func (c *Cluster) createService(role PostgresRole) ([]Action, error) {
c.setProcessName("creating %v service", role)
serviceSpec := c.generateService(role, &c.Spec)
return []Action{
Action{
CreateService,
serviceName.Namespace,
ServiceData{
spec: serviceSpec,
},
return []CreateService{
ActionData{
namespace: serviceName.Namespace,
},
ServiceData{
spec: serviceSpec,
},
}, nil
}
@@ -362,14 +361,14 @@ func (c *Cluster) updateService(role PostgresRole, newService *v1.Service) ([]Ac
if newService.Spec.Type != c.Services[role].Spec.Type {
// service type has changed, need to replace the service completely.
// we cannot use just pach the current service, since it may contain attributes incompatible with the new type.
return []Action{
Action{
RecreateService,
ServiceData{
name: serviceName.Name,
role: role,
spec: newService,
},
return []RecreateService{
ActionData{
namespace: serviceName.Namespace,
},
ServiceData{
name: serviceName.Name,
role: role,
spec: newService,
},
}, nil
}
@@ -380,14 +379,13 @@ func (c *Cluster) updateService(role PostgresRole, newService *v1.Service) ([]Ac
return NoActions, fmt.Errorf("could not form patch for the service %q: %v", serviceName, err)
}
return []Action{
Action{
CreateService,
serviceName.Namespace,
ServiceData{
name: serviceName.Name,
spec: newService,
},
return []CreateService{
ActionData{
namespace: serviceName.Namespace,
},
ServiceData{
name: serviceName.Name,
spec: newService,
},
}, nil
}
@@ -395,13 +393,12 @@ func (c *Cluster) updateService(role PostgresRole, newService *v1.Service) ([]Ac
func (c *Cluster) deleteService(role PostgresRole) error {
service := c.Services[role]
return []Action{
Action{
DeleteService,
serviceName.Namespace,
ServiceData{
name: service.Name,
},
return []DeleteService{
ActionData{
namespace: serviceName.Namespace,
},
ServiceData{
name: service.Name,
},
}, nil
}
+32 -63
View File
@@ -14,50 +14,9 @@ import (
"github.com/zalando-incubator/postgres-operator/pkg/util/volumes"
)
var syncChannel chan Action
func (c *Cluster) SyncActions(newSpec *spec.Postgresql) (err error) {
syncSecrets = Action{
SyncSecrets,
SyncSecretsData{
c.generateUserSecrets(),
},
}
syncServiceMaster = Action{
SyncService,
SyncServiceData{
Master,
},
}
syncServiceReplica = Action{
SyncService,
SyncServiceData{
Replica,
},
}
syncVolumes = Action{
SyncVolumes,
SyncVolumesData{
c.Spec.Volume,
},
}
return []Action{
syncSecrets,
syncServiceMaster,
syncServiceReplica,
syncVolumes,
}
}
// Sync syncs the cluster, making sure the actual Kubernetes objects correspond to what is defined in the manifest.
// Unlike the update, sync does not error out if some objects do not exist and takes care of creating them.
func (c *Cluster) Sync(newSpec *spec.Postgresql) (err error) {
syncChannel = make(chan Action)
c.mu.Lock()
defer c.mu.Unlock()
@@ -86,8 +45,13 @@ func (c *Cluster) Sync(newSpec *spec.Postgresql) (err error) {
}
c.logger.Debugf("syncing services")
if err = c.syncServices(); err != nil {
err = fmt.Errorf("could not sync services: %v", err)
if actions, err = c.syncServices(); err != nil {
err = fmt.Errorf("could not resolve actions to sync services: %v", err)
return
}
if err = c.applyActions(actions); err != nil {
err = fmt.Errorf("could not apply actions to sync services: %v", err)
return
}
@@ -150,6 +114,23 @@ func (c *Cluster) syncServices() error {
return nil
}
func (c *Cluster) applyActions(actions []Action) (err error) {
uniqueActions = []Actions{}
hashMap = map[ActionHash]bool{}
for idx, action := range actions {
if _, present := uniqueActions[action.hash()]; !present {
uniqueActions[action.hash()] = true
append(uniqueActions, action)
}
}
for action := range uniqueActions {
if dontStop, err := action.process(); err != nil && !dontStop {
c.logger.Errorf("Can't apply action %s: %v", action.name(), err)
}
}
}
func (c *Cluster) syncService(role PostgresRole) ([]Action, error) {
c.setProcessName("syncing %s service", role)
@@ -170,28 +151,16 @@ func (c *Cluster) syncService(role PostgresRole) ([]Action, error) {
} else if !k8sutil.ResourceNotFound(err) {
return NoActions, fmt.Errorf("could not get %s service: %v", role, err)
}
c.Services[role] = nil
c.logger.Infof("could not find the cluster's %s service", role)
if svc, err := c.createService(role); err != nil {
if k8sutil.ResourceAlreadyExists(err) {
c.logger.Infof("%s service %q already exists", role, util.NameFromMeta(svc.ObjectMeta))
svc, err := c.KubeClient.Services(c.Namespace).Get(c.serviceName(role), metav1.GetOptions{})
if err == nil {
c.Services[role] = svc
} else {
c.logger.Infof("could not fetch existing %s service: %v", role, err)
}
} else {
return fmt.Errorf("could not create missing %s service: %v", role, err)
}
} else {
c.logger.Infof("created missing %s service %q", role, util.NameFromMeta(svc.ObjectMeta))
c.Services[role] = svc
}
return nil
return []CreateService{
ActionData{
namespace: serviceName.Namespace,
},
ServiceData{
role: role,
},
}, nil
}
func (c *Cluster) syncEndpoint(role PostgresRole) error {