diff --git a/cmd/create/create.go b/cmd/create/create.go index 27b73b32..6111a60a 100644 --- a/cmd/create/create.go +++ b/cmd/create/create.go @@ -42,6 +42,9 @@ func NewCmdCreate() *cobra.Command { cmd.AddCommand(NewCmdCreateCluster()) 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 ea553c95..46812fc3 100644 --- a/cmd/create/createCluster.go +++ b/cmd/create/createCluster.go @@ -52,7 +52,6 @@ func NewCmdCreateCluster() *cobra.Command { cmd.Flags().StringP("name", "n", types.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") - cmd.Flags().StringP("runtime", "r", "docker", "Choose a container runtime environment [docker, containerd]") // add subcommands diff --git a/cmd/create/createNode.go b/cmd/create/createNode.go index 3925185e..5493527b 100644 --- a/cmd/create/createNode.go +++ b/cmd/create/createNode.go @@ -24,6 +24,8 @@ package create import ( "github.com/spf13/cobra" + "github.com/rancher/k3d/pkg/cluster" + k3d "github.com/rancher/k3d/pkg/types" log "github.com/sirupsen/logrus" ) @@ -37,6 +39,11 @@ func NewCmdCreateNode() *cobra.Command { Long: `Create a new containerized k3s node (k3s in docker).`, Run: func(cmd *cobra.Command, args []string) { log.Debugln("create node called") + rt, err := cmd.Flags().GetString("runtime") + if err != nil { + log.Debugln("runtime not defined") + } + cluster.CreateNode(&k3d.Node{}, rt) }, } diff --git a/pkg/cluster/node.go b/pkg/cluster/node.go index 68e0dcea..fb4856a5 100644 --- a/pkg/cluster/node.go +++ b/pkg/cluster/node.go @@ -23,10 +23,25 @@ THE SOFTWARE. package cluster import ( - "github.com/rancher/k3d/pkg/types" + 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" ) // CreateNode creates a new containerized k3s node -func CreateNode(nodeSpec *types.Node) error { +func CreateNode(nodeSpec *k3d.Node, runtimeChoice string) error { + var runtime k3drt.Runtime + if runtimeChoice == "docker" { + runtime = k3dDocker.Docker{} + } else { + runtime = k3dContainerd.Containerd{} + } + + if err := runtime.CreateContainer(&k3d.Node{}); err != nil { + log.Error(err) + } + log.Println("...success") return nil } diff --git a/pkg/runtimes/docker/container.go b/pkg/runtimes/docker/container.go index 6382e8dd..fd2a7e83 100644 --- a/pkg/runtimes/docker/container.go +++ b/pkg/runtimes/docker/container.go @@ -27,13 +27,34 @@ import ( "fmt" "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/network" "github.com/docker/docker/client" k3d "github.com/rancher/k3d/pkg/types" 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() + docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + if err != nil { + return fmt.Errorf("Failed to create docker client. %+v", err) + } + + containerConfig := container.Config{ + Cmd: []string{"sh"}, + Image: "nginx", + } + + resp, err := docker.ContainerCreate(ctx, &containerConfig, &container.HostConfig{}, &network.NetworkingConfig{}, "test") + if err != nil { + log.Error("couldn't create container") + return err + } + log.Println(resp.ID) + return nil }