Skip to content

Commit 72f672e

Browse files
committed
remove catalogmetadata types, introduce to resolver interface
Signed-off-by: Joe Lanford <[email protected]>
1 parent bfc65ee commit 72f672e

30 files changed

+1838
-3177
lines changed

cmd/manager/main.go

+29-15
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ import (
4646
"github.com/operator-framework/operator-controller/internal/controllers"
4747
"github.com/operator-framework/operator-controller/internal/httputil"
4848
"github.com/operator-framework/operator-controller/internal/labels"
49-
crdupgradesafety "github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
49+
"github.com/operator-framework/operator-controller/internal/resolve"
50+
"github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
5051
"github.com/operator-framework/operator-controller/internal/rukpak/source"
5152
"github.com/operator-framework/operator-controller/internal/version"
5253
"github.com/operator-framework/operator-controller/pkg/features"
@@ -152,19 +153,6 @@ func main() {
152153
os.Exit(1)
153154
}
154155

155-
httpClient, err := httputil.BuildHTTPClient(caCertDir)
156-
if err != nil {
157-
setupLog.Error(err, "unable to create catalogd http client")
158-
}
159-
160-
cl := mgr.GetClient()
161-
catalogsCachePath := filepath.Join(cachePath, "catalogs")
162-
if err := os.MkdirAll(catalogsCachePath, 0700); err != nil {
163-
setupLog.Error(err, "unable to create catalogs cache directory")
164-
os.Exit(1)
165-
}
166-
catalogClient := catalogclient.New(cl, cache.NewFilesystemCache(catalogsCachePath, httpClient))
167-
168156
installNamespaceMapper := helmclient.ObjectToStringMapper(func(obj client.Object) (string, error) {
169157
ext := obj.(*ocv1alpha1.ClusterExtension)
170158
return ext.Spec.InstallNamespace, nil
@@ -213,9 +201,35 @@ func main() {
213201
crdupgradesafety.NewPreflight(aeClient.CustomResourceDefinitions()),
214202
}
215203

204+
cl := mgr.GetClient()
205+
httpClient, err := httputil.BuildHTTPClient(caCertDir)
206+
if err != nil {
207+
setupLog.Error(err, "unable to create catalogd http client")
208+
}
209+
210+
catalogsCachePath := filepath.Join(cachePath, "catalogs")
211+
if err := os.MkdirAll(catalogsCachePath, 0700); err != nil {
212+
setupLog.Error(err, "unable to create catalogs cache directory")
213+
os.Exit(1)
214+
}
215+
catalogClient := catalogclient.New(cache.NewFilesystemCache(catalogsCachePath, httpClient))
216+
217+
resolver := &resolve.CatalogResolver{
218+
WalkCatalogsFunc: resolve.CatalogWalker(
219+
func(ctx context.Context, option ...client.ListOption) ([]catalogd.ClusterCatalog, error) {
220+
var catalogs catalogd.ClusterCatalogList
221+
if err := cl.List(ctx, &catalogs, option...); err != nil {
222+
return nil, err
223+
}
224+
return catalogs.Items, nil
225+
},
226+
catalogClient.GetPackage,
227+
),
228+
}
229+
216230
if err = (&controllers.ClusterExtensionReconciler{
217231
Client: cl,
218-
BundleProvider: catalogClient,
232+
Resolver: resolver,
219233
ActionClientGetter: acg,
220234
Unpacker: unpacker,
221235
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},

internal/bundleutil/bundle.go

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

internal/catalogmetadata/cache/cache_test.go

+12-12
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)