package nodeinsight // Agent update actions, stable for the agent and the dashboard. const ( UpdateNone = "none" UpdateInstall = "update" UpdateRollback = "rollback" UpdateHold = "hold" ) // UpdateFacts is what the control plane knows when an agent asks for its target. type UpdateFacts struct { Channel string ControlPlane string AgentVersion string AgentOnline bool PreviousGood string // RolledBackFrom holds the control plane version this node was rolled back from. RolledBackFrom string HealthFailures int // MaxHealthFailures is the runtime setting after which an update is undone. MaxHealthFailures int } // UpdateDecision is the answer to an agent update check. The agent only ever // installs a signed release it fetches itself; this never carries a command. type UpdateDecision struct { Action string `json:"action"` Channel string `json:"channel"` Target string `json:"target_version,omitempty"` Reason string `json:"reason"` // AutoApply mirrors the nodes.agent_auto_update runtime setting: when true // the agent restarts into the new version without an operator flag. AutoApply bool `json:"auto_apply"` // Waiting says why a pending update is not applying on its own (window, // hold, channel, automatic updates off); empty when nothing waits. Waiting string `json:"waiting,omitempty"` // Held is true when this node has a per-node hold. Held bool `json:"held,omitempty"` } // DecideUpdate keeps agents on the control plane version of the control plane // channel and rolls back to the last good version when health keeps failing. func DecideUpdate(f UpdateFacts) UpdateDecision { d := UpdateDecision{Action: UpdateNone, Channel: f.Channel} if f.MaxHealthFailures > 0 && f.HealthFailures >= f.MaxHealthFailures && f.PreviousGood != "" { d.Action, d.Target, d.Reason = UpdateRollback, f.PreviousGood, "health checks failed after the last update" return d } switch Classify(f.AgentVersion, f.ControlPlane) { case SkewBehind: if f.RolledBackFrom != "" && f.RolledBackFrom == f.ControlPlane { d.Action, d.Reason = UpdateHold, "rolled back from "+f.ControlPlane+"; held until the target changes or the hold is cleared" return d } if !f.AgentOnline { d.Action, d.Reason = UpdateHold, "agent is offline" return d } d.Action, d.Target, d.Reason = UpdateInstall, f.ControlPlane, "agent is behind the control plane" case SkewUnknown: d.Reason = "agent version is unknown" default: d.Reason = "agent matches the control plane" } return d } // LabelAgentCanary marks nodes whose agents update first; every other node // waits until all of them run the target version and are online. const LabelAgentCanary = "levelrail.io/agent-canary" // CanaryNode is one canary node's update state. type CanaryNode struct { ID string Online bool Version string } // CanaryWait says why a non-canary node must wait for the canaries, or "" // when it may update. Canary nodes themselves never wait on each other. func CanaryWait(selfIsCanary bool, canaries []CanaryNode, target string) string { if selfIsCanary || len(canaries) == 0 || target == "" { return "" } for _, c := range canaries { if !c.Online { return "canary node " + c.ID + " is offline" } if sk := Classify(c.Version, target); sk == SkewBehind || sk == SkewUnknown { return "canary node " + c.ID + " has not updated to " + target + " yet" } } return "" }