Skip to content

Commit

Permalink
move detection logic to runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Nima Kaviani <[email protected]>
  • Loading branch information
nimakaviani committed Apr 12, 2024
1 parent c86165d commit 9457f27
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
28 changes: 10 additions & 18 deletions pkg/kind/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ package kind
import (
"context"
"embed"
"errors"
"fmt"
"io/fs"
"os"
"strings"

"github.com/cnoe-io/idpbuilder/pkg/runtime"
"github.com/cnoe-io/idpbuilder/pkg/util"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
)

var (
setupLog = ctrl.Log.WithName("setup")
setupLog = log.Log.WithName("setup")
)

type Cluster struct {
Expand Down Expand Up @@ -109,18 +108,11 @@ func NewCluster(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMap
}
provider := cluster.NewProvider(detectOpt)

var rt runtime.IRuntime
if runtime.IsDockerAvailable() {
setupLog.Info("Detected runtim: Docker")
if rt, err = runtime.NewDockerRuntime(); err != nil {
return nil, err
}
} else if runtime.IsFinchAvailable() {
println("Detected runtime: Finch")
rt, _ = runtime.NewFinchRuntime()
} else {
return nil, errors.New("no runtime found")
rt, err := runtime.DetectRuntime()
if err != nil {
return nil, err
}
setupLog.Info("Runtime detected", "provider", rt.Name())

return &Cluster{
provider: provider,
Expand Down Expand Up @@ -181,7 +173,7 @@ func (c *Cluster) Reconcile(ctx context.Context, recreate bool) error {

if clusterExitsts {
if recreate {
setupLog.Info("Existing cluster %s found. Deleting.\n", c.name)
setupLog.Info("Existing cluster found. Deleting.", "cluster", c.name)
c.provider.Delete(c.name, "")
} else {
rightPort, err := c.RunsOnRightPort(ctx)
Expand All @@ -194,7 +186,7 @@ func (c *Cluster) Reconcile(ctx context.Context, recreate bool) error {
}

// reuse if there is no port conflict
setupLog.Info("Cluster %s already exists\n", c.name)
setupLog.Info("Cluster already exists", "cluster", c.name)
return nil
}
}
Expand All @@ -208,15 +200,15 @@ func (c *Cluster) Reconcile(ctx context.Context, recreate bool) error {
fmt.Printf("%s", rawConfig)
fmt.Print("\n######################### config end ############################\n")

setupLog.Info("Creating kind cluster %s\n", c.name)
setupLog.Info("Creating kind cluster", "cluster", c.name)
if err = c.provider.Create(
c.name,
cluster.CreateWithRawConfig(rawConfig),
); err != nil {
return err
}

setupLog.Info("Done creating cluster %s\n", c.name)
setupLog.Info("Done creating cluster", "cluster", c.name)

return nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/runtime/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func NewDockerRuntime() (IRuntime, error) {
}, nil
}

func (p *DockerRuntime) Name() string {
return "Docker"
}

func (p *DockerRuntime) GetContainerByName(ctx context.Context, name string) (*types.Container, error) {
gotContainers, err := p.client.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/runtime/finch.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ func NewFinchRuntime() (IRuntime, error) {
}, nil
}

func (p *FinchRuntime) Name() string {
return "Finch"
}

func (f *FinchRuntime) ContainerWithPort(ctx context.Context, name string, port string) (bool, error) {
println("checking the container for port", name, port)
// add arguments to inspect the container
f.cmd.Args = append([]string{"finch"}, "container", "inspect", name)

Expand Down
3 changes: 3 additions & 0 deletions pkg/runtime/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const (
)

type IRuntime interface {
// get runtime name
Name() string

// checks whether the container has the followin
ContainerWithPort(ctx context.Context, name, port string) (bool, error)
}
18 changes: 15 additions & 3 deletions pkg/runtime/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ import (
"sigs.k8s.io/kind/pkg/exec"
)

func IsDockerAvailable() bool {
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 {
Expand All @@ -17,11 +30,10 @@ func IsDockerAvailable() bool {
return strings.HasPrefix(lines[0], "Docker version")
}

func IsFinchAvailable() bool {
func isFinchAvailable() bool {
cmd := exec.Command("nerdctl", "-v")
lines, err := exec.OutputLines(cmd)
if err != nil || len(lines) != 1 {
// check finch
cmd = exec.Command("finch", "-v")
lines, err = exec.OutputLines(cmd)
if err != nil || len(lines) != 1 {
Expand Down

0 comments on commit 9457f27

Please sign in to comment.