/
scorecard.go
298 lines (251 loc) · 7.73 KB
/
scorecard.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
// 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 scorecard
import (
"context"
"fmt"
"sync"
"time"
"github.com/operator-framework/api/pkg/apis/scorecard/v1alpha3"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
registryutil "github.com/operator-framework/operator-sdk/internal/registry"
)
type TestRunner interface {
Initialize(context.Context) error
RunTest(context.Context, v1alpha3.TestConfiguration) (*v1alpha3.TestStatus, error)
Cleanup(context.Context) error
}
type Scorecard struct {
Config v1alpha3.Configuration
Selector labels.Selector
TestRunner TestRunner
SkipCleanup bool
}
type PodTestRunner struct {
Namespace string
ServiceAccount string
BundlePath string
TestOutput string
BundleMetadata registryutil.LabelsMap
Client kubernetes.Interface
RESTConfig *rest.Config
StorageImage string
UntarImage string
configMapName string
}
type FakeTestRunner struct {
Sleep time.Duration
TestStatus *v1alpha3.TestStatus
Error error
}
// cleanupTimeout is the time given to clean up resources, regardless of how long ctx's deadline is.
var cleanupTimeout = time.Second * 30
// Run executes the scorecard tests as configured
func (o Scorecard) Run(ctx context.Context) (testOutput v1alpha3.TestList, err error) {
testOutput = v1alpha3.NewTestList()
if err := o.TestRunner.Initialize(ctx); err != nil {
return testOutput, err
}
for _, stage := range o.Config.Stages {
tests := o.selectTests(stage)
if len(tests) == 0 {
continue
}
tests = o.setTestDefaults(tests)
output := make(chan v1alpha3.Test, len(tests))
if stage.Parallel {
o.runStageParallel(ctx, tests, output)
} else {
o.runStageSequential(ctx, tests, output)
}
close(output)
for o := range output {
testOutput.Items = append(testOutput.Items, o)
}
}
// Get timeout error, if any, before calling Cleanup() so deletes don't cause a timeout.
select {
case <-ctx.Done():
err = ctx.Err()
default:
}
if !o.SkipCleanup {
// Use a separate context for cleanup, which needs to run regardless of a prior timeout.
clctx, cancel := context.WithTimeout(context.Background(), cleanupTimeout)
defer cancel()
if err := o.TestRunner.Cleanup(clctx); err != nil {
return testOutput, err
}
}
return testOutput, err
}
func (o Scorecard) setTestDefaults(tests []v1alpha3.TestConfiguration) []v1alpha3.TestConfiguration {
for i := range tests {
if len(tests[i].Storage.Spec.MountPath.Path) == 0 {
tests[i].Storage.Spec.MountPath.Path = o.Config.Storage.Spec.MountPath.Path
}
}
return tests
}
func (o Scorecard) runStageParallel(ctx context.Context, tests []v1alpha3.TestConfiguration, results chan<- v1alpha3.Test) {
var wg sync.WaitGroup
for _, t := range tests {
wg.Add(1)
go func(test v1alpha3.TestConfiguration) {
results <- o.runTest(ctx, test)
wg.Done()
}(t)
}
wg.Wait()
}
func (o Scorecard) runStageSequential(ctx context.Context, tests []v1alpha3.TestConfiguration, results chan<- v1alpha3.Test) {
for _, test := range tests {
results <- o.runTest(ctx, test)
}
}
func (o Scorecard) runTest(ctx context.Context, test v1alpha3.TestConfiguration) v1alpha3.Test {
result, err := o.TestRunner.RunTest(ctx, test)
if err != nil {
result = convertErrorToStatus(err, "")
}
out := v1alpha3.NewTest()
//TODO: Add timestamp to result when API version updates
//out.Tstamp = time.Now().Format(time.RFC850)
out.Spec = test
out.Status = *result
return out
}
// selectTests applies an optionally passed selector expression
// against the configured set of tests, returning the selected tests
func (o *Scorecard) selectTests(stage v1alpha3.StageConfiguration) []v1alpha3.TestConfiguration {
selected := make([]v1alpha3.TestConfiguration, 0)
for _, test := range stage.Tests {
if o.Selector == nil || o.Selector.String() == "" || o.Selector.Matches(labels.Set(test.Labels)) {
// TODO olm manifests check
selected = append(selected, test)
}
}
return selected
}
func (r FakeTestRunner) Initialize(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
return nil
}
}
// Initialize sets up the bundle configmap for tests
func (r *PodTestRunner) Initialize(ctx context.Context) error {
bundleData, err := r.getBundleData()
if err != nil {
return fmt.Errorf("error getting bundle data %w", err)
}
r.configMapName, err = r.CreateConfigMap(ctx, bundleData)
if err != nil {
return fmt.Errorf("error creating ConfigMap %w", err)
}
return nil
}
func (r FakeTestRunner) Cleanup(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
return nil
}
}
// Cleanup deletes pods and configmap resources from this test run
func (r PodTestRunner) Cleanup(ctx context.Context) (err error) {
err = r.deletePods(ctx, r.configMapName)
if err != nil {
return err
}
err = r.deleteConfigMap(ctx, r.configMapName)
if err != nil {
return err
}
return nil
}
// RunTest executes a single test
func (r PodTestRunner) RunTest(ctx context.Context, test v1alpha3.TestConfiguration) (*v1alpha3.TestStatus, error) {
// Create a Pod to run the test
podDef := getPodDefinition(r.configMapName, test, r)
if test.Storage.Spec.MountPath.Path != "" {
addStorageToPod(podDef, test.Storage.Spec.MountPath.Path, r.StorageImage)
}
pod, err := r.Client.CoreV1().Pods(r.Namespace).Create(ctx, podDef, metav1.CreateOptions{})
if err != nil {
return nil, err
}
err = r.waitForTestToComplete(ctx, pod)
if err != nil {
return nil, err
}
// gather test output if necessary
if test.Storage.Spec.MountPath.Path != "" {
err := gatherTestOutput(r, test.Labels["suite"], test.Labels["test"], pod.Name, test.Storage.Spec.MountPath.Path)
if err != nil {
return nil, err
}
}
return r.getTestStatus(ctx, pod), nil
}
// RunTest executes a single test
func (r FakeTestRunner) RunTest(ctx context.Context, test v1alpha3.TestConfiguration) (result *v1alpha3.TestStatus, err error) {
select {
case <-time.After(r.Sleep):
return r.TestStatus, r.Error
case <-ctx.Done():
return nil, ctx.Err()
}
}
func ConfigDocLink() string {
return "https://sdk.operatorframework.io/docs/scorecard/"
}
// waitForTestToComplete waits for a fixed amount of time while
// checking for a test pod to complete
func (r PodTestRunner) waitForTestToComplete(ctx context.Context, p *v1.Pod) (err error) {
podCheck := wait.ConditionFunc(func() (done bool, err error) {
var tmp *v1.Pod
tmp, err = r.Client.CoreV1().Pods(p.Namespace).Get(ctx, p.Name, metav1.GetOptions{})
if err != nil {
return true, fmt.Errorf("error getting pod %s %w", p.Name, err)
}
for _, s := range tmp.Status.ContainerStatuses {
if s.Name == "scorecard-test" {
if s.State.Terminated != nil {
return true, nil
}
}
}
return false, nil
})
err = wait.PollImmediateUntil(1*time.Second, podCheck, ctx.Done())
return err
}
func convertErrorToStatus(err error, log string) *v1alpha3.TestStatus {
result := v1alpha3.TestResult{}
result.State = v1alpha3.FailState
result.Errors = []string{err.Error()}
result.Log = log
return &v1alpha3.TestStatus{
Results: []v1alpha3.TestResult{result},
}
}