Skip to content

Commit

Permalink
Add extension reader
Browse files Browse the repository at this point in the history
Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Sep 28, 2023
1 parent 6a60008 commit dd5c115
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 29 deletions.
37 changes: 22 additions & 15 deletions pkg/cnb/remote_store_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,29 +36,28 @@ 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
}

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{
Expand Down Expand Up @@ -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()
}
10 changes: 5 additions & 5 deletions pkg/cnb/remote_store_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/buildpack/buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/reconciler/clusterbuildpack/clusterbuildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/reconciler/clusterstore/clusterstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd5c115

Please sign in to comment.