Skip to content

Commit afdfe7b

Browse files
committed
wip
Signed-off-by: Joe Lanford <[email protected]>
1 parent 8947c3d commit afdfe7b

23 files changed

+1103
-2147
lines changed

cmd/manager/main.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func main() {
161161
}
162162

163163
cl := mgr.GetClient()
164-
catalogClient := catalogclient.New(cl, cache.NewFilesystemCache(cachePath, httpClient))
164+
catalogClient := catalogclient.New(cache.NewFilesystemCache(cachePath, httpClient))
165165

166166
installNamespaceMapper := helmclient.ObjectToStringMapper(func(obj client.Object) (string, error) {
167167
ext := obj.(*ocv1alpha1.ClusterExtension)
@@ -230,16 +230,16 @@ func main() {
230230
}
231231

232232
if err = (&controllers.ClusterExtensionReconciler{
233-
Client: cl,
234-
BundleProvider: catalogClient,
235-
ActionClientGetter: acg,
236-
Unpacker: unpacker,
237-
Storage: localStorage,
238-
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
239-
Handler: registryv1handler.HandlerFunc(registry.HandleBundleDeployment),
240-
Finalizers: clusterExtensionFinalizers,
241-
CaCertDir: caCertDir,
242-
Preflights: preflights,
233+
Client: cl,
234+
CatalogPackageProvider: catalogClient,
235+
ActionClientGetter: acg,
236+
Unpacker: unpacker,
237+
Storage: localStorage,
238+
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
239+
Handler: registryv1handler.HandlerFunc(registry.HandleBundleDeployment),
240+
Finalizers: clusterExtensionFinalizers,
241+
CaCertDir: caCertDir,
242+
Preflights: preflights,
243243
}).SetupWithManager(mgr); err != nil {
244244
setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension")
245245
os.Exit(1)

internal/bundleutil/bundle.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package bundleutil
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"sync"
7+
8+
bsemver "github.com/blang/semver/v4"
9+
10+
"github.com/operator-framework/operator-registry/alpha/declcfg"
11+
"github.com/operator-framework/operator-registry/alpha/property"
12+
13+
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
14+
)
15+
16+
var VersionGetter = &versionGetter{cache: make(map[*declcfg.Bundle]*bsemver.Version)}
17+
18+
type versionGetter struct {
19+
mu sync.Mutex
20+
cache map[*declcfg.Bundle]*bsemver.Version
21+
}
22+
23+
func (v *versionGetter) GetVersion(b *declcfg.Bundle) (*bsemver.Version, error) {
24+
v.mu.Lock()
25+
defer v.mu.Unlock()
26+
if vers, ok := v.cache[b]; ok {
27+
return &(*vers), nil
28+
}
29+
for _, p := range b.Properties {
30+
if p.Type == property.TypePackage {
31+
var pkg property.Package
32+
if err := json.Unmarshal(p.Value, &pkg); err != nil {
33+
return nil, fmt.Errorf("error unmarshalling package property: %w", err)
34+
}
35+
vers, err := bsemver.Parse(pkg.Version)
36+
if err != nil {
37+
return nil, err
38+
}
39+
v.cache[b] = &vers
40+
return &(*(&vers)), nil
41+
}
42+
}
43+
return nil, fmt.Errorf("no package property found in bundle %q", b.Name)
44+
}
45+
46+
// MetadataFor returns a BundleMetadata for the given bundle. If the provided bundle is nil,
47+
// this function panics. It is up to the caller to ensure that the bundle is non-nil.
48+
func MetadataFor(bundleName string, bundleVersion *bsemver.Version) *ocv1alpha1.BundleMetadata {
49+
return &ocv1alpha1.BundleMetadata{
50+
Name: bundleName,
51+
Version: bundleVersion.String(),
52+
}
53+
}

internal/catalogmetadata/cache/cache_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestFilesystemCache(t *testing.T) {
6868
catalog *catalogd.ClusterCatalog
6969
contents fstest.MapFS
7070
wantErr bool
71-
tripper *MockTripper
71+
tripper *mockTripper
7272
testCaching bool
7373
shouldHitCache bool
7474
}
@@ -89,7 +89,7 @@ func TestFilesystemCache(t *testing.T) {
8989
},
9090
},
9191
contents: defaultFS,
92-
tripper: &MockTripper{},
92+
tripper: &mockTripper{},
9393
},
9494
{
9595
name: "valid cached fetch",
@@ -107,7 +107,7 @@ func TestFilesystemCache(t *testing.T) {
107107
},
108108
},
109109
contents: defaultFS,
110-
tripper: &MockTripper{},
110+
tripper: &mockTripper{},
111111
testCaching: true,
112112
shouldHitCache: true,
113113
},
@@ -127,7 +127,7 @@ func TestFilesystemCache(t *testing.T) {
127127
},
128128
},
129129
contents: defaultFS,
130-
tripper: &MockTripper{},
130+
tripper: &mockTripper{},
131131
testCaching: true,
132132
shouldHitCache: false,
133133
},
@@ -147,7 +147,7 @@ func TestFilesystemCache(t *testing.T) {
147147
},
148148
},
149149
contents: defaultFS,
150-
tripper: &MockTripper{shouldError: true},
150+
tripper: &mockTripper{shouldError: true},
151151
wantErr: true,
152152
},
153153
{
@@ -166,14 +166,14 @@ func TestFilesystemCache(t *testing.T) {
166166
},
167167
},
168168
contents: defaultFS,
169-
tripper: &MockTripper{serverError: true},
169+
tripper: &mockTripper{serverError: true},
170170
wantErr: true,
171171
},
172172
{
173173
name: "nil catalog",
174174
catalog: nil,
175175
contents: defaultFS,
176-
tripper: &MockTripper{serverError: true},
176+
tripper: &mockTripper{serverError: true},
177177
wantErr: true,
178178
},
179179
{
@@ -187,7 +187,7 @@ func TestFilesystemCache(t *testing.T) {
187187
},
188188
},
189189
contents: defaultFS,
190-
tripper: &MockTripper{serverError: true},
190+
tripper: &mockTripper{serverError: true},
191191
wantErr: true,
192192
},
193193
{
@@ -203,7 +203,7 @@ func TestFilesystemCache(t *testing.T) {
203203
},
204204
},
205205
contents: defaultFS,
206-
tripper: &MockTripper{serverError: true},
206+
tripper: &mockTripper{serverError: true},
207207
wantErr: true,
208208
},
209209
} {
@@ -242,15 +242,15 @@ func TestFilesystemCache(t *testing.T) {
242242
}
243243
}
244244

245-
var _ http.RoundTripper = &MockTripper{}
245+
var _ http.RoundTripper = &mockTripper{}
246246

247-
type MockTripper struct {
247+
type mockTripper struct {
248248
content fstest.MapFS
249249
shouldError bool
250250
serverError bool
251251
}
252252

253-
func (mt *MockTripper) RoundTrip(_ *http.Request) (*http.Response, error) {
253+
func (mt *mockTripper) RoundTrip(_ *http.Request) (*http.Response, error) {
254254
if mt.shouldError {
255255
return nil, errors.New("mock tripper error")
256256
}

0 commit comments

Comments
 (0)