-
Notifications
You must be signed in to change notification settings - Fork 214
/
patch.go
301 lines (251 loc) · 8.27 KB
/
patch.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package object_patch
import (
"bytes"
"context"
"fmt"
"time"
"github.com/hashicorp/go-multierror"
gerror "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
)
type ObjectPatcher struct {
kubeClient KubeClient
logger *log.Entry
}
type KubeClient interface {
kubernetes.Interface
Dynamic() dynamic.Interface
GroupVersionResource(apiVersion string, kind string) (schema.GroupVersionResource, error)
}
func NewObjectPatcher(kubeClient KubeClient) *ObjectPatcher {
return &ObjectPatcher{
kubeClient: kubeClient,
logger: log.WithField("operator.component", "KubernetesObjectPatcher"),
}
}
func (o *ObjectPatcher) ExecuteOperations(ops []Operation) error {
log.Debug("Starting execute operations process")
defer log.Debug("Finished execute operations process")
applyErrors := &multierror.Error{}
for _, op := range ops {
log.Debugf("Applying operation: %s", op.Description())
if err := o.ExecuteOperation(op); err != nil {
err = gerror.WithMessage(err, op.Description())
applyErrors = multierror.Append(applyErrors, err)
}
}
return applyErrors.ErrorOrNil()
}
func (o *ObjectPatcher) ExecuteOperation(operation Operation) error {
if operation == nil {
return nil
}
switch v := operation.(type) {
case *createOperation:
return o.executeCreateOperation(v)
case *deleteOperation:
return o.executeDeleteOperation(v)
case *patchOperation:
return o.executePatchOperation(v)
case *filterOperation:
return o.executeFilterOperation(v)
}
return nil
}
func (o *ObjectPatcher) executeCreateOperation(op *createOperation) error {
if op.object == nil {
return fmt.Errorf("cannot create empty object")
}
// Convert object from interface{}.
object, err := toUnstructured(op.object)
if err != nil {
return err
}
apiVersion := object.GetAPIVersion()
kind := object.GetKind()
wrapErr := func(e error) error {
objectID := fmt.Sprintf("%s/%s/%s/%s", apiVersion, kind, object.GetNamespace(), object.GetName())
return gerror.WithMessage(e, objectID)
}
gvk, err := o.kubeClient.GroupVersionResource(apiVersion, kind)
if err != nil {
return wrapErr(err)
}
log.Debug("Started Create API call")
_, err = o.kubeClient.Dynamic().
Resource(gvk).
Namespace(object.GetNamespace()).
Create(context.TODO(), object, metav1.CreateOptions{}, generateSubresources(op.subresource)...)
log.Debug("Finished Create API call")
objectExists := errors.IsAlreadyExists(err)
if objectExists && op.ignoreIfExists {
log.Debug("resource already exists, exiting without error")
return nil
}
if objectExists && op.updateIfExists {
log.Debug("Object already exists, attempting to Update it with optimistic lock")
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
log.Debug("Started Get API call")
existingObj, err := o.kubeClient.Dynamic().
Resource(gvk).
Namespace(object.GetNamespace()).
Get(context.TODO(), object.GetName(), metav1.GetOptions{}, generateSubresources(op.subresource)...)
log.Debug("Finished Get API call")
if err != nil {
return wrapErr(err)
}
objCopy := object.DeepCopy()
objCopy.SetResourceVersion(existingObj.GetResourceVersion())
log.Debug("Started Update API call")
_, err = o.kubeClient.Dynamic().
Resource(gvk).
Namespace(objCopy.GetNamespace()).
Update(context.TODO(), objCopy, metav1.UpdateOptions{}, generateSubresources(op.subresource)...)
log.Debug("Finished Update API call")
return wrapErr(err)
})
}
// Simply return result of a Create call if no ignore options are in play.
return wrapErr(err)
}
// executePatchOperation applies a patch to the specified object using API call Patch.
//
// There 2 types of patches:
// - Merge — use Patch API call with MergePatchType.
// - JSON — use Patch API call with JSONPatchType.
//
// Other options:
// - WithSubresource — a subresource argument for Patch or Update API call.
// - IgnoreMissingObject — do not return error if the specified object is missing.
// - IgnoreHookError — allows applying patches for a Status subresource even if the hook fails
func (o *ObjectPatcher) executePatchOperation(op *patchOperation) error {
if op.patchType == types.MergePatchType {
log.Debug("Started MergePatchObject")
defer log.Debug("Finished MergePatchObject")
}
if op.patchType == types.JSONPatchType {
log.Debug("Started JSONPatchObject")
defer log.Debug("Finished JSONPatchObject")
}
patchBytes, err := convertPatchToBytes(op.patch)
if err != nil {
return fmt.Errorf("encode %s patch for %s/%s/%s/%s: %v", op.patchType, op.apiVersion, op.kind, op.namespace, op.name, err)
}
if patchBytes == nil {
return fmt.Errorf("%s patch is nil for %s/%s/%s/%s", op.patchType, op.apiVersion, op.kind, op.namespace, op.name)
}
gvk, err := o.kubeClient.GroupVersionResource(op.apiVersion, op.kind)
if err != nil {
return err
}
log.Debug("Started Patch API call")
_, err = o.kubeClient.Dynamic().
Resource(gvk).
Namespace(op.namespace).
Patch(context.TODO(), op.name, op.patchType, patchBytes, metav1.PatchOptions{}, generateSubresources(op.subresource)...)
log.Debug("Finished Patch API call")
if op.ignoreMissingObject && errors.IsNotFound(err) {
return nil
}
return err
}
// executeFilterOperation retrieves a specified object, modified it with
// filterFunc and calls update.
// Other options:
// - WithSubresource — a subresource argument for Patch or Update API call.
// - IgnoreMissingObject — do not return error if the specified object is missing.
// - IgnoreHookError — allows applying patches for a Status subresource even if the hook fails
func (o *ObjectPatcher) executeFilterOperation(op *filterOperation) error {
var err error
if op.filterFunc == nil {
return fmt.Errorf("FilterFunc is nil")
}
gvk, err := o.kubeClient.GroupVersionResource(op.apiVersion, op.kind)
if err != nil {
return err
}
err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
log.Debug("Started Get API call")
obj, err := o.kubeClient.Dynamic().
Resource(gvk).
Namespace(op.namespace).
Get(context.TODO(), op.name, metav1.GetOptions{})
log.Debug("Finished Get API call")
if op.ignoreMissingObject && errors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
log.Debug("Started filtering object")
filteredObj, err := op.filterFunc(obj)
log.Debug("Finished filtering object")
if err != nil {
return err
}
if equality.Semantic.DeepEqual(obj, filteredObj) {
return nil
}
var filteredObjBuf bytes.Buffer
err = unstructured.UnstructuredJSONScheme.Encode(filteredObj, &filteredObjBuf)
if err != nil {
return err
}
log.Debug("Started Update API call")
_, err = o.kubeClient.Dynamic().
Resource(gvk).
Namespace(op.namespace).
Update(context.TODO(), filteredObj, metav1.UpdateOptions{}, generateSubresources(op.subresource)...)
log.Debug("Finished Update API call")
if err != nil {
return err
}
return nil
})
return err
}
func (o *ObjectPatcher) executeDeleteOperation(op *deleteOperation) error {
gvk, err := o.kubeClient.GroupVersionResource(op.apiVersion, op.kind)
if err != nil {
return err
}
log.Debug("Started Delete API call")
err = o.kubeClient.Dynamic().
Resource(gvk).
Namespace(op.namespace).
Delete(context.TODO(), op.name, metav1.DeleteOptions{PropagationPolicy: &op.deletionPropagation}, op.subresource)
log.Debug("Finished Delete API call")
if errors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
if op.deletionPropagation != metav1.DeletePropagationForeground {
return nil
}
log.Debug("Waiting for object deletion")
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, 20*time.Second, false, func(ctx context.Context) (done bool, err error) {
log.Debug("Started Get API call")
_, err = o.kubeClient.Dynamic().
Resource(gvk).
Namespace(op.namespace).
Get(ctx, op.name, metav1.GetOptions{})
log.Debug("Finished Get API call")
if errors.IsNotFound(err) {
return true, nil
}
return false, err
})
return err
}