PatchWithOptimisticLock updates the status of the cluster using the passed transaction functions (in the given order). Important: after successfully updating the status, this function refreshes it into the passed cluster
( ctx context.Context, c client.Client, cluster *apiv1.Cluster, txs ...Transaction, )
| 36 | // Important: after successfully updating the status, this |
| 37 | // function refreshes it into the passed cluster |
| 38 | func PatchWithOptimisticLock( |
| 39 | ctx context.Context, |
| 40 | c client.Client, |
| 41 | cluster *apiv1.Cluster, |
| 42 | txs ...Transaction, |
| 43 | ) error { |
| 44 | if cluster == nil { |
| 45 | return nil |
| 46 | } |
| 47 | |
| 48 | contextLogger := log.FromContext(ctx) |
| 49 | |
| 50 | origCluster := cluster.DeepCopy() |
| 51 | |
| 52 | if err := retry.RetryOnConflict(retry.DefaultBackoff, func() error { |
| 53 | var currentCluster apiv1.Cluster |
| 54 | if err := c.Get(ctx, client.ObjectKeyFromObject(cluster), ¤tCluster); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | updatedCluster := currentCluster.DeepCopy() |
| 59 | for _, tx := range txs { |
| 60 | tx(updatedCluster) |
| 61 | } |
| 62 | |
| 63 | if equality.Semantic.DeepEqual(currentCluster.Status, updatedCluster.Status) { |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | if err := c.Status().Patch( |
| 68 | ctx, |
| 69 | updatedCluster, |
| 70 | client.MergeFromWithOptions(¤tCluster, client.MergeFromWithOptimisticLock{}), |
| 71 | ); err != nil { |
| 72 | return err |
| 73 | } |
| 74 | |
| 75 | cluster.Status = updatedCluster.Status |
| 76 | |
| 77 | return nil |
| 78 | }); err != nil { |
| 79 | return fmt.Errorf("while patching status: %w", err) |
| 80 | } |
| 81 | |
| 82 | if cluster.Status.Phase != apiv1.PhaseHealthy && origCluster.Status.Phase == apiv1.PhaseHealthy { |
| 83 | contextLogger.Info("Cluster has become unhealthy") |
| 84 | } |
| 85 | if cluster.Status.Phase == apiv1.PhaseHealthy && origCluster.Status.Phase != apiv1.PhaseHealthy { |
| 86 | contextLogger.Info("Cluster has become healthy") |
| 87 | } |
| 88 | |
| 89 | return nil |
| 90 | } |
no test coverage detected