|
| 1 | +package timestamp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math" |
| 6 | + |
| 7 | + "github.com/go-logr/logr" |
| 8 | + "github.com/lablabs/pod-deletion-cost-controller/internal/controller" |
| 9 | + appv1 "k8s.io/api/apps/v1" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + // TypeAnnotation is the algorithm type identifier for the timestamp handler. |
| 16 | + TypeAnnotation = "timestamp" |
| 17 | + |
| 18 | + // ManagedByAnnotation records which algorithm produced the current |
| 19 | + // pod-deletion-cost value. It lets the timestamp handler distinguish a value |
| 20 | + // it owns (safe to skip) from a stale value written by another algorithm |
| 21 | + // (e.g. "zone"), which it must recalculate when a Deployment switches type. |
| 22 | + ManagedByAnnotation = "pod-deletion-cost.lablabs.io/managed-by" |
| 23 | + |
| 24 | + // costBucketSeconds is the time granularity (in seconds) used to derive the |
| 25 | + // deletion cost from the pod creation timestamp. Bucketing by the minute keeps |
| 26 | + // the resulting int32 value far below the API server limit (overflow ~year 6053), |
| 27 | + // avoiding the Year-2038 problem a raw unix timestamp would hit. |
| 28 | + costBucketSeconds = 60 |
| 29 | +) |
| 30 | + |
| 31 | +// NewHandler creates a new timestamp-based Handler. |
| 32 | +func NewHandler(client client.Client) *Handler { |
| 33 | + return &Handler{client: client} |
| 34 | +} |
| 35 | + |
| 36 | +// Handler assigns pod-deletion-cost based on pod creation time. |
| 37 | +// Older pods get lower cost values and are deleted first during scale-down, |
| 38 | +// enabling natural fleet refresh through everyday scaling operations. |
| 39 | +type Handler struct { |
| 40 | + client client.Client |
| 41 | +} |
| 42 | + |
| 43 | +// AcceptType returns the algorithm type identifiers this handler responds to. |
| 44 | +func (h *Handler) AcceptType() []string { |
| 45 | + return []string{TypeAnnotation} |
| 46 | +} |
| 47 | + |
| 48 | +// Handle sets the pod-deletion-cost annotation from the pod's creation timestamp. |
| 49 | +// The cost is a pure function of CreationTimestamp (older pod → smaller timestamp → |
| 50 | +// lower cost → deleted first), so the resulting ordering is independent of when the |
| 51 | +// controller reconciles the pod. This matters because the annotation is written once |
| 52 | +// and never recalculated: deriving the cost from the pod age at reconcile time would |
| 53 | +// make a delayed or late-discovered pod receive a stale value and could invert the |
| 54 | +// intended oldest-first order after a controller restart or reconciliation delay. |
| 55 | +// |
| 56 | +// A pod is skipped only when the timestamp handler already owns its cost. If the pod |
| 57 | +// carries a cost written by a different algorithm (e.g. after a Deployment switches |
| 58 | +// from "zone" to "timestamp"), the value is recalculated so the pod is not left with |
| 59 | +// a stale, incompatible cost that would invert the deletion order. |
| 60 | +func (h *Handler) Handle(ctx context.Context, log logr.Logger, pod *corev1.Pod, _ *appv1.Deployment) error { |
| 61 | + if controller.IsDeleting(pod) { |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + if controller.HasPodDeletionCost(pod) && managedByTimestamp(pod) { |
| 66 | + return nil |
| 67 | + } |
| 68 | + |
| 69 | + cost := deletionCost(pod.CreationTimestamp.Unix()) |
| 70 | + |
| 71 | + patch := client.MergeFrom(pod.DeepCopy()) |
| 72 | + controller.ApplyPodDeletionCost(pod, cost) |
| 73 | + setManagedBy(pod) |
| 74 | + if err := h.client.Patch(ctx, pod, patch); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + log.WithValues(controller.PodDeletionCostAnnotation, cost).Info("updated") |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// deletionCost maps a pod creation unix timestamp to a pod-deletion-cost value. |
| 83 | +// The timestamp is bucketed by costBucketSeconds so the value stays within the |
| 84 | +// int32 range the Kubernetes API server enforces, then clamped defensively. |
| 85 | +// The mapping is monotonic: an earlier creation time always yields a lower or |
| 86 | +// equal cost, so older pods are never ranked above newer ones. |
| 87 | +func deletionCost(creationUnix int64) int { |
| 88 | + cost := creationUnix / costBucketSeconds |
| 89 | + |
| 90 | + if cost > math.MaxInt32 { |
| 91 | + return math.MaxInt32 |
| 92 | + } |
| 93 | + if cost < math.MinInt32 { |
| 94 | + return math.MinInt32 |
| 95 | + } |
| 96 | + return int(cost) |
| 97 | +} |
| 98 | + |
| 99 | +// managedByTimestamp reports whether the pod's current cost was set by this handler. |
| 100 | +func managedByTimestamp(pod *corev1.Pod) bool { |
| 101 | + if pod.Annotations == nil { |
| 102 | + return false |
| 103 | + } |
| 104 | + return pod.Annotations[ManagedByAnnotation] == TypeAnnotation |
| 105 | +} |
| 106 | + |
| 107 | +// setManagedBy stamps the pod as owned by the timestamp algorithm. |
| 108 | +func setManagedBy(pod *corev1.Pod) { |
| 109 | + if pod.Annotations == nil { |
| 110 | + pod.Annotations = make(map[string]string) |
| 111 | + } |
| 112 | + pod.Annotations[ManagedByAnnotation] = TypeAnnotation |
| 113 | +} |
0 commit comments