Skip to content

Commit aaf7764

Browse files
committed
updates based on PR comments
1 parent 3d7fdb5 commit aaf7764

File tree

14 files changed

+174
-121
lines changed

14 files changed

+174
-121
lines changed

cmd/operator-sdk/generate/bundle/bundle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func (c bundleCmd) generateMetadata(cfg *config.Config, manifestsDir, outputDir
307307
return nil
308308
}
309309

310-
// NB(estroz): these updates need to be atomic because the bundle's Dockerfile and annotations.yaml
310+
// TODO(estroz): these updates need to be atomic because the bundle's Dockerfile and annotations.yaml
311311
// cannot be out-of-sync.
312312
func updateMetadata(cfg *config.Config, bundleRoot string) error {
313313
bundleLabels := metricsannotations.MakeBundleMetadataLabels(cfg)

internal/generate/collector/collect.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ func (c *Manifests) UpdateFromDirs(deployDir, crdsDir string) error {
9090
case "MutatingWebhookConfiguration":
9191
err = c.addMutatingWebhookConfigurations(manifest)
9292
case scorecardv1alpha3.ConfigurationKind:
93-
err = c.addScorecardConfig(manifest)
93+
if gvk.GroupVersion() == scorecardv1alpha3.SchemeGroupVersion {
94+
err = c.addScorecardConfig(manifest)
95+
}
9496
default:
9597
err = c.addOthers(manifest)
9698
}
@@ -152,7 +154,9 @@ func (c *Manifests) UpdateFromReader(r io.Reader) error {
152154
case "MutatingWebhookConfiguration":
153155
err = c.addMutatingWebhookConfigurations(manifest)
154156
case scorecardv1alpha3.ConfigurationKind:
155-
err = c.addScorecardConfig(manifest)
157+
if gvk.GroupVersion() == scorecardv1alpha3.SchemeGroupVersion {
158+
err = c.addScorecardConfig(manifest)
159+
}
156160
default:
157161
err = c.addOthers(manifest)
158162
}
@@ -273,7 +277,7 @@ func (c *Manifests) addScorecardConfig(rawManifest []byte) error {
273277
return err
274278
}
275279
if c.ScorecardConfig.Metadata.Name != "" {
276-
return errors.New("duplicate ScorecardConfigurations in collector input")
280+
return errors.New("duplicate scorecard configurations in collector input")
277281
}
278282
c.ScorecardConfig = cfg
279283
return nil

internal/plugins/golang/v2/init.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ package v2
1616

1717
import (
1818
"fmt"
19-
"os"
20-
"path/filepath"
21-
"strings"
2219

2320
"github.com/spf13/pflag"
2421
"sigs.k8s.io/kubebuilder/pkg/model/config"
@@ -54,14 +51,8 @@ func (p *initPlugin) Run() error {
5451
return err
5552
}
5653

57-
// Assume projectName was validated by go.kubebuilder.io.
58-
wd, err := os.Getwd()
59-
if err != nil {
60-
return fmt.Errorf("error getting the current path: %v", err)
61-
}
62-
projectName := strings.ToLower(filepath.Base(wd))
6354
// Run the scorecard "phase 2" plugin.
64-
if err := scorecard.RunInit(projectName); err != nil {
55+
if err := scorecard.RunInit(p.config); err != nil {
6556
return err
6657
}
6758

internal/plugins/helm/v1/init.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,8 @@ func (p *initPlugin) Run() error {
133133
return err
134134
}
135135

136-
// Assume projectName was validated by Validate().
137-
wd, err := os.Getwd()
138-
if err != nil {
139-
return fmt.Errorf("error getting the current path: %v", err)
140-
}
141-
projectName := strings.ToLower(filepath.Base(wd))
142136
// Run the scorecard "phase 2" plugin.
143-
if err := scorecard.RunInit(projectName); err != nil {
137+
if err := scorecard.RunInit(p.config); err != nil {
144138
return err
145139
}
146140

internal/plugins/scorecard/init.go

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ import (
2222
"path/filepath"
2323
"text/template"
2424

25-
"github.com/operator-framework/operator-registry/pkg/registry"
25+
"k8s.io/apimachinery/pkg/runtime/schema"
26+
"sigs.k8s.io/kubebuilder/pkg/model/config"
2627
"sigs.k8s.io/yaml"
2728

2829
"github.com/operator-framework/operator-sdk/internal/scaffold/kustomize"
2930
"github.com/operator-framework/operator-sdk/internal/scorecard"
3031
"github.com/operator-framework/operator-sdk/pkg/apis/scorecard/v1alpha3"
3132
)
3233

33-
// kustomization.yaml file template for the scorecard componentconfig. This should always be written to
34-
// config/scorecard/kustomization.yaml since it only references files in config.
35-
const scorecardKustomizationTemplate = `resources:
34+
const (
35+
// kustomization.yaml file template for the scorecard componentconfig. This should always be written to
36+
// config/scorecard/kustomization.yaml since it only references files in config.
37+
scorecardKustomizationTemplate = `resources:
3638
{{- range $i, $path := .ResourcePaths }}
3739
- {{ $path }}
3840
{{- end }}
@@ -47,24 +49,28 @@ patchesJson6902:
4749
{{- end }}
4850
`
4951

52+
// YAML file fragment to append to kustomization.yaml files.
53+
kubebuilderScaffoldMarkerFragment = "# +kubebuilder:scaffold\n"
54+
)
55+
5056
const (
5157
// defaultTestImageTag points to the latest-released image.
5258
// TODO: change the tag to "latest" once config scaffolding is in a release,
5359
// as the new config spec won't work with the current latest image.
5460
defaultTestImageTag = "quay.io/operator-framework/scorecard-test:master"
5561

56-
// scorecardConfigName is the default scorecard componentconfig's metadata.name,
62+
// defaultConfigName is the default scorecard componentconfig's metadata.name,
5763
// which must be set on all kustomize-able bases. This name is only used for
5864
// `kustomize build` pattern match and not for on-cluster creation.
59-
scorecardConfigName = "config"
65+
defaultConfigName = "config"
6066
)
6167

6268
// defaultDir is the default directory in which to generate kustomize bases and the kustomization.yaml.
6369
var defaultDir = filepath.Join("config", "scorecard")
6470

6571
// RunInit scaffolds kustomize files for kustomizing a scorecard componentconfig.
66-
func RunInit(projectName string) error {
67-
return generate(projectName, defaultTestImageTag, defaultDir)
72+
func RunInit(*config.Config) error {
73+
return generate(defaultTestImageTag, defaultDir)
6874
}
6975

7076
// scorecardKustomizationValues holds data required to generate a scorecard's kustomization.yaml.
@@ -76,12 +82,18 @@ type scorecardKustomizationValues struct {
7682
// kustomizationJSON6902Patch holds path and target data to write a patchesJson6902 list in a kustomization.yaml.
7783
type kustomizationJSON6902Patch struct {
7884
Path string
79-
Target registry.DefinitionKey
85+
Target patchTarget
86+
}
87+
88+
// patchTarget holds target data for a kustomize patch.
89+
type patchTarget struct {
90+
schema.GroupVersionKind
91+
Name string
8092
}
8193

8294
// generate scaffolds kustomize bundle bases and a kustomization.yaml.
8395
// TODO(estroz): refactor this to be testable (in-mem fs) and easier to read.
84-
func generate(operatorName, testImageTag, outputDir string) error {
96+
func generate(testImageTag, outputDir string) error {
8597

8698
kustomizationValues := scorecardKustomizationValues{}
8799

@@ -91,7 +103,7 @@ func generate(operatorName, testImageTag, outputDir string) error {
91103
return err
92104
}
93105

94-
configBase := newScorecardConfigurationBase()
106+
configBase := newConfigurationBase(defaultConfigName)
95107
b, err := yaml.Marshal(configBase)
96108
if err != nil {
97109
return fmt.Errorf("error marshaling default config: %v", err)
@@ -102,11 +114,9 @@ func generate(operatorName, testImageTag, outputDir string) error {
102114
return fmt.Errorf("error writing default scorecard config: %v", err)
103115
}
104116
kustomizationValues.ResourcePaths = append(kustomizationValues.ResourcePaths, relBasePath)
105-
scorecardConfigTarget := registry.DefinitionKey{
106-
Group: v1alpha3.SchemeGroupVersion.Group,
107-
Version: v1alpha3.SchemeGroupVersion.Version,
108-
Kind: v1alpha3.ConfigurationKind,
109-
Name: scorecardConfigName,
117+
scorecardConfigTarget := patchTarget{
118+
GroupVersionKind: v1alpha3.SchemeGroupVersion.WithKind(v1alpha3.ConfigurationKind),
119+
Name: defaultConfigName,
110120
}
111121

112122
// Config patches.
@@ -116,7 +126,7 @@ func generate(operatorName, testImageTag, outputDir string) error {
116126
}
117127

118128
// Basic scorecard tests patch.
119-
basicPatch := newBasicScorecardConfigurationPatch(operatorName, testImageTag)
129+
basicPatch := newBasicConfigurationPatch(testImageTag)
120130
b, err = yaml.Marshal(basicPatch)
121131
if err != nil {
122132
return fmt.Errorf("error marshaling basic patch config: %v", err)
@@ -131,7 +141,7 @@ func generate(operatorName, testImageTag, outputDir string) error {
131141
})
132142

133143
// OLM scorecard tests patch.
134-
olmPatch := newOLMScorecardConfigurationPatch(operatorName, testImageTag)
144+
olmPatch := newOLMConfigurationPatch(testImageTag)
135145
b, err = yaml.Marshal(olmPatch)
136146
if err != nil {
137147
return fmt.Errorf("error marshaling OLM patch config: %v", err)
@@ -154,6 +164,8 @@ func generate(operatorName, testImageTag, outputDir string) error {
154164
if err = t.Execute(&buf, kustomizationValues); err != nil {
155165
return fmt.Errorf("error executing on default kustomize template: %v", err)
156166
}
167+
// Append the kubebuilder scaffold marker to make updates to this file in the future.
168+
buf.Write([]byte(kubebuilderScaffoldMarkerFragment))
157169
if err := kustomize.Write(outputDir, buf.String()); err != nil {
158170
return fmt.Errorf("error writing default scorecard kustomization.yaml: %v", err)
159171
}
@@ -172,11 +184,11 @@ type jsonPatchObject struct {
172184
Value v1alpha3.TestConfiguration `json:"value"`
173185
}
174186

175-
// newScorecardConfigurationBase returns a scorecard componentconfig object with one parallel stage.
187+
// newConfigurationBase returns a scorecard componentconfig object with one parallel stage.
176188
// The returned object is intended to be marshaled and written to disk as a kustomize base.
177-
func newScorecardConfigurationBase() (cfg v1alpha3.Configuration) {
189+
func newConfigurationBase(configName string) (cfg v1alpha3.Configuration) {
178190
cfg.SetGroupVersionKind(v1alpha3.SchemeGroupVersion.WithKind(v1alpha3.ConfigurationKind))
179-
cfg.Metadata.Name = scorecardConfigName
191+
cfg.Metadata.Name = configName
180192
cfg.Stages = []v1alpha3.StageConfiguration{
181193
{
182194
Parallel: true,
@@ -186,57 +198,54 @@ func newScorecardConfigurationBase() (cfg v1alpha3.Configuration) {
186198
return cfg
187199
}
188200

189-
func makeTestStageJSONPath(stageIdx, testIdx int) string {
190-
return fmt.Sprintf("/stages/%d/tests/%d", stageIdx, testIdx)
191-
}
201+
const defaultJSONPath = "/stages/0/tests/-"
192202

193-
// newBasicScorecardConfigurationPatch returns default "basic" test configurations as JSON patch objects
203+
// newBasicConfigurationPatch returns default "basic" test configurations as JSON patch objects
194204
// to be inserted into the componentconfig base as a first stage test element.
195205
// The returned patches are intended to be marshaled and written to disk as in a kustomize patch file.
196-
func newBasicScorecardConfigurationPatch(operatorName, testImageTag string) (ps jsonPatches) {
197-
for i, cfg := range makeDefaultBasicTestConfigs(operatorName, testImageTag) {
206+
func newBasicConfigurationPatch(testImageTag string) (ps jsonPatches) {
207+
for _, cfg := range makeDefaultBasicTestConfigs(testImageTag) {
198208
ps = append(ps, jsonPatchObject{
199209
Op: "add",
200-
Path: makeTestStageJSONPath(0, i),
210+
Path: defaultJSONPath,
201211
Value: cfg,
202212
})
203213
}
204214
return ps
205215
}
206216

207217
// makeDefaultBasicTestConfigs returns all default "basic" test configurations.
208-
func makeDefaultBasicTestConfigs(operatorName, testImageTag string) (cfgs []v1alpha3.TestConfiguration) {
218+
func makeDefaultBasicTestConfigs(testImageTag string) (cfgs []v1alpha3.TestConfiguration) {
209219
for _, testName := range []string{"basic-check-spec"} {
210220
cfgs = append(cfgs, v1alpha3.TestConfiguration{
211221
Image: testImageTag,
212222
Entrypoint: []string{"scorecard-test", testName},
213223
Labels: map[string]string{
214-
"operator": operatorName,
215-
"suite": "basic",
216-
"test": fmt.Sprintf("%s-test", testName),
224+
"suite": "basic",
225+
"test": fmt.Sprintf("%s-test", testName),
217226
},
218227
})
219228
}
220229

221230
return cfgs
222231
}
223232

224-
// newOLMScorecardConfigurationPatch returns default "olm" test configurations as JSON patch objects
233+
// newOLMConfigurationPatch returns default "olm" test configurations as JSON patch objects
225234
// to be inserted into the componentconfig base as a first stage test element.
226235
// The returned patches are intended to be marshaled and written to disk as in a kustomize patch file.
227-
func newOLMScorecardConfigurationPatch(operatorName, testImageTag string) (ps jsonPatches) {
228-
for i, cfg := range makeDefaultOLMTestConfigs(operatorName, testImageTag) {
236+
func newOLMConfigurationPatch(testImageTag string) (ps jsonPatches) {
237+
for _, cfg := range makeDefaultOLMTestConfigs(testImageTag) {
229238
ps = append(ps, jsonPatchObject{
230239
Op: "add",
231-
Path: makeTestStageJSONPath(0, i),
240+
Path: defaultJSONPath,
232241
Value: cfg,
233242
})
234243
}
235244
return ps
236245
}
237246

238247
// makeDefaultOLMTestConfigs returns all default "olm" test configurations.
239-
func makeDefaultOLMTestConfigs(operatorName, testImageTag string) (cfgs []v1alpha3.TestConfiguration) {
248+
func makeDefaultOLMTestConfigs(testImageTag string) (cfgs []v1alpha3.TestConfiguration) {
240249
for _, testName := range []string{
241250
"olm-bundle-validation",
242251
"olm-crds-have-validation",
@@ -248,9 +257,8 @@ func makeDefaultOLMTestConfigs(operatorName, testImageTag string) (cfgs []v1alph
248257
Image: testImageTag,
249258
Entrypoint: []string{"scorecard-test", testName},
250259
Labels: map[string]string{
251-
"operator": operatorName,
252-
"suite": "olm",
253-
"test": fmt.Sprintf("%s-test", testName),
260+
"suite": "olm",
261+
"test": fmt.Sprintf("%s-test", testName),
254262
},
255263
})
256264
}

internal/scorecard/examples/custom-scorecard-tests/bundle/tests/scorecard/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
kind: ScorecardConfiguration
1+
kind: Configuration
22
apiversion: scorecard.operatorframework.io/v1alpha3
33
metadata:
44
name: config

internal/scorecard/formatting.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ func (o Scorecard) List() v1alpha3.TestList {
4646
tests := o.selectTests(stage)
4747
for _, test := range tests {
4848
item := v1alpha3.NewTest()
49-
item.Spec.Image = test.Image
50-
item.Spec.Entrypoint = test.Entrypoint
51-
item.Spec.Labels = test.Labels
49+
item.Spec = test
5250
output.Items = append(output.Items, item)
5351
}
5452
}

internal/scorecard/scorecard.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ func (o Scorecard) runTest(ctx context.Context, test v1alpha3.TestConfiguration)
118118
}
119119

120120
out := v1alpha3.NewTest()
121-
out.Spec.Image = test.Image
122-
out.Spec.Entrypoint = test.Entrypoint
123-
out.Spec.Labels = test.Labels
121+
out.Spec = test
124122
out.Status = *result
125123
return out
126124
}

internal/scorecard/testdata/bundle/tests/scorecard/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
kind: ScorecardConfiguration
1+
kind: Configuration
22
apiversion: scorecard.operatorframework.io/v1alpha3
33
metadata:
44
name: config

pkg/apis/scorecard/v1alpha3/configuration_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ import (
1919
)
2020

2121
// ConfigurationKind is the default scorecard componentconfig kind.
22-
const ConfigurationKind = "ScorecardConfiguration"
22+
const ConfigurationKind = "Configuration"
2323

2424
// Configuration represents the set of test configurations which scorecard would run.
2525
type Configuration struct {
2626
metav1.TypeMeta `json:",inline" yaml:",inline"`
2727

2828
// Do not use metav1.ObjectMeta because this "object" should not be treated as an actual object.
2929
Metadata struct {
30+
// Name is a required field for kustomize-able manifests, and is not used on-cluster (nor is the config itself).
3031
Name string `json:"name,omitempty" yaml:"name,omitempty"`
3132
} `json:"metadata,omitempty" yaml:"metadata,omitempty"`
3233

0 commit comments

Comments
 (0)