diff --git a/pkg/apis/acid.zalan.do/v1/util.go b/pkg/apis/acid.zalan.do/v1/util.go index d8e9140d8..c30b8c631 100644 --- a/pkg/apis/acid.zalan.do/v1/util.go +++ b/pkg/apis/acid.zalan.do/v1/util.go @@ -102,5 +102,5 @@ func (postgresStatus PostgresStatus) Creating() bool { } func (postgresStatus PostgresStatus) String() string { - return fmt.Sprintf(`status=%s`, postgresStatus.PostgresClusterStatus) + return fmt.Sprintf(`"status":{"PostgresClusterStatus": %s}`, postgresStatus.PostgresClusterStatus) } diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 8840e2d6a..dd363053c 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -4,6 +4,7 @@ package cluster import ( "database/sql" + "encoding/json" "fmt" "reflect" "regexp" @@ -19,8 +20,6 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/cache" - "encoding/json" - acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1" "github.com/zalando/postgres-operator/pkg/spec" "github.com/zalando/postgres-operator/pkg/util" @@ -149,9 +148,9 @@ func (c *Cluster) setProcessName(procName string, args ...interface{}) { } } -func (c *Cluster) setStatus(status string) { +func (c *Cluster) SetStatus(status string) { // TODO: eventually switch to updateStatus() for kubernetes 1.11 and above - patch, err := json.Marshal(&acidv1.PostgresStatus{PostgresClusterStatus: status}) + patch, err := json.Marshal(acidv1.PostgresStatus{PostgresClusterStatus: status}) if err != nil { c.logger.Errorf("could not marshal status: %v", err) } @@ -210,13 +209,13 @@ func (c *Cluster) Create() error { defer func() { if err == nil { - c.setStatus(acidv1.ClusterStatusRunning) //TODO: are you sure it's running? + c.SetStatus(acidv1.ClusterStatusRunning) //TODO: are you sure it's running? } else { - c.setStatus(acidv1.ClusterStatusAddFailed) + c.SetStatus(acidv1.ClusterStatusAddFailed) } }() - c.setStatus(acidv1.ClusterStatusCreating) + c.SetStatus(acidv1.ClusterStatusCreating) for _, role := range []PostgresRole{Master, Replica} { @@ -483,14 +482,14 @@ func (c *Cluster) Update(oldSpec, newSpec *acidv1.Postgresql) error { c.mu.Lock() defer c.mu.Unlock() - c.setStatus(acidv1.ClusterStatusUpdating) + c.SetStatus(acidv1.ClusterStatusUpdating) c.setSpec(newSpec) defer func() { if updateFailed { - c.setStatus(acidv1.ClusterStatusUpdateFailed) + c.SetStatus(acidv1.ClusterStatusUpdateFailed) } else { - c.setStatus(acidv1.ClusterStatusRunning) + c.SetStatus(acidv1.ClusterStatusRunning) } }() diff --git a/pkg/cluster/cluster_test.go b/pkg/cluster/cluster_test.go index 4f95a70cb..0940dd034 100644 --- a/pkg/cluster/cluster_test.go +++ b/pkg/cluster/cluster_test.go @@ -20,10 +20,20 @@ const ( ) var logger = logrus.New().WithField("test", "cluster") -var cl = New(Config{OpConfig: config.Config{ProtectedRoles: []string{"admin"}, - Auth: config.Auth{SuperUsername: superUserName, - ReplicationUsername: replicationUserName}}}, - k8sutil.KubernetesClient{}, acidv1.Postgresql{}, logger) +var cl = New( + Config{ + OpConfig: config.Config{ + ProtectedRoles: []string{"admin"}, + Auth: config.Auth{ + SuperUsername: superUserName, + ReplicationUsername: replicationUserName, + }, + }, + }, + k8sutil.KubernetesClient{}, + acidv1.Postgresql{}, + logger, +) func TestInitRobotUsers(t *testing.T) { testName := "TestInitRobotUsers" @@ -318,31 +328,3 @@ func TestShouldDeleteSecret(t *testing.T) { } } } - -func TestSetStatus(t *testing.T) { - - tests := []struct { - status acidv1.PostgresStatus - outcome bool - }{ - { - status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusCreating}, - outcome: cl.Status.Creating(), - }, - { - status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusRunning}, - outcome: cl.Status.Running(), - }, - { - status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusSyncFailed}, - outcome: !cl.Status.Success(), - }, - } - - for _, tt := range tests { - cl.setStatus(tt.status.PostgresClusterStatus) - if tt.outcome { - t.Errorf("Wrong status: %s", cl.Status.String()) - } - } -} diff --git a/pkg/cluster/sync.go b/pkg/cluster/sync.go index 9ba2ee40a..259f421bf 100644 --- a/pkg/cluster/sync.go +++ b/pkg/cluster/sync.go @@ -27,9 +27,9 @@ func (c *Cluster) Sync(newSpec *acidv1.Postgresql) error { defer func() { if err != nil { c.logger.Warningf("error while syncing cluster state: %v", err) - c.setStatus(acidv1.ClusterStatusSyncFailed) + c.SetStatus(acidv1.ClusterStatusSyncFailed) } else if !c.Status.Running() { - c.setStatus(acidv1.ClusterStatusRunning) + c.SetStatus(acidv1.ClusterStatusRunning) } }() diff --git a/pkg/controller/util_test.go b/pkg/controller/util_test.go index cb782904c..ae2ab5147 100644 --- a/pkg/controller/util_test.go +++ b/pkg/controller/util_test.go @@ -6,12 +6,13 @@ import ( "testing" b64 "encoding/base64" + + acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1" + "github.com/zalando/postgres-operator/pkg/spec" + "github.com/zalando/postgres-operator/pkg/util/k8sutil" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1core "k8s.io/client-go/kubernetes/typed/core/v1" - - "github.com/zalando/postgres-operator/pkg/spec" - "github.com/zalando/postgres-operator/pkg/util/k8sutil" ) const ( @@ -186,3 +187,33 @@ func TestGetInfrastructureRoles(t *testing.T) { } } } + +func TestSetStatus(t *testing.T) { + + for _, cl := range c.clusters { + tests := []struct { + status acidv1.PostgresStatus + outcome bool + }{ + { + status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusCreating}, + outcome: cl.Status.Creating(), + }, + { + status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusRunning}, + outcome: cl.Status.Running(), + }, + { + status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusInvalid}, + outcome: !cl.Status.Success(), + }, + } + + for _, tt := range tests { + cl.SetStatus(tt.status.PostgresClusterStatus) + if tt.outcome { + t.Errorf("Wrong status: %s", cl.Status.String()) + } + } + } +}