From aafd3e863b40f08e941d991562d74e68e883b1f1 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Fri, 18 Mar 2022 11:30:56 -0400 Subject: [PATCH] Revert "Produce OCI images by default (#623)" (#653) This reverts commit 7e9709a63a6bedc92020d0c4335e81a07b10e0d2. --- doc/ko_apply.md | 1 - doc/ko_build.md | 1 - doc/ko_create.md | 1 - doc/ko_resolve.md | 1 - doc/ko_run.md | 1 - pkg/build/gobuild.go | 32 ++-------- pkg/build/gobuild_test.go | 109 ---------------------------------- pkg/build/options.go | 10 ---- pkg/commands/options/build.go | 6 -- pkg/commands/resolver.go | 1 - 10 files changed, 5 insertions(+), 158 deletions(-) diff --git a/doc/ko_apply.md b/doc/ko_apply.md index 73288128..feeb9822 100644 --- a/doc/ko_apply.md +++ b/doc/ko_apply.md @@ -70,7 +70,6 @@ ko apply -f FILENAME [flags] --password string Password for basic authentication to the API server (DEPRECATED) --platform strings Which platform to use when pulling a multi-platform base. Format: all | [/[/]][,platform]* -P, --preserve-import-paths Whether to preserve the full import path after KO_DOCKER_REPO. - --preserve-media-type If false, push images in OCI format regardless of base image format --push Push images to KO_DOCKER_REPO (default true) -R, --recursive Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory. --request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (DEPRECATED) diff --git a/doc/ko_build.md b/doc/ko_build.md index 9ed0a9e3..cee5b5f4 100644 --- a/doc/ko_build.md +++ b/doc/ko_build.md @@ -55,7 +55,6 @@ ko build IMPORTPATH... [flags] --oci-layout-path string Path to save the OCI image layout of the built images --platform strings Which platform to use when pulling a multi-platform base. Format: all | [/[/]][,platform]* -P, --preserve-import-paths Whether to preserve the full import path after KO_DOCKER_REPO. - --preserve-media-type If false, push images in OCI format regardless of base image format --push Push images to KO_DOCKER_REPO (default true) --sbom string The SBOM media type to use (none will disable SBOM synthesis and upload, also supports: spdx, go.version-m). (default "spdx") --tag-only Include tags but not digests in resolved image references. Useful when digests are not preserved when images are repopulated. diff --git a/doc/ko_create.md b/doc/ko_create.md index d6d6b2a5..06615e59 100644 --- a/doc/ko_create.md +++ b/doc/ko_create.md @@ -70,7 +70,6 @@ ko create -f FILENAME [flags] --password string Password for basic authentication to the API server (DEPRECATED) --platform strings Which platform to use when pulling a multi-platform base. Format: all | [/[/]][,platform]* -P, --preserve-import-paths Whether to preserve the full import path after KO_DOCKER_REPO. - --preserve-media-type If false, push images in OCI format regardless of base image format --push Push images to KO_DOCKER_REPO (default true) -R, --recursive Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory. --request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (DEPRECATED) diff --git a/doc/ko_resolve.md b/doc/ko_resolve.md index d046e59d..23552290 100644 --- a/doc/ko_resolve.md +++ b/doc/ko_resolve.md @@ -51,7 +51,6 @@ ko resolve -f FILENAME [flags] --oci-layout-path string Path to save the OCI image layout of the built images --platform strings Which platform to use when pulling a multi-platform base. Format: all | [/[/]][,platform]* -P, --preserve-import-paths Whether to preserve the full import path after KO_DOCKER_REPO. - --preserve-media-type If false, push images in OCI format regardless of base image format --push Push images to KO_DOCKER_REPO (default true) -R, --recursive Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory. --sbom string The SBOM media type to use (none will disable SBOM synthesis and upload, also supports: spdx, go.version-m). (default "spdx") diff --git a/doc/ko_run.md b/doc/ko_run.md index f7a2c8aa..02b08d59 100644 --- a/doc/ko_run.md +++ b/doc/ko_run.md @@ -42,7 +42,6 @@ ko run IMPORTPATH [flags] --oci-layout-path string Path to save the OCI image layout of the built images --platform strings Which platform to use when pulling a multi-platform base. Format: all | [/[/]][,platform]* -P, --preserve-import-paths Whether to preserve the full import path after KO_DOCKER_REPO. - --preserve-media-type If false, push images in OCI format regardless of base image format --push Push images to KO_DOCKER_REPO (default true) --sbom string The SBOM media type to use (none will disable SBOM synthesis and upload, also supports: spdx, go.version-m). (default "spdx") --tag-only Include tags but not digests in resolved image references. Useful when digests are not preserved when images are repopulated. diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index d3a50b69..39850b83 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -78,7 +78,6 @@ type gobuild struct { sbom sbomber disableOptimizations bool trimpath bool - preserveMediaType bool buildConfigs map[string]Config platformMatcher *platformMatcher dir string @@ -100,7 +99,6 @@ type gobuildOpener struct { sbom sbomber disableOptimizations bool trimpath bool - preserveMediaType bool buildConfigs map[string]Config platforms []string labels map[string]string @@ -128,7 +126,6 @@ func (gbo *gobuildOpener) Open() (Interface, error) { sbom: gbo.sbom, disableOptimizations: gbo.disableOptimizations, trimpath: gbo.trimpath, - preserveMediaType: gbo.preserveMediaType, buildConfigs: gbo.buildConfigs, labels: gbo.labels, dir: gbo.dir, @@ -697,16 +694,6 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl ref := newRef(refStr) - baseType := types.OCIManifestSchema1 - if g.preserveMediaType { - var err error - baseType, err = base.MediaType() - if err != nil { - return nil, err - } - } - base = mutate.MediaType(base, baseType) - cf, err := base.ConfigFile() if err != nil { return nil, err @@ -891,7 +878,7 @@ func (g *gobuild) Build(ctx context.Context, s string) (Result, error) { // Annotate the base image we pass to the build function with // annotations indicating the digest (and possibly tag) of the // base image. This will be inherited by the image produced. - if !(mt == types.DockerManifestList && g.preserveMediaType) { + if mt != types.DockerManifestList { baseDigest, err := base.Digest() if err != nil { return nil, err @@ -977,17 +964,11 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseIndex v1.ImageIn if err != nil { return err } - - mt := types.OCIManifestSchema1 - if g.preserveMediaType { - mt = desc.MediaType - } - adds[i] = ocimutate.IndexAddendum{ Add: img, Descriptor: v1.Descriptor{ URLs: desc.URLs, - MediaType: mt, + MediaType: desc.MediaType, Annotations: desc.Annotations, Platform: desc.Platform, }, @@ -999,12 +980,9 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseIndex v1.ImageIn return nil, err } - baseType := types.OCIImageIndex - if g.preserveMediaType { - baseType, err = baseIndex.MediaType() - if err != nil { - return nil, err - } + baseType, err := baseIndex.MediaType() + if err != nil { + return nil, err } idx := ocimutate.AppendManifests( diff --git a/pkg/build/gobuild_test.go b/pkg/build/gobuild_test.go index 3c408a35..0bd93af4 100644 --- a/pkg/build/gobuild_test.go +++ b/pkg/build/gobuild_test.go @@ -513,17 +513,6 @@ func TestGoBuildNoKoData(t *testing.T) { t.Errorf("created = %v, want %v", actual, creationTime) } }) - - t.Run("check OCI media type", func(t *testing.T) { - mt, err := img.MediaType() - if err != nil { - t.Errorf("MediaType() = %v", err) - } - - if got, want := mt, types.OCIManifestSchema1; got != want { - t.Errorf("mediaType = %v, want %v", got, want) - } - }) } func validateImage(t *testing.T, img oci.SignedImage, baseLayers int64, creationTime v1.Time, checkAnnotations bool, expectSBOM bool) { @@ -930,104 +919,6 @@ func TestGoBuildIndex(t *testing.T) { t.Errorf("Digest mismatch: %s != %s", d1, d2) } }) - - t.Run("check OCI media type", func(t *testing.T) { - mt, err := idx.MediaType() - if err != nil { - t.Fatalf("MediaType() = %v", err) - } - - if got, want := mt, types.OCIImageIndex; got != want { - t.Errorf("mediaType = %v, want %v", got, want) - } - - for i, mf := range im.Manifests { - if got, want := mf.MediaType, types.OCIManifestSchema1; got != want { - t.Errorf("manifest[%d] mediaType = %s, want %s", i, got, want) - } - } - }) -} - -func TestPreserveMediaType(t *testing.T) { - mustRandomImage := func(t *testing.T) v1.Image { - img, err := random.Image(1, 1) - if err != nil { - t.Fatal(err) - } - return img - } - mustRandomIndex := func(t *testing.T) v1.ImageIndex { - idx, err := random.Index(1, 1, 3) - if err != nil { - t.Fatal(err) - } - return idx - } - - for _, c := range []struct { - desc string - preserve bool - base Result - want types.MediaType - }{{ - desc: "docker image -> oci image", - preserve: false, - base: mustRandomImage(t), - want: types.OCIManifestSchema1, - }, { - desc: "docker index -> oci index", - preserve: false, - base: mustRandomIndex(t), - want: types.OCIImageIndex, - }, { - desc: "docker image, preserved", - preserve: true, - base: mustRandomImage(t), - want: types.DockerManifestSchema2, - }, { - desc: "docker index, preserved", - preserve: true, - base: mutate.IndexMediaType(mustRandomIndex(t), types.DockerManifestList), - want: types.DockerManifestList, - }, { - desc: "oci image", - preserve: true, - base: mutate.MediaType(mustRandomImage(t), types.OCIManifestSchema1), - want: types.OCIManifestSchema1, - }, { - desc: "oci index", - preserve: true, - base: mutate.IndexMediaType(mustRandomIndex(t), types.OCIImageIndex), - want: types.OCIImageIndex, - }} { - t.Run(c.desc, func(t *testing.T) { - importpath := "github.com/google/ko" - ng, err := NewGo( - context.Background(), - "", - WithBaseImages(func(context.Context, string) (name.Reference, Result, error) { return baseRef, c.base, nil }), - WithPlatforms("all"), - WithPreserveMediaType(c.preserve), - withBuilder(writeTempFile), - ) - if err != nil { - t.Fatalf("NewGo() = %v", err) - } - - result, err := ng.Build(context.Background(), StrictScheme+filepath.Join(importpath, "test")) - if err != nil { - t.Fatalf("Build() = %v", err) - } - - got, err := result.MediaType() - if err != nil { - t.Errorf("MediaType() = %v", err) - } else if got != c.want { - t.Errorf("Got %q, want %q", got, c.want) - } - }) - } } func TestNestedIndex(t *testing.T) { diff --git a/pkg/build/options.go b/pkg/build/options.go index ee25949b..e29bca5b 100644 --- a/pkg/build/options.go +++ b/pkg/build/options.go @@ -73,16 +73,6 @@ func WithTrimpath(v bool) Option { } } -// WithPreserveMediaType is a functional option that controls whether to -// preserve media types from base images. If false, images that are produced -// will use OCI media types instead. -func WithPreserveMediaType(v bool) Option { - return func(gbo *gobuildOpener) error { - gbo.preserveMediaType = v - return nil - } -} - // WithConfig is a functional option for providing GoReleaser Build influenced // build settings for importpaths. // diff --git a/pkg/commands/options/build.go b/pkg/commands/options/build.go index 6db10667..4f2fec97 100644 --- a/pkg/commands/options/build.go +++ b/pkg/commands/options/build.go @@ -67,9 +67,6 @@ type BuildOptions struct { // BuildConfigs stores the per-image build config from `.ko.yaml`. BuildConfigs map[string]build.Config - - // If true, don't convert Docker-typed base images to OCI when building. - PreserveMediaType bool } func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) { @@ -83,9 +80,6 @@ func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) { "Which platform to use when pulling a multi-platform base. Format: all | [/[/]][,platform]*") cmd.Flags().StringSliceVar(&bo.Labels, "image-label", []string{}, "Which labels (key=value) to add to the image.") - - cmd.Flags().BoolVar(&bo.PreserveMediaType, "preserve-media-type", false, "If false, push images in OCI format regardless of base image format") - bo.Trimpath = true } diff --git a/pkg/commands/resolver.go b/pkg/commands/resolver.go index 9526b640..91c09fba 100644 --- a/pkg/commands/resolver.go +++ b/pkg/commands/resolver.go @@ -108,7 +108,6 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) { opts = append(opts, build.WithSPDX(version())) } opts = append(opts, build.WithTrimpath(bo.Trimpath)) - opts = append(opts, build.WithPreserveMediaType(bo.PreserveMediaType)) for _, lf := range bo.Labels { parts := strings.SplitN(lf, "=", 2) if len(parts) != 2 {