adapt new way of choosing runtime

pull/227/head
iwilltry42 5 years ago
parent 8601e1ef6d
commit 5f5c516ba2
  1. 1
      cmd/create/create.go
  2. 28
      cmd/create/createCluster.go
  3. 14
      cmd/root.go
  4. 16
      pkg/cluster/cluster.go
  5. 4
      pkg/cluster/node.go
  6. 7
      pkg/runtimes/containerd/container.go
  7. 12
      pkg/runtimes/docker/container.go
  8. 20
      pkg/runtimes/runtime.go
  9. 3
      pkg/types/types.go

@ -43,7 +43,6 @@ func NewCmdCreate() *cobra.Command {
cmd.AddCommand(NewCmdCreateNode())
// add flags
cmd.PersistentFlags().StringP("runtime", "r", "docker", "Choose a container runtime environment [docker, containerd]")
// done
return cmd

@ -24,8 +24,9 @@ package create
import (
"github.com/spf13/cobra"
"github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/types"
k3dCluster "github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
log "github.com/sirupsen/logrus"
)
@ -39,17 +40,13 @@ func NewCmdCreateCluster() *cobra.Command {
Short: "Create a new k3s cluster in docker",
Long: `Create a new k3s cluster with containerized nodes (k3s in docker).`,
Run: func(cmd *cobra.Command, args []string) {
c := types.Cluster{} // TODO: fill
rt, err := cmd.Flags().GetString("runtime")
if err != nil {
log.Debugln("runtime not defined")
}
cluster.CreateCluster(&c, rt)
runtime, cluster := parseCmd(cmd, args)
k3dCluster.CreateCluster(cluster, runtime)
},
}
// add flags
cmd.Flags().StringP("name", "n", types.DefaultClusterName, "Set a name for the cluster")
cmd.Flags().StringP("name", "n", k3d.DefaultClusterName, "Set a name for the cluster")
cmd.Flags().StringP("api-port", "a", "6443", "Specify the Kubernetes cluster API server port (Format: `--api-port [host:]port`")
cmd.Flags().IntP("workers", "w", 0, "Specify how many workers you want to create")
@ -60,3 +57,16 @@ func NewCmdCreateCluster() *cobra.Command {
}
// parseCmd parses the command input into variables required to create a cluster
func parseCmd(cmd *cobra.Command, args []string) (runtimes.Runtime, *k3d.Cluster) {
rt, err := cmd.Flags().GetString("runtime")
if err != nil {
log.Fatalln("Runtime not defined")
}
runtime, err := runtimes.GetRuntime(rt)
if err != nil {
log.Fatalln(err)
}
cluster := k3d.Cluster{}
return runtime, &cluster
}

@ -34,8 +34,15 @@ import (
log "github.com/sirupsen/logrus"
)
// RootFlags describes a struct that holds flags that can be set on root level of the command
type RootFlags struct {
debugLogging bool
runtime string
}
var flags = RootFlags{}
// var cfgFile string
var debugLogging bool
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
@ -64,7 +71,8 @@ func init() {
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k3d.yaml)")
rootCmd.PersistentFlags().BoolVar(&debugLogging, "verbose", false, "Enable verbose output (debug logging)")
rootCmd.PersistentFlags().BoolVar(&flags.debugLogging, "verbose", false, "Enable verbose output (debug logging)")
rootCmd.PersistentFlags().StringVarP(&flags.runtime, "runtime", "r", "docker", "Choose a container runtime environment [docker, containerd]")
// Cobra also supports local flags, which will only run
// when this action is called directly.
@ -106,7 +114,7 @@ func initConfig() {
// initLogging initializes the logger
func initLogging() {
if debugLogging {
if flags.debugLogging {
log.SetLevel(log.DebugLevel)
} else {
switch logLevel := strings.ToUpper(os.Getenv("LOG_LEVEL")); logLevel {

@ -23,8 +23,6 @@ package cluster
import (
k3drt "github.com/rancher/k3d/pkg/runtimes"
k3dContainerd "github.com/rancher/k3d/pkg/runtimes/containerd"
k3dDocker "github.com/rancher/k3d/pkg/runtimes/docker"
k3d "github.com/rancher/k3d/pkg/types"
log "github.com/sirupsen/logrus"
)
@ -32,17 +30,11 @@ import (
// CreateCluster creates a new cluster consisting of
// - some containerized k3s nodes
// - a docker network
func CreateCluster(cluster *k3d.Cluster, runtimeChoice string) error {
var runtime k3drt.Runtime
if runtimeChoice == "docker" {
runtime = k3dDocker.Docker{}
} else {
runtime = k3dContainerd.Containerd{}
}
func CreateCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error {
if err := runtime.CreateContainer(&k3d.Node{}); err != nil {
log.Println("...failed")
if err := runtime.CreateNode(&k3d.Node{}); err != nil {
log.Debugln("...failed")
}
log.Println("...success")
log.Debugln("...success")
return nil
}

@ -39,9 +39,9 @@ func CreateNode(nodeSpec *k3d.Node, runtimeChoice string) error {
runtime = k3dContainerd.Containerd{}
}
if err := runtime.CreateContainer(&k3d.Node{}); err != nil {
if err := runtime.CreateNode(&k3d.Node{}); err != nil {
log.Error(err)
}
log.Println("...success")
log.Debugln("...success")
return nil
}

@ -23,11 +23,12 @@ THE SOFTWARE.
package containerd
import (
log "github.com/sirupsen/logrus"
k3d "github.com/rancher/k3d/pkg/types"
log "github.com/sirupsen/logrus"
)
func (d Containerd) CreateContainer(nodeSpec *k3d.Node) error {
log.Println("containerd.CreateContainer...")
// CreateNode creates a new k3d node
func (d Containerd) CreateNode(nodeSpec *k3d.Node) error {
log.Debugln("containerd.CreateContainer...")
return nil
}

@ -34,10 +34,10 @@ import (
log "github.com/sirupsen/logrus"
)
// CreateContainer creates a new container
func (d Docker) CreateContainer(nodeSpec *k3d.Node) error {
log.Println("docker.CreateContainer...")
ctx := context.Background()
// CreateNode creates a new container
func (d Docker) CreateNode(nodeSpec *k3d.Node) error {
log.Debugln("docker.CreateNode...")
ctx := context.Background() // TODO: check how kind handles contexts
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return fmt.Errorf("Failed to create docker client. %+v", err)
@ -50,10 +50,10 @@ func (d Docker) CreateContainer(nodeSpec *k3d.Node) error {
resp, err := docker.ContainerCreate(ctx, &containerConfig, &container.HostConfig{}, &network.NetworkingConfig{}, "test")
if err != nil {
log.Error("couldn't create container")
log.Error("Couldn't create container")
return err
}
log.Println(resp.ID)
log.Infoln("Created", resp.ID)
return nil
}

@ -22,15 +22,33 @@ THE SOFTWARE.
package runtimes
import (
"fmt"
"github.com/rancher/k3d/pkg/runtimes/containerd"
"github.com/rancher/k3d/pkg/runtimes/docker"
k3d "github.com/rancher/k3d/pkg/types"
)
// Runtimes defines a map of implemented k3d runtimes
var Runtimes = map[string]Runtime{
"docker": docker.Docker{},
"containerd": containerd.Containerd{},
}
// Runtime defines an interface that can be implemented for various container runtime environments (docker, containerd, etc.)
type Runtime interface {
CreateContainer(*k3d.Node) error
CreateNode(*k3d.Node) error
// StartContainer() error
// ExecContainer() error
// StopContainer() error
// DeleteContainer() error
// GetContainerLogs() error
}
// GetRuntime checks, if a given name is represented by an implemented k3d runtime and returns it
func GetRuntime(rt string) (Runtime, error) {
if runtime, ok := Runtimes[rt]; ok {
return runtime, nil
}
return nil, fmt.Errorf("Runtime '%s' not supported", rt)
}

@ -38,9 +38,6 @@ const DefaultK3sImageRepo = "docker.io/rancher/k3s"
// DefaultObjectNamePrefix defines the name prefix for every object created by k3d
const DefaultObjectNamePrefix = "k3d-"
// Runtimes defines a list of available container runtimes that we can talk to to handle resources
var Runtimes = []string{"docker"}
// DefaultObjectLabels specifies a set of labels that will be attached to k3d objects by default
var DefaultObjectLabels = map[string]string{
"app": "k3d",

Loading…
Cancel
Save