nested status field

This commit is contained in:
Felix Kunde 2019-04-04 15:15:32 +02:00
parent 3887938ab9
commit 5e87d48508
10 changed files with 64 additions and 57 deletions

View File

@ -2,14 +2,14 @@ package v1
// ClusterStatusUnknown etc : status of a Postgres cluster known to the operator // ClusterStatusUnknown etc : status of a Postgres cluster known to the operator
const ( const (
ClusterStatusUnknown PostgresStatus = "" ClusterStatusUnknown PostgresClusterStatus = ""
ClusterStatusCreating PostgresStatus = "Creating" ClusterStatusCreating PostgresClusterStatus = "Creating"
ClusterStatusUpdating PostgresStatus = "Updating" ClusterStatusUpdating PostgresClusterStatus = "Updating"
ClusterStatusUpdateFailed PostgresStatus = "UpdateFailed" ClusterStatusUpdateFailed PostgresClusterStatus = "UpdateFailed"
ClusterStatusSyncFailed PostgresStatus = "SyncFailed" ClusterStatusSyncFailed PostgresClusterStatus = "SyncFailed"
ClusterStatusAddFailed PostgresStatus = "CreateFailed" ClusterStatusAddFailed PostgresClusterStatus = "CreateFailed"
ClusterStatusRunning PostgresStatus = "Running" ClusterStatusRunning PostgresClusterStatus = "Running"
ClusterStatusInvalid PostgresStatus = "Invalid" ClusterStatusInvalid PostgresClusterStatus = "Invalid"
) )
const ( const (

View File

@ -81,7 +81,7 @@ func (p *Postgresql) UnmarshalJSON(data []byte) error {
} }
tmp.Error = err.Error() tmp.Error = err.Error()
tmp.PostgresClusterStatus = ClusterStatusInvalid tmp.Status = PostgresStatus{PostgresClusterStatus: ClusterStatusInvalid}
*p = Postgresql(tmp) *p = Postgresql(tmp)
@ -91,10 +91,10 @@ func (p *Postgresql) UnmarshalJSON(data []byte) error {
if clusterName, err := extractClusterName(tmp2.ObjectMeta.Name, tmp2.Spec.TeamID); err != nil { if clusterName, err := extractClusterName(tmp2.ObjectMeta.Name, tmp2.Spec.TeamID); err != nil {
tmp2.Error = err.Error() tmp2.Error = err.Error()
tmp2.PostgresClusterStatus = ClusterStatusInvalid tmp2.Status = PostgresStatus{PostgresClusterStatus: ClusterStatusInvalid}
} else if err := validateCloneClusterDescription(&tmp2.Spec.Clone); err != nil { } else if err := validateCloneClusterDescription(&tmp2.Spec.Clone); err != nil {
tmp2.Error = err.Error() tmp2.Error = err.Error()
tmp2.PostgresClusterStatus = ClusterStatusInvalid tmp2.Status = PostgresStatus{PostgresClusterStatus: ClusterStatusInvalid}
} else { } else {
tmp2.Spec.ClusterName = clusterName tmp2.Spec.ClusterName = clusterName
} }

View File

@ -15,9 +15,9 @@ type Postgresql struct {
metav1.TypeMeta `json:",inline"` metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"` metav1.ObjectMeta `json:"metadata,omitempty"`
Spec PostgresSpec `json:"spec"` Spec PostgresSpec `json:"spec"`
PostgresClusterStatus PostgresStatus `json:"status,omitempty"` Status PostgresStatus `json:"status,omitempty"`
Error string `json:"-"` Error string `json:"-"`
} }
// PostgresSpec defines the specification for the PostgreSQL TPR. // PostgresSpec defines the specification for the PostgreSQL TPR.
@ -129,4 +129,9 @@ type Sidecar struct {
type UserFlags []string type UserFlags []string
// PostgresStatus contains status of the PostgreSQL cluster (running, creation failed etc.) // PostgresStatus contains status of the PostgreSQL cluster (running, creation failed etc.)
type PostgresStatus string type PostgresStatus struct {
PostgresClusterStatus PostgresClusterStatus `json:"PostgresClusterStatus"`
}
// PostgresClusterStatus defines the status of the PostgreSQL cluster (running, creation failed etc.)
type PostgresClusterStatus string

View File

@ -85,12 +85,12 @@ func validateCloneClusterDescription(clone *CloneDescription) error {
} }
// Success of the current Status // Success of the current Status
func (postgresClusterStatus PostgresStatus) Success() bool { func (postgresStatus PostgresStatus) Success() bool {
return postgresClusterStatus != ClusterStatusAddFailed && return postgresStatus.PostgresClusterStatus != ClusterStatusAddFailed &&
postgresClusterStatus != ClusterStatusUpdateFailed && postgresStatus.PostgresClusterStatus != ClusterStatusUpdateFailed &&
postgresClusterStatus != ClusterStatusSyncFailed postgresStatus.PostgresClusterStatus != ClusterStatusSyncFailed
} }
func (postgresClusterStatus PostgresStatus) String() string { func (postgresStatus PostgresStatus) String() string {
return string(postgresClusterStatus) return string(postgresStatus.PostgresClusterStatus)
} }

View File

@ -128,11 +128,11 @@ var unmarshalCluster = []struct {
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "acid-testcluster1", Name: "acid-testcluster1",
}, },
PostgresClusterStatus: ClusterStatusInvalid, Status: PostgresStatus{PostgresClusterStatus: ClusterStatusInvalid},
// This error message can vary between Go versions, so compute it for the current version. // This error message can vary between Go versions, so compute it for the current version.
Error: json.Unmarshal([]byte(`{"teamId": 0}`), &PostgresSpec{}).Error(), Error: json.Unmarshal([]byte(`{"teamId": 0}`), &PostgresSpec{}).Error(),
}, },
[]byte(`{"kind":"Postgresql","apiVersion":"acid.zalan.do/v1","metadata":{"name":"acid-testcluster1","creationTimestamp":null},"spec":{"postgresql":{"version":"","parameters":null},"volume":{"size":"","storageClass":""},"patroni":{"initdb":null,"pg_hba":null,"ttl":0,"loop_wait":0,"retry_timeout":0,"maximum_lag_on_failover":0,"slots":null},"resources":{"requests":{"cpu":"","memory":""},"limits":{"cpu":"","memory":""}},"teamId":"","allowedSourceRanges":null,"numberOfInstances":0,"users":null,"clone":{}},"status":"Invalid"}`), nil}, []byte(`{"kind":"Postgresql","apiVersion":"acid.zalan.do/v1","metadata":{"name":"acid-testcluster1","creationTimestamp":null},"spec":{"postgresql":{"version":"","parameters":null},"volume":{"size":"","storageClass":""},"patroni":{"initdb":null,"pg_hba":null,"ttl":0,"loop_wait":0,"retry_timeout":0,"maximum_lag_on_failover":0,"slots":null},"resources":{"requests":{"cpu":"","memory":""},"limits":{"cpu":"","memory":""}},"teamId":"","allowedSourceRanges":null,"numberOfInstances":0,"users":null,"clone":{}},"status":{"PostgresClusterStatus":"Invalid"}}`), nil},
{[]byte(`{ {[]byte(`{
"kind": "Postgresql", "kind": "Postgresql",
"apiVersion": "acid.zalan.do/v1", "apiVersion": "acid.zalan.do/v1",
@ -273,7 +273,7 @@ var unmarshalCluster = []struct {
}, },
Error: "", Error: "",
}, },
[]byte(`{"kind":"Postgresql","apiVersion":"acid.zalan.do/v1","metadata":{"name":"acid-testcluster1","creationTimestamp":null},"spec":{"postgresql":{"version":"9.6","parameters":{"log_statement":"all","max_connections":"10","shared_buffers":"32MB"}},"volume":{"size":"5Gi","storageClass":"SSD"},"patroni":{"initdb":{"data-checksums":"true","encoding":"UTF8","locale":"en_US.UTF-8"},"pg_hba":["hostssl all all 0.0.0.0/0 md5","host all all 0.0.0.0/0 md5"],"ttl":30,"loop_wait":10,"retry_timeout":10,"maximum_lag_on_failover":33554432,"slots":{"permanent_logical_1":{"database":"foo","plugin":"pgoutput","type":"logical"}}},"resources":{"requests":{"cpu":"10m","memory":"50Mi"},"limits":{"cpu":"300m","memory":"3000Mi"}},"teamId":"ACID","allowedSourceRanges":["127.0.0.1/32"],"numberOfInstances":2,"users":{"zalando":["superuser","createdb"]},"maintenanceWindows":["Mon:01:00-06:00","Sat:00:00-04:00","05:00-05:15"],"clone":{"cluster":"acid-batman"}}}`), nil}, []byte(`{"kind":"Postgresql","apiVersion":"acid.zalan.do/v1","metadata":{"name":"acid-testcluster1","creationTimestamp":null},"spec":{"postgresql":{"version":"9.6","parameters":{"log_statement":"all","max_connections":"10","shared_buffers":"32MB"}},"volume":{"size":"5Gi","storageClass":"SSD"},"patroni":{"initdb":{"data-checksums":"true","encoding":"UTF8","locale":"en_US.UTF-8"},"pg_hba":["hostssl all all 0.0.0.0/0 md5","host all all 0.0.0.0/0 md5"],"ttl":30,"loop_wait":10,"retry_timeout":10,"maximum_lag_on_failover":33554432,"slots":{"permanent_logical_1":{"database":"foo","plugin":"pgoutput","type":"logical"}}},"resources":{"requests":{"cpu":"10m","memory":"50Mi"},"limits":{"cpu":"300m","memory":"3000Mi"}},"teamId":"ACID","allowedSourceRanges":["127.0.0.1/32"],"numberOfInstances":2,"users":{"zalando":["superuser","createdb"]},"maintenanceWindows":["Mon:01:00-06:00","Sat:00:00-04:00","05:00-05:15"],"clone":{"cluster":"acid-batman"}},"status":{"PostgresClusterStatus":""}}`), nil},
{ {
[]byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1","metadata": {"name": "teapot-testcluster1"}, "spec": {"teamId": "acid"}}`), []byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1","metadata": {"name": "teapot-testcluster1"}, "spec": {"teamId": "acid"}}`),
Postgresql{ Postgresql{
@ -284,11 +284,11 @@ var unmarshalCluster = []struct {
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "teapot-testcluster1", Name: "teapot-testcluster1",
}, },
Spec: PostgresSpec{TeamID: "acid"}, Spec: PostgresSpec{TeamID: "acid"},
PostgresClusterStatus: ClusterStatusInvalid, Status: PostgresStatus{PostgresClusterStatus: ClusterStatusInvalid},
Error: errors.New("name must match {TEAM}-{NAME} format").Error(), Error: errors.New("name must match {TEAM}-{NAME} format").Error(),
}, },
[]byte(`{"kind":"Postgresql","apiVersion":"acid.zalan.do/v1","metadata":{"name":"teapot-testcluster1","creationTimestamp":null},"spec":{"postgresql":{"version":"","parameters":null},"volume":{"size":"","storageClass":""},"patroni":{"initdb":null,"pg_hba":null,"ttl":0,"loop_wait":0,"retry_timeout":0,"maximum_lag_on_failover":0,"slots":null},"resources":{"requests":{"cpu":"","memory":""},"limits":{"cpu":"","memory":""}},"teamId":"acid","allowedSourceRanges":null,"numberOfInstances":0,"users":null,"clone":{}},"status":"Invalid"}`), nil}, []byte(`{"kind":"Postgresql","apiVersion":"acid.zalan.do/v1","metadata":{"name":"teapot-testcluster1","creationTimestamp":null},"spec":{"postgresql":{"version":"","parameters":null},"volume":{"size":"","storageClass":""},"patroni":{"initdb":null,"pg_hba":null,"ttl":0,"loop_wait":0,"retry_timeout":0,"maximum_lag_on_failover":0,"slots":null},"resources":{"requests":{"cpu":"","memory":""},"limits":{"cpu":"","memory":""}},"teamId":"acid","allowedSourceRanges":null,"numberOfInstances":0,"users":null,"clone":{}},"status":{"PostgresClusterStatus":"Invalid"}}`), nil},
{ {
in: []byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1","metadata": {"name": "acid-testcluster1"}, "spec": {"teamId": "acid", "clone": {"cluster": "team-batman"}}}`), in: []byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1","metadata": {"name": "acid-testcluster1"}, "spec": {"teamId": "acid", "clone": {"cluster": "team-batman"}}}`),
out: Postgresql{ out: Postgresql{
@ -308,12 +308,12 @@ var unmarshalCluster = []struct {
}, },
Error: "", Error: "",
}, },
marshal: []byte(`{"kind":"Postgresql","apiVersion":"acid.zalan.do/v1","metadata":{"name":"acid-testcluster1","creationTimestamp":null},"spec":{"postgresql":{"version":"","parameters":null},"volume":{"size":"","storageClass":""},"patroni":{"initdb":null,"pg_hba":null,"ttl":0,"loop_wait":0,"retry_timeout":0,"maximum_lag_on_failover":0,"slots":null},"resources":{"requests":{"cpu":"","memory":""},"limits":{"cpu":"","memory":""}},"teamId":"acid","allowedSourceRanges":null,"numberOfInstances":0,"users":null,"clone":{"cluster":"team-batman"}}}`), err: nil}, marshal: []byte(`{"kind":"Postgresql","apiVersion":"acid.zalan.do/v1","metadata":{"name":"acid-testcluster1","creationTimestamp":null},"spec":{"postgresql":{"version":"","parameters":null},"volume":{"size":"","storageClass":""},"patroni":{"initdb":null,"pg_hba":null,"ttl":0,"loop_wait":0,"retry_timeout":0,"maximum_lag_on_failover":0,"slots":null},"resources":{"requests":{"cpu":"","memory":""},"limits":{"cpu":"","memory":""}},"teamId":"acid","allowedSourceRanges":null,"numberOfInstances":0,"users":null,"clone":{"cluster":"team-batman"}},"status":{"PostgresClusterStatus":""}}`), err: nil},
{[]byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1"`), {[]byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1"`),
Postgresql{}, Postgresql{},
[]byte{}, []byte{},
errors.New("unexpected end of JSON input")}, errors.New("unexpected end of JSON input")},
{[]byte(`{"kind":"Postgresql","apiVersion":"acid.zalan.do/v1","metadata":{"name":"acid-testcluster","creationTimestamp":qaz},"spec":{"postgresql":{"version":"","parameters":null},"volume":{"size":"","storageClass":""},"patroni":{"initdb":null,"pg_hba":null,"ttl":0,"loop_wait":0,"retry_timeout":0,"maximum_lag_on_failover":0,"slots":null},"resources":{"requests":{"cpu":"","memory":""},"limits":{"cpu":"","memory":""}},"teamId":"acid","allowedSourceRanges":null,"numberOfInstances":0,"users":null,"clone":{}},"status":"Invalid"}`), {[]byte(`{"kind":"Postgresql","apiVersion":"acid.zalan.do/v1","metadata":{"name":"acid-testcluster","creationTimestamp":qaz},"spec":{"postgresql":{"version":"","parameters":null},"volume":{"size":"","storageClass":""},"patroni":{"initdb":null,"pg_hba":null,"ttl":0,"loop_wait":0,"retry_timeout":0,"maximum_lag_on_failover":0,"slots":null},"resources":{"requests":{"cpu":"","memory":""},"limits":{"cpu":"","memory":""}},"teamId":"acid","allowedSourceRanges":null,"numberOfInstances":0,"users":null,"clone":{}},"status":{"PostgresClusterStatus":"Invalid"}}`),
Postgresql{}, Postgresql{},
[]byte{}, []byte{},
errors.New("invalid character 'q' looking for beginning of value")}} errors.New("invalid character 'q' looking for beginning of value")}}
@ -350,8 +350,10 @@ var postgresqlList = []struct {
AllowedSourceRanges: []string{"185.85.220.0/22"}, AllowedSourceRanges: []string{"185.85.220.0/22"},
NumberOfInstances: 1, NumberOfInstances: 1,
}, },
PostgresClusterStatus: ClusterStatusRunning, Status: PostgresStatus{
Error: "", PostgresClusterStatus: ClusterStatusRunning,
},
Error: "",
}}, }},
}, },
nil}, nil},

View File

@ -149,13 +149,13 @@ func (c *Cluster) setProcessName(procName string, args ...interface{}) {
} }
} }
func (c *Cluster) setPostgresClusterStatus(postgresClusterStatus acidv1.PostgresStatus) { func (c *Cluster) setStatus(status acidv1.PostgresClusterStatus) {
// TODO: eventually switch to updateStatus() for kubernetes 1.11 and above // TODO: eventually switch to updateStatus() for kubernetes 1.11 and above
var ( var (
err error err error
b []byte b []byte
) )
if b, err = json.Marshal(postgresClusterStatus); err != nil { if b, err = json.Marshal(status); err != nil {
c.logger.Errorf("could not marshal Postgres cluster status: %v", err) c.logger.Errorf("could not marshal Postgres cluster status: %v", err)
} }
@ -172,7 +172,7 @@ func (c *Cluster) setPostgresClusterStatus(postgresClusterStatus acidv1.Postgres
} }
func (c *Cluster) isNewCluster() bool { func (c *Cluster) isNewCluster() bool {
return c.PostgresClusterStatus == acidv1.ClusterStatusCreating return c.Status == acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusCreating}
} }
// initUsers populates c.systemUsers and c.pgUsers maps. // initUsers populates c.systemUsers and c.pgUsers maps.
@ -214,13 +214,13 @@ func (c *Cluster) Create() error {
defer func() { defer func() {
if err == nil { if err == nil {
c.setPostgresClusterStatus(acidv1.ClusterStatusRunning) //TODO: are you sure it's running? c.setStatus(acidv1.ClusterStatusRunning) //TODO: are you sure it's running?
} else { } else {
c.setPostgresClusterStatus(acidv1.ClusterStatusAddFailed) c.setStatus(acidv1.ClusterStatusAddFailed)
} }
}() }()
c.setPostgresClusterStatus(acidv1.ClusterStatusCreating) c.setStatus(acidv1.ClusterStatusCreating)
for _, role := range []PostgresRole{Master, Replica} { for _, role := range []PostgresRole{Master, Replica} {
@ -487,14 +487,14 @@ func (c *Cluster) Update(oldSpec, newSpec *acidv1.Postgresql) error {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
c.setPostgresClusterStatus(acidv1.ClusterStatusUpdating) c.setStatus(acidv1.ClusterStatusUpdating)
c.setSpec(newSpec) c.setSpec(newSpec)
defer func() { defer func() {
if updateFailed { if updateFailed {
c.setPostgresClusterStatus(acidv1.ClusterStatusUpdateFailed) c.setStatus(acidv1.ClusterStatusUpdateFailed)
} else { } else {
c.setPostgresClusterStatus(acidv1.ClusterStatusRunning) c.setStatus(acidv1.ClusterStatusRunning)
} }
}() }()
@ -633,7 +633,7 @@ func (c *Cluster) Delete() {
func (c *Cluster) NeedsRepair() (bool, acidv1.PostgresStatus) { func (c *Cluster) NeedsRepair() (bool, acidv1.PostgresStatus) {
c.specMu.RLock() c.specMu.RLock()
defer c.specMu.RUnlock() defer c.specMu.RUnlock()
return !c.PostgresClusterStatus.Success(), c.PostgresClusterStatus return !c.Status.Success(), c.Status
} }
@ -853,12 +853,12 @@ func (c *Cluster) GetCurrentProcess() Process {
} }
// GetStatus provides status of the cluster // GetStatus provides status of the cluster
func (c *Cluster) GetPostgresClusterStatus() *ClusterStatus { func (c *Cluster) GetStatus() *ClusterStatus {
return &ClusterStatus{ return &ClusterStatus{
Cluster: c.Spec.ClusterName, Cluster: c.Spec.ClusterName,
Team: c.Spec.TeamID, Team: c.Spec.TeamID,
PostgresClusterStatus: c.PostgresClusterStatus, Status: c.Status,
Spec: c.Spec, Spec: c.Spec,
MasterService: c.GetServiceMaster(), MasterService: c.GetServiceMaster(),
ReplicaService: c.GetServiceReplica(), ReplicaService: c.GetServiceReplica(),

View File

@ -27,9 +27,9 @@ func (c *Cluster) Sync(newSpec *acidv1.Postgresql) error {
defer func() { defer func() {
if err != nil { if err != nil {
c.logger.Warningf("error while syncing cluster state: %v", err) c.logger.Warningf("error while syncing cluster state: %v", err)
c.setPostgresClusterStatus(acidv1.ClusterStatusSyncFailed) c.setStatus(acidv1.ClusterStatusSyncFailed)
} else if c.PostgresClusterStatus != acidv1.ClusterStatusRunning { } else if (c.Status != acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusRunning}) {
c.setPostgresClusterStatus(acidv1.ClusterStatusRunning) c.setStatus(acidv1.ClusterStatusRunning)
} }
}() }()

View File

@ -63,9 +63,9 @@ type ClusterStatus struct {
StatefulSet *v1beta1.StatefulSet StatefulSet *v1beta1.StatefulSet
PodDisruptionBudget *policybeta1.PodDisruptionBudget PodDisruptionBudget *policybeta1.PodDisruptionBudget
CurrentProcess Process CurrentProcess Process
Worker uint32 Worker uint32
PostgresClusterStatus acidv1.PostgresStatus Status acidv1.PostgresStatus
Spec acidv1.PostgresSpec Spec acidv1.PostgresSpec
Error error Error error
} }

View File

@ -29,7 +29,7 @@ func (c *Controller) ClusterStatus(team, namespace, cluster string) (*cluster.Cl
return nil, fmt.Errorf("could not find cluster") return nil, fmt.Errorf("could not find cluster")
} }
status := cl.GetPostgresClusterStatus() status := cl.GetStatus()
status.Worker = c.clusterWorkerID(clusterName) status.Worker = c.clusterWorkerID(clusterName)
return status, nil return status, nil

View File

@ -89,7 +89,7 @@ func (c *Controller) queueEvents(list *acidv1.PostgresqlList, event EventType) {
activeClustersCnt++ activeClustersCnt++
// check if that cluster needs repair // check if that cluster needs repair
if event == EventRepair { if event == EventRepair {
if pg.PostgresClusterStatus.Success() { if pg.Status.Success() {
continue continue
} else { } else {
clustersToRepair++ clustersToRepair++