Skip to content

Commit 6cd022e

Browse files
authored
remove catalogmetadata types, introduce to resolver interface (#1033)
Signed-off-by: Joe Lanford <[email protected]>
1 parent ca4f4e3 commit 6cd022e

30 files changed

+1843
-3184
lines changed

Diff for: cmd/manager/main.go

+36-22
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ import (
4747
"github.com/operator-framework/operator-controller/internal/controllers"
4848
"github.com/operator-framework/operator-controller/internal/httputil"
4949
"github.com/operator-framework/operator-controller/internal/labels"
50-
crdupgradesafety "github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
50+
"github.com/operator-framework/operator-controller/internal/resolve"
51+
"github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
5152
"github.com/operator-framework/operator-controller/internal/rukpak/source"
5253
"github.com/operator-framework/operator-controller/internal/version"
5354
"github.com/operator-framework/operator-controller/pkg/features"
@@ -153,25 +154,6 @@ func main() {
153154
os.Exit(1)
154155
}
155156

156-
certPool, err := httputil.NewCertPool(caCertDir)
157-
if err != nil {
158-
setupLog.Error(err, "unable to create CA certificate pool")
159-
os.Exit(1)
160-
}
161-
162-
httpClient, err := httputil.BuildHTTPClient(certPool)
163-
if err != nil {
164-
setupLog.Error(err, "unable to create catalogd http client")
165-
}
166-
167-
cl := mgr.GetClient()
168-
catalogsCachePath := filepath.Join(cachePath, "catalogs")
169-
if err := os.MkdirAll(catalogsCachePath, 0700); err != nil {
170-
setupLog.Error(err, "unable to create catalogs cache directory")
171-
os.Exit(1)
172-
}
173-
catalogClient := catalogclient.New(cl, cache.NewFilesystemCache(catalogsCachePath, httpClient))
174-
175157
installNamespaceMapper := helmclient.ObjectToStringMapper(func(obj client.Object) (string, error) {
176158
ext := obj.(*ocv1alpha1.ClusterExtension)
177159
return ext.Spec.InstallNamespace, nil
@@ -194,14 +176,19 @@ func main() {
194176
os.Exit(1)
195177
}
196178

197-
clusterExtensionFinalizers := crfinalizer.NewFinalizers()
179+
certPool, err := httputil.NewCertPool(caCertDir)
180+
if err != nil {
181+
setupLog.Error(err, "unable to create CA certificate pool")
182+
os.Exit(1)
183+
}
198184
unpacker := &source.ImageRegistry{
199185
BaseCachePath: filepath.Join(cachePath, "unpack"),
200186
// TODO: This needs to be derived per extension via ext.Spec.InstallNamespace
201187
AuthNamespace: systemNamespace,
202188
CaCertPool: certPool,
203189
}
204190

191+
clusterExtensionFinalizers := crfinalizer.NewFinalizers()
205192
domain := ocv1alpha1.GroupVersion.Group
206193
cleanupUnpackCacheKey := fmt.Sprintf("%s/cleanup-unpack-cache", domain)
207194
if err := clusterExtensionFinalizers.Register(cleanupUnpackCacheKey, finalizerFunc(func(ctx context.Context, obj client.Object) (crfinalizer.Result, error) {
@@ -222,9 +209,36 @@ func main() {
222209
crdupgradesafety.NewPreflight(aeClient.CustomResourceDefinitions()),
223210
}
224211

212+
cl := mgr.GetClient()
213+
httpClient, err := httputil.BuildHTTPClient(certPool)
214+
if err != nil {
215+
setupLog.Error(err, "unable to create catalogd http client")
216+
os.Exit(1)
217+
}
218+
219+
catalogsCachePath := filepath.Join(cachePath, "catalogs")
220+
if err := os.MkdirAll(catalogsCachePath, 0700); err != nil {
221+
setupLog.Error(err, "unable to create catalogs cache directory")
222+
os.Exit(1)
223+
}
224+
catalogClient := catalogclient.New(cache.NewFilesystemCache(catalogsCachePath, httpClient))
225+
226+
resolver := &resolve.CatalogResolver{
227+
WalkCatalogsFunc: resolve.CatalogWalker(
228+
func(ctx context.Context, option ...client.ListOption) ([]catalogd.ClusterCatalog, error) {
229+
var catalogs catalogd.ClusterCatalogList
230+
if err := cl.List(ctx, &catalogs, option...); err != nil {
231+
return nil, err
232+
}
233+
return catalogs.Items, nil
234+
},
235+
catalogClient.GetPackage,
236+
),
237+
}
238+
225239
if err = (&controllers.ClusterExtensionReconciler{
226240
Client: cl,
227-
BundleProvider: catalogClient,
241+
Resolver: resolver,
228242
ActionClientGetter: acg,
229243
Unpacker: unpacker,
230244
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},

Diff for: internal/bundleutil/bundle.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 name and version.
33+
func MetadataFor(bundleName string, bundleVersion bsemver.Version) *ocv1alpha1.BundleMetadata {
34+
return &ocv1alpha1.BundleMetadata{
35+
Name: bundleName,
36+
Version: bundleVersion.String(),
37+
}
38+
}

Diff for: 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)