forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
413 lines (387 loc) · 17.3 KB
/
controller.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
Copyright 2019 The Tekton 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 test
import (
"context"
"fmt"
"sync/atomic"
"testing"
// Link in the fakes so they get injected into injection.Fake
"github.com/tektoncd/pipeline/pkg/apis/config"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
resolutionv1alpha1 "github.com/tektoncd/pipeline/pkg/apis/resolution/v1beta1"
fakepipelineclientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/fake"
informersv1 "github.com/tektoncd/pipeline/pkg/client/informers/externalversions/pipeline/v1"
informersv1alpha1 "github.com/tektoncd/pipeline/pkg/client/informers/externalversions/pipeline/v1alpha1"
informersv1beta1 "github.com/tektoncd/pipeline/pkg/client/informers/externalversions/pipeline/v1beta1"
fakepipelineclient "github.com/tektoncd/pipeline/pkg/client/injection/client/fake"
fakepipelineinformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1/pipeline/fake"
fakepipelineruninformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1/pipelinerun/fake"
faketaskinformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1/task/fake"
faketaskruninformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1/taskrun/fake"
fakeverificationpolicyinformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1alpha1/verificationpolicy/fake"
fakeclustertaskinformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1beta1/clustertask/fake"
fakecustomruninformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1beta1/customrun/fake"
fakestepactioninformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1beta1/stepaction/fake"
fakeresolutionclientset "github.com/tektoncd/pipeline/pkg/client/resolution/clientset/versioned/fake"
resolutioninformersv1alpha1 "github.com/tektoncd/pipeline/pkg/client/resolution/informers/externalversions/resolution/v1beta1"
fakeresolutionrequestclient "github.com/tektoncd/pipeline/pkg/client/resolution/injection/client/fake"
fakeresolutionrequestinformer "github.com/tektoncd/pipeline/pkg/client/resolution/injection/informers/resolution/v1beta1/resolutionrequest/fake"
cloudeventclient "github.com/tektoncd/pipeline/pkg/reconciler/events/cloudevent"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
coreinformers "k8s.io/client-go/informers/core/v1"
fakekubeclientset "k8s.io/client-go/kubernetes/fake"
ktesting "k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake"
fakeconfigmapinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/configmap/fake"
fakelimitrangeinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/limitrange/fake"
fakefilteredpodinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/pod/filtered/fake"
fakesecretinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/secret/fake"
fakeserviceaccountinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/serviceaccount/fake"
"knative.dev/pkg/controller"
"knative.dev/pkg/system"
)
// Data represents the desired state of the system (i.e. existing resources) to seed controllers
// with.
type Data struct {
PipelineRuns []*v1.PipelineRun
Pipelines []*v1.Pipeline
TaskRuns []*v1.TaskRun
Tasks []*v1.Task
StepActions []*v1beta1.StepAction
ClusterTasks []*v1beta1.ClusterTask
CustomRuns []*v1beta1.CustomRun
Pods []*corev1.Pod
Namespaces []*corev1.Namespace
ConfigMaps []*corev1.ConfigMap
ServiceAccounts []*corev1.ServiceAccount
LimitRange []*corev1.LimitRange
ResolutionRequests []*resolutionv1alpha1.ResolutionRequest
ExpectedCloudEventCount int
VerificationPolicies []*v1alpha1.VerificationPolicy
Secrets []*corev1.Secret
}
// Clients holds references to clients which are useful for reconciler tests.
type Clients struct {
Pipeline *fakepipelineclientset.Clientset
Kube *fakekubeclientset.Clientset
CloudEvents cloudeventclient.CEClient
ResolutionRequests *fakeresolutionclientset.Clientset
}
// Informers holds references to informers which are useful for reconciler tests.
type Informers struct {
PipelineRun informersv1.PipelineRunInformer
Pipeline informersv1.PipelineInformer
TaskRun informersv1.TaskRunInformer
Run informersv1alpha1.RunInformer
CustomRun informersv1beta1.CustomRunInformer
Task informersv1.TaskInformer
StepAction informersv1beta1.StepActionInformer
ClusterTask informersv1beta1.ClusterTaskInformer
Pod coreinformers.PodInformer
ConfigMap coreinformers.ConfigMapInformer
ServiceAccount coreinformers.ServiceAccountInformer
LimitRange coreinformers.LimitRangeInformer
ResolutionRequest resolutioninformersv1alpha1.ResolutionRequestInformer
VerificationPolicy informersv1alpha1.VerificationPolicyInformer
Secret coreinformers.SecretInformer
}
// Assets holds references to the controller, logs, clients, and informers.
type Assets struct {
Logger *zap.SugaredLogger
Controller *controller.Impl
Clients Clients
Informers Informers
Recorder *record.FakeRecorder
Ctx context.Context
}
// AddToInformer returns a function to add ktesting.Actions to the cache store
func AddToInformer(t *testing.T, store cache.Store) func(ktesting.Action) (bool, runtime.Object, error) {
t.Helper()
return func(action ktesting.Action) (bool, runtime.Object, error) {
switch a := action.(type) {
case ktesting.CreateActionImpl:
if err := store.Add(a.GetObject()); err != nil {
t.Fatal(err)
}
case ktesting.UpdateActionImpl:
objMeta, err := meta.Accessor(a.GetObject())
if err != nil {
return true, nil, err
}
// Look up the old copy of this resource and perform the optimistic concurrency check.
old, exists, err := store.GetByKey(objMeta.GetNamespace() + "/" + objMeta.GetName())
if err != nil {
return true, nil, err
} else if !exists {
// Let the client return the error.
return false, nil, nil
}
oldMeta, err := meta.Accessor(old)
if err != nil {
return true, nil, err
}
// If the resource version is mismatched, then fail with a conflict.
if oldMeta.GetResourceVersion() != objMeta.GetResourceVersion() {
return true, nil, apierrs.NewConflict(
a.Resource.GroupResource(), objMeta.GetName(),
fmt.Errorf("resourceVersion mismatch, got: %v, wanted: %v",
objMeta.GetResourceVersion(), oldMeta.GetResourceVersion()))
}
// Update the store with the new object when it's fine.
if err := store.Update(a.GetObject()); err != nil {
t.Fatal(err)
}
}
return false, nil, nil
}
}
// SeedTestData returns Clients and Informers populated with the
// given Data.
//
//nolint:revive
func SeedTestData(t *testing.T, ctx context.Context, d Data) (Clients, Informers) {
t.Helper()
c := Clients{
Kube: fakekubeclient.Get(ctx),
Pipeline: fakepipelineclient.Get(ctx),
CloudEvents: cloudeventclient.Get(ctx),
ResolutionRequests: fakeresolutionrequestclient.Get(ctx),
}
// Every time a resource is modified, change the metadata.resourceVersion.
PrependResourceVersionReactor(&c.Pipeline.Fake)
i := Informers{
PipelineRun: fakepipelineruninformer.Get(ctx),
Pipeline: fakepipelineinformer.Get(ctx),
TaskRun: faketaskruninformer.Get(ctx),
CustomRun: fakecustomruninformer.Get(ctx),
Task: faketaskinformer.Get(ctx),
StepAction: fakestepactioninformer.Get(ctx),
ClusterTask: fakeclustertaskinformer.Get(ctx),
Pod: fakefilteredpodinformer.Get(ctx, v1.ManagedByLabelKey),
ConfigMap: fakeconfigmapinformer.Get(ctx),
ServiceAccount: fakeserviceaccountinformer.Get(ctx),
LimitRange: fakelimitrangeinformer.Get(ctx),
ResolutionRequest: fakeresolutionrequestinformer.Get(ctx),
VerificationPolicy: fakeverificationpolicyinformer.Get(ctx),
Secret: fakesecretinformer.Get(ctx),
}
// Attach reactors that add resource mutations to the appropriate
// informer index, and simulate optimistic concurrency failures when
// the resource version is mismatched.
c.Pipeline.PrependReactor("*", "pipelineruns", AddToInformer(t, i.PipelineRun.Informer().GetIndexer()))
for _, pr := range d.PipelineRuns {
pr := pr.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Pipeline.TektonV1().PipelineRuns(pr.Namespace).Create(ctx, pr, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Pipeline.PrependReactor("*", "pipelines", AddToInformer(t, i.Pipeline.Informer().GetIndexer()))
for _, p := range d.Pipelines {
p := p.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Pipeline.TektonV1().Pipelines(p.Namespace).Create(ctx, p, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Pipeline.PrependReactor("*", "taskruns", AddToInformer(t, i.TaskRun.Informer().GetIndexer()))
for _, tr := range d.TaskRuns {
tr := tr.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Pipeline.TektonV1().TaskRuns(tr.Namespace).Create(ctx, tr, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Pipeline.PrependReactor("*", "tasks", AddToInformer(t, i.Task.Informer().GetIndexer()))
for _, ta := range d.Tasks {
ta := ta.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Pipeline.TektonV1().Tasks(ta.Namespace).Create(ctx, ta, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Pipeline.PrependReactor("*", "stepactions", AddToInformer(t, i.StepAction.Informer().GetIndexer()))
for _, sa := range d.StepActions {
sa := sa.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Pipeline.TektonV1beta1().StepActions(sa.Namespace).Create(ctx, sa, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Pipeline.PrependReactor("*", "clustertasks", AddToInformer(t, i.ClusterTask.Informer().GetIndexer()))
for _, ct := range d.ClusterTasks {
ct := ct.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Pipeline.TektonV1beta1().ClusterTasks().Create(ctx, ct, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Pipeline.PrependReactor("*", "customruns", AddToInformer(t, i.CustomRun.Informer().GetIndexer()))
for _, customRun := range d.CustomRuns {
customRun := customRun.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Pipeline.TektonV1beta1().CustomRuns(customRun.Namespace).Create(ctx, customRun, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Kube.PrependReactor("*", "pods", AddToInformer(t, i.Pod.Informer().GetIndexer()))
for _, p := range d.Pods {
p := p.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Kube.CoreV1().Pods(p.Namespace).Create(ctx, p, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
for _, n := range d.Namespaces {
n := n.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Kube.CoreV1().Namespaces().Create(ctx, n, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Kube.PrependReactor("*", "configmaps", AddToInformer(t, i.ConfigMap.Informer().GetIndexer()))
for _, cm := range d.ConfigMaps {
cm := cm.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Kube.CoreV1().ConfigMaps(cm.Namespace).Create(ctx, cm, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Kube.PrependReactor("*", "serviceaccounts", AddToInformer(t, i.ServiceAccount.Informer().GetIndexer()))
for _, sa := range d.ServiceAccounts {
sa := sa.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Kube.CoreV1().ServiceAccounts(sa.Namespace).Create(ctx, sa, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.ResolutionRequests.PrependReactor("*", "resolutionrequests", AddToInformer(t, i.ResolutionRequest.Informer().GetIndexer()))
for _, rr := range d.ResolutionRequests {
rr := rr.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.ResolutionRequests.ResolutionV1beta1().ResolutionRequests(rr.Namespace).Create(ctx, rr, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Pipeline.PrependReactor("*", "verificationpolicies", AddToInformer(t, i.VerificationPolicy.Informer().GetIndexer()))
for _, vp := range d.VerificationPolicies {
vp := vp.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Pipeline.TektonV1alpha1().VerificationPolicies(vp.Namespace).Create(ctx, vp, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Kube.PrependReactor("*", "secrets", AddToInformer(t, i.Secret.Informer().GetIndexer()))
for _, s := range d.Secrets {
s := s.DeepCopy() // Avoid assumptions that the informer's copy is modified.
if _, err := c.Kube.CoreV1().Secrets(s.Namespace).Create(ctx, s, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
}
c.Pipeline.ClearActions()
c.Kube.ClearActions()
c.ResolutionRequests.ClearActions()
return c, i
}
// ResourceVersionReactor is an implementation of Reactor for our tests
type ResourceVersionReactor struct {
count int64
}
// Handles returns whether our test reactor can handle a given ktesting.Action
func (r *ResourceVersionReactor) Handles(action ktesting.Action) bool {
body := func(o runtime.Object) bool {
objMeta, err := meta.Accessor(o)
if err != nil {
return false
}
val := atomic.AddInt64(&r.count, 1)
objMeta.SetResourceVersion(fmt.Sprintf("%05d", val))
return false
}
switch o := action.(type) {
case ktesting.CreateActionImpl:
return body(o.GetObject())
case ktesting.UpdateActionImpl:
return body(o.GetObject())
default:
return false
}
}
// React is noop-function
func (r *ResourceVersionReactor) React(action ktesting.Action) (handled bool, ret runtime.Object, err error) {
return false, nil, nil
}
var _ ktesting.Reactor = (*ResourceVersionReactor)(nil)
// PrependResourceVersionReactor will instrument a client-go testing Fake
// with a reactor that simulates resourceVersion changes on mutations.
// This does not work with patches.
func PrependResourceVersionReactor(f *ktesting.Fake) {
f.ReactionChain = append([]ktesting.Reactor{&ResourceVersionReactor{}}, f.ReactionChain...)
}
// EnsureConfigurationConfigMapsExist makes sure all the configmaps exists.
func EnsureConfigurationConfigMapsExist(d *Data) {
var defaultsExists, featureFlagsExists, metricsExists, spireconfigExists, eventsExists, tracingExists bool
for _, cm := range d.ConfigMaps {
if cm.Name == config.GetDefaultsConfigName() {
defaultsExists = true
}
if cm.Name == config.GetFeatureFlagsConfigName() {
featureFlagsExists = true
}
if cm.Name == config.GetMetricsConfigName() {
metricsExists = true
}
if cm.Name == config.GetSpireConfigName() {
spireconfigExists = true
}
if cm.Name == config.GetEventsConfigName() {
eventsExists = true
}
if cm.Name == config.GetTracingConfigName() {
tracingExists = true
}
}
if !defaultsExists {
d.ConfigMaps = append(d.ConfigMaps, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: config.GetDefaultsConfigName(), Namespace: system.Namespace()},
Data: map[string]string{},
})
}
if !featureFlagsExists {
d.ConfigMaps = append(d.ConfigMaps, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: config.GetFeatureFlagsConfigName(), Namespace: system.Namespace()},
Data: map[string]string{},
})
}
if !metricsExists {
d.ConfigMaps = append(d.ConfigMaps, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: config.GetMetricsConfigName(), Namespace: system.Namespace()},
Data: map[string]string{},
})
}
if !spireconfigExists {
d.ConfigMaps = append(d.ConfigMaps, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: config.GetSpireConfigName(), Namespace: system.Namespace()},
Data: map[string]string{},
})
}
if !eventsExists {
d.ConfigMaps = append(d.ConfigMaps, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: config.GetEventsConfigName(), Namespace: system.Namespace()},
Data: map[string]string{},
})
}
if !tracingExists {
d.ConfigMaps = append(d.ConfigMaps, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: config.GetTracingConfigName(), Namespace: system.Namespace()},
Data: map[string]string{},
})
}
}