Skip to content

Commit

Permalink
--runtime use kind: kind support podman
Browse files Browse the repository at this point in the history
Signed-off-by: helen <haitao.zhang@daocloud.io>
  • Loading branch information
helen-frank committed Feb 23, 2023
1 parent d278022 commit 5df8e11
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
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.IsErrNotFound(err) {
panic(err)
}

if _, err := exec.LookPath("podman"); err != nil {
panic(err)
}
runtimeBinary = "podman"
}
}
5 changes: 5 additions & 0 deletions pkg/utils/exec/lp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ 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)
}

func IsErrNotFound(err error) bool {
return errors.Is(err, exec.ErrNotFound)
}

0 comments on commit 5df8e11

Please sign in to comment.