overall: add node status field
This commit is contained in:
parent
6bc3f97a36
commit
1285fb90c4
@ -106,7 +106,7 @@ func printNodes(nodes []*k3d.Node, headersOff bool) {
|
|||||||
defer tabwriter.Flush()
|
defer tabwriter.Flush()
|
||||||
|
|
||||||
if !headersOff {
|
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"))
|
_, err := fmt.Fprintf(tabwriter, "%s\n", strings.Join(headers, "\t"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed to print headers")
|
log.Fatalln("Failed to print headers")
|
||||||
@ -118,6 +118,6 @@ func printNodes(nodes []*k3d.Node, headersOff bool) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
for _, node := range nodes {
|
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,10 +72,22 @@ func (d Docker) GetNodesByLabel(ctx context.Context, labels map[string]string) (
|
|||||||
// (1) convert them to node structs
|
// (1) convert them to node structs
|
||||||
nodes := []*k3d.Node{}
|
nodes := []*k3d.Node{}
|
||||||
for _, container := range containers {
|
for _, container := range containers {
|
||||||
node, err := TranslateContainerToNode(&container)
|
var node *k3d.Node
|
||||||
|
var err error
|
||||||
|
|
||||||
|
containerDetails, err := getContainerDetails(ctx, container.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("Failed to get details for container %s", container.Names[0])
|
||||||
|
node, err = TranslateContainerToNode(&container)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
node, err = TranslateContainerDetailsToNode(containerDetails)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
nodes = append(nodes, node)
|
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
|
// GetNodeLogs returns the logs from a given node
|
||||||
func (d Docker) GetNodeLogs(ctx context.Context, node *k3d.Node, since time.Time) (io.ReadCloser, error) {
|
func (d Docker) GetNodeLogs(ctx context.Context, node *k3d.Node, since time.Time) (io.ReadCloser, error) {
|
||||||
// get the container for the given node
|
// 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{
|
node := &k3d.Node{
|
||||||
Name: strings.TrimPrefix(containerDetails.Name, "/"), // container name with leading '/' cut off
|
Name: strings.TrimPrefix(containerDetails.Name, "/"), // container name with leading '/' cut off
|
||||||
Role: k3d.NodeRoles[containerDetails.Config.Labels[k3d.LabelRole]],
|
Role: k3d.NodeRoles[containerDetails.Config.Labels[k3d.LabelRole]],
|
||||||
@ -189,6 +195,7 @@ func TranslateContainerDetailsToNode(containerDetails types.ContainerJSON) (*k3d
|
|||||||
Network: clusterNetwork,
|
Network: clusterNetwork,
|
||||||
ServerOpts: serverOpts,
|
ServerOpts: serverOpts,
|
||||||
AgentOpts: k3d.AgentOpts{},
|
AgentOpts: k3d.AgentOpts{},
|
||||||
|
State: nodeState,
|
||||||
}
|
}
|
||||||
return node, nil
|
return node, nil
|
||||||
}
|
}
|
||||||
|
@ -228,6 +228,7 @@ type Node struct {
|
|||||||
Network string // filled automatically
|
Network string // filled automatically
|
||||||
ServerOpts ServerOpts `yaml:"server_opts" json:"serverOpts,omitempty"`
|
ServerOpts ServerOpts `yaml:"server_opts" json:"serverOpts,omitempty"`
|
||||||
AgentOpts AgentOpts `yaml:"agent_opts" json:"agentOpts,omitempty"`
|
AgentOpts AgentOpts `yaml:"agent_opts" json:"agentOpts,omitempty"`
|
||||||
|
State NodeState // filled automatically
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServerOpts describes some additional server role specific opts
|
// ServerOpts describes some additional server role specific opts
|
||||||
@ -259,3 +260,9 @@ type AgentOpts struct{}
|
|||||||
func GetDefaultObjectName(name string) string {
|
func GetDefaultObjectName(name string) string {
|
||||||
return fmt.Sprintf("%s-%s", DefaultObjectNamePrefix, name)
|
return fmt.Sprintf("%s-%s", DefaultObjectNamePrefix, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NodeState describes the current state of a node
|
||||||
|
type NodeState struct {
|
||||||
|
Running bool
|
||||||
|
Status string
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user