mirror of
https://github.com/kairos-io/kairos-sdk.git
synced 2025-09-28 07:25:12 +00:00
add support for custom transport in oci image extractor (#140)
This commit is contained in:
@@ -3,6 +3,7 @@ package bundles
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -25,6 +26,7 @@ type BundleConfig struct {
|
|||||||
RootPath string
|
RootPath string
|
||||||
LocalFile bool
|
LocalFile bool
|
||||||
Auth *registrytypes.AuthConfig
|
Auth *registrytypes.AuthConfig
|
||||||
|
Transport http.RoundTripper
|
||||||
}
|
}
|
||||||
|
|
||||||
// BundleOption defines a configuration option for a bundle.
|
// BundleOption defines a configuration option for a bundle.
|
||||||
@@ -84,6 +86,13 @@ func WithAuth(c *registrytypes.AuthConfig) BundleOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithTransport(t http.RoundTripper) BundleOption {
|
||||||
|
return func(bc *BundleConfig) error {
|
||||||
|
bc.Transport = t
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (bc *BundleConfig) extractRepo() (string, string, error) {
|
func (bc *BundleConfig) extractRepo() (string, string, error) {
|
||||||
s := strings.Split(bc.Repository, "://")
|
s := strings.Split(bc.Repository, "://")
|
||||||
if len(s) != 2 {
|
if len(s) != 2 {
|
||||||
@@ -114,6 +123,7 @@ func defaultConfig() *BundleConfig {
|
|||||||
RootPath: "/",
|
RootPath: "/",
|
||||||
Repository: "docker://quay.io/kairos/packages",
|
Repository: "docker://quay.io/kairos/packages",
|
||||||
Auth: nil,
|
Auth: nil,
|
||||||
|
Transport: http.DefaultTransport,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +207,7 @@ func (e OCIImageExtractor) Install(config *BundleConfig) error {
|
|||||||
if e.Local {
|
if e.Local {
|
||||||
img, err = tarball.ImageFromPath(target, nil)
|
img, err = tarball.ImageFromPath(target, nil)
|
||||||
} else {
|
} else {
|
||||||
img, err = utils.GetImage(target, utils.GetCurrentPlatform(), config.Auth)
|
img, err = utils.GetImage(target, utils.GetCurrentPlatform(), config.Auth, config.Transport)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -226,7 +236,7 @@ func (e OCIImageRunner) Install(config *BundleConfig) error {
|
|||||||
if e.Local {
|
if e.Local {
|
||||||
img, err = tarball.ImageFromPath(target, nil)
|
img, err = tarball.ImageFromPath(target, nil)
|
||||||
} else {
|
} else {
|
||||||
img, err = utils.GetImage(target, utils.GetCurrentPlatform(), config.Auth)
|
img, err = utils.GetImage(target, utils.GetCurrentPlatform(), config.Auth, config.Transport)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@@ -72,7 +72,7 @@ func ExtractOCIImage(img v1.Image, targetDestination string) error {
|
|||||||
// GetImage if returns the proper image to pull with transport and auth
|
// GetImage if returns the proper image to pull with transport and auth
|
||||||
// tries local daemon first and then fallbacks into remote
|
// tries local daemon first and then fallbacks into remote
|
||||||
// if auth is nil, it will try to use the default keychain https://github.com/google/go-containerregistry/tree/main/pkg/authn#tldr-for-consumers-of-this-package
|
// if auth is nil, it will try to use the default keychain https://github.com/google/go-containerregistry/tree/main/pkg/authn#tldr-for-consumers-of-this-package
|
||||||
func GetImage(targetImage, targetPlatform string, auth *registrytypes.AuthConfig) (v1.Image, error) {
|
func GetImage(targetImage, targetPlatform string, auth *registrytypes.AuthConfig, t http.RoundTripper) (v1.Image, error) {
|
||||||
var platform *v1.Platform
|
var platform *v1.Platform
|
||||||
var image v1.Image
|
var image v1.Image
|
||||||
var err error
|
var err error
|
||||||
@@ -94,7 +94,11 @@ func GetImage(targetImage, targetPlatform string, auth *registrytypes.AuthConfig
|
|||||||
return image, err
|
return image, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tr := transport.NewRetry(http.DefaultTransport,
|
if t == nil {
|
||||||
|
t = http.DefaultTransport
|
||||||
|
}
|
||||||
|
|
||||||
|
tr := transport.NewRetry(t,
|
||||||
transport.WithRetryBackoff(defaultRetryBackoff),
|
transport.WithRetryBackoff(defaultRetryBackoff),
|
||||||
transport.WithRetryPredicate(defaultRetryPredicate),
|
transport.WithRetryPredicate(defaultRetryPredicate),
|
||||||
)
|
)
|
||||||
@@ -118,12 +122,12 @@ func GetImage(targetImage, targetPlatform string, auth *registrytypes.AuthConfig
|
|||||||
return image, err
|
return image, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOCIImageSize(targetImage, targetPlatform string, auth *registrytypes.AuthConfig) (int64, error) {
|
func GetOCIImageSize(targetImage, targetPlatform string, auth *registrytypes.AuthConfig, t http.RoundTripper) (int64, error) {
|
||||||
var size int64
|
var size int64
|
||||||
var img v1.Image
|
var img v1.Image
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
img, err = GetImage(targetImage, targetPlatform, auth)
|
img, err = GetImage(targetImage, targetPlatform, auth, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return size, err
|
return size, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user