Skip to content

Commit

Permalink
Fix bug: run bundle-upgrade with long image names
Browse files Browse the repository at this point in the history
Signed-off-by: Nahshon Unna-Tsameret <[email protected]>
  • Loading branch information
nunnatsa committed Jun 25, 2023
1 parent 50c6ac0 commit 35a4b8d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions changelog/fragments/01-hash-cache-directory-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# entries is a list of entries to include in
# release notes and/or the migration guide
entries:
- description: In `run bundle-upgrade`, hash the cache directory name to avoid error of too long file name.
kind: "bugfix"
breaking: false
12 changes: 11 additions & 1 deletion internal/olm/fbcutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package fbcutil
import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -134,7 +135,8 @@ func NullLogger() *log.Entry {
// RenderRefs will invoke Operator Registry APIs and return a declarative config object representation
// of the references that are passed in as a string array.
func RenderRefs(ctx context.Context, refs []string, skipTLSVerify bool, useHTTP bool) (*declarativeconfig.DeclarativeConfig, error) {
cacheDir := strings.ReplaceAll(strings.Join(refs, "_"), "/", "-")
cacheDir := dirNameFromRefs(refs)

if cacheDir == "" {
cacheDir = DefaultCacheDir
}
Expand Down Expand Up @@ -169,6 +171,14 @@ func RenderRefs(ctx context.Context, refs []string, skipTLSVerify bool, useHTTP
return declcfg, nil
}

func dirNameFromRefs(refs []string) string {
dirNameBytes := []byte(strings.ReplaceAll(strings.Join(refs, "_"), "/", "-"))
hash := sha256.New()
hash.Write(dirNameBytes)
hashBytes := hash.Sum(nil)
return fmt.Sprintf("%x", hashBytes)
}

// IsFBC will determine if an index image uses the File-Based Catalog or SQLite index image format.
// The default index image will adopt the File-Based Catalog format.
func IsFBC(ctx context.Context, indexImage string) (bool, error) {
Expand Down
38 changes: 38 additions & 0 deletions internal/olm/fbcutil/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fbcutil

import (
"crypto/sha256"
"fmt"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("test fbutil", func() {

emptyListHash := fmt.Sprintf("%x", sha256.New().Sum(nil))

Context("test dirNameFromRefs", func() {
DescribeTable("should name the directory with sha256 of the concatenation of the bundle image names", func(refs []string, expectedHash string) {
dirName := dirNameFromRefs(refs)

Expect(dirName).Should(HaveLen(sha256.Size * 2)) // in hex representation, each byte is two hex digits
Expect(dirName).Should(Equal(expectedHash))
},
Entry("long image names", []string{
"registry.build03.ci.openshift.org/ci/op/dl9pitlh/pipeline@sha256:123d009f7fb9d5b760b3eac831e0f50413d36199816d651b569367642c138ccd",
"registry.build03.ci.openshift.org/ci/op/dl9pitlh/pipeline@sha256:5498f1b841946c8da823fb11c08969efb36119f7867701f2cedac4a2e8f7cefc",
}, "6fe356da2fd4646b823936045b81d6312e9d7597af3b49d12c7f83d5089c2c11"),
Entry("multiple refs", []string{"a/b/c", "d/e/f", "g/h/i/j", "k/l/m/n/o"}, "df22ac3ac9e59ed300f70b9c2fdd7128be064652839a813948ee9fd1a2f36581"),
Entry("single ref", []string{"a/b/c"}, "cbd2be7b96f770a0326948ebd158cf539fab0627e8adbddc97f7a65c6a8ae59a"),
Entry("no refs", []string{}, emptyListHash),
Entry("no refs (nil)", nil, emptyListHash),
)
})
})

func TestRegistry(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "fbutil Suite")
}

0 comments on commit 35a4b8d

Please sign in to comment.