Skip to content

Commit

Permalink
feat(olm-catalog): Generated alm-examples in a pretty format (#1793)
Browse files Browse the repository at this point in the history
* feat(olm-catalog) : Generating alm-examples in a pretty format

* feat(olm-catalog) : update test-framework memcached-operator CSV version 0.0.3 with alm-examples in a pretty format

* feat(olm-catalog) : Changelog (ISSUE_1708)
  • Loading branch information
camilamacedo86 authored and Eric Stroczynski committed Aug 12, 2019
1 parent c1e2eae commit 9e1550e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

### Added
- The `operator-sdk olm-catalog gen-csv` command now produces indented JSON for the `alm-examples` annotation. ([#1793](https://github.com/operator-framework/operator-sdk/pull/1793))

### Changed

Expand Down
24 changes: 17 additions & 7 deletions internal/pkg/scaffold/olm-catalog/csv_updaters.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,26 @@ func (u *ALMExamplesUpdate) Apply(csv *olmapiv1alpha1.ClusterServiceVersion) err
if csv.GetAnnotations() == nil {
csv.SetAnnotations(make(map[string]string))
}
sb := &strings.Builder{}
sb.WriteString(`[`)
buf := &bytes.Buffer{}
buf.WriteString(`[`)
for i, example := range u.crs {
sb.WriteString(example)
buf.WriteString(example)
if i < len(u.crs)-1 {
sb.WriteString(`,`)
buf.WriteString(`,`)
}
}
sb.WriteString(`]`)

csv.GetAnnotations()["alm-examples"] = sb.String()
buf.WriteString(`]`)
examplesJSON, err := prettyJSON(buf.Bytes())
if err != nil {
return err
}
csv.GetAnnotations()["alm-examples"] = examplesJSON
return nil
}

// prettyJSON returns a JSON in a pretty format
func prettyJSON(b []byte) (string, error) {
var out bytes.Buffer
err := json.Indent(&out, b, "", " ")
return out.String(), err
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ apiVersion: operators.coreos.com/v1alpha1
kind: ClusterServiceVersion
metadata:
annotations:
alm-examples: '[{"apiVersion":"cache.example.com/v1alpha1","kind":"Memcached","metadata":{"name":"example-memcached"},"spec":{"size":3}}]'
capabilities: Basic Install
alm-examples: |-
[
{
"apiVersion": "cache.example.com/v1alpha1",
"kind": "Memcached",
"metadata": {
"name": "example-memcached"
},
"spec": {
"size": 3
}
}
]
capabilities: Basic Install
name: memcachedoperator.v0.0.3
namespace: placeholder
spec:
Expand Down

0 comments on commit 9e1550e

Please sign in to comment.