diff --git a/cmd/create/create.go b/cmd/create/create.go index 6111a60a..f5b0d190 100644 --- a/cmd/create/create.go +++ b/cmd/create/create.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 diff --git a/cmd/create/createCluster.go b/cmd/create/createCluster.go index 46812fc3..00a856f5 100644 --- a/cmd/create/createCluster.go +++ b/cmd/create/createCluster.go @@ -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 +} diff --git a/cmd/root.go b/cmd/root.go index 1641d473..add9c79e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 { diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index cb544baf..8eefc06b 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -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 } diff --git a/pkg/cluster/node.go b/pkg/cluster/node.go index fb4856a5..0cea2a8a 100644 --- a/pkg/cluster/node.go +++ b/pkg/cluster/node.go @@ -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 } diff --git a/pkg/runtimes/containerd/container.go b/pkg/runtimes/containerd/container.go index 7ca020a9..5aace9c7 100644 --- a/pkg/runtimes/containerd/container.go +++ b/pkg/runtimes/containerd/container.go @@ -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 } diff --git a/pkg/runtimes/docker/container.go b/pkg/runtimes/docker/container.go index fd2a7e83..70145207 100644 --- a/pkg/runtimes/docker/container.go +++ b/pkg/runtimes/docker/container.go @@ -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 } diff --git a/pkg/runtimes/runtime.go b/pkg/runtimes/runtime.go index b7648247..8d47b5cd 100644 --- a/pkg/runtimes/runtime.go +++ b/pkg/runtimes/runtime.go @@ -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) +} diff --git a/pkg/types/types.go b/pkg/types/types.go index 28b458b5..bb787fcb 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -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",