-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatches.go
More file actions
135 lines (116 loc) · 5.31 KB
/
patches.go
File metadata and controls
135 lines (116 loc) · 5.31 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
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
package framework
import (
"sync"
"github.com/deckhouse/module-sdk/pkg"
)
// PatchType identifies a recorded patch operation kind.
type PatchType string
const (
PatchTypeCreate PatchType = "Create"
PatchTypeCreateOrUpdate PatchType = "CreateOrUpdate"
PatchTypeCreateIfNotExists PatchType = "CreateIfNotExists"
PatchTypeDelete PatchType = "Delete"
PatchTypeDeleteInBackground PatchType = "DeleteInBackground"
PatchTypeDeleteNonCascading PatchType = "DeleteNonCascading"
PatchTypeJSONPatch PatchType = "JSONPatch"
PatchTypeMergePatch PatchType = "MergePatch"
PatchTypeJQFilter PatchType = "JQFilter"
)
// RecordedPatch is a structured copy of a single patch operation issued by
// a hook. It captures the operation type and all parameters so that tests
// can assert on the hook's intent.
type RecordedPatch struct {
Type PatchType
// For Create*: holds the runtime.Object / map / Unstructured.
Object any
// For Delete* / Patch* operations.
APIVersion string
Kind string
Namespace string
Name string
// For Patch operations.
JSONPatch any
MergePatch any
JQFilter string
// Original options as passed by the hook.
Options []pkg.PatchCollectorOption
}
// recordingPatchCollector is a pkg.PatchCollector that records every call as
// a RecordedPatch. It also implements pkg.PatchCollectorOperation per record
// so that hooks see the operations they would normally see.
type recordingPatchCollector struct {
mu sync.Mutex
records []RecordedPatch
}
func newRecordingPatchCollector() *recordingPatchCollector {
return &recordingPatchCollector{records: make([]RecordedPatch, 0)}
}
var _ pkg.PatchCollector = (*recordingPatchCollector)(nil)
func (c *recordingPatchCollector) add(r RecordedPatch) {
c.mu.Lock()
c.records = append(c.records, r)
c.mu.Unlock()
}
func (c *recordingPatchCollector) Records() []RecordedPatch {
c.mu.Lock()
defer c.mu.Unlock()
out := make([]RecordedPatch, len(c.records))
copy(out, c.records)
return out
}
func (c *recordingPatchCollector) Operations() []pkg.PatchCollectorOperation {
c.mu.Lock()
defer c.mu.Unlock()
ops := make([]pkg.PatchCollectorOperation, 0, len(c.records))
for i := range c.records {
ops = append(ops, &c.records[i])
}
return ops
}
// pkg.PatchCollectorOperation implementation.
func (r *RecordedPatch) Description() string { return string(r.Type) }
func (r *RecordedPatch) SetObjectPrefix(prefix string) {
if prefix == "" || r.Name == "" {
return
}
r.Name = prefix + "-" + r.Name
}
// === Create ===
func (c *recordingPatchCollector) Create(object any) {
c.add(RecordedPatch{Type: PatchTypeCreate, Object: object})
}
func (c *recordingPatchCollector) CreateIfNotExists(object any) {
c.add(RecordedPatch{Type: PatchTypeCreateIfNotExists, Object: object})
}
func (c *recordingPatchCollector) CreateOrUpdate(object any) {
c.add(RecordedPatch{Type: PatchTypeCreateOrUpdate, Object: object})
}
// === Delete ===
func (c *recordingPatchCollector) Delete(apiVersion, kind, namespace, name string) {
c.add(RecordedPatch{Type: PatchTypeDelete, APIVersion: apiVersion, Kind: kind, Namespace: namespace, Name: name})
}
func (c *recordingPatchCollector) DeleteInBackground(apiVersion, kind, namespace, name string) {
c.add(RecordedPatch{Type: PatchTypeDeleteInBackground, APIVersion: apiVersion, Kind: kind, Namespace: namespace, Name: name})
}
func (c *recordingPatchCollector) DeleteNonCascading(apiVersion, kind, namespace, name string) {
c.add(RecordedPatch{Type: PatchTypeDeleteNonCascading, APIVersion: apiVersion, Kind: kind, Namespace: namespace, Name: name})
}
// === Patch ===
func (c *recordingPatchCollector) JSONPatch(jsonPatch any, apiVersion, kind, namespace, name string, opts ...pkg.PatchCollectorOption) {
c.add(RecordedPatch{Type: PatchTypeJSONPatch, APIVersion: apiVersion, Kind: kind, Namespace: namespace, Name: name, JSONPatch: jsonPatch, Options: opts})
}
func (c *recordingPatchCollector) MergePatch(mergePatch any, apiVersion, kind, namespace, name string, opts ...pkg.PatchCollectorOption) {
c.add(RecordedPatch{Type: PatchTypeMergePatch, APIVersion: apiVersion, Kind: kind, Namespace: namespace, Name: name, MergePatch: mergePatch, Options: opts})
}
func (c *recordingPatchCollector) JQFilter(jqfilter, apiVersion, kind, namespace, name string, opts ...pkg.PatchCollectorOption) {
c.add(RecordedPatch{Type: PatchTypeJQFilter, APIVersion: apiVersion, Kind: kind, Namespace: namespace, Name: name, JQFilter: jqfilter, Options: opts})
}
func (c *recordingPatchCollector) PatchWithJSON(jsonPatch any, apiVersion, kind, namespace, name string, opts ...pkg.PatchCollectorOption) {
c.add(RecordedPatch{Type: PatchTypeJSONPatch, APIVersion: apiVersion, Kind: kind, Namespace: namespace, Name: name, JSONPatch: jsonPatch, Options: opts})
}
func (c *recordingPatchCollector) PatchWithMerge(mergePatch any, apiVersion, kind, namespace, name string, opts ...pkg.PatchCollectorOption) {
c.add(RecordedPatch{Type: PatchTypeMergePatch, APIVersion: apiVersion, Kind: kind, Namespace: namespace, Name: name, MergePatch: mergePatch, Options: opts})
}
func (c *recordingPatchCollector) PatchWithJQ(jqfilter, apiVersion, kind, namespace, name string, opts ...pkg.PatchCollectorOption) {
c.add(RecordedPatch{Type: PatchTypeJQFilter, APIVersion: apiVersion, Kind: kind, Namespace: namespace, Name: name, JQFilter: jqfilter, Options: opts})
}