mirror of
https://github.com/kairos-io/kairos-sdk.git
synced 2025-09-03 08:14:32 +00:00
Improve image extractor (#32)
This commit is contained in:
@@ -159,7 +159,7 @@ func (e OCIImageExtractor) Install(config *BundleConfig) error {
|
|||||||
return fmt.Errorf("could not create destination path %s: %s", config.RootPath, err)
|
return fmt.Errorf("could not create destination path %s: %s", config.RootPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return utils.ExtractOCIImage(config.Target, config.RootPath, utils.GetCurrentPlatform(), config.LocalFile)
|
return utils.ExtractOCIImage(config.Target, config.RootPath, utils.GetCurrentPlatform())
|
||||||
}
|
}
|
||||||
|
|
||||||
// OCIImageRunner will extract an OCI image and then run its run.sh
|
// OCIImageRunner will extract an OCI image and then run its run.sh
|
||||||
@@ -172,7 +172,7 @@ func (e OCIImageRunner) Install(config *BundleConfig) error {
|
|||||||
}
|
}
|
||||||
defer os.RemoveAll(tempDir)
|
defer os.RemoveAll(tempDir)
|
||||||
|
|
||||||
err = utils.ExtractOCIImage(config.Target, tempDir, utils.GetCurrentPlatform(), config.LocalFile)
|
err = utils.ExtractOCIImage(config.Target, tempDir, utils.GetCurrentPlatform())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@@ -2,21 +2,48 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/containerd/containerd/archive"
|
"github.com/containerd/containerd/archive"
|
||||||
"github.com/google/go-containerregistry/pkg/authn"
|
"github.com/google/go-containerregistry/pkg/authn"
|
||||||
|
"github.com/google/go-containerregistry/pkg/logs"
|
||||||
"github.com/google/go-containerregistry/pkg/name"
|
"github.com/google/go-containerregistry/pkg/name"
|
||||||
v1 "github.com/google/go-containerregistry/pkg/v1"
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
||||||
"github.com/google/go-containerregistry/pkg/v1/daemon"
|
"github.com/google/go-containerregistry/pkg/v1/daemon"
|
||||||
"github.com/google/go-containerregistry/pkg/v1/mutate"
|
"github.com/google/go-containerregistry/pkg/v1/mutate"
|
||||||
"github.com/google/go-containerregistry/pkg/v1/remote"
|
"github.com/google/go-containerregistry/pkg/v1/remote"
|
||||||
|
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExtractOCIImage will extract a given targetImage into a given targetDestination and pull from the local repo if set.
|
var defaultRetryBackoff = remote.Backoff{
|
||||||
func ExtractOCIImage(targetImage, targetDestination, targetPlatform string, isLocal bool) error {
|
Duration: 1.0 * time.Second,
|
||||||
|
Factor: 3.0,
|
||||||
|
Jitter: 0.1,
|
||||||
|
Steps: 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaultRetryPredicate = func(err error) bool {
|
||||||
|
if err == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) || errors.Is(err, syscall.EPIPE) || errors.Is(err, syscall.ECONNRESET) || strings.Contains(err.Error(), "connection refused") {
|
||||||
|
logs.Warn.Printf("retrying %v", err)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExtractOCIImage will extract a given targetImage into a given targetDestination
|
||||||
|
func ExtractOCIImage(targetImage, targetDestination, targetPlatform string) error {
|
||||||
var platform *v1.Platform
|
var platform *v1.Platform
|
||||||
|
var img v1.Image
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if targetPlatform != "" {
|
if targetPlatform != "" {
|
||||||
@@ -36,7 +63,7 @@ func ExtractOCIImage(targetImage, targetDestination, targetPlatform string, isLo
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
img, err := getimage(ref, *platform, isLocal)
|
img, err = getimage(ref, *platform)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -51,14 +78,26 @@ func ExtractOCIImage(targetImage, targetDestination, targetPlatform string, isLo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// image returns the proper image to pull with transport and auth
|
// image returns the proper image to pull with transport and auth
|
||||||
func getimage(ref name.Reference, platform v1.Platform, local bool) (v1.Image, error) {
|
// tries local daemon first and then fallbacks into remote
|
||||||
if local {
|
func getimage(ref name.Reference, platform v1.Platform) (v1.Image, error) {
|
||||||
return daemon.Image(ref)
|
var image v1.Image
|
||||||
|
var err error
|
||||||
|
tr := transport.NewRetry(http.DefaultTransport,
|
||||||
|
transport.WithRetryBackoff(defaultRetryBackoff),
|
||||||
|
transport.WithRetryPredicate(defaultRetryPredicate),
|
||||||
|
)
|
||||||
|
|
||||||
|
image, err = daemon.Image(ref)
|
||||||
|
fmt.Println("lo")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("re")
|
||||||
|
image, err = remote.Image(ref,
|
||||||
|
remote.WithTransport(tr),
|
||||||
|
remote.WithPlatform(platform),
|
||||||
|
remote.WithAuthFromKeychain(authn.DefaultKeychain),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return remote.Image(ref,
|
return image, err
|
||||||
remote.WithTransport(http.DefaultTransport),
|
|
||||||
remote.WithPlatform(platform),
|
|
||||||
remote.WithAuthFromKeychain(authn.DefaultKeychain),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user