Persist Floating IP UUIDs for direct Neutron operations
This commit is contained in:
1 parent
89ac4d5bcd
commit
94124747ce
24 files changed
+918
-116
No files matched your search
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"cloudipvalidator/internal/db"
|
||||
"cloudipvalidator/internal/openstack"
|
||||
@@ -47,30 +48,37 @@ var activeQueueStates = map[string]bool{
|
||||
// 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 err != nil {
|
||||
return rejected(addr, "internal error: "+err.Error())
|
||||
}
|
||||
if reason != "" {
|
||||
return rejected(addr, reason)
|
||||
}
|
||||
if err := o.DB.ClaimDiagnostic(ctx, v.ValidatorID, addr, fip.ID, note); err != nil {
|
||||
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 err := o.OS.AssociateFloatingIP(ctx, fip.ID, v.OSPortID); err != nil {
|
||||
if _, rerr := o.DB.ReleaseDiagnostic(ctx, addr); rerr != nil {
|
||||
o.Log.Error("roll back diagnostic hold", "ip", addr, "err", rerr)
|
||||
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())
|
||||
}
|
||||
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)
|
||||
@@ -89,12 +97,16 @@ func (o *Orchestrator) prepareDiagnosticAttach(ctx context.Context, it Diagnosti
|
||||
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.lookupDiagnosticFIP(ctx, operationID, addr)
|
||||
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
|
||||
}
|
||||
return nil, nil, "cloud lookup failed: " + err.Error(), 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)
|
||||
@@ -142,8 +154,13 @@ func (o *Orchestrator) pickDiagnosticValidator(ctx context.Context, id string) (
|
||||
// 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))
|
||||
@@ -158,32 +175,53 @@ func (o *Orchestrator) DiagnosticDetach(ctx context.Context, addresses []string,
|
||||
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
|
||||
}
|
||||
fipID := h.FIPID
|
||||
if fip, err := o.OS.GetFloatingIPByAddress(ctx, addr); err == nil {
|
||||
fipID = fip.ID
|
||||
if fip.PortID != "" {
|
||||
if err := o.OS.DisassociateFloatingIP(ctx, fipID); err != nil {
|
||||
out = append(out, rejected(addr, "disassociate floating ip: "+err.Error()))
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else if !openstack.IsNotFound(err) {
|
||||
out = append(out, rejected(addr, "cloud lookup failed: "+err.Error()))
|
||||
continue
|
||||
}
|
||||
if _, err := o.DB.ReleaseDiagnostic(ctx, addr); err != nil {
|
||||
out = append(out, rejected(addr, "release: "+err.Error()))
|
||||
continue
|
||||
}
|
||||
o.diagnosticEvent(ctx, "diagnostic_detached", addr, h.ValidatorID, fipID, h.Note)
|
||||
o.Log.Info("diagnostic hold detached", "ip", addr, "validator", h.ValidatorID)
|
||||
out = append(out, DiagnosticResult{Address: addr, Status: DiagDetached, ValidatorID: h.ValidatorID, FIPID: fipID})
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user