Merge branch 'main' into main-v5
This commit is contained in:
commit
8a745062ed
@ -28,6 +28,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -434,10 +435,15 @@ func applyCLIOverrides(cfg conf.SimpleConfig) (conf.SimpleConfig, error) {
|
|||||||
|
|
||||||
// Set to random port if port is empty string
|
// Set to random port if port is empty string
|
||||||
if len(exposeAPI.Binding.HostPort) == 0 {
|
if len(exposeAPI.Binding.HostPort) == 0 {
|
||||||
exposeAPI, err = cliutil.ParsePortExposureSpec("random", k3d.DefaultAPIPort)
|
var freePort string
|
||||||
if err != nil {
|
port, err := cliutil.GetFreePort()
|
||||||
return cfg, err
|
freePort = strconv.Itoa(port)
|
||||||
|
if err != nil || port == 0 {
|
||||||
|
l.Log().Warnf("Failed to get random free port: %+v", err)
|
||||||
|
l.Log().Warnf("Falling back to internal port %s (may be blocked though)...", k3d.DefaultAPIPort)
|
||||||
|
freePort = k3d.DefaultAPIPort
|
||||||
}
|
}
|
||||||
|
exposeAPI.Binding.HostPort = freePort
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.ExposeAPI = conf.SimpleExposureOpts{
|
cfg.ExposeAPI = conf.SimpleExposureOpts{
|
||||||
|
@ -178,7 +178,11 @@ func ImageImportIntoClusterMulti(ctx context.Context, runtime runtimes.Runtime,
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func findImages(ctx context.Context, runtime runtimes.Runtime, requestedImages []string) (imagesFromRuntime, imagesFromTar []string, err error) {
|
type runtimeImageGetter interface {
|
||||||
|
GetImages(context.Context) ([]string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func findImages(ctx context.Context, runtime runtimeImageGetter, requestedImages []string) (imagesFromRuntime, imagesFromTar []string, err error) {
|
||||||
runtimeImages, err := runtime.GetImages(ctx)
|
runtimeImages, err := runtime.GetImages(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log().Errorln("Failed to fetch list of existing images from runtime")
|
l.Log().Errorln("Failed to fetch list of existing images from runtime")
|
||||||
@ -189,14 +193,14 @@ func findImages(ctx context.Context, runtime runtimes.Runtime, requestedImages [
|
|||||||
if isFile(requestedImage) {
|
if isFile(requestedImage) {
|
||||||
imagesFromTar = append(imagesFromTar, requestedImage)
|
imagesFromTar = append(imagesFromTar, requestedImage)
|
||||||
l.Log().Debugf("Selected image '%s' is a file", requestedImage)
|
l.Log().Debugf("Selected image '%s' is a file", requestedImage)
|
||||||
break
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
runtimeImage, found := findRuntimeImage(requestedImage, runtimeImages)
|
runtimeImage, found := findRuntimeImage(requestedImage, runtimeImages)
|
||||||
if found {
|
if found {
|
||||||
imagesFromRuntime = append(imagesFromRuntime, runtimeImage)
|
imagesFromRuntime = append(imagesFromRuntime, runtimeImage)
|
||||||
l.Log().Debugf("Selected image '%s' (found as '%s') in runtime", requestedImage, runtimeImage)
|
l.Log().Debugf("Selected image '%s' (found as '%s') in runtime", requestedImage, runtimeImage)
|
||||||
break
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Log().Warnf("Image '%s' is not a file and couldn't be found in the container runtime", requestedImage)
|
l.Log().Warnf("Image '%s' is not a file and couldn't be found in the container runtime", requestedImage)
|
||||||
|
@ -23,7 +23,12 @@ THE SOFTWARE.
|
|||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/go-test/deep"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_findRuntimeImage(T *testing.T) {
|
func Test_findRuntimeImage(T *testing.T) {
|
||||||
@ -170,3 +175,44 @@ func Test_findRuntimeImage(T *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_findImages(t *testing.T) {
|
||||||
|
// given
|
||||||
|
tarImage, err := ioutil.TempFile("", "images.tgz")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Failed to create temporary file")
|
||||||
|
}
|
||||||
|
defer os.Remove(tarImage.Name())
|
||||||
|
|
||||||
|
tarImages := []string{tarImage.Name()}
|
||||||
|
runtimeImages := []string{
|
||||||
|
"alpine:version",
|
||||||
|
"busybox:latest",
|
||||||
|
}
|
||||||
|
runtime := &FakeRuntimeImageGetter{runtimeImages: runtimeImages}
|
||||||
|
|
||||||
|
requestedImages := append(runtimeImages, tarImages...)
|
||||||
|
|
||||||
|
// when
|
||||||
|
foundRuntimeImages, foundTarImages, err := findImages(context.Background(), runtime, requestedImages)
|
||||||
|
|
||||||
|
// then
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Got unexpected error %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if diff := deep.Equal(foundRuntimeImages, runtimeImages); diff != nil {
|
||||||
|
t.Errorf("Found runtime images\n%+v\ndoes not match expected runtime images\n%+v\nDiff:\n%+v", foundRuntimeImages, runtimeImages, diff)
|
||||||
|
}
|
||||||
|
if diff := deep.Equal(foundTarImages, tarImages); diff != nil {
|
||||||
|
t.Errorf("Found tar images\n%+v\ndoes not match expected tar images\n%+v\nDiff:\n%+v", foundTarImages, runtimeImages, diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type FakeRuntimeImageGetter struct {
|
||||||
|
runtimeImages []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FakeRuntimeImageGetter) GetImages(_ context.Context) ([]string, error) {
|
||||||
|
return f.runtimeImages, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user