@@ -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+
5056const (
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.
6369var 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.
7783type 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 }
0 commit comments