Skip to content

Modern Docker Management, Designed for Everyone

5.8 KB
Raw Blame
package swarm

import (
	"fmt"
	"time"

	"github.com/moby/moby/api/types/swarm"
)

type NodeAgentState string

type NodeAgentBindingKind string

const (
	NodeAgentStateNone       NodeAgentState = "none"
	NodeAgentStatePending    NodeAgentState = "pending"
	NodeAgentStateOffline    NodeAgentState = "offline"
	NodeAgentStateConnected  NodeAgentState = "connected"
	NodeAgentStateMismatched NodeAgentState = "mismatched"
	NodeAgentStateAmbiguous  NodeAgentState = "ambiguous"

	NodeAgentBindingKindLocal       NodeAgentBindingKind = "local"
	NodeAgentBindingKindEnvironment NodeAgentBindingKind = "environment"
	NodeAgentBindingKindDedicated   NodeAgentBindingKind = "dedicated"
)

type NodeAgentCandidate struct {
	EnvironmentID   string `json:"environmentId"`
	EnvironmentName string `json:"environmentName"`
	EnvironmentType string `json:"environmentType"`
}

type NodeAgentStatus struct {
	State            NodeAgentState        `json:"state"`
	BindingKind      *NodeAgentBindingKind `json:"bindingKind,omitempty"`
	EnvironmentID    *string               `json:"environmentId,omitempty"`
	EnvironmentName  *string               `json:"environmentName,omitempty"`
	EnvironmentType  *string               `json:"environmentType,omitempty"`
	Connected        *bool                 `json:"connected,omitempty"`
	LastHeartbeat    *time.Time            `json:"lastHeartbeat,omitempty"`
	LastPollAt       *time.Time            `json:"lastPollAt,omitempty"`
	ReportedNodeID   *string               `json:"reportedNodeId,omitempty"`
	ReportedHostname *string               `json:"reportedHostname,omitempty"`
	Candidates       []NodeAgentCandidate  `json:"candidates,omitempty"`
}

type NodeSummary struct {
	// ID is the unique identifier of the node.
	//
	// Required: true
	ID string `json:"id"`

	// Hostname is the node hostname.
	//
	// Required: true
	Hostname string `json:"hostname"`

	// Role indicates whether the node is a manager or worker.
	//
	// Required: true
	Role string `json:"role"`

	// Availability indicates if the node is active, paused, or drained.
	//
	// Required: true
	Availability string `json:"availability"`

	// Status is the node readiness state.
	//
	// Required: true
	Status string `json:"status"`

	// Address is the node address.
	//
	// Required: false
	Address string `json:"address,omitempty"`

	// ManagerStatus is the manager status string if applicable.
	//
	// Required: false
	ManagerStatus string `json:"managerStatus,omitempty"`

	// ManagerAddress is the manager's reachable swarm control-plane address.
	//
	// Required: false
	ManagerAddress string `json:"managerAddress,omitempty"`

	// Reachability is the manager reachability if applicable.
	//
	// Required: false
	Reachability string `json:"reachability,omitempty"`

	// Labels contains user-defined node labels from the node spec.
	//
	// Required: false
	Labels map[string]string `json:"labels,omitempty"`

	// SystemLabels contains read-only engine labels.
	//
	// Required: false
	SystemLabels map[string]string `json:"systemLabels,omitempty"`

	// EngineVersion is the Docker engine version.
	//
	// Required: false
	EngineVersion string `json:"engineVersion,omitempty"`

	// Platform is the node platform string.
	//
	// Required: false
	Platform string `json:"platform,omitempty"`

	// CreatedAt is when the node was created.
	//
	// Required: true
	CreatedAt time.Time `json:"createdAt"`

	// UpdatedAt is when the node was last updated.
	//
	// Required: true
	UpdatedAt time.Time `json:"updatedAt"`

	// Agent describes Arcane node-agent coverage for this swarm node.
	//
	// Required: true
	Agent NodeAgentStatus `json:"agent"`
}

type NodeUpdateRequest struct {
	// Version is the node version index to update.
	//
	// Required: false
	Version uint64 `json:"version,omitempty"`

	// Name overrides the node name annotation.
	//
	// Required: false
	Name *string `json:"name,omitzero"`

	// Labels updates node labels.
	//
	// Required: false
	Labels map[string]string `json:"labels,omitempty"`

	// Role updates node role (manager or worker).
	//
	// Required: false
	Role *swarm.NodeRole `json:"role,omitzero"`

	// Availability updates node availability (active, pause, drain).
	//
	// Required: false
	Availability *swarm.NodeAvailability `json:"availability,omitzero"`
}

// NewNodeSummary converts a Docker swarm node into the API-facing NodeSummary shape.
//
// It derives manager role labels, reachability, platform strings, and default
// node-agent state from the Docker SDK value so callers can return a stable,
// JSON-friendly representation.
//
// node is the Docker swarm node to summarize.
//
// Returns a NodeSummary populated from node.
func NewNodeSummary(node swarm.Node) NodeSummary {
	managerStatus := ""
	managerAddress := ""
	reachability := ""
	if node.ManagerStatus != nil {
		if node.ManagerStatus.Leader {
			managerStatus = "leader"
		} else {
			managerStatus = "manager"
		}
		reachability = string(node.ManagerStatus.Reachability)
		managerAddress = node.ManagerStatus.Addr
	}

	platform := ""
	if node.Description.Platform.OS != "" || node.Description.Platform.Architecture != "" {
		platform = fmt.Sprintf("%s/%s", node.Description.Platform.OS, node.Description.Platform.Architecture)
	}

	return NodeSummary{
		ID:             node.ID,
		Hostname:       node.Description.Hostname,
		Role:           string(node.Spec.Role),
		Availability:   string(node.Spec.Availability),
		Status:         string(node.Status.State),
		Address:        node.Status.Addr,
		ManagerStatus:  managerStatus,
		ManagerAddress: managerAddress,
		Reachability:   reachability,
		Labels:         node.Spec.Labels,
		SystemLabels:   node.Description.Engine.Labels,
		EngineVersion:  node.Description.Engine.EngineVersion,
		Platform:       platform,
		CreatedAt:      node.CreatedAt,
		UpdatedAt:      node.UpdatedAt,
		Agent: NodeAgentStatus{
			State: NodeAgentStateNone,
		},
	}
}