overall: add node status field

pull/328/head
iwilltry42 4 years ago
parent 6bc3f97a36
commit 1285fb90c4
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
  1. 4
      cmd/node/nodeList.go
  2. 56
      pkg/runtimes/docker/node.go
  3. 7
      pkg/runtimes/docker/translate.go
  4. 7
      pkg/types/types.go

@ -106,7 +106,7 @@ func printNodes(nodes []*k3d.Node, headersOff bool) {
defer tabwriter.Flush()
if !headersOff {
headers := []string{"NAME", "ROLE", "CLUSTER"} // TODO: add status
headers := []string{"NAME", "ROLE", "CLUSTER", "STATUS"}
_, err := fmt.Fprintf(tabwriter, "%s\n", strings.Join(headers, "\t"))
if err != nil {
log.Fatalln("Failed to print headers")
@ -118,6 +118,6 @@ func printNodes(nodes []*k3d.Node, headersOff bool) {
})
for _, node := range nodes {
fmt.Fprintf(tabwriter, "%s\t%s\t%s\n", strings.TrimPrefix(node.Name, "/"), string(node.Role), node.Labels[k3d.LabelClusterName])
fmt.Fprintf(tabwriter, "%s\t%s\t%s\t%s\n", strings.TrimPrefix(node.Name, "/"), string(node.Role), node.Labels[k3d.LabelClusterName], node.State.Status)
}
}

@ -72,9 +72,21 @@ func (d Docker) GetNodesByLabel(ctx context.Context, labels map[string]string) (
// (1) convert them to node structs
nodes := []*k3d.Node{}
for _, container := range containers {
node, err := TranslateContainerToNode(&container)
var node *k3d.Node
var err error
containerDetails, err := getContainerDetails(ctx, container.ID)
if err != nil {
return nil, err
log.Warnf("Failed to get details for container %s", container.Names[0])
node, err = TranslateContainerToNode(&container)
if err != nil {
return nil, err
}
} else {
node, err = TranslateContainerDetailsToNode(containerDetails)
if err != nil {
return nil, err
}
}
nodes = append(nodes, node)
}
@ -212,6 +224,46 @@ func (d Docker) GetNode(ctx context.Context, node *k3d.Node) (*k3d.Node, error)
}
// GetNodeStatus returns the status of a node (Running, Started, etc.)
func (d Docker) GetNodeStatus(ctx context.Context, node *k3d.Node) (bool, string, error) {
stateString := ""
running := false
// get the container for the given node
container, err := getNodeContainer(ctx, node)
if err != nil {
return running, stateString, err
}
// create docker client
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Errorln("Failed to create docker client")
return running, stateString, err
}
defer docker.Close()
containerInspectResponse, err := docker.ContainerInspect(ctx, container.ID)
if err != nil {
return running, stateString, err
}
running = containerInspectResponse.ContainerJSONBase.State.Running
stateString = containerInspectResponse.ContainerJSONBase.State.Status
return running, stateString, nil
}
// NodeIsRunning tells the caller if a given node is in "running" state
func (d Docker) NodeIsRunning(ctx context.Context, node *k3d.Node) (bool, error) {
isRunning, _, err := d.GetNodeStatus(ctx, node)
if err != nil {
return false, err
}
return isRunning, nil
}
// GetNodeLogs returns the logs from a given node
func (d Docker) GetNodeLogs(ctx context.Context, node *k3d.Node, since time.Time) (io.ReadCloser, error) {
// get the container for the given node

@ -175,6 +175,12 @@ func TranslateContainerDetailsToNode(containerDetails types.ContainerJSON) (*k3d
}
}
// status
nodeState := k3d.NodeState{
Running: containerDetails.ContainerJSONBase.State.Running,
Status: containerDetails.ContainerJSONBase.State.Status,
}
node := &k3d.Node{
Name: strings.TrimPrefix(containerDetails.Name, "/"), // container name with leading '/' cut off
Role: k3d.NodeRoles[containerDetails.Config.Labels[k3d.LabelRole]],
@ -189,6 +195,7 @@ func TranslateContainerDetailsToNode(containerDetails types.ContainerJSON) (*k3d
Network: clusterNetwork,
ServerOpts: serverOpts,
AgentOpts: k3d.AgentOpts{},
State: nodeState,
}
return node, nil
}

@ -228,6 +228,7 @@ type Node struct {
Network string // filled automatically
ServerOpts ServerOpts `yaml:"server_opts" json:"serverOpts,omitempty"`
AgentOpts AgentOpts `yaml:"agent_opts" json:"agentOpts,omitempty"`
State NodeState // filled automatically
}
// ServerOpts describes some additional server role specific opts
@ -259,3 +260,9 @@ type AgentOpts struct{}
func GetDefaultObjectName(name string) string {
return fmt.Sprintf("%s-%s", DefaultObjectNamePrefix, name)
}
// NodeState describes the current state of a node
type NodeState struct {
Running bool
Status string
}

Loading…
Cancel
Save