working commit
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/authn"
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
pkg "github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/google/go-containerregistry/pkg/v1/layout"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
)
|
||||
|
||||
func (util *ImageUtil) pushImage(params *PushImageParams) (*PushImageResult, error) {
|
||||
var err error
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Duration(params.Timeout)*time.Second)
|
||||
res := &PushImageResult{}
|
||||
|
||||
options := make([]crane.Option, 0)
|
||||
options = append(options, crane.WithContext(ctx))
|
||||
if params.Username != "" && params.Password != "" {
|
||||
ref, err := name.ParseReference(params.Imagepath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
repo := ref.Context()
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defaultTransport := &roundTripper{}
|
||||
|
||||
scopes := []string{
|
||||
repo.Scope(transport.PushScope),
|
||||
repo.Scope(transport.PullScope),
|
||||
}
|
||||
|
||||
regName := repo.RegistryStr()
|
||||
reg, err := name.NewRegistry(regName)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
basicAuth := &authn.Basic{
|
||||
Username: params.Username,
|
||||
Password: params.Password,
|
||||
}
|
||||
|
||||
authTransport, err := transport.NewWithContext(ctx, reg, basicAuth,
|
||||
defaultTransport, scopes)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
options = append(options, crane.WithTransport(authTransport))
|
||||
} else {
|
||||
defaultTransport := &roundTripper{}
|
||||
options = append(options, crane.WithTransport(defaultTransport))
|
||||
}
|
||||
|
||||
dstDir := makeTmpFileName(params.Filepath)
|
||||
err = unarchive(params.Filepath, dstDir)
|
||||
if err != nil {
|
||||
os.RemoveAll(dstDir)
|
||||
return res, err
|
||||
}
|
||||
image, err := imageLoader(dstDir)
|
||||
if err != nil {
|
||||
os.RemoveAll(dstDir)
|
||||
return res, err
|
||||
}
|
||||
err = crane.Push(image, params.Imagepath, options...)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
func imageLoader(dirPath string) (pkg.Image, error) {
|
||||
var err error
|
||||
var res pkg.Image
|
||||
layoutPath, err := layout.FromPath(dirPath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
imageIndex, err := layoutPath.ImageIndex()
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
indexManifest, err := imageIndex.IndexManifest()
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if indexManifest == nil {
|
||||
err := fmt.Errorf("Empty indexManifest referency")
|
||||
return res, err
|
||||
}
|
||||
|
||||
manifest := indexManifest.Manifests[0]
|
||||
image, err := layoutPath.Image(manifest.Digest)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res = image
|
||||
return res, err
|
||||
}
|
||||
Reference in New Issue
Block a user