-
Notifications
You must be signed in to change notification settings - Fork 214
/
operation.go
263 lines (230 loc) · 7.67 KB
/
operation.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
package object_patch
import (
"fmt"
"github.com/hashicorp/go-multierror"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
)
// A JSON and YAML representation of the operation for shell hooks
type OperationSpec struct {
Operation OperationType `json:"operation" yaml:"operation"`
ApiVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Subresource string `json:"subresource,omitempty" yaml:"subresource,omitempty"`
Object interface{} `json:"object,omitempty" yaml:"object,omitempty"`
JQFilter string `json:"jqFilter,omitempty" yaml:"jqFilter,omitempty"`
MergePatch interface{} `json:"mergePatch,omitempty" yaml:"mergePatch,omitempty"`
JSONPatch interface{} `json:"jsonPatch,omitempty" yaml:"jsonPatch,omitempty"`
IgnoreMissingObject bool `json:"ignoreMissingObject" yaml:"ignoreMissingObject"`
}
type OperationType string
const (
CreateOrUpdate OperationType = "CreateOrUpdate"
Create OperationType = "Create"
CreateIfNotExists OperationType = "CreateIfNotExists"
Delete OperationType = "Delete"
DeleteInBackground OperationType = "DeleteInBackground"
DeleteNonCascading OperationType = "DeleteNonCascading"
JQPatch OperationType = "JQPatch"
MergePatch OperationType = "MergePatch"
JSONPatch OperationType = "JSONPatch"
)
func ParseOperations(specBytes []byte) ([]Operation, error) {
log.Debugf("parsing patcher operations:\n%s", specBytes)
specs, err := unmarshalFromJSONOrYAML(specBytes)
if err != nil {
return nil, err
}
validationErrors := &multierror.Error{}
ops := make([]Operation, 0)
for _, spec := range specs {
err = ValidateOperationSpec(spec, GetSchema("v0"), "")
if err != nil {
validationErrors = multierror.Append(validationErrors, err)
break
}
ops = append(ops, NewFromOperationSpec(spec))
}
return ops, validationErrors.ErrorOrNil()
}
// Operation is a command for ObjectPatcher.
//
// There are 4 types of operations:
//
// - createOperation to create or update object via Create and Update API calls. Unstructured, map[string]interface{} or runtime.Object is required.
//
// - deleteOperation to delete object via Delete API call. deletionPropagation should be set, default is Foregound.
//
// - patchOperation to modify object via Patch API call. patchType should be set. patch can be string, []byte or map[string]interface{}
//
// - filterOperation to modify object via Get-filter-Update process. filterFunc should be set.
type Operation interface {
Description() string
}
type createOperation struct {
object interface{}
subresource string
ignoreIfExists bool
updateIfExists bool
}
func (op *createOperation) Description() string {
return "Create object"
}
type deleteOperation struct {
// Object coordinates.
apiVersion string
kind string
namespace string
name string
subresource string
// Delete options.
deletionPropagation metav1.DeletionPropagation
}
func (op *deleteOperation) Description() string {
return fmt.Sprintf("Delete object %s/%s/%s/%s", op.apiVersion, op.kind, op.namespace, op.name)
}
type patchOperation struct {
// Object coordinates for patch and delete.
apiVersion string
kind string
namespace string
name string
subresource string
// Patch options.
patchType types.PatchType
patch interface{}
ignoreMissingObject bool
}
func (op *patchOperation) Description() string {
return fmt.Sprintf("Patch object %s/%s/%s/%s using %s patch", op.apiVersion, op.kind, op.namespace, op.name, op.patchType)
}
type filterOperation struct {
// Object coordinates for patch and delete.
apiVersion string
kind string
namespace string
name string
subresource string
// Patch options.
filterFunc func(*unstructured.Unstructured) (*unstructured.Unstructured, error)
ignoreMissingObject bool
}
func (op *filterOperation) Description() string {
return fmt.Sprintf("Filter object %s/%s/%s/%s", op.apiVersion, op.kind, op.namespace, op.name)
}
func NewFromOperationSpec(spec OperationSpec) Operation {
switch spec.Operation {
case Create:
return NewCreateOperation(spec.Object,
WithSubresource(spec.Subresource))
case CreateIfNotExists:
return NewCreateOperation(spec.Object,
WithSubresource(spec.Subresource),
IgnoreIfExists())
case CreateOrUpdate:
return NewCreateOperation(spec.Object,
WithSubresource(spec.Subresource),
UpdateIfExists())
case Delete:
return NewDeleteOperation(spec.ApiVersion, spec.Kind, spec.Namespace, spec.Name,
WithSubresource(spec.Subresource))
case DeleteInBackground:
return NewDeleteOperation(spec.ApiVersion, spec.Kind, spec.Namespace, spec.Name,
WithSubresource(spec.Subresource),
InBackground())
case DeleteNonCascading:
return NewDeleteOperation(spec.ApiVersion, spec.Kind, spec.Namespace, spec.Name,
WithSubresource(spec.Subresource),
NonCascading())
case JQPatch:
return NewFilterPatchOperation(
func(u *unstructured.Unstructured) (*unstructured.Unstructured, error) {
return applyJQPatch(spec.JQFilter, u)
},
spec.ApiVersion, spec.Kind, spec.Namespace, spec.Name,
WithSubresource(spec.Subresource),
WithIgnoreMissingObject(spec.IgnoreMissingObject),
)
case MergePatch:
return NewMergePatchOperation(spec.MergePatch,
spec.ApiVersion, spec.Kind, spec.Namespace, spec.Name,
WithSubresource(spec.Subresource),
WithIgnoreMissingObject(spec.IgnoreMissingObject),
)
case JSONPatch:
return NewJSONPatchOperation(spec.JSONPatch,
spec.ApiVersion, spec.Kind, spec.Namespace, spec.Name,
WithSubresource(spec.Subresource),
WithIgnoreMissingObject(spec.IgnoreMissingObject),
)
}
// Should not be reached!
return nil
}
func NewCreateOperation(obj interface{}, options ...CreateOption) Operation {
op := &createOperation{
object: obj,
}
for _, option := range options {
option.applyToCreate(op)
}
return op
}
func NewDeleteOperation(apiVersion, kind, namespace, name string, options ...DeleteOption) Operation {
op := &deleteOperation{
apiVersion: apiVersion,
kind: kind,
namespace: namespace,
name: name,
deletionPropagation: metav1.DeletePropagationForeground,
}
for _, option := range options {
option.applyToDelete(op)
}
return op
}
func NewMergePatchOperation(mergePatch interface{}, apiVersion, kind, namespace, name string, options ...PatchOption) Operation {
op := &patchOperation{
apiVersion: apiVersion,
kind: kind,
namespace: namespace,
name: name,
patch: mergePatch,
patchType: types.MergePatchType,
}
for _, option := range options {
option.applyToPatch(op)
}
return op
}
func NewJSONPatchOperation(jsonPatch interface{}, apiVersion, kind, namespace, name string, options ...PatchOption) Operation {
op := &patchOperation{
apiVersion: apiVersion,
kind: kind,
namespace: namespace,
name: name,
patch: jsonPatch,
patchType: types.JSONPatchType,
}
for _, option := range options {
option.applyToPatch(op)
}
return op
}
func NewFilterPatchOperation(filter func(*unstructured.Unstructured) (*unstructured.Unstructured, error), apiVersion, kind, namespace, name string, options ...FilterOption) Operation {
op := &filterOperation{
apiVersion: apiVersion,
kind: kind,
namespace: namespace,
name: name,
filterFunc: filter,
}
for _, option := range options {
option.applyToFilter(op)
}
return op
}