/
resourcePatcher.go
49 lines (41 loc) · 1.21 KB
/
resourcePatcher.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
package patch
import (
"context"
"encoding/json"
"errors"
"fmt"
)
type resourcePatcher struct {
applier jsonPatchApplier
}
// NewResourcePatcher creates a new resource patcher.
func NewResourcePatcher(applier jsonPatchApplier) *resourcePatcher {
return &resourcePatcher{applier: applier}
}
func filterPatchesByPhase(phase Phase, patches []ResourcePatch) []ResourcePatch {
var filtered []ResourcePatch
for _, patch := range patches {
if patch.Phase == phase {
filtered = append(filtered, patch)
}
}
return filtered
}
// Patch applies a configured patch in JSON patch format to a Kubernetes resource.
func (r *resourcePatcher) Patch(ctx context.Context, phase Phase, patches []ResourcePatch) error {
var errs []error
for _, patch := range filterPatchesByPhase(phase, patches) {
err := r.patchSingle(ctx, patch)
if err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
func (r *resourcePatcher) patchSingle(ctx context.Context, patch ResourcePatch) error {
jsonPatch, err := json.Marshal(patch.Patches)
if err != nil {
return fmt.Errorf("failed to marshal json patch: %w", err)
}
return r.applier.Patch(ctx, jsonPatch, patch.Resource.GroupVersionKind(), patch.Resource.Name)
}