1
0
mirror of https://github.com/ko-build/ko.git synced 2025-03-17 20:47:51 +02:00

Don't set image.base.name if base is specified by digest (#408)

* Don't set image.base.name if base is specified by digest

* don't set empty annotation

* annotate Results, not Images and Indexes separately

* moar cleanup

* skip annotations check for images in indexes, these won't be annotated anymore
This commit is contained in:
Jason Hall 2021-08-11 16:52:01 -04:00 committed by GitHub
parent 466dbab6c4
commit 86a7b6f4b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 22 deletions

View File

@ -702,7 +702,7 @@ func (g *gobuild) configForImportPath(ip string) Config {
return config
}
func (g *gobuild) buildOne(ctx context.Context, refStr string, baseRef name.Reference, baseDigest v1.Hash, base v1.Image, platform *v1.Platform) (v1.Image, error) {
func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, platform *v1.Platform) (v1.Image, error) {
ref := newRef(refStr)
cf, err := base.ConfigFile()
@ -779,11 +779,6 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, baseRef name.Refe
return nil, err
}
withApp = mutate.Annotations(withApp, map[string]string{
specsv1.AnnotationBaseImageDigest: baseDigest.String(),
specsv1.AnnotationBaseImageName: baseRef.Name(),
}).(v1.Image)
// Start from a copy of the base image's config file, and set
// the entrypoint to our app.
cfg, err := withApp.ConfigFile()
@ -863,26 +858,44 @@ func (g *gobuild) Build(ctx context.Context, s string) (Result, error) {
return nil, err
}
var res Result
switch mt {
case types.OCIImageIndex, types.DockerManifestList:
baseIndex, ok := base.(v1.ImageIndex)
if !ok {
return nil, fmt.Errorf("failed to interpret base as index: %v", base)
}
return g.buildAll(ctx, s, baseRef, baseDigest, baseIndex)
res, err = g.buildAll(ctx, s, baseIndex)
case types.OCIManifestSchema1, types.DockerManifestSchema2:
baseImage, ok := base.(v1.Image)
if !ok {
return nil, fmt.Errorf("failed to interpret base as image: %v", base)
}
return g.buildOne(ctx, s, baseRef, baseDigest, baseImage, nil)
res, err = g.buildOne(ctx, s, baseImage, nil)
default:
return nil, fmt.Errorf("base image media type: %s", mt)
}
if err != nil {
return nil, err
}
// Annotate the image or index with base image information.
// (Docker manifest lists don't support annotations)
if mt != types.DockerManifestList {
anns := map[string]string{
specsv1.AnnotationBaseImageDigest: baseDigest.String(),
}
if _, ok := baseRef.(name.Tag); ok {
anns[specsv1.AnnotationBaseImageName] = baseRef.Name()
}
res = mutate.Annotations(res, anns).(Result)
}
return res, nil
}
// TODO(#192): Do these in parallel?
func (g *gobuild) buildAll(ctx context.Context, ref string, baseRef name.Reference, baseDigest v1.Hash, baseIndex v1.ImageIndex) (v1.ImageIndex, error) {
func (g *gobuild) buildAll(ctx context.Context, ref string, baseIndex v1.ImageIndex) (v1.ImageIndex, error) {
im, err := baseIndex.IndexManifest()
if err != nil {
return nil, err
@ -904,7 +917,7 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseRef name.Referen
if err != nil {
return nil, err
}
img, err := g.buildOne(ctx, ref, baseRef, baseDigest, baseImage, desc.Platform)
img, err := g.buildOne(ctx, ref, baseImage, desc.Platform)
if err != nil {
return nil, err
}
@ -925,15 +938,6 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseRef name.Referen
}
idx := mutate.IndexMediaType(mutate.AppendManifests(empty.Index, adds...), baseType)
// Annotate the index with base image information, if the index is an OCI image index.
// (Docker manifest lists don't support annotations)
if baseType == types.OCIImageIndex {
idx = mutate.Annotations(idx, map[string]string{
specsv1.AnnotationBaseImageName: baseRef.Name(),
specsv1.AnnotationBaseImageDigest: baseDigest.String(),
}).(v1.ImageIndex)
}
return idx, nil
}

View File

@ -343,7 +343,7 @@ func TestGoBuildNoKoData(t *testing.T) {
})
}
func validateImage(t *testing.T, img v1.Image, baseLayers int64, creationTime v1.Time) {
func validateImage(t *testing.T, img v1.Image, baseLayers int64, creationTime v1.Time, checkAnnotations bool) {
t.Helper()
ls, err := img.Layers()
@ -483,6 +483,9 @@ func validateImage(t *testing.T, img v1.Image, baseLayers int64, creationTime v1
})
t.Run("check annotations", func(t *testing.T) {
if !checkAnnotations {
t.Skip("skipping annotations check")
}
mf, err := img.Manifest()
if err != nil {
t.Fatalf("Manifest() = %v", err)
@ -540,7 +543,7 @@ func TestGoBuild(t *testing.T) {
t.Fatalf("Build() not an image: %v", result)
}
validateImage(t, img, baseLayers, creationTime)
validateImage(t, img, baseLayers, creationTime, true)
// Check that rebuilding the image again results in the same image digest.
t.Run("check determinism", func(t *testing.T) {
@ -622,7 +625,7 @@ func TestGoBuildIndex(t *testing.T) {
if err != nil {
t.Fatalf("idx.Image(%s) = %v", desc.Digest, err)
}
validateImage(t, img, baseLayers, creationTime)
validateImage(t, img, baseLayers, creationTime, false)
}
if want, got := images, int64(len(im.Manifests)); want != got {