MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / PatchWithOptimisticLock

Function PatchWithOptimisticLock

pkg/resources/status/patch.go:38–90  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

36// Important: after successfully updating the status, this
37// function refreshes it into the passed cluster
38func 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), &currentCluster); 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(&currentCluster, 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}

Callers 13

ReconcileFunction · 0.92
clearStaleUpgradeTargetFunction · 0.92
handleRollbackIfNeededFunction · 0.92
registerPhaseFunction · 0.92
startTransitionFunction · 0.92
ReconcileFunction · 0.92
reconcileImageMethod · 0.92
RegisterPhaseMethod · 0.92
instanceRestartFunction · 0.92
PromoteFunction · 0.92

Calls 6

DeepCopyMethod · 0.95
StatusMethod · 0.80
InfoMethod · 0.80
DeepCopyMethod · 0.45
GetMethod · 0.45
PatchMethod · 0.45

Tested by

no test coverage detected