Skip to content

Commit 9094fed

Browse files
joelanfordPer Goncalves da Silva
authored and
Per Goncalves da Silva
committed
remove catalogmetadata types, introduce to resolver interface (operator-framework#1033)
Signed-off-by: Joe Lanford <[email protected]>
1 parent 07b24bb commit 9094fed

31 files changed

+1852
-3205
lines changed

Makefile

+9-21
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ help-extended: #HELP Display extended help.
9090
lint: $(GOLANGCI_LINT) #HELP Run golangci linter.
9191
$(GOLANGCI_LINT) run $(GOLANGCI_LINT_ARGS)
9292

93-
.PHONY: lint-fix
94-
lint-fix: $(GOLANGCI_LINT) #HELP Run golangci linter.
95-
$(GOLANGCI_LINT) run $(GOLANGCI_LINT_ARGS) --fix
96-
9793
.PHONY: tidy
9894
tidy: #HELP Update dependencies.
9995
$(Q)go mod tidy
@@ -110,13 +106,9 @@ generate: $(CONTROLLER_GEN) #EXHELP Generate code containing DeepCopy, DeepCopyI
110106
verify: tidy fmt vet generate manifests #HELP Verify all generated code is up-to-date.
111107
git diff --exit-code
112108

113-
.PHONY: fix-lint
114-
fix-lint: $(GOLANGCI_LINT) #EXHELP Fix lint issues
115-
$(GOLANGCI_LINT) run --fix $(GOLANGCI_LINT_ARGS)
116-
117109
.PHONY: fmt
118-
fmt: #EXHELP Formats code
119-
go fmt ./...
110+
fmt: $(GOLANGCI_LINT) #EXHELP Formats code
111+
go fmt ./... && $(GOLANGCI_LINT) run --fix $(GOLANGCI_LINT_ARGS)
120112

121113
.PHONY: vet
122114
vet: #EXHELP Run go vet against code.
@@ -133,11 +125,8 @@ E2E_REGISTRY_NAME := docker-registry
133125
E2E_REGISTRY_NAMESPACE := operator-controller-e2e
134126

135127
export REG_PKG_NAME := registry-operator
136-
export LOCAL_REGISTRY_HOST := $(E2E_REGISTRY_NAME).$(E2E_REGISTRY_NAMESPACE).svc:5000
137-
export CLUSTER_REGISTRY_HOST := localhost:30000
138-
export E2E_TEST_CATALOG_V1 := e2e/test-catalog:v1
139-
export E2E_TEST_CATALOG_V2 := e2e/test-catalog:v2
140-
export CATALOG_IMG := $(LOCAL_REGISTRY_HOST)/$(E2E_TEST_CATALOG_V1)
128+
export REGISTRY_ROOT := $(E2E_REGISTRY_NAME).$(E2E_REGISTRY_NAMESPACE).svc:5000
129+
export CATALOG_IMG := $(REGISTRY_ROOT)/e2e/test-catalog:e2e
141130
.PHONY: test-ext-dev-e2e
142131
test-ext-dev-e2e: $(OPERATOR_SDK) $(KUSTOMIZE) $(KIND) #HELP Run extension create, upgrade and delete tests.
143132
test/extension-developer-e2e/setup.sh $(OPERATOR_SDK) $(CONTAINER_RUNTIME) $(KUSTOMIZE) $(KIND) $(KIND_CLUSTER_NAME) $(E2E_REGISTRY_NAMESPACE)
@@ -155,8 +144,7 @@ image-registry: ## Setup in-cluster image registry
155144
./test/tools/image-registry.sh $(E2E_REGISTRY_NAMESPACE) $(E2E_REGISTRY_NAME)
156145

157146
build-push-e2e-catalog: ## Build the testdata catalog used for e2e tests and push it to the image registry
158-
./test/tools/build-push-e2e-catalog.sh $(E2E_REGISTRY_NAMESPACE) $(LOCAL_REGISTRY_HOST)/$(E2E_TEST_CATALOG_V1)
159-
./test/tools/build-push-e2e-catalog.sh $(E2E_REGISTRY_NAMESPACE) $(LOCAL_REGISTRY_HOST)/$(E2E_TEST_CATALOG_V2)
147+
./test/tools/build-push-e2e-catalog.sh $(E2E_REGISTRY_NAMESPACE) $(CATALOG_IMG)
160148

161149
# When running the e2e suite, you can set the ARTIFACT_PATH variable to the absolute path
162150
# of the directory for the operator-controller e2e tests to store the artifacts, which
@@ -217,10 +205,10 @@ kind-clean: $(KIND) #EXHELP Delete the kind cluster.
217205
$(KIND) delete cluster --name $(KIND_CLUSTER_NAME)
218206

219207
registry-load-bundles: ## Load selected e2e testdata container images created in kind-load-bundles into registry
220-
testdata/bundles/registry-v1/build-push-e2e-bundle.sh ${E2E_REGISTRY_NAMESPACE} $(LOCAL_REGISTRY_HOST)/bundles/registry-v1/prometheus-operator:v1.0.0 prometheus-operator.v1.0.0 prometheus-operator.v1.0.0
221-
testdata/bundles/registry-v1/build-push-e2e-bundle.sh ${E2E_REGISTRY_NAMESPACE} $(LOCAL_REGISTRY_HOST)/bundles/registry-v1/prometheus-operator:v1.0.1 prometheus-operator.v1.0.1 prometheus-operator.v1.0.0
222-
testdata/bundles/registry-v1/build-push-e2e-bundle.sh ${E2E_REGISTRY_NAMESPACE} $(LOCAL_REGISTRY_HOST)/bundles/registry-v1/prometheus-operator:v1.2.0 prometheus-operator.v1.2.0 prometheus-operator.v1.0.0
223-
testdata/bundles/registry-v1/build-push-e2e-bundle.sh ${E2E_REGISTRY_NAMESPACE} $(LOCAL_REGISTRY_HOST)/bundles/registry-v1/prometheus-operator:v2.0.0 prometheus-operator.v2.0.0 prometheus-operator.v1.0.0
208+
testdata/bundles/registry-v1/build-push-e2e-bundle.sh ${E2E_REGISTRY_NAMESPACE} $(REGISTRY_ROOT)/bundles/registry-v1/prometheus-operator:v1.0.0 prometheus-operator.v1.0.0 prometheus-operator.v1.0.0
209+
testdata/bundles/registry-v1/build-push-e2e-bundle.sh ${E2E_REGISTRY_NAMESPACE} $(REGISTRY_ROOT)/bundles/registry-v1/prometheus-operator:v1.0.1 prometheus-operator.v1.0.1 prometheus-operator.v1.0.0
210+
testdata/bundles/registry-v1/build-push-e2e-bundle.sh ${E2E_REGISTRY_NAMESPACE} $(REGISTRY_ROOT)/bundles/registry-v1/prometheus-operator:v1.2.0 prometheus-operator.v1.2.0 prometheus-operator.v1.0.0
211+
testdata/bundles/registry-v1/build-push-e2e-bundle.sh ${E2E_REGISTRY_NAMESPACE} $(REGISTRY_ROOT)/bundles/registry-v1/prometheus-operator:v2.0.0 prometheus-operator.v2.0.0 prometheus-operator.v1.0.0
224212

225213
#SECTION Build
226214

cmd/manager/main.go

+36-22
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ import (
4848
"github.com/operator-framework/operator-controller/internal/features"
4949
"github.com/operator-framework/operator-controller/internal/httputil"
5050
"github.com/operator-framework/operator-controller/internal/labels"
51-
crdupgradesafety "github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
51+
"github.com/operator-framework/operator-controller/internal/resolve"
52+
"github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
5253
"github.com/operator-framework/operator-controller/internal/rukpak/source"
5354
"github.com/operator-framework/operator-controller/internal/scheme"
5455
"github.com/operator-framework/operator-controller/internal/version"
@@ -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},

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+
}

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)