@@ -18,7 +18,6 @@ import (
18
18
"bytes"
19
19
"context"
20
20
"encoding/json"
21
- "errors"
22
21
"fmt"
23
22
"io"
24
23
"io/ioutil"
@@ -38,10 +37,10 @@ import (
38
37
"github.com/ghodss/yaml"
39
38
olmapiv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
40
39
olminstall "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
40
+ "github.com/pkg/errors"
41
41
"github.com/sirupsen/logrus"
42
42
v1 "k8s.io/api/core/v1"
43
43
extscheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
44
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
45
44
"k8s.io/apimachinery/pkg/runtime"
46
45
"k8s.io/apimachinery/pkg/runtime/schema"
47
46
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -103,50 +102,16 @@ func RunInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig, lo
103
102
if config .Namespace == "" {
104
103
config .Namespace = tmpNamespaceVar
105
104
}
106
- scheme := runtime .NewScheme ()
107
- // scheme for client go
108
- if err := cgoscheme .AddToScheme (scheme ); err != nil {
109
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed to add client-go scheme to client: %v" , err )
110
- }
111
- // api extensions scheme (CRDs)
112
- if err := extscheme .AddToScheme (scheme ); err != nil {
113
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed to add failed to add extensions api scheme to client: %v" , err )
114
- }
115
- // olm api (CSVs)
116
- if err := olmapiv1alpha1 .AddToScheme (scheme ); err != nil {
117
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed to add failed to add oml api scheme (CSVs) to client: %v" , err )
118
- }
119
- dynamicDecoder = serializer .NewCodecFactory (scheme ).UniversalDeserializer ()
120
- // if a user creates a new CRD, we need to be able to reset the rest mapper
121
- // temporary kubeclient to get a cached discovery
122
- kubeclient , err := kubernetes .NewForConfig (kubeconfig )
123
- if err != nil {
124
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed to get a kubeclient: %v" , err )
105
+
106
+ if err := setupRuntimeClient (); err != nil {
107
+ return scapiv1alpha1.ScorecardOutput {}, err
125
108
}
126
109
127
110
csv := & olmapiv1alpha1.ClusterServiceVersion {}
128
111
if pluginType == OLMIntegration || config .OLMDeployed {
129
112
err := getCSV (config .CSVManifest , csv )
130
113
if err != nil {
131
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed to read csv: %v" , err )
132
- }
133
- if err = yaml .Unmarshal (yamlSpec , csv ); err != nil {
134
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("error getting ClusterServiceVersion: %v" , err )
135
- }
136
-
137
- csvValidator := validation .ClusterServiceVersionValidator
138
- results := csvValidator .Validate (csv )
139
- for _ , r := range results {
140
- if len (r .Errors ) > 0 {
141
- var errorMsgs strings.Builder
142
- for _ , e := range r .Errors {
143
- errorMsgs .WriteString (fmt .Sprintf ("%s\n " , e .Error ()))
144
- }
145
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("error validating ClusterServiceVersion: %s" , errorMsgs .String ())
146
- }
147
- for _ , w := range r .Warnings {
148
- log .Warnf ("CSV validation warning: type [%s] %s" , w .Type , w .Detail )
149
- }
114
+ return scapiv1alpha1.ScorecardOutput {}, err
150
115
}
151
116
}
152
117
@@ -164,54 +129,9 @@ func RunInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig, lo
164
129
return scapiv1alpha1.ScorecardOutput {}, err
165
130
}
166
131
167
- logCRMsg := false
168
- if crMans := config .CRManifest ; len (crMans ) == 0 {
169
- // Create a temporary CR manifest from metadata if one is not provided.
170
- if crJSONStr , ok := csv .ObjectMeta .Annotations ["alm-examples" ]; ok {
171
- var crs []interface {}
172
- if err = json .Unmarshal ([]byte (crJSONStr ), & crs ); err != nil {
173
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("metadata.annotations['alm-examples'] in CSV %s incorrectly formatted: %v" , csv .GetName (), err )
174
- }
175
- if len (crs ) == 0 {
176
- return scapiv1alpha1.ScorecardOutput {}, errors .Errorf ("no CRs found in metadata.annotations['alm-examples'] in CSV %s and cr-manifest config option not set" , csv .GetName ())
177
- }
178
- // TODO: run scorecard against all CR's in CSV.
179
- cr := crs [0 ]
180
- logCRMsg = len (crs ) > 1
181
- crJSONBytes , err := json .Marshal (cr )
182
- if err != nil {
183
- return scapiv1alpha1.ScorecardOutput {}, err
184
- }
185
- crYAMLBytes , err := yaml .JSONToYAML (crJSONBytes )
186
- if err != nil {
187
- return scapiv1alpha1.ScorecardOutput {}, err
188
- }
189
- crFile , err := ioutil .TempFile ("" , "*.cr.yaml" )
190
- if err != nil {
191
- return scapiv1alpha1.ScorecardOutput {}, err
192
- }
193
- if _ , err := crFile .Write (crYAMLBytes ); err != nil {
194
- return scapiv1alpha1.ScorecardOutput {}, err
195
- }
196
- config .CRManifest = []string {crFile .Name ()}
197
- defer func () {
198
- for _ , f := range config .CRManifest {
199
- if err := os .Remove (f ); err != nil {
200
- log .Errorf ("Could not delete temporary CR manifest file: (%v)" , err )
201
- }
202
- }
203
- }()
204
- } else {
205
- return scapiv1alpha1.ScorecardOutput {}, errors .New ("cr-manifest config option must be set if CSV has no metadata.annotations['alm-examples']" )
206
- }
207
- } else {
208
- // TODO: run scorecard against all CR's in CSV.
209
- config .CRManifest = []string {crMans [0 ]}
210
- logCRMsg = len (crMans ) > 1
211
- }
212
- // Let users know that only the first CR is being tested.
213
- if logCRMsg {
214
- log .Infof ("The scorecard does not support testing multiple CR's at once when run with --olm-deployed. Testing the first CR %s" , config .CRManifest [0 ])
132
+ config .CRManifest , err = getCRFromCSV (config .CRManifest , csv .ObjectMeta .Annotations ["alm-examples" ], csv .GetName ())
133
+ if err != nil {
134
+ return scapiv1alpha1.ScorecardOutput {}, err
215
135
}
216
136
217
137
} else {
@@ -251,100 +171,16 @@ func RunInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig, lo
251
171
}
252
172
}
253
173
254
- crs := config .CRManifest
255
- // check if there are duplicate CRs
256
- gvks := []schema.GroupVersionKind {}
257
- for _ , cr := range crs {
258
- file , err := ioutil .ReadFile (cr )
259
- if err != nil {
260
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed to read file: %s" , cr )
261
- }
262
- newGVKs , err := getGVKs (file )
263
- if err != nil {
264
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("could not get GVKs for resource(s) in file: %s, due to error: %v" , cr , err )
265
- }
266
- gvks = append (gvks , newGVKs ... )
267
- }
268
- dupMap := make (map [schema.GroupVersionKind ]bool )
269
- for _ , gvk := range gvks {
270
- if _ , ok := dupMap [gvk ]; ok {
271
- log .Warnf ("Duplicate gvks in CR list detected (%s); results may be inaccurate" , gvk )
272
- }
273
- dupMap [gvk ] = true
174
+ err = duplicateCRCheck (config .CRManifest )
175
+ if err != nil {
176
+ return scapiv1alpha1.ScorecardOutput {}, err
274
177
}
275
178
276
179
var suites []schelpers.TestSuite
277
- for _ , cr := range crs {
278
- logReadWriter := & bytes.Buffer {}
279
- log .SetOutput (logReadWriter )
280
- log .Printf ("Running for cr: %s" , cr )
281
- var obj * unstructured.Unstructured
282
- if ! config .OLMDeployed {
283
- if err := createFromYAMLFile (config .Namespace , config .GlobalManifest , config .ProxyImage , config .ProxyPullPolicy ); err != nil {
284
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed to create global resources: %v" , err )
285
- }
286
- if err := createFromYAMLFile (config .Namespace , config .NamespacedManifest , config .ProxyImage , config .ProxyPullPolicy ); err != nil {
287
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed to create namespaced resources: %v" , err )
288
- }
289
- }
290
- if err := createFromYAMLFile (config .Namespace , cr , config .ProxyImage , config .ProxyPullPolicy ); err != nil {
291
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed to create cr resource: %v" , err )
292
- }
293
- obj , err = yamlToUnstructured (config .Namespace , cr )
180
+ for _ , cr := range config .CRManifest {
181
+ crSuites , err := runTests (csv , pluginType , config , cr , logFile )
294
182
if err != nil {
295
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed to decode custom resource manifest into object: %v" , err )
296
- }
297
- if err := waitUntilCRStatusExists (time .Second * time .Duration (config .InitTimeout ), obj ); err != nil {
298
- return scapiv1alpha1.ScorecardOutput {}, fmt .Errorf ("failed waiting to check if CR status exists: %v" , err )
299
- }
300
- if pluginType == BasicOperator {
301
- conf := BasicTestConfig {
302
- Client : runtimeClient ,
303
- CR : obj ,
304
- ProxyPod : proxyPodGlobal ,
305
- }
306
- basicTests := NewBasicTestSuite (conf )
307
- if schelpers .IsV1alpha2 (config .Version ) {
308
- basicTests .ApplySelector (config .Selector )
309
- }
310
-
311
- basicTests .Run (context .TODO ())
312
- logs , err := ioutil .ReadAll (logReadWriter )
313
- if err != nil {
314
- basicTests .Log = fmt .Sprintf ("failed to read log buffer: %v" , err )
315
- } else {
316
- basicTests .Log = string (logs )
317
- }
318
- suites = append (suites , * basicTests )
319
- }
320
- if pluginType == OLMIntegration {
321
- conf := OLMTestConfig {
322
- Client : runtimeClient ,
323
- CR : obj ,
324
- CSV : csv ,
325
- CRDsDir : config .CRDsDir ,
326
- ProxyPod : proxyPodGlobal ,
327
- Bundle : config .Bundle ,
328
- }
329
- olmTests := NewOLMTestSuite (conf )
330
- if schelpers .IsV1alpha2 (config .Version ) {
331
- olmTests .ApplySelector (config .Selector )
332
- }
333
-
334
- olmTests .Run (context .TODO ())
335
- logs , err := ioutil .ReadAll (logReadWriter )
336
- if err != nil {
337
- olmTests .Log = fmt .Sprintf ("failed to read log buffer: %v" , err )
338
- } else {
339
- olmTests .Log = string (logs )
340
- }
341
- suites = append (suites , * olmTests )
342
- }
343
- // change logging back to main log
344
- log .SetOutput (logFile )
345
- // set up clean environment for every CR
346
- if err := cleanupScorecard (); err != nil {
347
- log .Errorf ("Failed to cleanup resources: (%v)" , err )
183
+ return scapiv1alpha1.ScorecardOutput {}, err
348
184
}
349
185
suites = append (suites , crSuites ... )
350
186
}
@@ -487,7 +323,7 @@ func getCRFromCSV(currentCRMans []string, crJSONStr string, csvName string) ([]s
487
323
if crJSONStr != "" {
488
324
var crs []interface {}
489
325
if err := json .Unmarshal ([]byte (crJSONStr ), & crs ); err != nil {
490
- return finalCR , errors . Wrapf ( err , "metadata.annotations['alm-examples'] in CSV %s incorrectly formatted" , csvName )
326
+ return finalCR , fmt . Errorf ( "metadata.annotations['alm-examples'] in CSV %s incorrectly formatted: %v " , csvName , err )
491
327
}
492
328
if len (crs ) == 0 {
493
329
return finalCR , errors .Errorf ("no CRs found in metadata.annotations['alm-examples'] in CSV %s and cr-manifest config option not set" , csvName )
0 commit comments