mirror of
https://github.com/zalando/postgres-operator.git
synced 2026-10-04 17:51:56 +02:00
cluster history api endpoint
This commit is contained in:
@@ -32,10 +32,11 @@ type Controller struct {
|
||||
|
||||
stopCh chan struct{}
|
||||
|
||||
clustersMu sync.RWMutex
|
||||
clusters map[spec.NamespacedName]*cluster.Cluster
|
||||
clusterLogs map[spec.NamespacedName]ringlog.RingLogger
|
||||
teamClusters map[string][]spec.NamespacedName
|
||||
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
|
||||
@@ -52,14 +53,15 @@ func NewController(controllerConfig *spec.ControllerConfig) *Controller {
|
||||
logger := logrus.New()
|
||||
|
||||
c := &Controller{
|
||||
config: *controllerConfig,
|
||||
opConfig: &config.Config{},
|
||||
logger: logger.WithField("pkg", "controller"),
|
||||
clusters: make(map[spec.NamespacedName]*cluster.Cluster),
|
||||
clusterLogs: 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"),
|
||||
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)
|
||||
|
||||
|
||||
@@ -138,12 +138,14 @@ func (c *Controller) addCluster(lg *logrus.Entry, clusterName spec.NamespacedNam
|
||||
c.teamClusters[teamName] = append(c.teamClusters[teamName], clusterName)
|
||||
c.clusters[clusterName] = cl
|
||||
c.clusterLogs[clusterName] = ringlog.New(c.opConfig.RingLogLines)
|
||||
c.clusterHistory[clusterName] = ringlog.New(c.opConfig.ClusterHistoryEntries)
|
||||
|
||||
return cl
|
||||
}
|
||||
|
||||
func (c *Controller) processEvent(event spec.ClusterEvent) {
|
||||
var clusterName spec.NamespacedName
|
||||
var clHistory ringlog.RingLogger
|
||||
|
||||
lg := c.logger.WithField("worker", event.WorkerID)
|
||||
|
||||
@@ -156,6 +158,9 @@ func (c *Controller) processEvent(event spec.ClusterEvent) {
|
||||
|
||||
c.clustersMu.RLock()
|
||||
cl, clusterFound := c.clusters[clusterName]
|
||||
if clusterFound {
|
||||
clHistory = c.clusterHistory[clusterName]
|
||||
}
|
||||
c.clustersMu.RUnlock()
|
||||
|
||||
switch event.EventType {
|
||||
@@ -192,6 +197,12 @@ func (c *Controller) processEvent(event spec.ClusterEvent) {
|
||||
}
|
||||
cl.Error = nil
|
||||
lg.Infoln("cluster has been updated")
|
||||
|
||||
clHistory.Insert(&spec.Diff{
|
||||
EventTime: event.EventTime,
|
||||
ProcessTime: time.Now(),
|
||||
Diff: util.Diff(event.OldSpec, event.NewSpec),
|
||||
})
|
||||
case spec.EventDelete:
|
||||
if !clusterFound {
|
||||
lg.Errorf("unknown cluster: %q", clusterName)
|
||||
@@ -211,6 +222,7 @@ func (c *Controller) processEvent(event spec.ClusterEvent) {
|
||||
|
||||
delete(c.clusters, clusterName)
|
||||
delete(c.clusterLogs, clusterName)
|
||||
delete(c.clusterHistory, clusterName)
|
||||
for i, val := range c.teamClusters[teamName] {
|
||||
if val == clusterName {
|
||||
copy(c.teamClusters[teamName][i:], c.teamClusters[teamName][i+1:])
|
||||
|
||||
@@ -166,3 +166,25 @@ func (c *Controller) ListQueue(workerID uint32) (*spec.QueueDump, error) {
|
||||
func (c *Controller) GetWorkersCnt() uint32 {
|
||||
return c.opConfig.Workers
|
||||
}
|
||||
|
||||
// ClusterHistory dumps history of cluster changes
|
||||
func (c *Controller) ClusterHistory(team, name string) ([]*spec.Diff, error) {
|
||||
clusterName := spec.NamespacedName{
|
||||
Namespace: c.opConfig.Namespace,
|
||||
Name: team + "-" + name,
|
||||
}
|
||||
|
||||
c.clustersMu.RLock()
|
||||
cl, ok := c.clusterHistory[clusterName]
|
||||
c.clustersMu.RUnlock()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("could not find cluster")
|
||||
}
|
||||
|
||||
res := make([]*spec.Diff, 0)
|
||||
for _, e := range cl.Walk() {
|
||||
res = append(res, e.(*spec.Diff))
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user