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

Commit

Permalink
internal/pkg/scorecard: access CRManifestOpt correctly with `--olm-…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Eric Stroczynski authored and estroz committed Jul 19, 2019
1 parent 99b7b09 commit c0afaf4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Bug Fixes

- Fixes header file content validation when the content contains empty lines or centered text. ([#1544](https://github.com/operator-framework/operator-sdk/pull/1544))
- 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))

## v0.8.1

Expand Down
76 changes: 45 additions & 31 deletions internal/pkg/scorecard/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -34,6 +33,7 @@ import (
"github.com/ghodss/yaml"
olmapiv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
olminstall "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -170,37 +170,51 @@ func runTests() ([]scapiv1alpha1.ScorecardOutput, error) {
return nil, err
}

// Create a temporary CR manifest from metadata if one is not provided.
crJSONStr, ok := csv.ObjectMeta.Annotations["alm-examples"]
if ok && viper.GetString(CRManifestOpt) == "" {
var crs []interface{}
if err = json.Unmarshal([]byte(crJSONStr), &crs); err != nil {
return nil, err
}
// TODO: run scorecard against all CR's in CSV.
cr := crs[0]
crJSONBytes, err := json.Marshal(cr)
if err != nil {
return nil, err
}
crYAMLBytes, err := yaml.JSONToYAML(crJSONBytes)
if err != nil {
return nil, err
}
crFile, err := ioutil.TempFile("", "cr.yaml")
if err != nil {
return nil, err
}
if _, err := crFile.Write(crYAMLBytes); err != nil {
return nil, err
}
viper.Set(CRManifestOpt, crFile.Name())
defer func() {
err := os.Remove(viper.GetString(CRManifestOpt))
logCRMsg := false
if crMans := viper.GetStringSlice(CRManifestOpt); len(crMans) == 0 {
// Create a temporary CR manifest from metadata if one is not provided.
if crJSONStr, ok := csv.ObjectMeta.Annotations["alm-examples"]; ok {
var crs []interface{}
if err = json.Unmarshal([]byte(crJSONStr), &crs); err != nil {
return nil, err
}
// TODO: run scorecard against all CR's in CSV.
cr := crs[0]
logCRMsg = len(crs) > 1
crJSONBytes, err := json.Marshal(cr)
if err != nil {
log.Errorf("Could not delete temporary CR manifest file: (%v)", err)
return nil, err
}
}()
crYAMLBytes, err := yaml.JSONToYAML(crJSONBytes)
if err != nil {
return nil, err
}
crFile, err := ioutil.TempFile("", "*.cr.yaml")
if err != nil {
return nil, err
}
if _, err := crFile.Write(crYAMLBytes); err != nil {
return nil, err
}
viper.Set(CRManifestOpt, []string{crFile.Name()})
defer func() {
for _, f := range viper.GetStringSlice(CRManifestOpt) {
if err := os.Remove(f); err != nil {
log.Errorf("Could not delete temporary CR manifest file: (%v)", err)
}
}
}()
} else {
return nil, errors.New("cr-manifest config option must be set if CSV has no metadata.annotations['alm-examples']")
}
} else {
// TODO: run scorecard against all CR's in CSV.
viper.Set(CRManifestOpt, []string{crMans[0]})
logCRMsg = len(crMans) > 1
}
// Let users know that only the first CR is being tested.
if logCRMsg {
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])
}

} else {
Expand Down Expand Up @@ -485,7 +499,7 @@ func configureLogger() error {
}

func validateScorecardFlags() error {
if !viper.GetBool(OlmDeployedOpt) && viper.GetStringSlice(CRManifestOpt) == nil {
if !viper.GetBool(OlmDeployedOpt) && len(viper.GetStringSlice(CRManifestOpt)) == 0 {
return errors.New("cr-manifest config option must be set")
}
if !viper.GetBool(BasicTestsOpt) && !viper.GetBool(OLMTestsOpt) {
Expand Down

0 comments on commit c0afaf4

Please sign in to comment.