Skip to content

Commit

Permalink
Add olm bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Kralik committed May 20, 2024
1 parent 201ae2c commit 106dca2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
66 changes: 56 additions & 10 deletions tools/veneer-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"gopkg.in/yaml.v3"
)

const imageNameTemplate = "docker.io/perconalab/everest-operator-bundle:%s"

func main() {
var (
versionType string
Expand Down Expand Up @@ -61,7 +63,12 @@ func main() {
}

func createPatchVersionVeneer(veneerFile, channel, currentVersion, newVersion string) ([]byte, error) {
return updateVeneerFile(veneerFile, "olm.channel", channel, func(m map[string]any) (map[string]any, error) {
source, err := os.ReadFile(veneerFile)
if err != nil {
return nil, fmt.Errorf("could not read veneer file: %w", err)
}

ret, err := updateVeneerFile(source, "olm.channel", "name", channel, func(m map[string]any) (map[string]any, error) {
entries, ok := m["entries"]
if !ok {
return nil, errors.New("could not find entries")
Expand Down Expand Up @@ -102,10 +109,21 @@ func createPatchVersionVeneer(veneerFile, channel, currentVersion, newVersion st

return m, nil
})

if err != nil {
return nil, err
}

return addOLMBundle(ret, newVersion)
}

func createMinorVersionVeneer(veneerFile, channel, currentVersion, newVersion string) ([]byte, error) {
return updateVeneerFile(veneerFile, "olm.channel", channel, func(m map[string]any) (map[string]any, error) {
source, err := os.ReadFile(veneerFile)
if err != nil {
return nil, fmt.Errorf("could not read veneer file: %w", err)
}

ret, err := updateVeneerFile(source, "olm.channel", "name", channel, func(m map[string]any) (map[string]any, error) {
entries, ok := m["entries"]
if !ok {
return nil, errors.New("could not find entries")
Expand Down Expand Up @@ -154,6 +172,12 @@ func createMinorVersionVeneer(veneerFile, channel, currentVersion, newVersion st

return m, nil
})

if err != nil {
return nil, err
}

return addOLMBundle(ret, newVersion)
}

func findEntry(entries []any, version string) (int, map[string]any, error) {
Expand All @@ -178,13 +202,8 @@ func findEntry(entries []any, version string) (int, map[string]any, error) {
return 0, nil, errors.New("not found")
}

func updateVeneerFile(veneerFile, schema, name string, replace func(map[string]any) (map[string]any, error)) ([]byte, error) {
source, err := os.ReadFile(veneerFile)
if err != nil {
return nil, fmt.Errorf("could not read veneer file: %w", err)
}

d := yaml.NewDecoder(bytes.NewReader(source))
func updateVeneerFile(veneerFile []byte, schema, fieldName, fieldValue string, replace func(map[string]any) (map[string]any, error)) ([]byte, error) {
d := yaml.NewDecoder(bytes.NewReader(veneerFile))
docs := make([]map[string]any, 0, 10)
for {
var doc map[string]any
Expand All @@ -200,7 +219,7 @@ func updateVeneerFile(veneerFile, schema, name string, replace func(map[string]a
continue
}

if n, ok := doc["name"]; !ok || n != name {
if f, ok := doc[fieldName]; !ok || f != fieldValue {
docs = append(docs, doc)
continue
}
Expand Down Expand Up @@ -228,3 +247,30 @@ func updateVeneerFile(veneerFile, schema, name string, replace func(map[string]a

return b.Bytes(), nil
}

func addOLMBundle(veneeerContents []byte, newVersion string) ([]byte, error) {
ret := veneeerContents

var (
foundImage bool
imageName = fmt.Sprintf(imageNameTemplate, newVersion)
)

_, err := updateVeneerFile(veneeerContents, "olm.bundle", "image", imageName, func(m map[string]any) (map[string]any, error) {
foundImage = true
return m, nil
})
if err != nil {
return nil, errors.Join(err, errors.New("could not find olm.bundle"))
}

if !foundImage {
ret = append(ret, []byte(fmt.Sprintf(
"image: %s\n"+
"schema: olm.bundle\n"+
"---\n", imageName,
))...)
}

return ret, nil
}
15 changes: 15 additions & 0 deletions tools/veneer-update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ schema: olm.channel
image: docker.io/perconalab/everest-operator-bundle:0.0.0
schema: olm.bundle
---
image: docker.io/perconalab/everest-operator-bundle:0.9.2
schema: olm.bundle
---
`

var patchStableRCExpected = `defaultChannel: stable-v0
Expand Down Expand Up @@ -177,6 +180,9 @@ schema: olm.channel
image: docker.io/perconalab/everest-operator-bundle:0.0.0
schema: olm.bundle
---
image: docker.io/perconalab/everest-operator-bundle:0.9.2-rc1
schema: olm.bundle
---
`

var patchRCExpected = `defaultChannel: stable-v0
Expand Down Expand Up @@ -221,6 +227,9 @@ schema: olm.channel
image: docker.io/perconalab/everest-operator-bundle:0.0.0
schema: olm.bundle
---
image: docker.io/perconalab/everest-operator-bundle:0.9.1-rc2
schema: olm.bundle
---
`

var minorStableExpected = `defaultChannel: stable-v0
Expand Down Expand Up @@ -268,6 +277,9 @@ schema: olm.channel
image: docker.io/perconalab/everest-operator-bundle:0.0.0
schema: olm.bundle
---
image: docker.io/perconalab/everest-operator-bundle:0.10.0
schema: olm.bundle
---
`

var minorStableRCExpected = `defaultChannel: stable-v0
Expand Down Expand Up @@ -315,4 +327,7 @@ schema: olm.channel
image: docker.io/perconalab/everest-operator-bundle:0.0.0
schema: olm.bundle
---
image: docker.io/perconalab/everest-operator-bundle:0.10.0-rc1
schema: olm.bundle
---
`

0 comments on commit 106dca2

Please sign in to comment.