/
desiredset.go
275 lines (240 loc) · 6.91 KB
/
desiredset.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
package apply
import (
"context"
"errors"
"fmt"
"github.com/rancher/wrangler/v2/pkg/apply/injectors"
"github.com/rancher/wrangler/v2/pkg/kv"
"github.com/rancher/wrangler/v2/pkg/merr"
"github.com/rancher/wrangler/v2/pkg/objectset"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/cache"
)
// Indexer name added for cached types
const byHash = "wrangler.byObjectSetHash"
type patchKey struct {
schema.GroupVersionKind
objectset.ObjectKey
}
type desiredSet struct {
a *apply
ctx context.Context
defaultNamespace string
listerNamespace string
ignorePreviousApplied bool
setOwnerReference bool
ownerReferenceController bool
ownerReferenceBlock bool
strictCaching bool
restrictClusterScoped bool
pruneTypes map[schema.GroupVersionKind]cache.SharedIndexInformer
patchers map[schema.GroupVersionKind]Patcher
reconcilers map[schema.GroupVersionKind]Reconciler
diffPatches map[patchKey][][]byte
informerFactory InformerFactory
remove bool
noDelete bool
noDeleteGVK map[schema.GroupVersionKind]struct{}
setID string
objs *objectset.ObjectSet
owner runtime.Object
injectors []injectors.ConfigInjector
ratelimitingQps float32
injectorNames []string
errs []error
createPlan bool
plan Plan
}
func (o *desiredSet) err(err error) error {
o.errs = append(o.errs, err)
return o.Err()
}
func (o desiredSet) Err() error {
return merr.NewErrors(append(o.errs, o.objs.Err())...)
}
func (o desiredSet) DryRun(objs ...runtime.Object) (Plan, error) {
o.objs = objectset.NewObjectSet()
o.objs.Add(objs...)
return o.dryRun()
}
func (o desiredSet) Apply(set *objectset.ObjectSet) error {
if set == nil {
set = objectset.NewObjectSet()
}
o.objs = set
return o.apply()
}
func (o desiredSet) ApplyObjects(objs ...runtime.Object) error {
os := objectset.NewObjectSet()
os.Add(objs...)
return o.Apply(os)
}
func (o desiredSet) WithDiffPatch(gvk schema.GroupVersionKind, namespace, name string, patch []byte) Apply {
patches := map[patchKey][][]byte{}
for k, v := range o.diffPatches {
patches[k] = v
}
key := patchKey{
GroupVersionKind: gvk,
ObjectKey: objectset.ObjectKey{
Name: name,
Namespace: namespace,
},
}
patches[key] = append(patches[key], patch)
o.diffPatches = patches
return o
}
// WithGVK uses a known listing of existing gvks to modify the the prune types to allow for deletion of objects
func (o desiredSet) WithGVK(gvks ...schema.GroupVersionKind) Apply {
pruneTypes := make(map[schema.GroupVersionKind]cache.SharedIndexInformer, len(gvks))
for k, v := range o.pruneTypes {
pruneTypes[k] = v
}
for _, gvk := range gvks {
pruneTypes[gvk] = nil
}
o.pruneTypes = pruneTypes
return o
}
func (o desiredSet) WithSetID(id string) Apply {
o.setID = id
return o
}
func (o desiredSet) WithOwnerKey(key string, gvk schema.GroupVersionKind) Apply {
obj := &v1.PartialObjectMetadata{}
obj.Namespace, obj.Name = kv.RSplit(key, "/")
obj.SetGroupVersionKind(gvk)
o.owner = obj
return o
}
func (o desiredSet) WithOwner(obj runtime.Object) Apply {
o.owner = obj
return o
}
func (o desiredSet) WithSetOwnerReference(controller, block bool) Apply {
o.setOwnerReference = true
o.ownerReferenceController = controller
o.ownerReferenceBlock = block
return o
}
func (o desiredSet) WithInjector(injs ...injectors.ConfigInjector) Apply {
o.injectors = append(o.injectors, injs...)
return o
}
func (o desiredSet) WithInjectorName(injs ...string) Apply {
o.injectorNames = append(o.injectorNames, injs...)
return o
}
func (o desiredSet) WithCacheTypeFactory(factory InformerFactory) Apply {
o.informerFactory = factory
return o
}
func (o desiredSet) WithIgnorePreviousApplied() Apply {
o.ignorePreviousApplied = true
return o
}
func (o desiredSet) WithCacheTypes(igs ...InformerGetter) Apply {
pruneTypes := make(map[schema.GroupVersionKind]cache.SharedIndexInformer, len(igs))
for k, v := range o.pruneTypes {
pruneTypes[k] = v
}
for _, ig := range igs {
informer := ig.Informer()
if err := addIndexerByHash(informer.GetIndexer()); err != nil {
// Ignore repeatedly adding the same indexer for different types
if !errors.Is(err, indexerAlreadyExistsErr) {
logrus.Warnf("Problem adding hash indexer to informer [%s]: %v", ig.GroupVersionKind().Kind, err)
}
}
pruneTypes[ig.GroupVersionKind()] = informer
}
o.pruneTypes = pruneTypes
return o
}
// addIndexerByHash an Informer to index objects by the hash annotation value
func addIndexerByHash(indexer cache.Indexer) error {
if _, alreadyAdded := indexer.GetIndexers()[byHash]; alreadyAdded {
return fmt.Errorf("adding indexer %q: %w", byHash, indexerAlreadyExistsErr)
}
return indexer.AddIndexers(map[string]cache.IndexFunc{
byHash: func(obj interface{}) ([]string, error) {
metadata, err := meta.Accessor(obj)
if err != nil {
return nil, err
}
labels := metadata.GetLabels()
if labels == nil || labels[LabelHash] == "" {
return nil, nil
}
return []string{labels[LabelHash]}, nil
},
})
}
func (o desiredSet) WithPatcher(gvk schema.GroupVersionKind, patcher Patcher) Apply {
patchers := map[schema.GroupVersionKind]Patcher{}
for k, v := range o.patchers {
patchers[k] = v
}
patchers[gvk] = patcher
o.patchers = patchers
return o
}
func (o desiredSet) WithReconciler(gvk schema.GroupVersionKind, reconciler Reconciler) Apply {
reconcilers := map[schema.GroupVersionKind]Reconciler{}
for k, v := range o.reconcilers {
reconcilers[k] = v
}
reconcilers[gvk] = reconciler
o.reconcilers = reconcilers
return o
}
func (o desiredSet) WithStrictCaching() Apply {
o.strictCaching = true
return o
}
func (o desiredSet) WithDynamicLookup() Apply {
o.strictCaching = false
return o
}
func (o desiredSet) WithRestrictClusterScoped() Apply {
o.restrictClusterScoped = true
return o
}
func (o desiredSet) WithDefaultNamespace(ns string) Apply {
if ns == "" {
o.defaultNamespace = defaultNamespace
} else {
o.defaultNamespace = ns
}
return o
}
func (o desiredSet) WithListerNamespace(ns string) Apply {
o.listerNamespace = ns
return o
}
func (o desiredSet) WithRateLimiting(ratelimitingQps float32) Apply {
o.ratelimitingQps = ratelimitingQps
return o
}
func (o desiredSet) WithNoDelete() Apply {
o.noDelete = true
return o
}
func (o desiredSet) WithNoDeleteGVK(gvks ...schema.GroupVersionKind) Apply {
if o.noDeleteGVK == nil {
o.noDeleteGVK = make(map[schema.GroupVersionKind]struct{})
}
for _, curr := range gvks {
o.noDeleteGVK[curr] = struct{}{}
}
return o
}
func (o desiredSet) WithContext(ctx context.Context) Apply {
o.ctx = ctx
return o
}