240 lines
8.2 KiB
Go
240 lines
8.2 KiB
Go
package orchestrator
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"cloudipvalidator/internal/db"
|
|
"cloudipvalidator/internal/openstack"
|
|
)
|
|
|
|
// Diagnostic hold results (per address).
|
|
const (
|
|
DiagAttached = "attached"
|
|
DiagDetached = "detached"
|
|
DiagRejected = "rejected"
|
|
)
|
|
|
|
// DiagnosticItem is one address to attach; ValidatorID is optional (an idle
|
|
// validator is picked when empty).
|
|
type DiagnosticItem struct {
|
|
Address string
|
|
ValidatorID string
|
|
}
|
|
|
|
// DiagnosticResult is the outcome for one address.
|
|
type DiagnosticResult struct {
|
|
Address string `json:"address"`
|
|
Status string `json:"status"`
|
|
ValidatorID string `json:"validator_id,omitempty"`
|
|
FIPID string `json:"fip_id,omitempty"`
|
|
Reason string `json:"reason,omitempty"`
|
|
}
|
|
|
|
func rejected(address, reason string) DiagnosticResult {
|
|
return DiagnosticResult{Address: address, Status: DiagRejected, Reason: reason}
|
|
}
|
|
|
|
// activeQueueStates are the states in which an address is being checked.
|
|
var activeQueueStates = map[string]bool{
|
|
db.IPAssigningFIP: true, db.IPAwaitingSelfCheck: true, db.IPChecking: true, db.IPAggregating: true,
|
|
}
|
|
|
|
// DiagnosticAttach binds each address to a validator's port and holds it
|
|
// there until DiagnosticDetach. Partial success is normal: every address gets
|
|
// its own result.
|
|
func (o *Orchestrator) DiagnosticAttach(ctx context.Context, items []DiagnosticItem, note string) []DiagnosticResult {
|
|
start := time.Now()
|
|
out := make([]DiagnosticResult, 0, len(items))
|
|
for _, it := range items {
|
|
out = append(out, o.diagnosticAttachOne(ctx, it, note))
|
|
}
|
|
o.Log.Info("diagnostic attach request", "items", len(out), "duration_ms", time.Since(start).Milliseconds())
|
|
return out
|
|
}
|
|
|
|
func (o *Orchestrator) diagnosticAttachOne(ctx context.Context, it DiagnosticItem, note string) DiagnosticResult {
|
|
addr := it.Address
|
|
v, fip, reason, err := o.prepareDiagnosticAttach(ctx, it, "")
|
|
if reason != "" {
|
|
return rejected(addr, reason)
|
|
}
|
|
if err != nil {
|
|
return rejected(addr, "internal error: "+err.Error())
|
|
}
|
|
if err := o.DB.ClaimDiagnosticAtPort(ctx, v.ValidatorID, v.OSPortID, addr, fip.ID, note); err != nil {
|
|
return rejected(addr, "validator "+v.ValidatorID+" cannot be claimed: "+err.Error())
|
|
}
|
|
if fip.PortID == "" {
|
|
start := time.Now()
|
|
err := o.OS.AssociateFloatingIP(ctx, fip.ID, v.OSPortID)
|
|
o.logDiagnosticStage("", addr, "associate", start, err)
|
|
if err != nil {
|
|
if _, rerr := o.DB.ReleaseDiagnostic(ctx, addr); rerr != nil {
|
|
o.Log.Error("roll back diagnostic hold", "ip", addr, "err", rerr)
|
|
}
|
|
return rejected(addr, "associate floating ip: "+err.Error())
|
|
}
|
|
}
|
|
o.diagnosticEvent(ctx, "diagnostic_attached", addr, v.ValidatorID, fip.ID, note)
|
|
o.Log.Info("diagnostic hold attached", "ip", addr, "validator", v.ValidatorID)
|
|
return DiagnosticResult{Address: addr, Status: DiagAttached, ValidatorID: v.ValidatorID, FIPID: fip.ID}
|
|
}
|
|
|
|
func (o *Orchestrator) prepareDiagnosticAttach(ctx context.Context, it DiagnosticItem, operationID string) (*db.Validator, *openstack.FloatingIP, string, error) {
|
|
addr := it.Address
|
|
held, err := o.DB.HasDiagnosticHold(ctx, addr)
|
|
if err != nil {
|
|
return nil, nil, "", err
|
|
}
|
|
if held {
|
|
return nil, nil, "address is already on diagnostic", nil
|
|
}
|
|
if item, err := o.DB.GetIPByAddress(ctx, addr); err == nil && activeQueueStates[item.State] {
|
|
return nil, nil, "address is being checked (" + item.State + ")", nil
|
|
}
|
|
fip, err := o.resolveFloatingIP(ctx, operationID, addr)
|
|
if err != nil {
|
|
if openstack.IsNotFound(err) {
|
|
return nil, nil, "floating ip not found in the project", nil
|
|
}
|
|
reason := "cloud lookup failed: " + err.Error()
|
|
if openstack.IsRetryableRequestError(err) {
|
|
return nil, nil, reason, err
|
|
}
|
|
return nil, nil, reason, nil
|
|
}
|
|
|
|
v, reason := o.pickDiagnosticValidator(ctx, it.ValidatorID)
|
|
if v == nil {
|
|
return nil, nil, reason, nil
|
|
}
|
|
if fip.PortID != "" && fip.PortID != v.OSPortID {
|
|
return nil, nil, "floating ip is attached to another port", nil
|
|
}
|
|
return v, fip, "", nil
|
|
}
|
|
|
|
// pickDiagnosticValidator returns the requested validator if it is idle, or
|
|
// the first idle one with a port when none is requested.
|
|
func (o *Orchestrator) pickDiagnosticValidator(ctx context.Context, id string) (*db.Validator, string) {
|
|
if id != "" {
|
|
v, err := o.DB.GetValidator(ctx, id)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, "validator not found"
|
|
}
|
|
if err != nil {
|
|
return nil, "internal error: " + err.Error()
|
|
}
|
|
if v.State != db.ValidatorIdle {
|
|
return nil, fmt.Sprintf("validator is not idle (%s)", v.State)
|
|
}
|
|
if v.OSPortID == "" {
|
|
return nil, "validator has no port"
|
|
}
|
|
return v, ""
|
|
}
|
|
idle, err := o.DB.ListIdleValidators(ctx)
|
|
if err != nil {
|
|
return nil, "internal error: " + err.Error()
|
|
}
|
|
for i := range idle {
|
|
if idle[i].OSPortID != "" {
|
|
return &idle[i], ""
|
|
}
|
|
}
|
|
return nil, "no idle validators left"
|
|
}
|
|
|
|
// DiagnosticDetach unbinds the given addresses (all holds when all is true)
|
|
// and returns their validators to idle. A floating ip that is already free in
|
|
// the cloud is not an error; a cloud failure keeps the hold so it can be retried.
|
|
func (o *Orchestrator) DiagnosticDetach(ctx context.Context, addresses []string, all bool) []DiagnosticResult {
|
|
start := time.Now()
|
|
holds, err := o.DB.ListDiagnosticHolds(ctx)
|
|
defer func() {
|
|
o.Log.Info("diagnostic detach request", "items", len(addresses), "duration_ms", time.Since(start).Milliseconds())
|
|
}()
|
|
if err != nil {
|
|
o.Log.Error("list holds for diagnostic detach", "err", err)
|
|
return []DiagnosticResult{rejected("", "internal error: "+err.Error())}
|
|
}
|
|
byAddr := make(map[string]db.DiagnosticHold, len(holds))
|
|
for _, h := range holds {
|
|
byAddr[h.Address] = h
|
|
if all {
|
|
addresses = append(addresses, h.Address)
|
|
}
|
|
}
|
|
out := make([]DiagnosticResult, 0, len(addresses))
|
|
for _, addr := range addresses {
|
|
h, ok := byAddr[addr]
|
|
if !ok {
|
|
out = append(out, rejected(addr, "address is not on diagnostic"))
|
|
o.Log.Warn("diagnostic detach rejected", "ip", addr, "reason", "address is not on diagnostic")
|
|
continue
|
|
}
|
|
out = append(out, o.diagnosticDetachOne(ctx, h))
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (o *Orchestrator) diagnosticDetachOne(ctx context.Context, hold db.DiagnosticHold) DiagnosticResult {
|
|
fipID := hold.FIPID
|
|
if fipID == "" {
|
|
return rejected(hold.Address, "diagnostic hold has no recorded floating ip UUID")
|
|
}
|
|
fip, err := o.readFloatingIP(ctx, "", hold.Address, fipID)
|
|
if err != nil && !openstack.IsNotFound(err) {
|
|
return rejected(hold.Address, "cloud lookup failed: "+err.Error())
|
|
}
|
|
if err == nil && fip.PortID != "" {
|
|
expectedPort := hold.PortID
|
|
if expectedPort == "" {
|
|
validator, err := o.DB.GetValidator(ctx, hold.ValidatorID)
|
|
if err != nil {
|
|
return rejected(hold.Address, "cannot verify the diagnostic validator port: "+err.Error())
|
|
}
|
|
expectedPort = validator.OSPortID
|
|
}
|
|
if fip.PortID != expectedPort {
|
|
return rejected(hold.Address, "floating ip is attached to another port")
|
|
}
|
|
start := time.Now()
|
|
err := o.OS.DisassociateFloatingIP(ctx, fipID)
|
|
o.logDiagnosticStage("", hold.Address, "disassociate", start, err)
|
|
if err != nil && !openstack.IsNotFound(err) {
|
|
return rejected(hold.Address, "disassociate floating ip: "+err.Error())
|
|
}
|
|
}
|
|
start := time.Now()
|
|
_, err = o.DB.ReleaseDiagnostic(ctx, hold.Address)
|
|
o.logDiagnosticStage("", hold.Address, "detach_release", start, err)
|
|
if err != nil {
|
|
return rejected(hold.Address, "release: "+err.Error())
|
|
}
|
|
o.diagnosticEvent(ctx, "diagnostic_detached", hold.Address, hold.ValidatorID, fipID, hold.Note)
|
|
o.Log.Info("diagnostic hold detached", "ip", hold.Address, "validator", hold.ValidatorID)
|
|
return DiagnosticResult{Address: hold.Address, Status: DiagDetached, ValidatorID: hold.ValidatorID, FIPID: fipID}
|
|
}
|
|
|
|
// DiagnosticList returns the current holds.
|
|
func (o *Orchestrator) DiagnosticList(ctx context.Context) ([]db.DiagnosticHold, error) {
|
|
return o.DB.ListDiagnosticHolds(ctx)
|
|
}
|
|
|
|
func (o *Orchestrator) diagnosticEvent(ctx context.Context, typ, addr, validatorID, fipID, note string) {
|
|
o.event(ctx, "control-api", validatorID, nil, typ, diagnosticPayload(addr, validatorID, fipID, note))
|
|
}
|
|
|
|
func diagnosticPayload(addr, validatorID, fipID, note string) string {
|
|
payload, _ := json.Marshal(map[string]string{
|
|
"address": addr, "validator_id": validatorID, "fip_id": fipID, "note": note,
|
|
})
|
|
return string(payload)
|
|
}
|