diff --git a/pkg/runtime/docker.go b/pkg/runtime/docker.go index fd5a2282..83a7c59b 100644 --- a/pkg/runtime/docker.go +++ b/pkg/runtime/docker.go @@ -10,9 +10,10 @@ import ( type DockerRuntime struct { client *dockerClient.Client + name string } -func NewDockerRuntime() (IRuntime, error) { +func NewDockerRuntime(name string) (IRuntime, error) { client, err := dockerClient.NewClientWithOpts( dockerClient.WithAPIVersionNegotiation(), ) @@ -22,11 +23,12 @@ func NewDockerRuntime() (IRuntime, error) { return &DockerRuntime{ client: client, + name: name, }, nil } func (p *DockerRuntime) Name() string { - return "Docker" + return p.name } func (p *DockerRuntime) GetContainerByName(ctx context.Context, name string) (*types.Container, error) { diff --git a/pkg/runtime/finch.go b/pkg/runtime/finch.go index d0fc8f19..284a97c4 100644 --- a/pkg/runtime/finch.go +++ b/pkg/runtime/finch.go @@ -37,7 +37,7 @@ func NewFinchRuntime() (IRuntime, error) { } func (p *FinchRuntime) Name() string { - return "Finch" + return "finch" } func (f *FinchRuntime) ContainerWithPort(ctx context.Context, name string, port string) (bool, error) { diff --git a/pkg/runtime/util.go b/pkg/runtime/util.go index 8c7dd7f9..891071f7 100644 --- a/pkg/runtime/util.go +++ b/pkg/runtime/util.go @@ -2,46 +2,23 @@ package runtime import ( "errors" + "os" "strconv" - "strings" - - "sigs.k8s.io/kind/pkg/exec" ) func DetectRuntime() (rt IRuntime, err error) { - if isDockerAvailable() { - if rt, err = NewDockerRuntime(); err != nil { - return nil, err - } - } else if isFinchAvailable() { - rt, _ = NewFinchRuntime() - } else { - return nil, errors.New("no runtime found") - } - return rt, nil -} - -func isDockerAvailable() bool { - cmd := exec.Command("docker", "-v") - lines, err := exec.OutputLines(cmd) - if err != nil || len(lines) != 1 { - return false - } - return strings.HasPrefix(lines[0], "Docker version") -} - -func isFinchAvailable() bool { - cmd := exec.Command("nerdctl", "-v") - lines, err := exec.OutputLines(cmd) - if err != nil || len(lines) != 1 { - cmd = exec.Command("finch", "-v") - lines, err = exec.OutputLines(cmd) - if err != nil || len(lines) != 1 { - return false - } - return strings.HasPrefix(lines[0], "finch version") + var notFoundErr = errors.New("runtime not found") + + switch p := os.Getenv("KIND_EXPERIMENTAL_PROVIDER"); p { + case "", "docker": + return NewDockerRuntime("docker") + case "podman": + return NewDockerRuntime("podman") + case "finch": + return NewFinchRuntime() + default: + return nil, notFoundErr } - return strings.HasPrefix(lines[0], "nerdctl version") } func toUint16(portString string) (uint16, error) {