Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] runtime use kind: kind support podman #323

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions pkg/kwokctl/runtime/kind/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ func (c *Cluster) Install(ctx context.Context) error {
if conf.PrometheusPort != 0 {
images = append(images, conf.PrometheusImage)
}
err = image.PullImages(ctx, "docker", images, conf.QuietPull)

err = image.PullImages(ctx, runtimeBinary, images, conf.QuietPull)
if err != nil {
return err
}
Expand Down Expand Up @@ -212,9 +213,17 @@ func (c *Cluster) Up(ctx context.Context) error {
if conf.PrometheusPort != 0 {
images = append(images, conf.PrometheusImage)
}
err = loadImages(ctx, kindPath, c.Name(), images)
if err != nil {
return err

if runtimeBinary == "docker" {
err = loadImagesFormDocker(ctx, kindPath, c.Name(), images)
if err != nil {
return err
}
} else {
err = loadImages(ctx, kindPath, c.Name(), images)
if err != nil {
return err
}
}

kubeconfig, err := c.InHostKubeconfig()
Expand All @@ -235,7 +244,7 @@ func (c *Cluster) Up(ctx context.Context) error {
return err
}

err = exec.Exec(ctx, "", exec.IOStreams{}, "docker", "cp", c.GetWorkdirPath(runtime.KwokPod), c.Name()+"-control-plane:/etc/kubernetes/manifests/kwok-controller.yaml")
err = exec.Exec(ctx, "", exec.IOStreams{}, runtimeBinary, "cp", c.GetWorkdirPath(runtime.KwokPod), c.Name()+"-control-plane:/etc/kubernetes/manifests/kwok-controller.yaml")
if err != nil {
return err
}
Expand Down Expand Up @@ -315,6 +324,22 @@ func (c *Cluster) Up(ctx context.Context) error {
}

func loadImages(ctx context.Context, command, kindCluster string, images []string) error {
logger := log.FromContext(ctx)
for _, image := range images {
err := exec.Exec(ctx, "", exec.IOStreams{},
command, "load", "image-load",
image,
"--name", kindCluster,
)
if err != nil {
return err
}
logger.Info("Loaded image", "image", image)
}
return nil
}

func loadImagesFormDocker(ctx context.Context, command, kindCluster string, images []string) error {
logger := log.FromContext(ctx)
for _, image := range images {
err := exec.Exec(ctx, "", exec.IOStreams{},
Expand Down Expand Up @@ -423,7 +448,7 @@ func (c *Cluster) Down(ctx context.Context) error {

// Start starts the cluster
func (c *Cluster) Start(ctx context.Context) error {
err := exec.Exec(ctx, "", exec.IOStreams{}, "docker", "start", c.getClusterName())
err := exec.Exec(ctx, "", exec.IOStreams{}, runtimeBinary, "start", c.getClusterName())
if err != nil {
return err
}
Expand All @@ -432,7 +457,7 @@ func (c *Cluster) Start(ctx context.Context) error {

// Stop stops the cluster
func (c *Cluster) Stop(ctx context.Context) error {
err := exec.Exec(ctx, "", exec.IOStreams{}, "docker", "stop", c.getClusterName())
err := exec.Exec(ctx, "", exec.IOStreams{}, runtimeBinary, "stop", c.getClusterName())
if err != nil {
return err
}
Expand All @@ -441,7 +466,7 @@ func (c *Cluster) Stop(ctx context.Context) error {

// StartComponent starts a component in the cluster
func (c *Cluster) StartComponent(ctx context.Context, name string) error {
err := exec.Exec(ctx, "", exec.IOStreams{}, "docker", "exec", c.Name()+"-control-plane", "mv", "/etc/kubernetes/"+name+".yaml.bak", "/etc/kubernetes/manifests/"+name+".yaml")
err := exec.Exec(ctx, "", exec.IOStreams{}, runtimeBinary, "exec", c.Name()+"-control-plane", "mv", "/etc/kubernetes/"+name+".yaml.bak", "/etc/kubernetes/manifests/"+name+".yaml")
if err != nil {
return err
}
Expand All @@ -450,7 +475,7 @@ func (c *Cluster) StartComponent(ctx context.Context, name string) error {

// StopComponent stops a component in the cluster
func (c *Cluster) StopComponent(ctx context.Context, name string) error {
err := exec.Exec(ctx, "", exec.IOStreams{}, "docker", "exec", c.Name()+"-control-plane", "mv", "/etc/kubernetes/manifests/"+name+".yaml", "/etc/kubernetes/"+name+".yaml.bak")
err := exec.Exec(ctx, "", exec.IOStreams{}, runtimeBinary, "exec", c.Name()+"-control-plane", "mv", "/etc/kubernetes/manifests/"+name+".yaml", "/etc/kubernetes/"+name+".yaml.bak")
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/kwokctl/runtime/kind/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,21 @@ package kind
import (
"sigs.k8s.io/kwok/pkg/consts"
"sigs.k8s.io/kwok/pkg/kwokctl/runtime"
"sigs.k8s.io/kwok/pkg/utils/exec"
)

var runtimeBinary = "docker"

func init() {
runtime.DefaultRegistry.Register(consts.RuntimeTypeKind, NewCluster)
if _, err := exec.LookPath(runtimeBinary); err != nil {
if !exec.IsNotFound(err) {
panic(err)
}

if _, err := exec.LookPath("podman"); err != nil {
panic(err)
}
runtimeBinary = "podman"
}
}
7 changes: 7 additions & 0 deletions pkg/utils/exec/lp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ limitations under the License.
package exec

import (
"errors"
"os/exec"
)

// LookPath is a wrapper around exec.LookPath.
func LookPath(file string) (string, error) {
return exec.LookPath(file)
}

// IsNotFound returns true if the specified error was created by NewNotFound.
// It supports wrapped errors and returns false when the error is nil.
func IsNotFound(err error) bool {
return errors.Is(err, exec.ErrNotFound)
}