...

Source file src/edge-infra.dev/pkg/k8s/runtime/patch/serial.go

Documentation: edge-infra.dev/pkg/k8s/runtime/patch

     1  package patch
     2  
     3  import (
     4  	"context"
     5  
     6  	"sigs.k8s.io/controller-runtime/pkg/client"
     7  )
     8  
     9  // SerialPatcher provides serial patching of object using the patch helper. It
    10  // remembers the state of the last patched object and uses that to calculate
    11  // the patch against a new object.
    12  type SerialPatcher struct {
    13  	client       client.Client
    14  	beforeObject client.Object
    15  }
    16  
    17  // NewSerialPatcher returns a SerialPatcher with the given object as the initial
    18  // base object for the patching operations.
    19  func NewSerialPatcher(obj client.Object, c client.Client) *SerialPatcher {
    20  	return &SerialPatcher{
    21  		client:       c,
    22  		beforeObject: obj.DeepCopyObject().(client.Object),
    23  	}
    24  }
    25  
    26  // Patch performs patching operation of the SerialPatcher and updates the
    27  // beforeObject after a successful patch for subsequent patching.
    28  func (sp *SerialPatcher) Patch(ctx context.Context, obj client.Object, options ...Option) error {
    29  	// Create a new patch helper with the before object.
    30  	patcher, err := NewHelper(sp.beforeObject, sp.client)
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	// Patch with the changes from the new object.
    36  	if err := patcher.Patch(ctx, obj, options...); err != nil {
    37  		return err
    38  	}
    39  
    40  	// Update the before object for next patch.
    41  	sp.beforeObject = obj.DeepCopyObject().(client.Object)
    42  
    43  	return nil
    44  }
    45  

View as plain text