| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/hostclaim" |
| 9 | "github.com/carlosframework/platform/internal/provision" |
| 10 | ) |
| 11 | |
| 12 | func (c Controller) RetireRecord(ctx context.Context, ref provision.RetireRef) error { |
| 13 | if err := c.ready(); err != nil { |
| 14 | return err |
| 15 | } |
| 16 | key := provision.RecordKey(ref.Account, ref.App, ref.Host) |
| 17 | if ref.Family == "sidecar" { |
| 18 | key = provision.SidecarRecordKey(ref.Account, ref.App, ref.Name) |
| 19 | } |
| 20 | rec, rerr := ReadRecord(ctx, c.Store, key) |
| 21 | if rerr != nil && !errors.Is(rerr, os.ErrNotExist) { |
| 22 | return rerr |
| 23 | } |
| 24 | obs, err := hostclaim.Read(ctx, c.Store, ref.Host) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | if obs.Absent { |
| 29 | if errors.Is(rerr, os.ErrNotExist) { |
| 30 | return nil |
| 31 | } |
| 32 | return ErrPending |
| 33 | } |
| 34 | h := obs.Claim.Holder |
| 35 | if h == nil { |
| 36 | if rerr == nil { |
| 37 | _, err = RemoveOrphan(ctx, c.Store, rec) |
| 38 | return err |
| 39 | } |
| 40 | if obs.Claim.Free() { |
| 41 | return nil |
| 42 | } |
| 43 | return ErrPending |
| 44 | } |
| 45 | if h.Key != key || h.Family != ref.Family || h.Account != ref.Account || h.App != ref.App || (ref.Incarnation != "" && ref.Incarnation != h.Incarnation) { |
| 46 | return ErrIdentity |
| 47 | } |
| 48 | if rerr == nil && !matches(rec, h) { |
| 49 | return ErrIdentity |
| 50 | } |
| 51 | switch obs.Claim.Op.Kind { |
| 52 | case hostclaim.KindServing: |
| 53 | if rerr != nil { |
| 54 | return ErrPending |
| 55 | } // Old-console deletion needs operator recovery. |
| 56 | obs, err = c.BeginRetire(ctx, key, ref.By, h.Incarnation) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | return c.Retire(ctx, ref.Host, obs.Claim.Op.ID) |
| 61 | case hostclaim.KindDeleting: |
| 62 | return c.Retire(ctx, ref.Host, obs.Claim.Op.ID) |
| 63 | case hostclaim.KindParking: |
| 64 | if len(obs.Claim.Obligations) != 0 { |
| 65 | return ErrPending |
| 66 | } |
| 67 | return c.Free(ctx, ref.Host, obs.Claim.Op.ID) |
| 68 | default: |
| 69 | return ErrPending // A purge is driven by its journal, never this path. |
| 70 | } |
| 71 | } |
| 72 | |