parsecmd funcs

pull/227/head
iwilltry42 5 years ago
parent cd72e22dce
commit 1ec7a3e02a
  1. 29
      cmd/get/getCluster.go
  2. 11
      pkg/cluster/cluster.go
  3. 4
      pkg/runtimes/containerd/container.go
  4. 44
      pkg/runtimes/docker/container.go
  5. 1
      pkg/runtimes/runtime.go

@ -22,6 +22,9 @@ THE SOFTWARE.
package get
import (
"github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
@ -37,6 +40,12 @@ func NewCmdGetCluster() *cobra.Command {
Long: `Get cluster.`,
Run: func(cmd *cobra.Command, args []string) {
log.Debugln("get cluster called")
c, rt := parseGetClusterCmd(cmd, args)
if c == nil {
cluster.GetClusters(rt)
} else {
cluster.GetCluster(c, rt)
}
},
}
@ -45,3 +54,23 @@ func NewCmdGetCluster() *cobra.Command {
// done
return cmd
}
func parseGetClusterCmd(cmd *cobra.Command, args []string) (*k3d.Cluster, runtimes.Runtime) {
// --runtime
rt, err := cmd.Flags().GetString("runtime")
if err != nil {
log.Fatalln("No runtime specified")
}
runtime, err := runtimes.GetRuntime(rt)
if err != nil {
log.Fatalln(err)
}
if len(args) == 0 {
return nil, runtime
}
cluster := &k3d.Cluster{Name: args[0]} // TODO: validate name first?
return cluster, runtime
}

@ -73,3 +73,14 @@ func CreateCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error {
func DeleteCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) error {
return nil
}
// GetClusters returns a list of all existing clusters
func GetClusters(runtime k3drt.Runtime) ([]*k3d.Cluster, error) {
runtime.GetNodesByLabel(map[string]string{"role": "master"})
return []*k3d.Cluster{}, nil
}
// GetCluster returns an existing cluster
func GetCluster(cluster *k3d.Cluster, runtime k3drt.Runtime) (*k3d.Cluster, error) {
return cluster, nil
}

@ -100,3 +100,7 @@ func (d Containerd) DeleteNode(node *k3d.Node) error {
return nil
}
func (d Containerd) GetNodesByLabel(labels map[string]string) ([]*k3d.Node, error) {
return nil, nil
}

@ -26,6 +26,8 @@ import (
"context"
"fmt"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
@ -96,3 +98,45 @@ func (d Docker) DeleteNode(nodeSpec *k3d.Node) error {
log.Debugln("docker.DeleteNode...")
return removeContainer(nodeSpec.Name)
}
func (d Docker) GetNodesByLabel(labels map[string]string) ([]*k3d.Node, error) {
// (0) create docker client
ctx := context.Background()
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, fmt.Errorf("Failed to create docker client. %+v", err)
}
// (1) list containers which have the default k3d labels attached
filters := filters.NewArgs()
for k, v := range k3d.DefaultObjectLabels {
filters.Add("label", fmt.Sprintf("%s=%s", k, v))
}
for k, v := range labels {
filters.Add("label", fmt.Sprintf("%s=%s", k, v))
}
containers, err := docker.ContainerList(ctx, types.ContainerListOptions{
Filters: filters,
All: true,
})
if err != nil {
log.Errorln("Failed to list containers")
return nil, err
}
// (2) convert them to node structs
nodes := []*k3d.Node{}
for _, container := range containers {
node := &k3d.Node{
Name: container.Names[0],
Role: container.Labels["role"], // TODO: catch keyerror
Labels: container.Labels,
}
nodes = append(nodes, node)
}
return nodes, nil
}

@ -39,6 +39,7 @@ var Runtimes = map[string]Runtime{
type Runtime interface {
CreateNode(*k3d.Node) error
DeleteNode(*k3d.Node) error
GetNodesByLabel(map[string]string) ([]*k3d.Node, error)
// StartContainer() error
// ExecContainer() error
// StopContainer() error

Loading…
Cancel
Save