Skip to content

Commit a99d7ed

Browse files
authored
Fix bug: run bundle-upgrade with long image names (#6477)
Signed-off-by: Nahshon Unna-Tsameret <[email protected]>
1 parent 50c6ac0 commit a99d7ed

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# entries is a list of entries to include in
2+
# release notes and/or the migration guide
3+
entries:
4+
- description: In `run bundle-upgrade`, hash the cache directory name to avoid error of too long file name.
5+
kind: "bugfix"
6+
breaking: false

internal/olm/fbcutil/util.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package fbcutil
1717
import (
1818
"bytes"
1919
"context"
20+
"crypto/sha256"
2021
"errors"
2122
"fmt"
2223
"io"
@@ -134,7 +135,8 @@ func NullLogger() *log.Entry {
134135
// RenderRefs will invoke Operator Registry APIs and return a declarative config object representation
135136
// of the references that are passed in as a string array.
136137
func RenderRefs(ctx context.Context, refs []string, skipTLSVerify bool, useHTTP bool) (*declarativeconfig.DeclarativeConfig, error) {
137-
cacheDir := strings.ReplaceAll(strings.Join(refs, "_"), "/", "-")
138+
cacheDir := dirNameFromRefs(refs)
139+
138140
if cacheDir == "" {
139141
cacheDir = DefaultCacheDir
140142
}
@@ -169,6 +171,14 @@ func RenderRefs(ctx context.Context, refs []string, skipTLSVerify bool, useHTTP
169171
return declcfg, nil
170172
}
171173

174+
func dirNameFromRefs(refs []string) string {
175+
dirNameBytes := []byte(strings.ReplaceAll(strings.Join(refs, "_"), "/", "-"))
176+
hash := sha256.New()
177+
hash.Write(dirNameBytes)
178+
hashBytes := hash.Sum(nil)
179+
return fmt.Sprintf("%x", hashBytes)
180+
}
181+
172182
// IsFBC will determine if an index image uses the File-Based Catalog or SQLite index image format.
173183
// The default index image will adopt the File-Based Catalog format.
174184
func IsFBC(ctx context.Context, indexImage string) (bool, error) {

internal/olm/fbcutil/util_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2023 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package fbcutil
16+
17+
import (
18+
"crypto/sha256"
19+
"fmt"
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
var _ = Describe("test fbutil", func() {
27+
28+
emptyListHash := fmt.Sprintf("%x", sha256.New().Sum(nil))
29+
30+
Context("test dirNameFromRefs", func() {
31+
DescribeTable("should name the directory with sha256 of the concatenation of the bundle image names", func(refs []string, expectedHash string) {
32+
dirName := dirNameFromRefs(refs)
33+
34+
Expect(dirName).Should(HaveLen(sha256.Size * 2)) // in hex representation, each byte is two hex digits
35+
Expect(dirName).Should(Equal(expectedHash))
36+
},
37+
Entry("long image names", []string{
38+
"BtU5KRr8IWafnGTvckShoj3xBb5duLZp/XKHZRpOqdVxhHQkCL0Dy0lSRw0a0M/y158JRQKk1S@6KAauQHujQ30my9sivVYGZahR7R7UUSoUBUmnuFdqGHiUTT0aV5Di2",
39+
"lKM63iKupQcUPKd6AAsmRRABbGYNmwFTmQEX6fpswndQdb/niJPLRG8WhzaH84Q3kfZC/7hc3nK7Oeq@L5KxmVqbAz6jXlv1yKna2cH4zbZ3be0pcYNHyCSVUG/ZVqRAo",
40+
}, "cc048355dce06491fd090ce0c3ce5a48db3528250ad13a4fbf4090a3de8c325a"),
41+
Entry("multiple refs", []string{"a/b/c", "d/e/f", "g/h/i/j", "k/l/m/n/o"}, "df22ac3ac9e59ed300f70b9c2fdd7128be064652839a813948ee9fd1a2f36581"),
42+
Entry("single ref", []string{"a/b/c"}, "cbd2be7b96f770a0326948ebd158cf539fab0627e8adbddc97f7a65c6a8ae59a"),
43+
Entry("no refs", []string{}, emptyListHash),
44+
Entry("no refs (nil)", nil, emptyListHash),
45+
)
46+
})
47+
})
48+
49+
func TestRegistry(t *testing.T) {
50+
RegisterFailHandler(Fail)
51+
RunSpecs(t, "fbutil Suite")
52+
}

0 commit comments

Comments
 (0)