Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(olm-catalog) : Generated alm-examples in a pretty format #1793

Merged
merged 3 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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