@@ -17,6 +17,7 @@ package catalog
17
17
import (
18
18
"encoding/json"
19
19
"fmt"
20
+ "strings"
20
21
21
22
"github.com/ghodss/yaml"
22
23
olmapiv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
@@ -34,24 +35,28 @@ type CSVUpdater interface {
34
35
}
35
36
36
37
type updaterStore struct {
37
- installStrategy * CSVInstallStrategyUpdate
38
- crdUpdate * CSVCustomResourceDefinitionsUpdate
38
+ installStrategy * InstallStrategyUpdate
39
+ crds * CustomResourceDefinitionsUpdate
40
+ almExamples * ALMExamplesUpdate
39
41
}
40
42
41
43
func NewUpdaterStore () * updaterStore {
42
44
return & updaterStore {
43
- installStrategy : & CSVInstallStrategyUpdate {
45
+ installStrategy : & InstallStrategyUpdate {
44
46
& olminstall.StrategyDetailsDeployment {},
45
47
},
46
- crdUpdate : & CSVCustomResourceDefinitionsUpdate {
48
+ crds : & CustomResourceDefinitionsUpdate {
47
49
& olmapiv1alpha1.CustomResourceDefinitions {},
50
+ make (map [string ]struct {}),
48
51
},
52
+ almExamples : & ALMExamplesUpdate {},
49
53
}
50
54
}
51
55
52
56
// Apply iteratively calls each stored CSVUpdater's Apply() method.
53
57
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 {
55
60
if err := updater .Apply (csv ); err != nil {
56
61
return err
57
62
}
@@ -60,7 +65,6 @@ func (s *updaterStore) Apply(csv *olmapiv1alpha1.ClusterServiceVersion) error {
60
65
}
61
66
62
67
func getKindfromYAML (yamlData []byte ) (string , error ) {
63
- // Get Kind for inital categorization.
64
68
var temp struct {
65
69
Kind string
66
70
}
@@ -70,27 +74,25 @@ func getKindfromYAML(yamlData []byte) (string, error) {
70
74
return temp .Kind , nil
71
75
}
72
76
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
79
79
switch kind {
80
80
case "Role" :
81
- return s .AddRole (yamlSpec )
81
+ err = s .AddRole (yamlSpec )
82
82
case "ClusterRole" :
83
- return s .AddClusterRole (yamlSpec )
83
+ err = s .AddClusterRole (yamlSpec )
84
84
case "Deployment" :
85
- return s .AddDeploymentSpec (yamlSpec )
85
+ err = s .AddDeploymentSpec (yamlSpec )
86
86
case "CustomResourceDefinition" :
87
87
// All CRD's present will be 'owned'.
88
- return s .AddOwnedCRD (yamlSpec )
88
+ err = s .AddOwnedCRD (yamlSpec )
89
+ default :
90
+ found = false
89
91
}
90
- return nil
92
+ return found , err
91
93
}
92
94
93
- type CSVInstallStrategyUpdate struct {
95
+ type InstallStrategyUpdate struct {
94
96
* olminstall.StrategyDetailsDeployment
95
97
}
96
98
@@ -136,7 +138,7 @@ func (store *updaterStore) AddDeploymentSpec(yamlDoc []byte) error {
136
138
return nil
137
139
}
138
140
139
- func (u * CSVInstallStrategyUpdate ) Apply (csv * olmapiv1alpha1.ClusterServiceVersion ) (err error ) {
141
+ func (u * InstallStrategyUpdate ) Apply (csv * olmapiv1alpha1.ClusterServiceVersion ) (err error ) {
140
142
// Get install strategy from csv. Default to a deployment strategy if none found.
141
143
var strat olminstall.Strategy
142
144
if csv .Spec .InstallStrategy .StrategyName == "" {
@@ -170,44 +172,46 @@ func (u *CSVInstallStrategyUpdate) Apply(csv *olmapiv1alpha1.ClusterServiceVersi
170
172
return nil
171
173
}
172
174
173
- func (u * CSVInstallStrategyUpdate ) updatePermissions (strat * olminstall.StrategyDetailsDeployment ) {
175
+ func (u * InstallStrategyUpdate ) updatePermissions (strat * olminstall.StrategyDetailsDeployment ) {
174
176
if len (u .Permissions ) != 0 {
175
177
strat .Permissions = u .Permissions
176
178
}
177
179
}
178
180
179
- func (u * CSVInstallStrategyUpdate ) updateClusterPermissions (strat * olminstall.StrategyDetailsDeployment ) {
181
+ func (u * InstallStrategyUpdate ) updateClusterPermissions (strat * olminstall.StrategyDetailsDeployment ) {
180
182
if len (u .ClusterPermissions ) != 0 {
181
183
strat .ClusterPermissions = u .ClusterPermissions
182
184
}
183
185
}
184
186
185
- func (u * CSVInstallStrategyUpdate ) updateDeploymentSpecs (strat * olminstall.StrategyDetailsDeployment ) {
187
+ func (u * InstallStrategyUpdate ) updateDeploymentSpecs (strat * olminstall.StrategyDetailsDeployment ) {
186
188
if len (u .DeploymentSpecs ) != 0 {
187
189
strat .DeploymentSpecs = u .DeploymentSpecs
188
190
}
189
191
}
190
192
191
- type CSVCustomResourceDefinitionsUpdate struct {
193
+ type CustomResourceDefinitionsUpdate struct {
192
194
* olmapiv1alpha1.CustomResourceDefinitions
195
+ crKinds map [string ]struct {}
193
196
}
194
197
195
198
func (store * updaterStore ) AddOwnedCRD (yamlDoc []byte ) error {
196
199
crd := & apiextv1beta1.CustomResourceDefinition {}
197
200
if err := yaml .Unmarshal (yamlDoc , crd ); err != nil {
198
201
return err
199
202
}
200
- store .crdUpdate .Owned = append (store .crdUpdate .Owned , olmapiv1alpha1.CRDDescription {
203
+ store .crds .Owned = append (store .crds .Owned , olmapiv1alpha1.CRDDescription {
201
204
Name : crd .ObjectMeta .Name ,
202
205
Version : crd .Spec .Version ,
203
206
Kind : crd .Spec .Names .Kind ,
204
207
})
208
+ store .crds .crKinds [crd .Spec .Names .Kind ] = struct {}{}
205
209
return nil
206
210
}
207
211
208
212
// Apply updates csv's "owned" CRDDescriptions. "required" CRDDescriptions are
209
213
// 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 {
211
215
set := make (map [string ]olmapiv1alpha1.CRDDescription )
212
216
for _ , csvDesc := range csv .Spec .CustomResourceDefinitions .Owned {
213
217
set [csvDesc .Name ] = csvDesc
@@ -224,3 +228,40 @@ func (u *CSVCustomResourceDefinitionsUpdate) Apply(csv *olmapiv1alpha1.ClusterSe
224
228
csv .Spec .CustomResourceDefinitions .Owned = du .Owned
225
229
return nil
226
230
}
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
+ }
0 commit comments