Skip to content

Commit 6d26d94

Browse files
authored
gen-csv: generate 'alm-examples' annotations from CR's (#1116)
1 parent 8aa2373 commit 6d26d94

File tree

3 files changed

+93
-27
lines changed

3 files changed

+93
-27
lines changed

internal/pkg/scaffold/olm-catalog/csv.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ func replaceAllBytes(v interface{}, old, new []byte) error {
357357
// user-defined manifests and updates csv.
358358
func (s *CSV) updateCSVFromManifestFiles(cfg *CSVConfig, csv *olmapiv1alpha1.ClusterServiceVersion) error {
359359
store := NewUpdaterStore()
360+
otherSpecs := make(map[string][][]byte)
360361
for _, f := range append(cfg.CRDCRPaths, cfg.OperatorPath, cfg.RolePath) {
361362
yamlData, err := afero.ReadFile(s.getFS(), f)
362363
if err != nil {
@@ -366,10 +367,33 @@ func (s *CSV) updateCSVFromManifestFiles(cfg *CSVConfig, csv *olmapiv1alpha1.Clu
366367
scanner := yamlutil.NewYAMLScanner(yamlData)
367368
for scanner.Scan() {
368369
yamlSpec := scanner.Bytes()
369-
370-
if err = store.AddToUpdater(yamlSpec); err != nil {
370+
kind, err := getKindfromYAML(yamlSpec)
371+
if err != nil {
372+
return err
373+
}
374+
found, err := store.AddToUpdater(yamlSpec, kind)
375+
if err != nil {
371376
return err
372377
}
378+
if !found {
379+
if _, ok := otherSpecs[kind]; !ok {
380+
otherSpecs[kind] = make([][]byte, 0)
381+
}
382+
otherSpecs[kind] = append(otherSpecs[kind], yamlSpec)
383+
}
384+
}
385+
if err = scanner.Err(); err != nil {
386+
return err
387+
}
388+
}
389+
390+
for k := range store.crds.crKinds {
391+
if crSpecs, ok := otherSpecs[k]; ok {
392+
for _, spec := range crSpecs {
393+
if err := store.AddCR(spec); err != nil {
394+
return err
395+
}
396+
}
373397
}
374398
}
375399

internal/pkg/scaffold/olm-catalog/csv_updaters.go

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package catalog
1717
import (
1818
"encoding/json"
1919
"fmt"
20+
"strings"
2021

2122
"github.com/ghodss/yaml"
2223
olmapiv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
@@ -34,24 +35,28 @@ type CSVUpdater interface {
3435
}
3536

3637
type updaterStore struct {
37-
installStrategy *CSVInstallStrategyUpdate
38-
crdUpdate *CSVCustomResourceDefinitionsUpdate
38+
installStrategy *InstallStrategyUpdate
39+
crds *CustomResourceDefinitionsUpdate
40+
almExamples *ALMExamplesUpdate
3941
}
4042

4143
func NewUpdaterStore() *updaterStore {
4244
return &updaterStore{
43-
installStrategy: &CSVInstallStrategyUpdate{
45+
installStrategy: &InstallStrategyUpdate{
4446
&olminstall.StrategyDetailsDeployment{},
4547
},
46-
crdUpdate: &CSVCustomResourceDefinitionsUpdate{
48+
crds: &CustomResourceDefinitionsUpdate{
4749
&olmapiv1alpha1.CustomResourceDefinitions{},
50+
make(map[string]struct{}),
4851
},
52+
almExamples: &ALMExamplesUpdate{},
4953
}
5054
}
5155

5256
// Apply iteratively calls each stored CSVUpdater's Apply() method.
5357
func (s *updaterStore) Apply(csv *olmapiv1alpha1.ClusterServiceVersion) error {
54-
for _, updater := range []CSVUpdater{s.installStrategy, s.crdUpdate} {
58+
updaters := []CSVUpdater{s.installStrategy, s.crds, s.almExamples}
59+
for _, updater := range updaters {
5560
if err := updater.Apply(csv); err != nil {
5661
return err
5762
}
@@ -60,7 +65,6 @@ func (s *updaterStore) Apply(csv *olmapiv1alpha1.ClusterServiceVersion) error {
6065
}
6166

6267
func getKindfromYAML(yamlData []byte) (string, error) {
63-
// Get Kind for inital categorization.
6468
var temp struct {
6569
Kind string
6670
}
@@ -70,27 +74,25 @@ func getKindfromYAML(yamlData []byte) (string, error) {
7074
return temp.Kind, nil
7175
}
7276

73-
func (s *updaterStore) AddToUpdater(yamlSpec []byte) error {
74-
kind, err := getKindfromYAML(yamlSpec)
75-
if err != nil {
76-
return err
77-
}
78-
77+
func (s *updaterStore) AddToUpdater(yamlSpec []byte, kind string) (found bool, err error) {
78+
found = true
7979
switch kind {
8080
case "Role":
81-
return s.AddRole(yamlSpec)
81+
err = s.AddRole(yamlSpec)
8282
case "ClusterRole":
83-
return s.AddClusterRole(yamlSpec)
83+
err = s.AddClusterRole(yamlSpec)
8484
case "Deployment":
85-
return s.AddDeploymentSpec(yamlSpec)
85+
err = s.AddDeploymentSpec(yamlSpec)
8686
case "CustomResourceDefinition":
8787
// All CRD's present will be 'owned'.
88-
return s.AddOwnedCRD(yamlSpec)
88+
err = s.AddOwnedCRD(yamlSpec)
89+
default:
90+
found = false
8991
}
90-
return nil
92+
return found, err
9193
}
9294

93-
type CSVInstallStrategyUpdate struct {
95+
type InstallStrategyUpdate struct {
9496
*olminstall.StrategyDetailsDeployment
9597
}
9698

@@ -136,7 +138,7 @@ func (store *updaterStore) AddDeploymentSpec(yamlDoc []byte) error {
136138
return nil
137139
}
138140

139-
func (u *CSVInstallStrategyUpdate) Apply(csv *olmapiv1alpha1.ClusterServiceVersion) (err error) {
141+
func (u *InstallStrategyUpdate) Apply(csv *olmapiv1alpha1.ClusterServiceVersion) (err error) {
140142
// Get install strategy from csv. Default to a deployment strategy if none found.
141143
var strat olminstall.Strategy
142144
if csv.Spec.InstallStrategy.StrategyName == "" {
@@ -170,44 +172,46 @@ func (u *CSVInstallStrategyUpdate) Apply(csv *olmapiv1alpha1.ClusterServiceVersi
170172
return nil
171173
}
172174

173-
func (u *CSVInstallStrategyUpdate) updatePermissions(strat *olminstall.StrategyDetailsDeployment) {
175+
func (u *InstallStrategyUpdate) updatePermissions(strat *olminstall.StrategyDetailsDeployment) {
174176
if len(u.Permissions) != 0 {
175177
strat.Permissions = u.Permissions
176178
}
177179
}
178180

179-
func (u *CSVInstallStrategyUpdate) updateClusterPermissions(strat *olminstall.StrategyDetailsDeployment) {
181+
func (u *InstallStrategyUpdate) updateClusterPermissions(strat *olminstall.StrategyDetailsDeployment) {
180182
if len(u.ClusterPermissions) != 0 {
181183
strat.ClusterPermissions = u.ClusterPermissions
182184
}
183185
}
184186

185-
func (u *CSVInstallStrategyUpdate) updateDeploymentSpecs(strat *olminstall.StrategyDetailsDeployment) {
187+
func (u *InstallStrategyUpdate) updateDeploymentSpecs(strat *olminstall.StrategyDetailsDeployment) {
186188
if len(u.DeploymentSpecs) != 0 {
187189
strat.DeploymentSpecs = u.DeploymentSpecs
188190
}
189191
}
190192

191-
type CSVCustomResourceDefinitionsUpdate struct {
193+
type CustomResourceDefinitionsUpdate struct {
192194
*olmapiv1alpha1.CustomResourceDefinitions
195+
crKinds map[string]struct{}
193196
}
194197

195198
func (store *updaterStore) AddOwnedCRD(yamlDoc []byte) error {
196199
crd := &apiextv1beta1.CustomResourceDefinition{}
197200
if err := yaml.Unmarshal(yamlDoc, crd); err != nil {
198201
return err
199202
}
200-
store.crdUpdate.Owned = append(store.crdUpdate.Owned, olmapiv1alpha1.CRDDescription{
203+
store.crds.Owned = append(store.crds.Owned, olmapiv1alpha1.CRDDescription{
201204
Name: crd.ObjectMeta.Name,
202205
Version: crd.Spec.Version,
203206
Kind: crd.Spec.Names.Kind,
204207
})
208+
store.crds.crKinds[crd.Spec.Names.Kind] = struct{}{}
205209
return nil
206210
}
207211

208212
// Apply updates csv's "owned" CRDDescriptions. "required" CRDDescriptions are
209213
// left as-is, since they are user-defined values.
210-
func (u *CSVCustomResourceDefinitionsUpdate) Apply(csv *olmapiv1alpha1.ClusterServiceVersion) error {
214+
func (u *CustomResourceDefinitionsUpdate) Apply(csv *olmapiv1alpha1.ClusterServiceVersion) error {
211215
set := make(map[string]olmapiv1alpha1.CRDDescription)
212216
for _, csvDesc := range csv.Spec.CustomResourceDefinitions.Owned {
213217
set[csvDesc.Name] = csvDesc
@@ -224,3 +228,40 @@ func (u *CSVCustomResourceDefinitionsUpdate) Apply(csv *olmapiv1alpha1.ClusterSe
224228
csv.Spec.CustomResourceDefinitions.Owned = du.Owned
225229
return nil
226230
}
231+
232+
type ALMExamplesUpdate struct {
233+
crs []string
234+
}
235+
236+
func (store *updaterStore) AddCR(yamlDoc []byte) error {
237+
if len(yamlDoc) == 0 {
238+
return nil
239+
}
240+
crBytes, err := yaml.YAMLToJSON(yamlDoc)
241+
if err != nil {
242+
return err
243+
}
244+
store.almExamples.crs = append(store.almExamples.crs, string(crBytes))
245+
return nil
246+
}
247+
248+
func (u *ALMExamplesUpdate) Apply(csv *olmapiv1alpha1.ClusterServiceVersion) error {
249+
if len(u.crs) == 0 {
250+
return nil
251+
}
252+
if csv.GetAnnotations() == nil {
253+
csv.SetAnnotations(make(map[string]string))
254+
}
255+
sb := &strings.Builder{}
256+
sb.WriteString(`[`)
257+
for i, example := range u.crs {
258+
sb.WriteString(example)
259+
if i < len(u.crs)-1 {
260+
sb.WriteString(`,`)
261+
}
262+
}
263+
sb.WriteString(`]`)
264+
265+
csv.GetAnnotations()["alm-examples"] = sb.String()
266+
return nil
267+
}

internal/pkg/scaffold/olm-catalog/testdata/deploy/olm-catalog/app-operator/0.1.0/app-operator.v0.1.0.clusterserviceversion.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apiVersion: operators.coreos.com/v1alpha1
22
kind: ClusterServiceVersion
33
metadata:
44
annotations:
5+
alm-examples: '[{"apiVersion":"app.example.com/v1alpha1","kind":"App","metadata":{"name":"example-app"},"spec":{"size":3}}]'
56
capabilities: Basic Install
67
name: app-operator.v0.1.0
78
namespace: placeholder

0 commit comments

Comments
 (0)