-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add api call which uses go-container registry to create OCI images from standard tar archives. Consume new API when generating final images instead of docker building them and adapts/add tests as necessary. This change now allows to carry over xattrs to final images. Fixes #266
- Loading branch information
Showing
16 changed files
with
306 additions
and
69 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
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,81 @@ | ||
// Copyright © 2021 Ettore Di Giacinto <[email protected]> | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 2 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along | ||
// with this program; if not, see <http://www.gnu.org/licenses/>. | ||
|
||
package image | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/empty" | ||
"github.com/google/go-containerregistry/pkg/v1/mutate" | ||
"github.com/google/go-containerregistry/pkg/v1/tarball" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func imageFromTar(imagename string, r io.Reader) (name.Reference, v1.Image, error) { | ||
newRef, err := name.ParseReference(imagename) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
layer, err := tarball.LayerFromReader(r) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
img, err := mutate.Append(empty.Image, mutate.Addendum{ | ||
Layer: layer, | ||
History: v1.History{ | ||
CreatedBy: "luet", | ||
Comment: "Custom image", | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return newRef, img, nil | ||
} | ||
|
||
// CreateTar a imagetarball from a standard tarball | ||
func CreateTar(srctar, dstimageTar, imagename string) error { | ||
f, err := os.Open(srctar) | ||
if err != nil { | ||
return errors.Wrap(err, "Cannot open "+srctar) | ||
} | ||
defer f.Close() | ||
|
||
return CreateTarReader(f, dstimageTar, imagename) | ||
} | ||
|
||
// CreateTarReader a imagetarball from a standard tarball | ||
func CreateTarReader(r io.Reader, dstimageTar, imagename string) error { | ||
dstFile, err := os.Create(dstimageTar) | ||
if err != nil { | ||
return errors.Wrap(err, "Cannot create "+dstimageTar) | ||
} | ||
defer dstFile.Close() | ||
|
||
newRef, img, err := imageFromTar(imagename, r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// NOTE: We might also stream that back to the daemon with daemon.Write(tag, img) | ||
return tarball.Write(newRef, img, dstFile) | ||
} |
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,79 @@ | ||
// Copyright © 2021 Ettore Di Giacinto <[email protected]> | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 2 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along | ||
// with this program; if not, see <http://www.gnu.org/licenses/>. | ||
|
||
package image_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
. "github.com/mudler/luet/pkg/api/core/image" | ||
"github.com/mudler/luet/pkg/api/core/types" | ||
"github.com/mudler/luet/pkg/api/core/types/artifact" | ||
"github.com/mudler/luet/pkg/compiler/backend" | ||
"github.com/mudler/luet/pkg/helpers/file" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Create", func() { | ||
Context("Creates an OCI image from a standard tar", func() { | ||
It("creates an image which is loadable", func() { | ||
ctx := types.NewContext() | ||
|
||
dst, err := ctx.Config.System.TempFile("dst") | ||
Expect(err).ToNot(HaveOccurred()) | ||
defer os.RemoveAll(dst.Name()) | ||
srcTar, err := ctx.Config.System.TempFile("srcTar") | ||
Expect(err).ToNot(HaveOccurred()) | ||
defer os.RemoveAll(srcTar.Name()) | ||
|
||
b := backend.NewSimpleDockerBackend(ctx) | ||
|
||
b.DownloadImage(backend.Options{ImageName: "alpine"}) | ||
img, err := b.ImageReference("alpine", false) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
_, dir, err := Extract(ctx, img, false, nil) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
defer os.RemoveAll(dir) | ||
|
||
Expect(file.Touch(filepath.Join(dir, "test"))).ToNot(HaveOccurred()) | ||
Expect(file.Exists(filepath.Join(dir, "bin"))).To(BeTrue()) | ||
|
||
a := artifact.NewPackageArtifact(srcTar.Name()) | ||
a.Compress(dir, 1) | ||
|
||
// Unfortunately there is no other easy way to test this | ||
err = CreateTar(srcTar.Name(), dst.Name(), "testimage") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
b.LoadImage(dst.Name()) | ||
|
||
Expect(b.ImageExists("testimage")).To(BeTrue()) | ||
|
||
img, err = b.ImageReference("testimage", false) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
_, dir, err = Extract(ctx, img, false, nil) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
defer os.RemoveAll(dir) | ||
Expect(file.Exists(filepath.Join(dir, "bin"))).To(BeTrue()) | ||
Expect(file.Exists(filepath.Join(dir, "test"))).To(BeTrue()) | ||
}) | ||
}) | ||
}) |
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
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
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
Oops, something went wrong.