@@ -32,6 +32,7 @@ import (
3232 v1 "k8s.io/api/core/v1"
3333 policyv1 "k8s.io/api/policy/v1"
3434 rbacv1 "k8s.io/api/rbac/v1"
35+ "k8s.io/apimachinery/pkg/api/equality"
3536 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3637 "k8s.io/apimachinery/pkg/types"
3738 "k8s.io/client-go/rest"
@@ -271,26 +272,29 @@ func (c *Cluster) Create() (err error) {
271272 )
272273
273274 defer func () {
274- var (
275- pgUpdatedStatus * acidv1.Postgresql
276- errStatus error
277- )
278- if err == nil {
279- pgUpdatedStatus , errStatus = c .KubeClient .SetPostgresCRDStatus (c .clusterName (), acidv1 .ClusterStatusRunning ) //TODO: are you sure it's running?
280- } else {
275+ currentStatus := c .Status .DeepCopy ()
276+ pg := c .Postgresql .DeepCopy ()
277+ pg .Status .PostgresClusterStatus = acidv1 .ClusterStatusRunning
278+
279+ if err != nil {
281280 c .logger .Warningf ("cluster created failed: %v" , err )
282- pgUpdatedStatus , errStatus = c .KubeClient .SetPostgresCRDStatus (c .clusterName (), acidv1 .ClusterStatusAddFailed )
283- }
284- if errStatus != nil {
285- c .logger .Warningf ("could not set cluster status: %v" , errStatus )
286- return
281+ pg .Status .PostgresClusterStatus = acidv1 .ClusterStatusAddFailed
287282 }
288- if pgUpdatedStatus != nil {
283+
284+ if ! equality .Semantic .DeepEqual (currentStatus , pg .Status ) {
285+ pgUpdatedStatus , err := c .KubeClient .SetPostgresCRDStatus (c .clusterName (), pg )
286+ if err != nil {
287+ c .logger .Warningf ("could not set cluster status: %v" , err )
288+ return
289+ }
289290 c .setSpec (pgUpdatedStatus )
290291 }
291292 }()
292293
293- pgCreateStatus , err = c .KubeClient .SetPostgresCRDStatus (c .clusterName (), acidv1 .ClusterStatusCreating )
294+ pg := c .Postgresql .DeepCopy ()
295+ pg .Status .PostgresClusterStatus = acidv1 .ClusterStatusCreating
296+
297+ pgCreateStatus , err = c .KubeClient .SetPostgresCRDStatus (c .clusterName (), pg )
294298 if err != nil {
295299 return fmt .Errorf ("could not set cluster status: %v" , err )
296300 }
@@ -978,7 +982,12 @@ func (c *Cluster) Update(oldSpec, newSpec *acidv1.Postgresql) error {
978982 c .mu .Lock ()
979983 defer c .mu .Unlock ()
980984
981- c .KubeClient .SetPostgresCRDStatus (c .clusterName (), acidv1 .ClusterStatusUpdating )
985+ newSpec .Status .PostgresClusterStatus = acidv1 .ClusterStatusUpdating
986+
987+ newSpec , err := c .KubeClient .SetPostgresCRDStatus (c .clusterName (), newSpec )
988+ if err != nil {
989+ return fmt .Errorf ("could not set cluster status to updating: %w" , err )
990+ }
982991
983992 if ! isInMaintenanceWindow (newSpec .Spec .MaintenanceWindows ) {
984993 // do not apply any major version related changes yet
@@ -987,20 +996,19 @@ func (c *Cluster) Update(oldSpec, newSpec *acidv1.Postgresql) error {
987996 c .setSpec (newSpec )
988997
989998 defer func () {
990- var (
991- pgUpdatedStatus * acidv1.Postgresql
992- err error
993- )
999+ currentStatus := newSpec .Status .DeepCopy ()
1000+ newSpec .Status .PostgresClusterStatus = acidv1 .ClusterStatusRunning
1001+
9941002 if updateFailed {
995- pgUpdatedStatus , err = c .KubeClient .SetPostgresCRDStatus (c .clusterName (), acidv1 .ClusterStatusUpdateFailed )
996- } else {
997- pgUpdatedStatus , err = c .KubeClient .SetPostgresCRDStatus (c .clusterName (), acidv1 .ClusterStatusRunning )
998- }
999- if err != nil {
1000- c .logger .Warningf ("could not set cluster status: %v" , err )
1001- return
1003+ newSpec .Status .PostgresClusterStatus = acidv1 .ClusterStatusUpdateFailed
10021004 }
1003- if pgUpdatedStatus != nil {
1005+
1006+ if ! equality .Semantic .DeepEqual (currentStatus , newSpec .Status ) {
1007+ pgUpdatedStatus , err := c .KubeClient .SetPostgresCRDStatus (c .clusterName (), newSpec )
1008+ if err != nil {
1009+ c .logger .Warningf ("could not set cluster status: %v" , err )
1010+ return
1011+ }
10041012 c .setSpec (pgUpdatedStatus )
10051013 }
10061014 }()
0 commit comments