Skip to content

Commit 1d61874

Browse files
committed
fixup PR comments
Signed-off-by: Joe Lanford <[email protected]>
1 parent 19d10a0 commit 1d61874

File tree

12 files changed

+161
-177
lines changed

12 files changed

+161
-177
lines changed

catalogd/internal/controllers/core/clustercatalog_controller.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,14 @@ func (r *ClusterCatalogReconciler) reconcile(ctx context.Context, catalog *catal
234234
}
235235

236236
if catalog.Spec.Source.Type != catalogdv1.SourceTypeImage {
237-
panic(fmt.Sprintf("programmer error: source type %q is unable to handle specified catalog source type %q", catalogdv1.SourceTypeImage, catalog.Spec.Source.Type))
237+
err := reconcile.TerminalError(fmt.Errorf("unknown source type %q", catalog.Spec.Source.Type))
238+
updateStatusProgressing(&catalog.Status, catalog.GetGeneration(), err)
239+
return ctrl.Result{}, err
238240
}
239241
if catalog.Spec.Source.Image == nil {
240-
unpackErr := reconcile.TerminalError(fmt.Errorf("error parsing catalog, catalog %s has a nil image source", catalog.Name))
241-
updateStatusProgressing(&catalog.Status, catalog.GetGeneration(), unpackErr)
242-
return ctrl.Result{}, unpackErr
242+
err := reconcile.TerminalError(fmt.Errorf("error parsing catalog, catalog %s has a nil image source", catalog.Name))
243+
updateStatusProgressing(&catalog.Status, catalog.GetGeneration(), err)
244+
return ctrl.Result{}, err
243245
}
244246

245247
fsys, canonicalRef, unpackTime, err := r.ImagePuller.Pull(ctx, catalog.Name, catalog.Spec.Source.Image.Ref, r.ImageCache)
@@ -271,7 +273,7 @@ func (r *ClusterCatalogReconciler) reconcile(ctx context.Context, catalog *catal
271273
observedGeneration: catalog.GetGeneration(),
272274
}
273275
r.storedCatalogsMu.Unlock()
274-
return nextPollResult(time.Now(), catalog), nil
276+
return nextPollResult(lastSuccessfulPoll, catalog), nil
275277
}
276278

277279
func (r *ClusterCatalogReconciler) getCurrentState(catalog *catalogdv1.ClusterCatalog) (*catalogdv1.ClusterCatalogStatus, storedCatalogData, bool) {
@@ -450,7 +452,7 @@ func (r *ClusterCatalogReconciler) deleteCatalogCache(ctx context.Context, catal
450452
return err
451453
}
452454
updateStatusNotServing(&catalog.Status, catalog.GetGeneration())
453-
if err := r.ImageCache.DeleteID(ctx, catalog.Name); err != nil {
455+
if err := r.ImageCache.Delete(ctx, catalog.Name); err != nil {
454456
updateStatusProgressing(&catalog.Status, catalog.GetGeneration(), err)
455457
return err
456458
}

catalogd/internal/controllers/core/clustercatalog_controller_test.go

Lines changed: 34 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"io/fs"
8-
"iter"
98
"net/http"
109
"testing"
1110
"testing/fstest"
@@ -14,7 +13,6 @@ import (
1413
"github.com/containers/image/v5/docker/reference"
1514
"github.com/google/go-cmp/cmp"
1615
"github.com/google/go-cmp/cmp/cmpopts"
17-
ocispecv1 "github.com/opencontainers/image-spec/specs-go/v1"
1816
"github.com/stretchr/testify/assert"
1917
"github.com/stretchr/testify/require"
2018
"k8s.io/apimachinery/pkg/api/meta"
@@ -28,50 +26,6 @@ import (
2826
imageutil "github.com/operator-framework/operator-controller/internal/util/image"
2927
)
3028

31-
var _ imageutil.Puller = &MockPuller{}
32-
33-
// MockPuller is a utility for mocking out an imageutil.Puller interface
34-
type MockPuller struct {
35-
// result is the result that should be returned when MockPuller.Unpack is called
36-
fsys fs.FS
37-
ref reference.Canonical
38-
modTime time.Time
39-
40-
// error is the error to be returned when MockPuller.Unpack is called
41-
pullError error
42-
}
43-
44-
func (ms *MockPuller) Pull(_ context.Context, _, _ string, _ imageutil.Cache) (fs.FS, reference.Canonical, time.Time, error) {
45-
if ms.pullError != nil {
46-
return nil, nil, time.Time{}, ms.pullError
47-
}
48-
49-
return ms.fsys, ms.ref, ms.modTime, nil
50-
}
51-
52-
var _ imageutil.Cache = (*MockCache)(nil)
53-
54-
type MockCache struct {
55-
// cleanupError is the error to be returned when MockPuller.Cleanup is called
56-
cleanupError error
57-
}
58-
59-
func (ms *MockCache) Fetch(_ context.Context, _ string, _ reference.Canonical) (fs.FS, time.Time, error) {
60-
panic("not implemented")
61-
}
62-
63-
func (ms *MockCache) Store(_ context.Context, _ string, _ reference.Named, _ reference.Canonical, _ ocispecv1.Image, _ iter.Seq[imageutil.LayerData]) (fs.FS, time.Time, error) {
64-
panic("not implemented")
65-
}
66-
67-
func (ms *MockCache) GarbageCollect(_ context.Context, _ string, _ reference.Canonical) error {
68-
panic("not implemented")
69-
}
70-
71-
func (ms *MockCache) DeleteID(_ context.Context, _ string) error {
72-
return ms.cleanupError
73-
}
74-
7529
var _ storage.Instance = &MockStore{}
7630

7731
type MockStore struct {
@@ -109,15 +63,14 @@ func TestCatalogdControllerReconcile(t *testing.T) {
10963
name string
11064
catalog *catalogdv1.ClusterCatalog
11165
expectedError error
112-
shouldPanic bool
11366
expectedCatalog *catalogdv1.ClusterCatalog
114-
source imageutil.Puller
67+
puller imageutil.Puller
11568
cache imageutil.Cache
11669
store storage.Instance
11770
}{
11871
{
119-
name: "invalid source type, panics",
120-
source: &MockPuller{},
72+
name: "invalid source type, returns error",
73+
puller: &imageutil.MockPuller{},
12174
store: &MockStore{},
12275
catalog: &catalogdv1.ClusterCatalog{
12376
ObjectMeta: metav1.ObjectMeta{
@@ -130,7 +83,7 @@ func TestCatalogdControllerReconcile(t *testing.T) {
13083
},
13184
},
13285
},
133-
shouldPanic: true,
86+
expectedError: reconcile.TerminalError(errors.New(`unknown source type "invalid"`)),
13487
expectedCatalog: &catalogdv1.ClusterCatalog{
13588
ObjectMeta: metav1.ObjectMeta{
13689
Name: "catalog",
@@ -154,9 +107,9 @@ func TestCatalogdControllerReconcile(t *testing.T) {
154107
},
155108
{
156109
name: "valid source type, unpack returns error, status updated to reflect error state and error is returned",
157-
expectedError: fmt.Errorf("source catalog content: %w", fmt.Errorf("mocksource error")),
158-
source: &MockPuller{
159-
pullError: errors.New("mocksource error"),
110+
expectedError: fmt.Errorf("source catalog content: %w", fmt.Errorf("mockpuller error")),
111+
puller: &imageutil.MockPuller{
112+
Error: errors.New("mockpuller error"),
160113
},
161114
store: &MockStore{},
162115
catalog: &catalogdv1.ClusterCatalog{
@@ -199,9 +152,9 @@ func TestCatalogdControllerReconcile(t *testing.T) {
199152
},
200153
{
201154
name: "valid source type, unpack returns terminal error, status updated to reflect terminal error state(Blocked) and error is returned",
202-
expectedError: fmt.Errorf("source catalog content: %w", reconcile.TerminalError(fmt.Errorf("mocksource terminal error"))),
203-
source: &MockPuller{
204-
pullError: reconcile.TerminalError(errors.New("mocksource terminal error")),
155+
expectedError: fmt.Errorf("source catalog content: %w", reconcile.TerminalError(fmt.Errorf("mockpuller terminal error"))),
156+
puller: &imageutil.MockPuller{
157+
Error: reconcile.TerminalError(errors.New("mockpuller terminal error")),
205158
},
206159
store: &MockStore{},
207160
catalog: &catalogdv1.ClusterCatalog{
@@ -244,9 +197,9 @@ func TestCatalogdControllerReconcile(t *testing.T) {
244197
},
245198
{
246199
name: "valid source type, unpack state == Unpacked, should reflect in status that it's progressing, and is serving",
247-
source: &MockPuller{
248-
fsys: &fstest.MapFS{},
249-
ref: mustRef(t, "my.org/someimage@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
200+
puller: &imageutil.MockPuller{
201+
ImageFS: &fstest.MapFS{},
202+
Ref: mustRef(t, "my.org/someimage@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
250203
},
251204
store: &MockStore{},
252205
catalog: &catalogdv1.ClusterCatalog{
@@ -303,8 +256,8 @@ func TestCatalogdControllerReconcile(t *testing.T) {
303256
{
304257
name: "valid source type, unpack state == Unpacked, storage fails, failure reflected in status and error returned",
305258
expectedError: fmt.Errorf("error storing fbc: mockstore store error"),
306-
source: &MockPuller{
307-
fsys: &fstest.MapFS{},
259+
puller: &imageutil.MockPuller{
260+
ImageFS: &fstest.MapFS{},
308261
},
309262
store: &MockStore{
310263
shouldError: true,
@@ -349,8 +302,8 @@ func TestCatalogdControllerReconcile(t *testing.T) {
349302
},
350303
{
351304
name: "storage finalizer not set, storage finalizer gets set",
352-
source: &MockPuller{
353-
fsys: &fstest.MapFS{},
305+
puller: &imageutil.MockPuller{
306+
ImageFS: &fstest.MapFS{},
354307
},
355308
store: &MockStore{},
356309
catalog: &catalogdv1.ClusterCatalog{
@@ -383,8 +336,8 @@ func TestCatalogdControllerReconcile(t *testing.T) {
383336
},
384337
{
385338
name: "storage finalizer set, catalog deletion timestamp is not zero (or nil), finalizer removed",
386-
source: &MockPuller{
387-
fsys: &fstest.MapFS{},
339+
puller: &imageutil.MockPuller{
340+
ImageFS: &fstest.MapFS{},
388341
},
389342
store: &MockStore{},
390343
catalog: &catalogdv1.ClusterCatalog{
@@ -456,8 +409,8 @@ func TestCatalogdControllerReconcile(t *testing.T) {
456409
{
457410
name: "storage finalizer set, catalog deletion timestamp is not zero (or nil), storage delete failed, error returned, finalizer not removed and catalog continues serving",
458411
expectedError: fmt.Errorf("finalizer %q failed: %w", fbcDeletionFinalizer, fmt.Errorf("mockstore delete error")),
459-
source: &MockPuller{
460-
fsys: &fstest.MapFS{},
412+
puller: &imageutil.MockPuller{
413+
ImageFS: &fstest.MapFS{},
461414
},
462415
store: &MockStore{
463416
shouldError: true,
@@ -525,9 +478,9 @@ func TestCatalogdControllerReconcile(t *testing.T) {
525478
},
526479
{
527480
name: "storage finalizer set, catalog deletion timestamp is not zero (or nil), unpack cleanup failed, error returned, finalizer not removed but catalog stops serving",
528-
expectedError: fmt.Errorf("finalizer %q failed: %w", fbcDeletionFinalizer, fmt.Errorf("mocksource cleanup error")),
529-
source: &MockPuller{},
530-
cache: &MockCache{fmt.Errorf("mocksource cleanup error")},
481+
expectedError: fmt.Errorf("finalizer %q failed: %w", fbcDeletionFinalizer, fmt.Errorf("mockcache delete error")),
482+
puller: &imageutil.MockPuller{},
483+
cache: &imageutil.MockCache{DeleteErr: fmt.Errorf("mockcache delete error")},
531484
store: &MockStore{
532485
shouldError: false,
533486
},
@@ -592,8 +545,8 @@ func TestCatalogdControllerReconcile(t *testing.T) {
592545
},
593546
{
594547
name: "catalog availability set to disabled, status.urls should get unset",
595-
source: &MockPuller{
596-
fsys: &fstest.MapFS{},
548+
puller: &imageutil.MockPuller{
549+
ImageFS: &fstest.MapFS{},
597550
},
598551
store: &MockStore{},
599552
catalog: &catalogdv1.ClusterCatalog{
@@ -663,8 +616,8 @@ func TestCatalogdControllerReconcile(t *testing.T) {
663616
},
664617
{
665618
name: "catalog availability set to disabled, finalizer should get removed",
666-
source: &MockPuller{
667-
fsys: &fstest.MapFS{},
619+
puller: &imageutil.MockPuller{
620+
ImageFS: &fstest.MapFS{},
668621
},
669622
store: &MockStore{},
670623
catalog: &catalogdv1.ClusterCatalog{
@@ -738,22 +691,17 @@ func TestCatalogdControllerReconcile(t *testing.T) {
738691
t.Run(tt.name, func(t *testing.T) {
739692
reconciler := &ClusterCatalogReconciler{
740693
Client: nil,
741-
ImagePuller: tt.source,
694+
ImagePuller: tt.puller,
742695
ImageCache: tt.cache,
743696
Storage: tt.store,
744697
storedCatalogs: map[string]storedCatalogData{},
745698
}
746699
if reconciler.ImageCache == nil {
747-
reconciler.ImageCache = &MockCache{}
700+
reconciler.ImageCache = &imageutil.MockCache{}
748701
}
749702
require.NoError(t, reconciler.setupFinalizers())
750703
ctx := context.Background()
751704

752-
if tt.shouldPanic {
753-
assert.Panics(t, func() { _, _ = reconciler.reconcile(ctx, tt.catalog) })
754-
return
755-
}
756-
757705
res, err := reconciler.reconcile(ctx, tt.catalog)
758706
assert.Equal(t, ctrl.Result{}, res)
759707
// errors are aggregated/wrapped
@@ -818,9 +766,9 @@ func TestPollingRequeue(t *testing.T) {
818766
t.Run(name, func(t *testing.T) {
819767
reconciler := &ClusterCatalogReconciler{
820768
Client: nil,
821-
ImagePuller: &MockPuller{
822-
fsys: &fstest.MapFS{},
823-
ref: mustRef(t, "my.org/someimage@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
769+
ImagePuller: &imageutil.MockPuller{
770+
ImageFS: &fstest.MapFS{},
771+
Ref: mustRef(t, "my.org/someimage@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
824772
},
825773
Storage: &MockStore{},
826774
storedCatalogs: map[string]storedCatalogData{},
@@ -1037,7 +985,7 @@ func TestPollingReconcilerUnpack(t *testing.T) {
1037985
}
1038986
reconciler := &ClusterCatalogReconciler{
1039987
Client: nil,
1040-
ImagePuller: &MockPuller{pullError: errors.New("mocksource error")},
988+
ImagePuller: &imageutil.MockPuller{Error: errors.New("mockpuller error")},
1041989
Storage: &MockStore{},
1042990
storedCatalogs: scd,
1043991
}

cmd/operator-controller/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"time"
2929

3030
"github.com/containers/image/v5/types"
31-
"github.com/go-logr/logr"
3231
"github.com/spf13/pflag"
3332
corev1 "k8s.io/api/core/v1"
3433
apiextensionsv1client "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
@@ -47,6 +46,7 @@ import (
4746
"sigs.k8s.io/controller-runtime/pkg/client"
4847
crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer"
4948
"sigs.k8s.io/controller-runtime/pkg/healthz"
49+
"sigs.k8s.io/controller-runtime/pkg/log"
5050
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
5151
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
5252

@@ -313,7 +313,7 @@ func main() {
313313
DockerCertPath: pullCasDir,
314314
OCICertPath: pullCasDir,
315315
}
316-
logger := logr.FromContextOrDiscard(ctx)
316+
logger := log.FromContext(ctx)
317317
if _, err := os.Stat(authFilePath); err == nil && globalPullSecretKey != nil {
318318
logger.Info("using available authentication information for pulling image")
319319
srcContext.AuthFilePath = authFilePath
@@ -328,7 +328,7 @@ func main() {
328328

329329
clusterExtensionFinalizers := crfinalizer.NewFinalizers()
330330
if err := clusterExtensionFinalizers.Register(controllers.ClusterExtensionCleanupUnpackCacheFinalizer, finalizers.FinalizerFunc(func(ctx context.Context, obj client.Object) (crfinalizer.Result, error) {
331-
return crfinalizer.Result{}, imageCache.DeleteID(ctx, obj.GetName())
331+
return crfinalizer.Result{}, imageCache.Delete(ctx, obj.GetName())
332332
})); err != nil {
333333
setupLog.Error(err, "unable to register finalizer", "finalizerKey", controllers.ClusterExtensionCleanupUnpackCacheFinalizer)
334334
os.Exit(1)

0 commit comments

Comments
 (0)