Skip to content
This repository was archived by the owner on Dec 11, 2021. It is now read-only.

Commit c0afaf4

Browse files
Eric Stroczynskiestroz
Eric Stroczynski
authored andcommitted
internal/pkg/scorecard: access CRManifestOpt correctly with --olm-deployed (operator-framework#1565)
* internal/pkg/scorecard/scorecard.go: use viper.GetStringSlice() for getting and use []string when setting CRManifestOpt * CHANGELOG.md: update with scorecard cr-manifests bug fix
1 parent 99b7b09 commit c0afaf4

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
### Bug Fixes
1212

1313
- Fixes header file content validation when the content contains empty lines or centered text. ([#1544](https://github.com/operator-framework/operator-sdk/pull/1544))
14+
- Running the [scorecard](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#up) with `--olm-deployed` will now only use the first CR set in either the `cr-manifest` config option or the CSV's `metadata.annotations['alm-examples']` as was intended, and access manifests correctly from the config. ([#1565](https://github.com/operator-framework/operator-sdk/pull/1565))
1415

1516
## v0.8.1
1617

internal/pkg/scorecard/scorecard.go

+45-31
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"bytes"
1919
"context"
2020
"encoding/json"
21-
"errors"
2221
"fmt"
2322
"io"
2423
"io/ioutil"
@@ -34,6 +33,7 @@ import (
3433
"github.com/ghodss/yaml"
3534
olmapiv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
3635
olminstall "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
36+
"github.com/pkg/errors"
3737
"github.com/sirupsen/logrus"
3838
"github.com/spf13/cobra"
3939
"github.com/spf13/viper"
@@ -170,37 +170,51 @@ func runTests() ([]scapiv1alpha1.ScorecardOutput, error) {
170170
return nil, err
171171
}
172172

173-
// Create a temporary CR manifest from metadata if one is not provided.
174-
crJSONStr, ok := csv.ObjectMeta.Annotations["alm-examples"]
175-
if ok && viper.GetString(CRManifestOpt) == "" {
176-
var crs []interface{}
177-
if err = json.Unmarshal([]byte(crJSONStr), &crs); err != nil {
178-
return nil, err
179-
}
180-
// TODO: run scorecard against all CR's in CSV.
181-
cr := crs[0]
182-
crJSONBytes, err := json.Marshal(cr)
183-
if err != nil {
184-
return nil, err
185-
}
186-
crYAMLBytes, err := yaml.JSONToYAML(crJSONBytes)
187-
if err != nil {
188-
return nil, err
189-
}
190-
crFile, err := ioutil.TempFile("", "cr.yaml")
191-
if err != nil {
192-
return nil, err
193-
}
194-
if _, err := crFile.Write(crYAMLBytes); err != nil {
195-
return nil, err
196-
}
197-
viper.Set(CRManifestOpt, crFile.Name())
198-
defer func() {
199-
err := os.Remove(viper.GetString(CRManifestOpt))
173+
logCRMsg := false
174+
if crMans := viper.GetStringSlice(CRManifestOpt); len(crMans) == 0 {
175+
// Create a temporary CR manifest from metadata if one is not provided.
176+
if crJSONStr, ok := csv.ObjectMeta.Annotations["alm-examples"]; ok {
177+
var crs []interface{}
178+
if err = json.Unmarshal([]byte(crJSONStr), &crs); err != nil {
179+
return nil, err
180+
}
181+
// TODO: run scorecard against all CR's in CSV.
182+
cr := crs[0]
183+
logCRMsg = len(crs) > 1
184+
crJSONBytes, err := json.Marshal(cr)
200185
if err != nil {
201-
log.Errorf("Could not delete temporary CR manifest file: (%v)", err)
186+
return nil, err
202187
}
203-
}()
188+
crYAMLBytes, err := yaml.JSONToYAML(crJSONBytes)
189+
if err != nil {
190+
return nil, err
191+
}
192+
crFile, err := ioutil.TempFile("", "*.cr.yaml")
193+
if err != nil {
194+
return nil, err
195+
}
196+
if _, err := crFile.Write(crYAMLBytes); err != nil {
197+
return nil, err
198+
}
199+
viper.Set(CRManifestOpt, []string{crFile.Name()})
200+
defer func() {
201+
for _, f := range viper.GetStringSlice(CRManifestOpt) {
202+
if err := os.Remove(f); err != nil {
203+
log.Errorf("Could not delete temporary CR manifest file: (%v)", err)
204+
}
205+
}
206+
}()
207+
} else {
208+
return nil, errors.New("cr-manifest config option must be set if CSV has no metadata.annotations['alm-examples']")
209+
}
210+
} else {
211+
// TODO: run scorecard against all CR's in CSV.
212+
viper.Set(CRManifestOpt, []string{crMans[0]})
213+
logCRMsg = len(crMans) > 1
214+
}
215+
// Let users know that only the first CR is being tested.
216+
if logCRMsg {
217+
log.Infof("The scorecard does not support testing multiple CR's at once when run with --olm-deployed. Testing the first CR %s", viper.GetStringSlice(CRManifestOpt)[0])
204218
}
205219

206220
} else {
@@ -485,7 +499,7 @@ func configureLogger() error {
485499
}
486500

487501
func validateScorecardFlags() error {
488-
if !viper.GetBool(OlmDeployedOpt) && viper.GetStringSlice(CRManifestOpt) == nil {
502+
if !viper.GetBool(OlmDeployedOpt) && len(viper.GetStringSlice(CRManifestOpt)) == 0 {
489503
return errors.New("cr-manifest config option must be set")
490504
}
491505
if !viper.GetBool(BasicTestsOpt) && !viper.GetBool(OLMTestsOpt) {

0 commit comments

Comments
 (0)