mirror of
https://github.com/zalando/postgres-operator.git
synced 2026-09-30 19:31:23 +02:00
Extend diagnostic api with worker status info
This commit is contained in:
@@ -30,13 +30,14 @@ type Controller struct {
|
||||
|
||||
stopCh chan struct{}
|
||||
|
||||
curWorkerID uint32 //initialized with 0
|
||||
clusterWorkers map[spec.NamespacedName]uint32
|
||||
clustersMu sync.RWMutex
|
||||
clusters map[spec.NamespacedName]*cluster.Cluster
|
||||
clusterLogs map[spec.NamespacedName]ringlog.RingLogger
|
||||
clusterHistory map[spec.NamespacedName]ringlog.RingLogger // history of the cluster changes
|
||||
teamClusters map[string][]spec.NamespacedName
|
||||
curWorkerID uint32 //initialized with 0
|
||||
curWorkerCluster sync.Map
|
||||
clusterWorkers map[spec.NamespacedName]uint32
|
||||
clustersMu sync.RWMutex
|
||||
clusters map[spec.NamespacedName]*cluster.Cluster
|
||||
clusterLogs map[spec.NamespacedName]ringlog.RingLogger
|
||||
clusterHistory map[spec.NamespacedName]ringlog.RingLogger // history of the cluster changes
|
||||
teamClusters map[string][]spec.NamespacedName
|
||||
|
||||
postgresqlInformer cache.SharedIndexInformer
|
||||
podInformer cache.SharedIndexInformer
|
||||
@@ -53,16 +54,17 @@ func NewController(controllerConfig *spec.ControllerConfig) *Controller {
|
||||
logger := logrus.New()
|
||||
|
||||
c := &Controller{
|
||||
config: *controllerConfig,
|
||||
opConfig: &config.Config{},
|
||||
logger: logger.WithField("pkg", "controller"),
|
||||
clusterWorkers: make(map[spec.NamespacedName]uint32),
|
||||
clusters: make(map[spec.NamespacedName]*cluster.Cluster),
|
||||
clusterLogs: make(map[spec.NamespacedName]ringlog.RingLogger),
|
||||
clusterHistory: make(map[spec.NamespacedName]ringlog.RingLogger),
|
||||
teamClusters: make(map[string][]spec.NamespacedName),
|
||||
stopCh: make(chan struct{}),
|
||||
podCh: make(chan spec.PodEvent),
|
||||
config: *controllerConfig,
|
||||
opConfig: &config.Config{},
|
||||
logger: logger.WithField("pkg", "controller"),
|
||||
curWorkerCluster: sync.Map{},
|
||||
clusterWorkers: make(map[spec.NamespacedName]uint32),
|
||||
clusters: make(map[spec.NamespacedName]*cluster.Cluster),
|
||||
clusterLogs: make(map[spec.NamespacedName]ringlog.RingLogger),
|
||||
clusterHistory: make(map[spec.NamespacedName]ringlog.RingLogger),
|
||||
teamClusters: make(map[string][]spec.NamespacedName),
|
||||
stopCh: make(chan struct{}),
|
||||
podCh: make(chan spec.PodEvent),
|
||||
}
|
||||
logger.Hooks.Add(c)
|
||||
|
||||
|
||||
@@ -86,16 +86,16 @@ func (c *Controller) clusterListFunc(options metav1.ListOptions) (runtime.Object
|
||||
return &list, err
|
||||
}
|
||||
|
||||
type tprDecoder struct {
|
||||
type crdDecoder struct {
|
||||
dec *json.Decoder
|
||||
close func() error
|
||||
}
|
||||
|
||||
func (d *tprDecoder) Close() {
|
||||
func (d *crdDecoder) Close() {
|
||||
d.close()
|
||||
}
|
||||
|
||||
func (d *tprDecoder) Decode() (action watch.EventType, object runtime.Object, err error) {
|
||||
func (d *crdDecoder) Decode() (action watch.EventType, object runtime.Object, err error) {
|
||||
var e struct {
|
||||
Type watch.EventType
|
||||
Object spec.Postgresql
|
||||
@@ -121,7 +121,7 @@ func (c *Controller) clusterWatchFunc(options metav1.ListOptions) (watch.Interfa
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return watch.NewStreamWatcher(&tprDecoder{
|
||||
return watch.NewStreamWatcher(&crdDecoder{
|
||||
dec: json.NewDecoder(r),
|
||||
close: r.Close,
|
||||
}), nil
|
||||
@@ -163,6 +163,8 @@ func (c *Controller) processEvent(event spec.ClusterEvent) {
|
||||
}
|
||||
c.clustersMu.RUnlock()
|
||||
|
||||
defer c.curWorkerCluster.Store(event.WorkerID, nil)
|
||||
|
||||
switch event.EventType {
|
||||
case spec.EventAdd:
|
||||
if clusterFound {
|
||||
@@ -174,6 +176,8 @@ func (c *Controller) processEvent(event spec.ClusterEvent) {
|
||||
|
||||
cl = c.addCluster(lg, clusterName, event.NewSpec)
|
||||
|
||||
c.curWorkerCluster.Store(event.WorkerID, cl)
|
||||
|
||||
if err := cl.Create(); err != nil {
|
||||
cl.Error = fmt.Errorf("could not create cluster: %v", err)
|
||||
lg.Error(cl.Error)
|
||||
@@ -189,6 +193,7 @@ func (c *Controller) processEvent(event spec.ClusterEvent) {
|
||||
lg.Warnln("cluster does not exist")
|
||||
return
|
||||
}
|
||||
c.curWorkerCluster.Store(event.WorkerID, cl)
|
||||
if err := cl.Update(event.NewSpec); err != nil {
|
||||
cl.Error = fmt.Errorf("could not update cluster: %v", err)
|
||||
lg.Error(cl.Error)
|
||||
@@ -212,6 +217,7 @@ func (c *Controller) processEvent(event spec.ClusterEvent) {
|
||||
|
||||
teamName := strings.ToLower(cl.Spec.TeamID)
|
||||
|
||||
c.curWorkerCluster.Store(event.WorkerID, cl)
|
||||
if err := cl.Delete(); err != nil {
|
||||
lg.Errorf("could not delete cluster: %v", err)
|
||||
}
|
||||
@@ -242,6 +248,7 @@ func (c *Controller) processEvent(event spec.ClusterEvent) {
|
||||
cl = c.addCluster(lg, clusterName, event.NewSpec)
|
||||
}
|
||||
|
||||
c.curWorkerCluster.Store(event.WorkerID, cl)
|
||||
if err := cl.Sync(); err != nil {
|
||||
cl.Error = fmt.Errorf("could not sync cluster: %v", err)
|
||||
lg.Error(cl.Error)
|
||||
|
||||
@@ -6,7 +6,9 @@ import (
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
|
||||
"github.com/zalando-incubator/postgres-operator/pkg/cluster"
|
||||
"github.com/zalando-incubator/postgres-operator/pkg/spec"
|
||||
"github.com/zalando-incubator/postgres-operator/pkg/util"
|
||||
"github.com/zalando-incubator/postgres-operator/pkg/util/config"
|
||||
)
|
||||
|
||||
@@ -167,6 +169,24 @@ func (c *Controller) GetWorkersCnt() uint32 {
|
||||
return c.opConfig.Workers
|
||||
}
|
||||
|
||||
//WorkerStatus provides status of the worker
|
||||
func (c *Controller) WorkerStatus(workerID uint32) (*spec.WorkerStatus, error) {
|
||||
obj, ok := c.curWorkerCluster.Load(workerID)
|
||||
if !ok || obj == nil {
|
||||
return nil, fmt.Errorf("worker has no status")
|
||||
}
|
||||
|
||||
cl, ok := obj.(*cluster.Cluster)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("could not cast to Cluster struct")
|
||||
}
|
||||
|
||||
return &spec.WorkerStatus{
|
||||
CurrentCluster: util.NameFromMeta(cl.ObjectMeta),
|
||||
CurrentProcess: cl.GetCurrentProcess(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ClusterHistory dumps history of cluster changes
|
||||
func (c *Controller) ClusterHistory(team, name string) ([]*spec.Diff, error) {
|
||||
clusterName := spec.NamespacedName{
|
||||
|
||||
@@ -29,9 +29,9 @@ func (c *Controller) makeClusterConfig() cluster.Config {
|
||||
}
|
||||
|
||||
func (c *Controller) clusterWorkerID(clusterName spec.NamespacedName) uint32 {
|
||||
workerId, ok := c.clusterWorkers[clusterName]
|
||||
workerID, ok := c.clusterWorkers[clusterName]
|
||||
if ok {
|
||||
return workerId
|
||||
return workerID
|
||||
}
|
||||
|
||||
c.clusterWorkers[clusterName] = c.curWorkerID
|
||||
|
||||
Reference in New Issue
Block a user