2018-10-23 11:05:44 -04:00
|
|
|
package node
|
2017-01-24 16:17:40 -05:00
|
|
|
|
|
|
|
import (
|
2017-05-08 13:48:24 -04:00
|
|
|
"encoding/base64"
|
2017-03-26 13:18:40 -04:00
|
|
|
"fmt"
|
2017-05-08 13:48:24 -04:00
|
|
|
"reflect"
|
2017-03-26 13:18:40 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command/inspect"
|
2017-01-24 16:17:40 -05:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2023-07-14 17:42:40 -04:00
|
|
|
"github.com/docker/docker/api/types/system"
|
2017-03-26 13:18:40 -04:00
|
|
|
units "github.com/docker/go-units"
|
2017-01-24 16:17:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-10-23 11:05:44 -04:00
|
|
|
defaultNodeTableFormat = "table {{.ID}} {{if .Self}}*{{else}} {{ end }}\t{{.Hostname}}\t{{.Status}}\t{{.Availability}}\t{{.ManagerStatus}}\t{{.EngineVersion}}"
|
|
|
|
nodeInspectPrettyTemplate formatter.Format = `ID: {{.ID}}
|
2017-03-26 13:18:40 -04:00
|
|
|
{{- if .Name }}
|
|
|
|
Name: {{.Name}}
|
|
|
|
{{- end }}
|
|
|
|
{{- if .Labels }}
|
|
|
|
Labels:
|
|
|
|
{{- range $k, $v := .Labels }}
|
|
|
|
- {{ $k }}{{if $v }}={{ $v }}{{ end }}
|
|
|
|
{{- end }}{{ end }}
|
|
|
|
Hostname: {{.Hostname}}
|
|
|
|
Joined at: {{.CreatedAt}}
|
|
|
|
Status:
|
|
|
|
State: {{.StatusState}}
|
|
|
|
{{- if .HasStatusMessage}}
|
|
|
|
Message: {{.StatusMessage}}
|
|
|
|
{{- end}}
|
|
|
|
Availability: {{.SpecAvailability}}
|
|
|
|
{{- if .Status.Addr}}
|
|
|
|
Address: {{.StatusAddr}}
|
|
|
|
{{- end}}
|
|
|
|
{{- if .HasManagerStatus}}
|
|
|
|
Manager Status:
|
|
|
|
Address: {{.ManagerStatusAddr}}
|
|
|
|
Raft Status: {{.ManagerStatusReachability}}
|
|
|
|
{{- if .IsManagerStatusLeader}}
|
|
|
|
Leader: Yes
|
|
|
|
{{- else}}
|
|
|
|
Leader: No
|
|
|
|
{{- end}}
|
|
|
|
{{- end}}
|
|
|
|
Platform:
|
|
|
|
Operating System: {{.PlatformOS}}
|
|
|
|
Architecture: {{.PlatformArchitecture}}
|
|
|
|
Resources:
|
|
|
|
CPUs: {{.ResourceNanoCPUs}}
|
|
|
|
Memory: {{.ResourceMemory}}
|
|
|
|
{{- if .HasEnginePlugins}}
|
|
|
|
Plugins:
|
|
|
|
{{- range $k, $v := .EnginePlugins }}
|
|
|
|
{{ $k }}:{{if $v }} {{ $v }}{{ end }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
Engine Version: {{.EngineVersion}}
|
|
|
|
{{- if .EngineLabels}}
|
|
|
|
Engine Labels:
|
|
|
|
{{- range $k, $v := .EngineLabels }}
|
|
|
|
- {{ $k }}{{if $v }}={{ $v }}{{ end }}
|
|
|
|
{{- end }}{{- end }}
|
2017-05-08 13:48:24 -04:00
|
|
|
{{- if .HasTLSInfo}}
|
|
|
|
TLS Info:
|
|
|
|
TrustRoot:
|
|
|
|
{{.TLSInfoTrustRoot}}
|
|
|
|
Issuer Subject: {{.TLSInfoCertIssuerSubject}}
|
|
|
|
Issuer Public Key: {{.TLSInfoCertIssuerPublicKey}}
|
2017-05-15 18:23:20 -04:00
|
|
|
{{- end}}`
|
2017-01-24 16:17:40 -05:00
|
|
|
nodeIDHeader = "ID"
|
2017-03-08 13:29:15 -05:00
|
|
|
selfHeader = ""
|
2017-01-24 16:17:40 -05:00
|
|
|
hostnameHeader = "HOSTNAME"
|
|
|
|
availabilityHeader = "AVAILABILITY"
|
|
|
|
managerStatusHeader = "MANAGER STATUS"
|
2018-01-29 16:44:26 -05:00
|
|
|
engineVersionHeader = "ENGINE VERSION"
|
2017-05-08 13:48:24 -04:00
|
|
|
tlsStatusHeader = "TLS STATUS"
|
2017-01-24 16:17:40 -05:00
|
|
|
)
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// NewFormat returns a Format for rendering using a node Context
|
|
|
|
func NewFormat(source string, quiet bool) formatter.Format {
|
2017-01-24 16:17:40 -05:00
|
|
|
switch source {
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.PrettyFormatKey:
|
2017-03-26 13:18:40 -04:00
|
|
|
return nodeInspectPrettyTemplate
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.TableFormatKey:
|
2017-01-24 16:17:40 -05:00
|
|
|
if quiet {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.DefaultQuietFormat
|
2017-01-24 16:17:40 -05:00
|
|
|
}
|
|
|
|
return defaultNodeTableFormat
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.RawFormatKey:
|
2017-01-24 16:17:40 -05:00
|
|
|
if quiet {
|
|
|
|
return `node_id: {{.ID}}`
|
|
|
|
}
|
|
|
|
return `node_id: {{.ID}}\nhostname: {{.Hostname}}\nstatus: {{.Status}}\navailability: {{.Availability}}\nmanager_status: {{.ManagerStatus}}\n`
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.Format(source)
|
2017-01-24 16:17:40 -05:00
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// FormatWrite writes the context
|
2023-07-14 17:42:40 -04:00
|
|
|
func FormatWrite(ctx formatter.Context, nodes []swarm.Node, info system.Info) error {
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2017-01-24 16:17:40 -05:00
|
|
|
for _, node := range nodes {
|
|
|
|
nodeCtx := &nodeContext{n: node, info: info}
|
|
|
|
if err := format(nodeCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
nodeCtx := nodeContext{}
|
|
|
|
nodeCtx.Header = formatter.SubHeaderContext{
|
2017-01-24 16:17:40 -05:00
|
|
|
"ID": nodeIDHeader,
|
2017-03-08 13:29:15 -05:00
|
|
|
"Self": selfHeader,
|
2017-01-24 16:17:40 -05:00
|
|
|
"Hostname": hostnameHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Status": formatter.StatusHeader,
|
2017-01-24 16:17:40 -05:00
|
|
|
"Availability": availabilityHeader,
|
|
|
|
"ManagerStatus": managerStatusHeader,
|
2018-01-29 16:44:26 -05:00
|
|
|
"EngineVersion": engineVersionHeader,
|
2017-05-08 13:48:24 -04:00
|
|
|
"TLSStatus": tlsStatusHeader,
|
2017-01-24 16:17:40 -05:00
|
|
|
}
|
|
|
|
return ctx.Write(&nodeCtx, render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2017-01-24 16:17:40 -05:00
|
|
|
n swarm.Node
|
2023-07-14 17:42:40 -04:00
|
|
|
info system.Info
|
2017-01-24 16:17:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nodeContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.MarshalJSON(c)
|
2017-01-24 16:17:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nodeContext) ID() string {
|
2017-03-08 13:29:15 -05:00
|
|
|
return c.n.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nodeContext) Self() bool {
|
|
|
|
return c.n.ID == c.info.Swarm.NodeID
|
2017-01-24 16:17:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nodeContext) Hostname() string {
|
|
|
|
return c.n.Description.Hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nodeContext) Status() string {
|
|
|
|
return command.PrettyPrint(string(c.n.Status.State))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nodeContext) Availability() string {
|
|
|
|
return command.PrettyPrint(string(c.n.Spec.Availability))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nodeContext) ManagerStatus() string {
|
|
|
|
reachability := ""
|
|
|
|
if c.n.ManagerStatus != nil {
|
|
|
|
if c.n.ManagerStatus.Leader {
|
|
|
|
reachability = "Leader"
|
|
|
|
} else {
|
|
|
|
reachability = string(c.n.ManagerStatus.Reachability)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return command.PrettyPrint(reachability)
|
|
|
|
}
|
2017-03-26 13:18:40 -04:00
|
|
|
|
2017-05-08 13:48:24 -04:00
|
|
|
func (c *nodeContext) TLSStatus() string {
|
|
|
|
if c.info.Swarm.Cluster == nil || reflect.DeepEqual(c.info.Swarm.Cluster.TLSInfo, swarm.TLSInfo{}) || reflect.DeepEqual(c.n.Description.TLSInfo, swarm.TLSInfo{}) {
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
if reflect.DeepEqual(c.n.Description.TLSInfo, c.info.Swarm.Cluster.TLSInfo) {
|
|
|
|
return "Ready"
|
|
|
|
}
|
|
|
|
return "Needs Rotation"
|
|
|
|
}
|
|
|
|
|
2018-01-29 16:44:26 -05:00
|
|
|
func (c *nodeContext) EngineVersion() string {
|
|
|
|
return c.n.Description.Engine.EngineVersion
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// InspectFormatWrite renders the context for a list of nodes
|
|
|
|
func InspectFormatWrite(ctx formatter.Context, refs []string, getRef inspect.GetRefFunc) error {
|
2017-03-26 13:18:40 -04:00
|
|
|
if ctx.Format != nodeInspectPrettyTemplate {
|
|
|
|
return inspect.Inspect(ctx.Output, refs, string(ctx.Format), getRef)
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2017-03-26 13:18:40 -04:00
|
|
|
for _, ref := range refs {
|
|
|
|
nodeI, _, err := getRef(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node, ok := nodeI.(swarm.Node)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("got wrong object to inspect :%v", ok)
|
|
|
|
}
|
|
|
|
if err := format(&nodeInspectContext{Node: node}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(&nodeInspectContext{}, render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeInspectContext struct {
|
|
|
|
swarm.Node
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.SubContext
|
2017-03-26 13:18:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) ID() string {
|
|
|
|
return ctx.Node.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) Name() string {
|
|
|
|
return ctx.Node.Spec.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) Labels() map[string]string {
|
|
|
|
return ctx.Node.Spec.Labels
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) Hostname() string {
|
|
|
|
return ctx.Node.Description.Hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) CreatedAt() string {
|
|
|
|
return command.PrettyPrint(ctx.Node.CreatedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) StatusState() string {
|
|
|
|
return command.PrettyPrint(ctx.Node.Status.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) HasStatusMessage() bool {
|
|
|
|
return ctx.Node.Status.Message != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) StatusMessage() string {
|
|
|
|
return command.PrettyPrint(ctx.Node.Status.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) SpecAvailability() string {
|
|
|
|
return command.PrettyPrint(ctx.Node.Spec.Availability)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) HasStatusAddr() bool {
|
|
|
|
return ctx.Node.Status.Addr != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) StatusAddr() string {
|
|
|
|
return ctx.Node.Status.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) HasManagerStatus() bool {
|
|
|
|
return ctx.Node.ManagerStatus != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) ManagerStatusAddr() string {
|
|
|
|
return ctx.Node.ManagerStatus.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) ManagerStatusReachability() string {
|
|
|
|
return command.PrettyPrint(ctx.Node.ManagerStatus.Reachability)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) IsManagerStatusLeader() bool {
|
|
|
|
return ctx.Node.ManagerStatus.Leader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) PlatformOS() string {
|
|
|
|
return ctx.Node.Description.Platform.OS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) PlatformArchitecture() string {
|
|
|
|
return ctx.Node.Description.Platform.Architecture
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) ResourceNanoCPUs() int {
|
|
|
|
if ctx.Node.Description.Resources.NanoCPUs == 0 {
|
|
|
|
return int(0)
|
|
|
|
}
|
|
|
|
return int(ctx.Node.Description.Resources.NanoCPUs) / 1e9
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) ResourceMemory() string {
|
|
|
|
if ctx.Node.Description.Resources.MemoryBytes == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return units.BytesSize(float64(ctx.Node.Description.Resources.MemoryBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) HasEnginePlugins() bool {
|
|
|
|
return len(ctx.Node.Description.Engine.Plugins) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) EnginePlugins() map[string]string {
|
|
|
|
pluginMap := map[string][]string{}
|
|
|
|
for _, p := range ctx.Node.Description.Engine.Plugins {
|
|
|
|
pluginMap[p.Type] = append(pluginMap[p.Type], p.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginNamesByType := map[string]string{}
|
|
|
|
for k, v := range pluginMap {
|
|
|
|
pluginNamesByType[k] = strings.Join(v, ", ")
|
|
|
|
}
|
|
|
|
return pluginNamesByType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) EngineLabels() map[string]string {
|
|
|
|
return ctx.Node.Description.Engine.Labels
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) EngineVersion() string {
|
|
|
|
return ctx.Node.Description.Engine.EngineVersion
|
|
|
|
}
|
2017-05-08 13:48:24 -04:00
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) HasTLSInfo() bool {
|
|
|
|
tlsInfo := ctx.Node.Description.TLSInfo
|
|
|
|
return !reflect.DeepEqual(tlsInfo, swarm.TLSInfo{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) TLSInfoTrustRoot() string {
|
|
|
|
return ctx.Node.Description.TLSInfo.TrustRoot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) TLSInfoCertIssuerPublicKey() string {
|
|
|
|
return base64.StdEncoding.EncodeToString(ctx.Node.Description.TLSInfo.CertIssuerPublicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *nodeInspectContext) TLSInfoCertIssuerSubject() string {
|
|
|
|
return base64.StdEncoding.EncodeToString(ctx.Node.Description.TLSInfo.CertIssuerSubject)
|
|
|
|
}
|