Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds flag to update the version #456

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions buildpack/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
buildpackMetadataLabel = "io.buildpacks.buildpackage.metadata"
)

func Rename(buildpack, tag, newID string) (string, error) {
func Rename(buildpack, tag, newID, newVersion string) (string, error) {
reference, err := name.ParseReference(buildpack)
if err != nil {
return "", fmt.Errorf("unable to parse reference for existing buildpack tag\n%w", err)
Expand All @@ -39,7 +39,7 @@ func Rename(buildpack, tag, newID string) (string, error) {
return "", fmt.Errorf("unable to get buildpack layer metadata\n%w", err)
}

newLayersMetedata, layers, err := layerMetadata.metadataAndLayersFor(image, metadata.Id, metadata.Version, newID)
newLayersMetedata, layers, err := layerMetadata.metadataAndLayersFor(image, metadata.Id, metadata.Version, newID, newVersion)
if err != nil {
return "", fmt.Errorf("unable to generate new metadata\n%w", err)
}
Expand All @@ -55,6 +55,7 @@ func Rename(buildpack, tag, newID string) (string, error) {
}

metadata.Id = newID
metadata.Version = newVersion
newBuildpackage, err = SetLabels(newBuildpackage, map[string]interface{}{
layerMetadataLabel: newLayersMetedata,
buildpackMetadataLabel: metadata,
Expand Down Expand Up @@ -83,7 +84,7 @@ func Rename(buildpack, tag, newID string) (string, error) {
return identifer, nil
}

func (m BuildpackLayerMetadata) metadataAndLayersFor(sourceImage v1.Image, oldId string, oldVersion string, newId string) (BuildpackLayerMetadata, []v1.Layer, error) {
func (m BuildpackLayerMetadata) metadataAndLayersFor(sourceImage v1.Image, oldId string, oldVersion string, newId string, newVersion string) (BuildpackLayerMetadata, []v1.Layer, error) {
newLayerMetdata := BuildpackLayerMetadata{}

var layers []v1.Layer
Expand Down Expand Up @@ -121,9 +122,9 @@ func (m BuildpackLayerMetadata) metadataAndLayersFor(sourceImage v1.Image, oldId
return nil, nil, fmt.Errorf("unable to fetch layer by diff id %s for matching layer\n%w", diffId, err)
}

layer, err = rewriteLayer(layer, oldId, newId)
layer, err = rewriteLayer(layer, oldId, newId, oldVersion, newVersion)
if err != nil {
return nil, nil, fmt.Errorf("unable to rewrite layer, old id: %s, new id: %s\n%w", oldId, newId, err)
return nil, nil, fmt.Errorf("unable to rewrite layer, old id: %s, new id: %s, new version: %s\n%w", oldId, newId, newVersion, err)
}

diffID, err := layer.DiffID()
Expand All @@ -133,7 +134,7 @@ func (m BuildpackLayerMetadata) metadataAndLayersFor(sourceImage v1.Image, oldId

buildpack.LayerDiffID = diffID.String()

newLayerMetdata[newId][v] = buildpack
newLayerMetdata[newId][newVersion] = buildpack
layers = append(layers, layer)
}
}
Expand Down
11 changes: 8 additions & 3 deletions buildpack/rewriteLayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/google/go-containerregistry/pkg/v1/tarball"
)

func rewriteLayer(layer v1.Layer, old, new string) (v1.Layer, error) {
func rewriteLayer(layer v1.Layer, oldID, newID, oldVersion, newVersion string) (v1.Layer, error) {
b := &bytes.Buffer{}
tw := tar.NewWriter(b)

Expand All @@ -30,8 +30,12 @@ func rewriteLayer(layer v1.Layer, old, new string) (v1.Layer, error) {
break
}

newName := strings.ReplaceAll(header.Name, escapedID(old), escapedID(new))
// replace buildpack id and version in folder names
newName := strings.ReplaceAll(
strings.ReplaceAll(header.Name, escapedID(oldID), escapedID(newID)),
oldVersion, newVersion)

// replace buildpack id and version in buildpack.toml
if strings.HasSuffix(path.Clean(header.Name), "buildpack.toml") {
buf, err := ioutil.ReadAll(tr)
if err != nil {
Expand All @@ -44,7 +48,8 @@ func rewriteLayer(layer v1.Layer, old, new string) (v1.Layer, error) {
return nil, fmt.Errorf("unable to decode buildpack.toml\n%w", err)
}

bd.Info.ID = new
bd.Info.ID = newID
bd.Info.Version = newVersion

updatedBuildpackToml := &bytes.Buffer{}
err = toml.NewEncoder(updatedBuildpackToml).Encode(bd)
Expand Down
7 changes: 6 additions & 1 deletion cmd/update-buildpack-image-id/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
image := flagSet.String("image", "", "The exisiting buildpack image")
newImage := flagSet.String("new-image", "", "The new buildpack image")
id := flagSet.String("id", "", "The new id of the buildpack")
version := flagSet.String("version", "", "The new version of the buildpack")

if err := flagSet.Parse(os.Args[1:]); err != nil {
log.Fatal(fmt.Errorf("unable to parse flags\n%w", err))
Expand All @@ -31,7 +32,11 @@ func main() {
log.Fatal("--id is required")
}

rename, err := buildpack.Rename(*image, *newImage, *id)
if *version == "" {
log.Fatal("--version is required")
}

rename, err := buildpack.Rename(*image, *newImage, *id, *version)
if err != nil {
log.Fatal(err)
}
Expand Down