mirror of
https://github.com/labring/sealos.git
synced 2026-09-24 15:46:19 +08:00
feat: refactor buildah's inspect method so we can inspect different types of image (#2405)
Signed-off-by: fengxsong <fengxsong@outlook.com> Signed-off-by: fengxsong <fengxsong@outlook.com>
This commit is contained in:
@@ -85,7 +85,7 @@ func SyncClusterStatus(cluster *v2.Cluster, bdah buildah.Interface, reset bool)
|
||||
}
|
||||
|
||||
type imageInspector interface {
|
||||
InspectImage(string) (v1.Image, error)
|
||||
InspectImage(imgName string, opts ...string) (*v1.Image, error)
|
||||
}
|
||||
|
||||
func OCIToImageMount(mount *v2.MountImage, inspector imageInspector) error {
|
||||
|
||||
@@ -19,6 +19,15 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/containers/buildah/define"
|
||||
"github.com/containers/image/v5/directory"
|
||||
"github.com/containers/image/v5/docker"
|
||||
"github.com/containers/image/v5/docker/archive"
|
||||
ociarchive "github.com/containers/image/v5/oci/archive"
|
||||
"github.com/containers/image/v5/oci/layout"
|
||||
"github.com/containers/image/v5/openshift"
|
||||
"github.com/containers/image/v5/sif"
|
||||
"github.com/containers/image/v5/storage"
|
||||
"github.com/containers/image/v5/tarball"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -47,3 +56,26 @@ const (
|
||||
PullIfNewer = define.PullIfNewer
|
||||
PullNever = define.PullNever
|
||||
)
|
||||
|
||||
var (
|
||||
TransportDir = directory.Transport.Name()
|
||||
TransportDocker = docker.Transport.Name()
|
||||
TransportDockerArchive = archive.Transport.Name()
|
||||
TransportOCIArchive = ociarchive.Transport.Name()
|
||||
TransportOCI = layout.Transport.Name()
|
||||
TransportAtomic = openshift.Transport.Name()
|
||||
TransportSif = sif.Transport.Name()
|
||||
TransportTarball = tarball.Transport.Name()
|
||||
TransportContainersStorage = storage.Transport.Name()
|
||||
)
|
||||
|
||||
func formatReferenceWithTransportName(tr string, ref string) string {
|
||||
switch tr {
|
||||
case TransportAtomic, TransportDir, TransportDockerArchive, TransportOCI, TransportOCIArchive, TransportTarball, TransportSif:
|
||||
return tr + ":" + ref
|
||||
case TransportContainersStorage, TransportDocker:
|
||||
return tr + "://" + ref
|
||||
default:
|
||||
panic(fmt.Errorf("unknown transport %s", tr))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@ import (
|
||||
|
||||
"github.com/containers/buildah"
|
||||
"github.com/containers/buildah/pkg/parse"
|
||||
"github.com/containers/image/v5/image"
|
||||
imagestorage "github.com/containers/image/v5/storage"
|
||||
"github.com/containers/image/v5/transports"
|
||||
"github.com/containers/image/v5/types"
|
||||
"github.com/containers/storage"
|
||||
storagetypes "github.com/containers/storage/types"
|
||||
@@ -33,7 +36,7 @@ import (
|
||||
type Interface interface {
|
||||
Pull(imageNames []string, opts ...FlagSetter) error
|
||||
Load(input string, ociType string) (string, error)
|
||||
InspectImage(name string) (v1.Image, error)
|
||||
InspectImage(name string, opts ...string) (*v1.Image, error)
|
||||
Create(name string, image string, opts ...FlagSetter) (buildah.BuilderInfo, error)
|
||||
Delete(name string) error
|
||||
InspectContainer(name string) (buildah.BuilderInfo, error)
|
||||
@@ -115,13 +118,50 @@ func (impl *realImpl) Pull(imageNames []string, opts ...FlagSetter) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (impl *realImpl) InspectImage(name string) (v1.Image, error) {
|
||||
builder, err := openImage(getContext(), impl.systemContext, impl.store, name)
|
||||
if err != nil {
|
||||
return v1.Image{}, err
|
||||
func finalizeReference(transport types.ImageTransport, imgName string) (types.ImageTransport, string) {
|
||||
parts := strings.SplitN(imgName, ":", 2)
|
||||
if len(parts) == 2 {
|
||||
if transport := transports.Get(parts[0]); transport != nil {
|
||||
return transport, imgName
|
||||
}
|
||||
}
|
||||
out := buildah.GetBuildInfo(builder)
|
||||
return out.OCIv1, nil
|
||||
return transport, formatReferenceWithTransportName(transport.Name(), imgName)
|
||||
}
|
||||
|
||||
func (impl *realImpl) InspectImage(name string, opts ...string) (*v1.Image, error) {
|
||||
transportName := TransportContainersStorage
|
||||
if len(opts) > 0 {
|
||||
transportName = opts[0]
|
||||
}
|
||||
transport := transports.Get(transportName)
|
||||
if transport == nil {
|
||||
return nil, fmt.Errorf(`unknown transport "%s"`, opts[0])
|
||||
}
|
||||
transport, name = finalizeReference(transport, name)
|
||||
parts := strings.SplitN(name, ":", 2)
|
||||
// should never happened
|
||||
if len(parts) != 2 {
|
||||
return nil, fmt.Errorf(`invalid image name "%s", expected colon-separated transport:reference`, name)
|
||||
}
|
||||
imgName := parts[1]
|
||||
if st, ok := transport.(imagestorage.StoreTransport); ok {
|
||||
st.SetStore(impl.store)
|
||||
}
|
||||
ref, err := transport.ParseReference(imgName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx := getContext()
|
||||
src, err := ref.NewImageSource(ctx, impl.systemContext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer src.Close()
|
||||
img, err := image.FromUnparsedImage(ctx, impl.systemContext, image.UnparsedInstance(src, nil))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return img.OCIConfig(ctx)
|
||||
}
|
||||
|
||||
func (impl *realImpl) Create(name string, image string, opts ...FlagSetter) (buildah.BuilderInfo, error) {
|
||||
|
||||
@@ -133,7 +133,7 @@ func mergeImagesWithScratchContainer(newImageName string, images []string, platf
|
||||
imageObjList := make([]map[string]v1.Image, 0)
|
||||
for _, i := range images {
|
||||
obj, _ := b.InspectImage(i)
|
||||
imageObjList = append(imageObjList, map[string]v1.Image{i: obj})
|
||||
imageObjList = append(imageObjList, map[string]v1.Image{i: *obj})
|
||||
}
|
||||
|
||||
dockerfile, err := buildimage.MergeDockerfileFromImages(imageObjList)
|
||||
|
||||
Reference in New Issue
Block a user