diff --git a/pkg/apis/acid.zalan.do/v1/marshal.go b/pkg/apis/acid.zalan.do/v1/marshal.go index 8ad79eb0d..d180f784c 100644 --- a/pkg/apis/acid.zalan.do/v1/marshal.go +++ b/pkg/apis/acid.zalan.do/v1/marshal.go @@ -8,6 +8,7 @@ import ( ) type postgresqlCopy Postgresql +type postgresStatusCopy PostgresStatus // MarshalJSON converts a maintenance window definition to JSON. func (m *MaintenanceWindow) MarshalJSON() ([]byte, error) { @@ -71,25 +72,20 @@ func (m *MaintenanceWindow) UnmarshalJSON(data []byte) error { // UnmarshalJSON converts a JSON to the status subresource definition. func (ps *PostgresStatus) UnmarshalJSON(data []byte) error { - var got PostgresStatus + var ( + tmp postgresStatusCopy + status string + ) - str := string(data) - str = strings.Replace(str, "{", "", -1) - str = strings.Replace(str, "}", "", -1) - str = strings.Replace(str, "\"", "", -1) - parts := strings.Split(str, ":") - - if len(parts) == 2 || len(parts) == 3 { - if parts[1] != "" { - got.PostgresClusterStatus = parts[len(parts)-1] - } else { - got.PostgresClusterStatus = ClusterStatusUnknown + err := json.Unmarshal(data, &tmp) + if err != nil { + metaErr := json.Unmarshal(data, &status) + if metaErr != nil { + return fmt.Errorf("Could not parse status: %v; err %v", string(data), metaErr) } - } else { - return fmt.Errorf("incorrect status field of CR") + tmp.PostgresClusterStatus = status } - - *ps = got + *ps = PostgresStatus(tmp) return nil } diff --git a/pkg/apis/acid.zalan.do/v1/util_test.go b/pkg/apis/acid.zalan.do/v1/util_test.go index 84a434471..6b9f3df99 100644 --- a/pkg/apis/acid.zalan.do/v1/util_test.go +++ b/pkg/apis/acid.zalan.do/v1/util_test.go @@ -116,15 +116,13 @@ var postgresStatus = []struct { out PostgresStatus err error }{ - {[]byte(`"status":"Running"`), + {[]byte(`{"PostgresClusterStatus":"Running"}`), PostgresStatus{PostgresClusterStatus: ClusterStatusRunning}, nil}, - {[]byte(`"status":{"PostgresClusterStatus":"Running"}`), + {[]byte(`{"PostgresClusterStatus":""}`), + PostgresStatus{PostgresClusterStatus: ClusterStatusUnknown}, nil}, + {[]byte(`"Running"`), PostgresStatus{PostgresClusterStatus: ClusterStatusRunning}, nil}, - {[]byte(`"status":""`), - PostgresStatus{PostgresClusterStatus: ClusterStatusUnknown}, nil}, - {[]byte(`"status":{}`), - PostgresStatus{PostgresClusterStatus: ClusterStatusUnknown}, nil}, - {[]byte(`"status":`), + {[]byte(`""`), PostgresStatus{PostgresClusterStatus: ClusterStatusUnknown}, nil}} var unmarshalCluster = []struct {