-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Implement annotation-based rebase hints
Adds crane.Rebase helper function for higher-level functionality including annotation-based hints
- Loading branch information
Showing
5 changed files
with
153 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
.project | ||
bazel* | ||
.idea | ||
*.iml | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package crane | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/mutate" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
) | ||
|
||
const ( | ||
// TODO: better annotations | ||
baseDigestAnnotation = "base-digest" | ||
baseTagAnnotation = "base-tag" | ||
) | ||
|
||
func Rebase(orig, oldBase, newBase string, opt ...Option) (v1.Image, error) { | ||
o := makeOptions(opt...) | ||
origRef, err := name.ParseReference(orig, o.name...) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing tag %q: %v", origRef, err) | ||
} | ||
origImg, err := remote.Image(origRef, o.remote...) | ||
|
||
m, err := origImg.Manifest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if oldBase == "" && m.Annotations != nil { | ||
oldBase = m.Annotations[baseDigestAnnotation] | ||
if oldBase != "" { | ||
log.Printf("Detected old base from %q annotation: %s", baseDigestAnnotation, oldBase) | ||
} | ||
} | ||
if oldBase == "" { | ||
return nil, fmt.Errorf("either oldBase or %q annotation is required", baseDigestAnnotation) | ||
} | ||
if newBase == "" && m.Annotations != nil { | ||
newBase = m.Annotations[baseTagAnnotation] | ||
if newBase != "" { | ||
log.Printf("Detected new base from %q annotation: %s", baseTagAnnotation, newBase) | ||
} | ||
} | ||
if newBase == "" { | ||
return nil, fmt.Errorf("either newBase or %q annotation is required", baseTagAnnotation) | ||
} | ||
|
||
oldBaseRef, err := name.ParseReference(oldBase, o.name...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
oldBaseImg, err := remote.Image(oldBaseRef, o.remote...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
newBaseRef, err := name.ParseReference(newBase, o.name...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
newBaseImg, err := remote.Image(newBaseRef, o.remote...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rebased, err := mutate.Rebase(origImg, oldBaseImg, newBaseImg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m, err = rebased.Manifest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Update annotations for the new image manifest. | ||
// - the current ("old") base image is the new base image by digest. | ||
// - the future ("new") base image is the new base image by tag. | ||
d, err := newBaseImg.Digest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if m.Annotations == nil { | ||
m.Annotations = map[string]string{} | ||
} | ||
m.Annotations[baseDigestAnnotation] = newBaseRef.Context().Digest(d.String()).String() | ||
m.Annotations[baseTagAnnotation] = newBaseRef.String() | ||
log.Printf("Set annotation %q: %s", baseDigestAnnotation, m.Annotations[baseDigestAnnotation]) | ||
log.Printf("Set annotation %q: %s", baseTagAnnotation, m.Annotations[baseTagAnnotation]) | ||
return mutate.Manifest(rebased, m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters