Skip to content

Commit fe9b17f

Browse files
committed
fix build errors after rebase
1 parent b993659 commit fe9b17f

File tree

5 files changed

+27
-227
lines changed

5 files changed

+27
-227
lines changed

cmd/operator-sdk/internal/genutil/crds.go

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,10 @@ func CRDGen() error {
3131

3232
log.Info("Running CRD generator.")
3333

34-
gvMap, err := k8sutil.ParseGroupSubpackages(scaffold.ApisDir)
35-
if err != nil {
36-
return fmt.Errorf("failed to parse group versions: %v", err)
37-
}
38-
gvb := &strings.Builder{}
39-
for g, vs := range gvMap {
40-
gvb.WriteString(fmt.Sprintf("%s:%v, ", g, vs))
41-
}
42-
43-
log.Infof("Running CRD generation for Custom Resource group versions: [%v]\n", gvb.String())
44-
45-
s := &scaffold.Scaffold{}
46-
cfg := &input.Config{
47-
Repo: repoPkg,
48-
AbsProjectPath: absProjectPath,
49-
ProjectName: filepath.Base(absProjectPath),
50-
}
51-
crds, err := k8sutil.GetCRDs(scaffold.CRDsDir)
52-
if err != nil {
53-
return err
54-
}
55-
for _, crd := range crds {
56-
g, v, k := crd.Spec.Group, crd.Spec.Version, crd.Spec.Names.Kind
57-
if v == "" {
58-
if len(crd.Spec.Versions) != 0 {
59-
v = crd.Spec.Versions[0].Name
60-
} else {
61-
return fmt.Errorf("crd of group %s kind %s has no version", g, k)
62-
}
63-
}
64-
r, err := scaffold.NewResource(g+"/"+v, k)
65-
if err != nil {
66-
return err
67-
}
68-
err = s.Execute(cfg,
69-
&scaffold.CRD{Resource: r, IsOperatorGo: projutil.IsOperatorGo()},
70-
)
71-
if err != nil {
72-
return err
73-
}
34+
cfg := gen.Config{}
35+
crd := gencrd.NewCRDGo(cfg)
36+
if err := crd.Generate(); err != nil {
37+
return fmt.Errorf("error generating CRDs from APIs in %s: %w", scaffold.ApisDir, err)
7438
}
7539

7640
log.Info("CRD generation complete.")

internal/olm/client/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (c Client) GetInstalledVersion(ctx context.Context) (string, error) {
223223
// There is more than one version of OLM installed in the cluster,
224224
// so we can't resolve the version being used.
225225
if pkgServerCSV != nil {
226-
return "", errors.Errorf("more than one OLM (package server) version installed: %q and %q", pkgServerCSV.GetName(), name)
226+
return "", fmt.Errorf("more than one OLM (package server) version installed: %q and %q", pkgServerCSV.GetName(), name)
227227
}
228228
pkgServerCSV = &csv
229229
}
@@ -260,5 +260,5 @@ func getOLMVersionFromPackageServerCSV(csv *olmapiv1alpha1.ClusterServiceVersion
260260
if _, err := semver.Parse(ver); err == nil {
261261
return ver, nil
262262
}
263-
return "", errors.Errorf("no OLM version found in CSV %q spec", csv.GetName())
263+
return "", fmt.Errorf("no OLM version found in CSV %q spec", csv.GetName())
264264
}

internal/scaffold/olm-catalog/descriptor/descriptor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func getTypesForPkg(pkgPath string, universe types.Universe) (pkgTypes []*types.
144144
}
145145
}
146146
if pkg == nil {
147-
return nil, errors.Errorf("no package found for API %s", pkgPath)
147+
return nil, fmt.Errorf("no package found for API %s", pkgPath)
148148
}
149149
for _, t := range pkg.Types {
150150
pkgTypes = append(pkgTypes, t)

internal/scaffold/olm-catalog/descriptor/parse.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ func parseCSVGenAnnotations(comments []string) (pd parsedCRDDescriptions, err er
104104
pd.resources = append(pd.resources, r)
105105
}
106106
default:
107-
return parsedCRDDescriptions{}, errors.Errorf("unsupported %s child path element %s", parentPathElem, childPathElems[0])
107+
return parsedCRDDescriptions{}, fmt.Errorf("unsupported %s child path element %s", parentPathElem, childPathElems[0])
108108
}
109109
default:
110-
return parsedCRDDescriptions{}, errors.Errorf("unsupported path element %s", parentPathElem)
110+
return parsedCRDDescriptions{}, fmt.Errorf("unsupported path element %s", parentPathElem)
111111
}
112112
}
113113
pd.descriptors = append(pd.descriptors, specd, statusd)
@@ -138,10 +138,10 @@ func parseMemberAnnotation(d *descriptor, pathElems []string, val string) (err e
138138
}
139139
d.XDescriptors = strings.Split(xdStr, ",")
140140
default:
141-
return errors.Errorf("unsupported descriptor path element %s", pathElems[1])
141+
return fmt.Errorf("unsupported descriptor path element %s", pathElems[1])
142142
}
143143
default:
144-
return errors.Errorf("unsupported descriptor path %s", annotations.JoinPath(pathElems...))
144+
return fmt.Errorf("unsupported descriptor path %s", annotations.JoinPath(pathElems...))
145145
}
146146
return nil
147147
}
@@ -155,7 +155,7 @@ func parseResource(rStr string) (r olmapiv1alpha1.APIResourceReference, err erro
155155
}
156156
rSplit := strings.SplitN(rStr, ",", 3)
157157
if len(rSplit) < 2 {
158-
return r, errors.Errorf("resource string %s did not have at least a kind and a version", rStr)
158+
return r, fmt.Errorf("resource string %s did not have at least a kind and a version", rStr)
159159
}
160160
r.Kind, r.Version = strings.TrimSpace(rSplit[0]), strings.TrimSpace(rSplit[1])
161161
if len(rSplit) == 3 {

internal/scorecard/plugins/plugin_runner.go

Lines changed: 15 additions & 179 deletions
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"
@@ -38,10 +37,10 @@ import (
3837
"github.com/ghodss/yaml"
3938
olmapiv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
4039
olminstall "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
40+
"github.com/pkg/errors"
4141
"github.com/sirupsen/logrus"
4242
v1 "k8s.io/api/core/v1"
4343
extscheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
44-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
4544
"k8s.io/apimachinery/pkg/runtime"
4645
"k8s.io/apimachinery/pkg/runtime/schema"
4746
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -103,50 +102,16 @@ func RunInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig, lo
103102
if config.Namespace == "" {
104103
config.Namespace = tmpNamespaceVar
105104
}
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
125108
}
126109

127110
csv := &olmapiv1alpha1.ClusterServiceVersion{}
128111
if pluginType == OLMIntegration || config.OLMDeployed {
129112
err := getCSV(config.CSVManifest, csv)
130113
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
150115
}
151116
}
152117

@@ -164,54 +129,9 @@ func RunInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig, lo
164129
return scapiv1alpha1.ScorecardOutput{}, err
165130
}
166131

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
215135
}
216136

217137
} else {
@@ -251,100 +171,16 @@ func RunInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig, lo
251171
}
252172
}
253173

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
274177
}
275178

276179
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)
294182
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
348184
}
349185
suites = append(suites, crSuites...)
350186
}
@@ -487,7 +323,7 @@ func getCRFromCSV(currentCRMans []string, crJSONStr string, csvName string) ([]s
487323
if crJSONStr != "" {
488324
var crs []interface{}
489325
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)
491327
}
492328
if len(crs) == 0 {
493329
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

Comments
 (0)