From dd5c1151cae3ff43fe427c88f6f700f22d60cc11 Mon Sep 17 00:00:00 2001 From: Natalie Arellano Date: Thu, 28 Sep 2023 13:50:37 -0400 Subject: [PATCH] Add extension reader Signed-off-by: Natalie Arellano --- pkg/cnb/remote_store_reader.go | 37 +++++++++++-------- pkg/cnb/remote_store_reader_test.go | 10 ++--- pkg/reconciler/buildpack/buildpack.go | 4 +- .../buildpackfakes/fake_store_reader.go | 2 +- .../clusterbuildpack/clusterbuildpack.go | 4 +- .../fake_store_reader.go | 2 +- pkg/reconciler/clusterstore/clusterstore.go | 4 +- .../clusterstorefakes/fake_store_reader.go | 2 +- 8 files changed, 36 insertions(+), 29 deletions(-) diff --git a/pkg/cnb/remote_store_reader.go b/pkg/cnb/remote_store_reader.go index b1369e54b..ac7afe6ff 100644 --- a/pkg/cnb/remote_store_reader.go +++ b/pkg/cnb/remote_store_reader.go @@ -16,7 +16,15 @@ type RemoteBuildpackReader struct { RegistryClient RegistryClient } -func (r *RemoteBuildpackReader) Read(keychain authn.Keychain, storeImages []corev1alpha1.ImageSource) ([]corev1alpha1.BuildpackStatus, error) { +func (r *RemoteBuildpackReader) ReadBuildpack(keychain authn.Keychain, storeImages []corev1alpha1.ImageSource) ([]corev1alpha1.BuildpackStatus, error) { + return r.readModule(keychain, storeImages, buildpackLayersLabel) +} + +func (r *RemoteBuildpackReader) ReadExtension(keychain authn.Keychain, storeImages []corev1alpha1.ImageSource) ([]corev1alpha1.BuildpackStatus, error) { + return r.readModule(keychain, storeImages, extensionLayersLabel) +} + +func (r *RemoteBuildpackReader) readModule(keychain authn.Keychain, storeImages []corev1alpha1.ImageSource, layersLabelName string) ([]corev1alpha1.BuildpackStatus, error) { var g errgroup.Group c := make(chan corev1alpha1.BuildpackStatus) @@ -28,19 +36,18 @@ func (r *RemoteBuildpackReader) Read(keychain authn.Keychain, storeImages []core return err } - bpMetadata := BuildpackageMetadata{} + packageMetadata := BuildpackageMetadata{} if ok, err := imagehelpers.HasLabel(image, buildpackageMetadataLabel); err != nil { return err } else if ok { - err := imagehelpers.GetLabel(image, buildpackageMetadataLabel, &bpMetadata) + err := imagehelpers.GetLabel(image, buildpackageMetadataLabel, &packageMetadata) if err != nil { return err } } - // TODO: read extensionLayersLabel layerMetadata := BuildpackLayerMetadata{} - err = imagehelpers.GetLabel(image, buildpackLayersLabel, &layerMetadata) + err = imagehelpers.GetLabel(image, layersLabelName, &layerMetadata) if err != nil { return err } @@ -48,9 +55,9 @@ func (r *RemoteBuildpackReader) Read(keychain authn.Keychain, storeImages []core for id := range layerMetadata { for version, metadata := range layerMetadata[id] { packageInfo := corev1alpha1.BuildpackageInfo{ - Id: bpMetadata.Id, - Version: bpMetadata.Version, - Homepage: bpMetadata.Homepage, + Id: packageMetadata.Id, + Version: packageMetadata.Version, + Homepage: packageMetadata.Homepage, } info := corev1alpha1.BuildpackInfo{ @@ -101,18 +108,18 @@ func (r *RemoteBuildpackReader) Read(keychain authn.Keychain, storeImages []core close(c) }() - var buildpacks []corev1alpha1.BuildpackStatus + var statuses []corev1alpha1.BuildpackStatus for b := range c { - buildpacks = append(buildpacks, b) + statuses = append(statuses, b) } - sort.Slice(buildpacks, func(i, j int) bool { - if buildpacks[i].String() == buildpacks[j].String() { - return buildpacks[i].StoreImage.Image < buildpacks[j].StoreImage.Image + sort.Slice(statuses, func(i, j int) bool { + if statuses[i].String() == statuses[j].String() { + return statuses[i].StoreImage.Image < statuses[j].StoreImage.Image } - return buildpacks[i].String() < buildpacks[j].String() + return statuses[i].String() < statuses[j].String() }) - return buildpacks, g.Wait() + return statuses, g.Wait() } diff --git a/pkg/cnb/remote_store_reader_test.go b/pkg/cnb/remote_store_reader_test.go index 9e9424c6c..20fde4b47 100644 --- a/pkg/cnb/remote_store_reader_test.go +++ b/pkg/cnb/remote_store_reader_test.go @@ -205,7 +205,7 @@ func testRemoteStoreReader(t *testing.T, when spec.G, it spec.S) { }) it("returns all buildpacks from multiple images", func() { - storeBuildpacks, err := remoteStoreReader.Read(expectedKeychain, []corev1alpha1.ImageSource{ + storeBuildpacks, err := remoteStoreReader.ReadBuildpack(expectedKeychain, []corev1alpha1.ImageSource{ { Image: buildpackageA, }, @@ -356,7 +356,7 @@ func testRemoteStoreReader(t *testing.T, when spec.G, it spec.S) { }) it("returns all buildpacks in a deterministic order", func() { - expectedBuildpackOrder, err := remoteStoreReader.Read(expectedKeychain, []corev1alpha1.ImageSource{ + expectedBuildpackOrder, err := remoteStoreReader.ReadBuildpack(expectedKeychain, []corev1alpha1.ImageSource{ { Image: buildpackageA, }, @@ -367,7 +367,7 @@ func testRemoteStoreReader(t *testing.T, when spec.G, it spec.S) { require.NoError(t, err) for i := 1; i <= 50; i++ { - subsequentOrder, err := remoteStoreReader.Read(expectedKeychain, []corev1alpha1.ImageSource{ + subsequentOrder, err := remoteStoreReader.ReadBuildpack(expectedKeychain, []corev1alpha1.ImageSource{ { Image: buildpackageA, }, @@ -445,11 +445,11 @@ func testRemoteStoreReader(t *testing.T, when spec.G, it spec.S) { Image: "image/with_duplicates", }, } - expectedBuildpackOrder, err := remoteStoreReader.Read(expectedKeychain, images) + expectedBuildpackOrder, err := remoteStoreReader.ReadBuildpack(expectedKeychain, images) require.NoError(t, err) for i := 1; i <= 50; i++ { - subsequentOrder, err := remoteStoreReader.Read(expectedKeychain, images) + subsequentOrder, err := remoteStoreReader.ReadBuildpack(expectedKeychain, images) require.NoError(t, err) require.Equal(t, expectedBuildpackOrder, subsequentOrder) diff --git a/pkg/reconciler/buildpack/buildpack.go b/pkg/reconciler/buildpack/buildpack.go index 68b30b803..b818d6059 100644 --- a/pkg/reconciler/buildpack/buildpack.go +++ b/pkg/reconciler/buildpack/buildpack.go @@ -29,7 +29,7 @@ const ( //go:generate counterfeiter . StoreReader type StoreReader interface { - Read(keychain authn.Keychain, storeImages []corev1alpha1.ImageSource) ([]corev1alpha1.BuildpackStatus, error) + ReadBuildpack(keychain authn.Keychain, storeImages []corev1alpha1.ImageSource) ([]corev1alpha1.BuildpackStatus, error) } func NewController( @@ -129,7 +129,7 @@ func (c *Reconciler) reconcileBuildpackStatus(ctx context.Context, buildpack *bu return buildpack, err } - buildpacks, err := c.StoreReader.Read(keychain, []corev1alpha1.ImageSource{buildpack.Spec.ImageSource}) + buildpacks, err := c.StoreReader.ReadBuildpack(keychain, []corev1alpha1.ImageSource{buildpack.Spec.ImageSource}) if err != nil { buildpack.Status = buildapi.BuildpackStatus{ Status: corev1alpha1.CreateStatusWithReadyCondition(buildpack.Generation, err), diff --git a/pkg/reconciler/buildpack/buildpackfakes/fake_store_reader.go b/pkg/reconciler/buildpack/buildpackfakes/fake_store_reader.go index 428c8df29..ef38f006d 100644 --- a/pkg/reconciler/buildpack/buildpackfakes/fake_store_reader.go +++ b/pkg/reconciler/buildpack/buildpackfakes/fake_store_reader.go @@ -28,7 +28,7 @@ type FakeStoreReader struct { invocationsMutex sync.RWMutex } -func (fake *FakeStoreReader) Read(arg1 authn.Keychain, arg2 []v1alpha1.ImageSource) ([]v1alpha1.BuildpackStatus, error) { +func (fake *FakeStoreReader) ReadBuildpack(arg1 authn.Keychain, arg2 []v1alpha1.ImageSource) ([]v1alpha1.BuildpackStatus, error) { var arg2Copy []v1alpha1.ImageSource if arg2 != nil { arg2Copy = make([]v1alpha1.ImageSource, len(arg2)) diff --git a/pkg/reconciler/clusterbuildpack/clusterbuildpack.go b/pkg/reconciler/clusterbuildpack/clusterbuildpack.go index 4f933c838..300539b35 100644 --- a/pkg/reconciler/clusterbuildpack/clusterbuildpack.go +++ b/pkg/reconciler/clusterbuildpack/clusterbuildpack.go @@ -29,7 +29,7 @@ const ( //go:generate counterfeiter . StoreReader type StoreReader interface { - Read(keychain authn.Keychain, storeImages []corev1alpha1.ImageSource) ([]corev1alpha1.BuildpackStatus, error) + ReadBuildpack(keychain authn.Keychain, storeImages []corev1alpha1.ImageSource) ([]corev1alpha1.BuildpackStatus, error) } func NewController( @@ -129,7 +129,7 @@ func (c *Reconciler) reoncileClusterBuildpackStatus(ctx context.Context, cluster return clusterBuildpack, err } - buildpacks, err := c.StoreReader.Read(keychain, []corev1alpha1.ImageSource{clusterBuildpack.Spec.ImageSource}) + buildpacks, err := c.StoreReader.ReadBuildpack(keychain, []corev1alpha1.ImageSource{clusterBuildpack.Spec.ImageSource}) if err != nil { clusterBuildpack.Status = buildapi.ClusterBuildpackStatus{ Status: corev1alpha1.CreateStatusWithReadyCondition(clusterBuildpack.Generation, err), diff --git a/pkg/reconciler/clusterbuildpack/clusterbuildpackfakes/fake_store_reader.go b/pkg/reconciler/clusterbuildpack/clusterbuildpackfakes/fake_store_reader.go index 88282f611..d7dc8905b 100644 --- a/pkg/reconciler/clusterbuildpack/clusterbuildpackfakes/fake_store_reader.go +++ b/pkg/reconciler/clusterbuildpack/clusterbuildpackfakes/fake_store_reader.go @@ -28,7 +28,7 @@ type FakeStoreReader struct { invocationsMutex sync.RWMutex } -func (fake *FakeStoreReader) Read(arg1 authn.Keychain, arg2 []v1alpha1.ImageSource) ([]v1alpha1.BuildpackStatus, error) { +func (fake *FakeStoreReader) ReadBuildpack(arg1 authn.Keychain, arg2 []v1alpha1.ImageSource) ([]v1alpha1.BuildpackStatus, error) { var arg2Copy []v1alpha1.ImageSource if arg2 != nil { arg2Copy = make([]v1alpha1.ImageSource, len(arg2)) diff --git a/pkg/reconciler/clusterstore/clusterstore.go b/pkg/reconciler/clusterstore/clusterstore.go index f99834524..9273d0233 100644 --- a/pkg/reconciler/clusterstore/clusterstore.go +++ b/pkg/reconciler/clusterstore/clusterstore.go @@ -28,7 +28,7 @@ const ( //go:generate counterfeiter . StoreReader type StoreReader interface { - Read(keychain authn.Keychain, storeImages []corev1alpha1.ImageSource) ([]corev1alpha1.BuildpackStatus, error) + ReadBuildpack(keychain authn.Keychain, storeImages []corev1alpha1.ImageSource) ([]corev1alpha1.BuildpackStatus, error) } func NewController( @@ -128,7 +128,7 @@ func (c *Reconciler) reconcileClusterStoreStatus(ctx context.Context, clusterSto return clusterStore, err } - buildpacks, err := c.StoreReader.Read(keychain, clusterStore.Spec.Sources) + buildpacks, err := c.StoreReader.ReadBuildpack(keychain, clusterStore.Spec.Sources) if err != nil { clusterStore.Status = buildapi.ClusterStoreStatus{ Status: corev1alpha1.CreateStatusWithReadyCondition(clusterStore.Generation, err), diff --git a/pkg/reconciler/clusterstore/clusterstorefakes/fake_store_reader.go b/pkg/reconciler/clusterstore/clusterstorefakes/fake_store_reader.go index cc030c62a..0631770bc 100644 --- a/pkg/reconciler/clusterstore/clusterstorefakes/fake_store_reader.go +++ b/pkg/reconciler/clusterstore/clusterstorefakes/fake_store_reader.go @@ -28,7 +28,7 @@ type FakeStoreReader struct { invocationsMutex sync.RWMutex } -func (fake *FakeStoreReader) Read(arg1 authn.Keychain, arg2 []v1alpha1.ImageSource) ([]v1alpha1.BuildpackStatus, error) { +func (fake *FakeStoreReader) ReadBuildpack(arg1 authn.Keychain, arg2 []v1alpha1.ImageSource) ([]v1alpha1.BuildpackStatus, error) { var arg2Copy []v1alpha1.ImageSource if arg2 != nil { arg2Copy = make([]v1alpha1.ImageSource, len(arg2))