Skip to content

Commit

Permalink
follow the Kind pattern to select the runtime
Browse files Browse the repository at this point in the history
- respect KIND_EXPERIMENTAL_PROVIDER

Signed-off-by: Nima Kaviani <[email protected]>
  • Loading branch information
nimakaviani committed Apr 25, 2024
1 parent 36f4083 commit c82b3bf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 38 deletions.
6 changes: 4 additions & 2 deletions pkg/runtime/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/finch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
47 changes: 12 additions & 35 deletions pkg/runtime/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c82b3bf

Please sign in to comment.