initial test to create container

pull/227/head
iwilltry42 5 years ago
parent 6b7de5cab6
commit 24ba46fc2a
  1. 3
      cmd/create/create.go
  2. 1
      cmd/create/createCluster.go
  3. 7
      cmd/create/createNode.go
  4. 19
      pkg/cluster/node.go
  5. 21
      pkg/runtimes/docker/container.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
}

@ -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

@ -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)
},
}

@ -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
}

@ -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
}

Loading…
Cancel
Save