-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathserial.go
More file actions
60 lines (48 loc) · 1.81 KB
/
serial.go
File metadata and controls
60 lines (48 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Copyright 2022 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package patch
import (
"context"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// SerialPatcher provides serial patching of object using the patch helper. It
// remembers the state of the last patched object and uses that to calculate
// the patch aginst a new object.
type SerialPatcher struct {
client client.Client
beforeObject client.Object
}
// NewSerialPatcher returns a SerialPatcher with the given object as the initial
// base object for the patching operations.
func NewSerialPatcher(obj client.Object, c client.Client) *SerialPatcher {
return &SerialPatcher{
client: c,
beforeObject: obj.DeepCopyObject().(client.Object),
}
}
// Patch performs patching operation of the SerialPatcher and updates the
// beforeObject after a successful patch for subsequent patching.
func (sp *SerialPatcher) Patch(ctx context.Context, obj client.Object, options ...Option) error {
// Create a new patch helper with the before object.
patcher, err := NewHelper(sp.beforeObject, sp.client)
if err != nil {
return err
}
// Patch with the changes from the new object.
if err := patcher.Patch(ctx, obj, options...); err != nil {
return err
}
// Update the before object for next patch.
sp.beforeObject = obj.DeepCopyObject().(client.Object)
return nil
}