-
Notifications
You must be signed in to change notification settings - Fork 48
/
hook_test.go
336 lines (318 loc) · 12.4 KB
/
hook_test.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Copyright 2020 The Operator-SDK Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hook_test
import (
"context"
"strings"
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"helm.sh/helm/v3/pkg/release"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
"sigs.k8s.io/controller-runtime/pkg/handler"
sdkhandler "github.com/operator-framework/operator-lib/handler"
"github.com/operator-framework/helm-operator-plugins/pkg/hook"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/fake"
internalhook "github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/hook"
)
var _ = Describe("Hook", func() {
Describe("dependentResourceWatcher", func() {
var (
drw hook.PostHook
c *fake.Controller
rm *meta.DefaultRESTMapper
cache cache.Cache
owner *unstructured.Unstructured
rel *release.Release
sch *runtime.Scheme
log logr.Logger
ctx context.Context
)
BeforeEach(func() {
rm = meta.NewDefaultRESTMapper([]schema.GroupVersion{})
c = &fake.Controller{}
log = logr.Discard()
cache = &informertest.FakeInformers{}
sch = runtime.NewScheme()
ctx = context.Background()
// Since this is a fake informer and controller, no need to wait for sync.
Expect(cache.Start(ctx)).NotTo(HaveOccurred())
})
Context("with unknown APIs", func() {
BeforeEach(func() {
owner = &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "testDeployment",
"namespace": "ownerNamespace",
},
},
}
rel = &release.Release{
Manifest: strings.Join([]string{rsOwnerNamespace}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
})
It("should fail with an invalid release manifest", func() {
rel.Manifest = "---\nfoobar"
err := drw.Exec(owner, *rel, log)
Expect(err).To(HaveOccurred())
})
It("should fail with unknown owner kind", func() {
Expect(drw.Exec(owner, *rel, log)).To(MatchError(&meta.NoKindMatchError{
GroupKind: schema.GroupKind{Group: "apps", Kind: "Deployment"},
SearchedVersions: []string{"v1"},
}))
})
It("should fail with unknown dependent kind", func() {
rm.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, meta.RESTScopeNamespace)
Expect(drw.Exec(owner, *rel, log)).To(MatchError(&meta.NoKindMatchError{
GroupKind: schema.GroupKind{Group: "apps", Kind: "ReplicaSet"},
SearchedVersions: []string{"v1"},
}))
})
})
Context("with known APIs", func() {
BeforeEach(func() {
rm.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, meta.RESTScopeNamespace)
rm.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "ReplicaSet"}, meta.RESTScopeNamespace)
rm.Add(schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}, meta.RESTScopeNamespace)
rm.Add(schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRole"}, meta.RESTScopeRoot)
rm.Add(schema.GroupVersionKind{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRoleBinding"}, meta.RESTScopeRoot)
})
It("should watch resource kinds only once each", func() {
owner = &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRole",
"metadata": map[string]interface{}{
"name": "testClusterRole",
},
},
}
rel = &release.Release{
Manifest: strings.Join([]string{clusterRole, clusterRole, rsOwnerNamespace, rsOwnerNamespace}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(2))
Expect(c.WatchCalls[0].Handler).To(BeAssignableToTypeOf(handler.EnqueueRequestForOwner(sch, rm, owner, handler.OnlyControllerOwner())))
Expect(c.WatchCalls[1].Handler).To(BeAssignableToTypeOf(handler.EnqueueRequestForOwner(sch, rm, owner, handler.OnlyControllerOwner())))
})
Context("when the owner is cluster-scoped", func() {
BeforeEach(func() {
owner = &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRole",
"metadata": map[string]interface{}{
"name": "testClusterRole",
},
},
}
})
It("should watch namespace-scoped resources with ownerRef handler", func() {
rel = &release.Release{
Manifest: strings.Join([]string{rsOwnerNamespace, ssOtherNamespace}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(2))
Expect(c.WatchCalls[0].Handler).To(BeAssignableToTypeOf(handler.EnqueueRequestForOwner(sch, rm, owner, handler.OnlyControllerOwner())))
Expect(c.WatchCalls[1].Handler).To(BeAssignableToTypeOf(handler.EnqueueRequestForOwner(sch, rm, owner, handler.OnlyControllerOwner())))
})
It("should watch cluster-scoped resources with ownerRef handler", func() {
rel = &release.Release{
Manifest: strings.Join([]string{clusterRole, clusterRoleBinding}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(2))
Expect(c.WatchCalls[0].Handler).To(BeAssignableToTypeOf(handler.EnqueueRequestForOwner(sch, rm, owner, handler.OnlyControllerOwner())))
Expect(c.WatchCalls[1].Handler).To(BeAssignableToTypeOf(handler.EnqueueRequestForOwner(sch, rm, owner, handler.OnlyControllerOwner())))
})
It("should watch resource policy keep resources with annotation handler", func() {
rel = &release.Release{
Manifest: strings.Join([]string{rsOwnerNamespaceWithKeep, ssOtherNamespaceWithKeep, clusterRoleWithKeep, clusterRoleBindingWithKeep}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(4))
Expect(c.WatchCalls[0].Handler).To(BeAssignableToTypeOf(&sdkhandler.EnqueueRequestForAnnotation{}))
Expect(c.WatchCalls[1].Handler).To(BeAssignableToTypeOf(&sdkhandler.EnqueueRequestForAnnotation{}))
Expect(c.WatchCalls[2].Handler).To(BeAssignableToTypeOf(&sdkhandler.EnqueueRequestForAnnotation{}))
Expect(c.WatchCalls[3].Handler).To(BeAssignableToTypeOf(&sdkhandler.EnqueueRequestForAnnotation{}))
})
})
Context("when the owner is namespace-scoped", func() {
BeforeEach(func() {
owner = &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "testDeployment",
"namespace": "ownerNamespace",
},
},
}
})
It("should watch namespace-scoped dependent resources in the same namespace with ownerRef handler", func() {
rel = &release.Release{
Manifest: strings.Join([]string{rsOwnerNamespace}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(1))
Expect(c.WatchCalls[0].Handler).To(BeAssignableToTypeOf(handler.EnqueueRequestForOwner(sch, rm, owner, handler.OnlyControllerOwner())))
})
It("should watch cluster-scoped resources with annotation handler", func() {
rel = &release.Release{
Manifest: strings.Join([]string{clusterRole}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(1))
Expect(c.WatchCalls[0].Handler).To(BeAssignableToTypeOf(&sdkhandler.EnqueueRequestForAnnotation{}))
})
It("should watch namespace-scoped resources in a different namespace with annotation handler", func() {
rel = &release.Release{
Manifest: strings.Join([]string{ssOtherNamespace}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(1))
Expect(c.WatchCalls[0].Handler).To(BeAssignableToTypeOf(&sdkhandler.EnqueueRequestForAnnotation{}))
})
It("should watch resource policy keep resources with annotation handler", func() {
rel = &release.Release{
Manifest: strings.Join([]string{rsOwnerNamespaceWithKeep, ssOtherNamespaceWithKeep, clusterRoleWithKeep}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(3))
Expect(c.WatchCalls[0].Handler).To(BeAssignableToTypeOf(&sdkhandler.EnqueueRequestForAnnotation{}))
Expect(c.WatchCalls[1].Handler).To(BeAssignableToTypeOf(&sdkhandler.EnqueueRequestForAnnotation{}))
Expect(c.WatchCalls[2].Handler).To(BeAssignableToTypeOf(&sdkhandler.EnqueueRequestForAnnotation{}))
})
It("should iterate the kind list and be able to set watches on each item", func() {
rel = &release.Release{
Manifest: strings.Join([]string{replicaSetList}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
Expect(drw.Exec(owner, *rel, log)).To(Succeed())
Expect(c.WatchCalls).To(HaveLen(2))
Expect(c.WatchCalls[0].Handler).To(BeAssignableToTypeOf(handler.EnqueueRequestForOwner(sch, rm, owner, handler.OnlyControllerOwner())))
Expect(c.WatchCalls[1].Handler).To(BeAssignableToTypeOf(handler.EnqueueRequestForOwner(sch, rm, owner, handler.OnlyControllerOwner())))
})
It("should error when unable to list objects", func() {
rel = &release.Release{
Manifest: strings.Join([]string{errReplicaSetList}, "---\n"),
}
drw = internalhook.NewDependentResourceWatcher(c, rm, cache, sch)
err := drw.Exec(owner, *rel, log)
Expect(err).To(HaveOccurred())
})
})
})
})
})
var (
rsOwnerNamespace = `
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: testReplicaSet
namespace: ownerNamespace
`
rsOwnerNamespaceWithKeep = `
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: testReplicaSet
namespace: ownerNamespace
annotations:
helm.sh/resource-policy: keep
`
ssOtherNamespace = `
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: otherTestStatefulSet
namespace: otherNamespace
`
ssOtherNamespaceWithKeep = `
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: otherTestStatefulSet
namespace: otherNamespace
annotations:
helm.sh/resource-policy: keep
`
clusterRole = `
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: testClusterRole
`
clusterRoleWithKeep = `
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: testClusterRole
annotations:
helm.sh/resource-policy: keep
`
clusterRoleBinding = `
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: testClusterRoleBinding
`
clusterRoleBindingWithKeep = `
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: testClusterRoleBinding
annotations:
helm.sh/resource-policy: keep
`
replicaSetList = `
apiVersion: v1
kind: List
items:
- apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: testReplicaSet1
namespace: ownerNamespace
- apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: testReplicaSet2
namespace: ownerNamespace
`
errReplicaSetList = `
apiVersion: v1
kind: List
items:
`
)