|
| 1 | +/* |
| 2 | +Copyright 2024 The etcd-operator Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +*/ |
| 10 | + |
| 11 | +package main |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "fmt" |
| 16 | + "io" |
| 17 | + "time" |
| 18 | + |
| 19 | + appsv1 "k8s.io/api/apps/v1" |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + policyv1 "k8s.io/api/policy/v1" |
| 22 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + "k8s.io/client-go/dynamic" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | + |
| 28 | + lll "github.com/cozystack/etcd-operator/api/v1alpha2" |
| 29 | + "github.com/cozystack/etcd-operator/controllers" |
| 30 | + "github.com/cozystack/etcd-operator/internal/migrate" |
| 31 | +) |
| 32 | + |
| 33 | +// applyAdoption executes one cluster's in-place adoption. The etcd pods are |
| 34 | +// never restarted: only object ownership, labels, member annotations and CRs |
| 35 | +// change. Every step is idempotent, so an interrupted run is completed by |
| 36 | +// re-running the tool. |
| 37 | +// |
| 38 | +// Ordering is load-bearing in three places: |
| 39 | +// |
| 40 | +// - The new-API CRs are created with their status prefilled before the |
| 41 | +// user scales the new operator up (the tool runs with both operators |
| 42 | +// down), so the cluster controller's bootstrap branch never fires. |
| 43 | +// - The legacy headless Service is owner-referenced to the adopted members |
| 44 | +// BEFORE the legacy CRs are deleted — otherwise the Service is briefly |
| 45 | +// sole-owned by a now-missing object and GC could reap it. |
| 46 | +// - The legacy StatefulSet is orphan-deleted (and its deletion awaited) |
| 47 | +// BEFORE pod owner references are rewritten — while it exists, its |
| 48 | +// controller would adopt the pods right back. |
| 49 | +func applyAdoption(ctx context.Context, c client.Client, dyn dynamic.Interface, p *migrate.ResourcePlan, out io.Writer) error { |
| 50 | + a := p.Adoption |
| 51 | + cluster := p.Target.(*lll.EtcdCluster) |
| 52 | + ns := p.Namespace |
| 53 | + |
| 54 | + // 1. Create the new-API cluster (+ companion Secret) with prefilled |
| 55 | + // status. Done first: the prefilled status.clusterID keeps the bootstrap |
| 56 | + // branch from ever firing, and the live cluster UID owns the headless |
| 57 | + // Service recreated in step 6. |
| 58 | + for _, extra := range p.Extras { |
| 59 | + if err := c.Create(ctx, extra); err != nil && !apierrors.IsAlreadyExists(err) { |
| 60 | + return fmt.Errorf("create %s %s/%s: %w", |
| 61 | + extra.GetObjectKind().GroupVersionKind().Kind, ns, extra.GetName(), err) |
| 62 | + } |
| 63 | + } |
| 64 | + if err := c.Create(ctx, cluster); err != nil && !apierrors.IsAlreadyExists(err) { |
| 65 | + return fmt.Errorf("create EtcdCluster: %w", err) |
| 66 | + } |
| 67 | + liveCluster := &lll.EtcdCluster{} |
| 68 | + if err := c.Get(ctx, types.NamespacedName{Namespace: ns, Name: cluster.Name}, liveCluster); err != nil { |
| 69 | + return fmt.Errorf("re-read EtcdCluster: %w", err) |
| 70 | + } |
| 71 | + // Fill-if-empty: a re-run must not clobber status once the new operator |
| 72 | + // has taken over (both operators are down during a normal run, but stay |
| 73 | + // safe against misuse). |
| 74 | + if liveCluster.Status.ClusterID == "" { |
| 75 | + liveCluster.Status = a.ClusterStatus |
| 76 | + if err := c.Status().Update(ctx, liveCluster); err != nil { |
| 77 | + return fmt.Errorf("prefill EtcdCluster status: %w", err) |
| 78 | + } |
| 79 | + fmt.Fprintf(out, " created EtcdCluster %q (clusterID=%s prefilled — bootstrap will not fire)\n", |
| 80 | + cluster.Name, a.ClusterStatus.ClusterID) |
| 81 | + } |
| 82 | + |
| 83 | + // 2. Create the per-pod EtcdMembers (+ status prefill) and capture their |
| 84 | + // live UIDs — required before owner-referencing the legacy headless |
| 85 | + // Service to them in step 3. |
| 86 | + liveMembers := make([]*lll.EtcdMember, len(a.Members)) |
| 87 | + for i, ma := range a.Members { |
| 88 | + if err := c.Create(ctx, ma.Member); err != nil && !apierrors.IsAlreadyExists(err) { |
| 89 | + return fmt.Errorf("create EtcdMember %q: %w", ma.Member.Name, err) |
| 90 | + } |
| 91 | + liveMember := &lll.EtcdMember{} |
| 92 | + if err := c.Get(ctx, types.NamespacedName{Namespace: ns, Name: ma.Member.Name}, liveMember); err != nil { |
| 93 | + return fmt.Errorf("re-read EtcdMember %q: %w", ma.Member.Name, err) |
| 94 | + } |
| 95 | + if liveMember.Status.MemberID == "" { |
| 96 | + liveMember.Status = ma.Status |
| 97 | + if err := c.Status().Update(ctx, liveMember); err != nil { |
| 98 | + return fmt.Errorf("prefill EtcdMember %q status: %w", ma.Member.Name, err) |
| 99 | + } |
| 100 | + } |
| 101 | + liveMembers[i] = liveMember |
| 102 | + } |
| 103 | + |
| 104 | + // 3. Point the legacy headless Service's ownerReferences at the adopted |
| 105 | + // members (replacing the legacy controller owner). Kubernetes deletes a |
| 106 | + // dependent once all its owners are gone, so with one owner ref per |
| 107 | + // adopted member the Service survives while any adopted member remains |
| 108 | + // and is auto-GC'd when the last one rolls away. Replacement (native) |
| 109 | + // members are not owners, so they never keep it alive. Done BEFORE the |
| 110 | + // legacy-CR deletion to avoid a premature-GC race. |
| 111 | + if a.HeadlessServiceName != "" { |
| 112 | + if err := pointServiceAtMembers(ctx, c, ns, a.HeadlessServiceName, liveMembers); err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + fmt.Fprintf(out, " owner-referenced legacy headless Service %q to %d adopted member(s) (auto-GCs as they roll)\n", |
| 116 | + a.HeadlessServiceName, len(liveMembers)) |
| 117 | + } |
| 118 | + |
| 119 | + // 4. Dismantle the legacy control plane, keeping the data plane. Orphan |
| 120 | + // propagation everywhere so the pods/PVCs/Services survive. |
| 121 | + if p.DeleteRef != nil { |
| 122 | + orphan := metav1.DeletePropagationOrphan |
| 123 | + err := dyn.Resource(p.DeleteRef.GVR).Namespace(ns). |
| 124 | + Delete(ctx, p.DeleteRef.Name, metav1.DeleteOptions{PropagationPolicy: &orphan}) |
| 125 | + if err != nil && !apierrors.IsNotFound(err) { |
| 126 | + return fmt.Errorf("orphan-delete legacy EtcdCluster: %w", err) |
| 127 | + } |
| 128 | + fmt.Fprintf(out, " orphan-deleted legacy EtcdCluster (children survive)\n") |
| 129 | + } |
| 130 | + |
| 131 | + sts := &appsv1.StatefulSet{ObjectMeta: metav1.ObjectMeta{Namespace: ns, Name: a.StatefulSetName}} |
| 132 | + orphan := metav1.DeletePropagationOrphan |
| 133 | + if err := c.Delete(ctx, sts, &client.DeleteOptions{PropagationPolicy: &orphan}); err != nil && !apierrors.IsNotFound(err) { |
| 134 | + return fmt.Errorf("orphan-delete legacy StatefulSet: %w", err) |
| 135 | + } |
| 136 | + if err := waitGone(ctx, c, types.NamespacedName{Namespace: ns, Name: a.StatefulSetName}, &appsv1.StatefulSet{}, 2*time.Minute); err != nil { |
| 137 | + return fmt.Errorf("await StatefulSet deletion: %w", err) |
| 138 | + } |
| 139 | + fmt.Fprintf(out, " orphan-deleted legacy StatefulSet %q (pods survive)\n", a.StatefulSetName) |
| 140 | + |
| 141 | + cm := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Namespace: ns, Name: a.ConfigMapName}} |
| 142 | + if err := c.Delete(ctx, cm); err != nil && !apierrors.IsNotFound(err) { |
| 143 | + return fmt.Errorf("delete legacy cluster-state ConfigMap: %w", err) |
| 144 | + } |
| 145 | + // The new operator emits its own PDB under the same name; remove the |
| 146 | + // legacy one so the two never select the same pods concurrently. |
| 147 | + pdb := &policyv1.PodDisruptionBudget{ObjectMeta: metav1.ObjectMeta{Namespace: ns, Name: a.PDBName}} |
| 148 | + if err := c.Delete(ctx, pdb); err != nil && !apierrors.IsNotFound(err) { |
| 149 | + return fmt.Errorf("delete legacy PodDisruptionBudget: %w", err) |
| 150 | + } |
| 151 | + |
| 152 | + // 5. Re-own the pods and PVCs to their EtcdMembers — only now that the |
| 153 | + // StatefulSet is gone and its controller can no longer fight us. |
| 154 | + for i, ma := range a.Members { |
| 155 | + if err := adoptPod(ctx, c, ns, ma.Member.Name, cluster.Name, liveMembers[i]); err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + if err := adoptPVC(ctx, c, ns, ma.PVCName, cluster.Name, liveMembers[i]); err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + fmt.Fprintf(out, " adopted member %q (pod + PVC re-owned, memberID=%s)\n", ma.Member.Name, ma.Status.MemberID) |
| 162 | + } |
| 163 | + |
| 164 | + // 6. Client-Service cutover. The legacy client Service is named after the |
| 165 | + // cluster, which collides with the operator's native headless Service. |
| 166 | + // Delete it and immediately recreate a headless Service of the same name |
| 167 | + // (owned by the new cluster) so the DNS name keeps resolving with the |
| 168 | + // minimum possible gap, rather than leaving the window open until the |
| 169 | + // operator's first reconcile. |
| 170 | + if err := cutoverHeadlessService(ctx, c, ns, cluster.Name, liveCluster); err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + fmt.Fprintf(out, " cut over Service %q to the operator's native headless Service\n", cluster.Name) |
| 174 | + |
| 175 | + return nil |
| 176 | +} |
| 177 | + |
| 178 | +// pointServiceAtMembers replaces a Service's ownerReferences with one |
| 179 | +// non-controller, non-blocking entry per EtcdMember. A full Update (not a |
| 180 | +// merge patch) is used deliberately: the legacy controller owner reference |
| 181 | +// must be STRIPPED, and a strategic merge patch keyed on owner UID would |
| 182 | +// merge the new refs in alongside the stale one rather than replacing the |
| 183 | +// list. Idempotent — a re-run rewrites the same refs. |
| 184 | +func pointServiceAtMembers(ctx context.Context, c client.Client, ns, name string, members []*lll.EtcdMember) error { |
| 185 | + svc := &corev1.Service{} |
| 186 | + if err := c.Get(ctx, types.NamespacedName{Namespace: ns, Name: name}, svc); err != nil { |
| 187 | + if apierrors.IsNotFound(err) { |
| 188 | + // Already GC'd by a prior complete run (all adopted members |
| 189 | + // rolled away) — nothing to keep alive. |
| 190 | + return nil |
| 191 | + } |
| 192 | + return fmt.Errorf("read legacy headless Service %q: %w", name, err) |
| 193 | + } |
| 194 | + gvk := lll.GroupVersion.WithKind("EtcdMember") |
| 195 | + refs := make([]metav1.OwnerReference, 0, len(members)) |
| 196 | + for _, m := range members { |
| 197 | + refs = append(refs, metav1.OwnerReference{ |
| 198 | + APIVersion: gvk.GroupVersion().String(), |
| 199 | + Kind: gvk.Kind, |
| 200 | + Name: m.Name, |
| 201 | + UID: m.UID, |
| 202 | + Controller: ptrTo(false), |
| 203 | + BlockOwnerDeletion: ptrTo(false), |
| 204 | + }) |
| 205 | + } |
| 206 | + svc.OwnerReferences = refs |
| 207 | + if err := c.Update(ctx, svc); err != nil { |
| 208 | + return fmt.Errorf("owner-reference legacy headless Service %q to members: %w", name, err) |
| 209 | + } |
| 210 | + return nil |
| 211 | +} |
| 212 | + |
| 213 | +// cutoverHeadlessService ensures `name` is the operator's native headless |
| 214 | +// Service, owned by the new EtcdCluster. If a ClusterIP Service already holds |
| 215 | +// the name (the legacy client Service, whose name collides with the native |
| 216 | +// headless), it is deleted and recreated headless — clusterIP is immutable, |
| 217 | +// so an in-place flip is impossible. Idempotent: an already-headless Service |
| 218 | +// at the name is left untouched. |
| 219 | +func cutoverHeadlessService(ctx context.Context, c client.Client, ns, name string, owner *lll.EtcdCluster) error { |
| 220 | + svc := &corev1.Service{} |
| 221 | + err := c.Get(ctx, types.NamespacedName{Namespace: ns, Name: name}, svc) |
| 222 | + switch { |
| 223 | + case apierrors.IsNotFound(err): |
| 224 | + // Nothing at the name — just create the headless Service below. |
| 225 | + case err != nil: |
| 226 | + return fmt.Errorf("read Service %q: %w", name, err) |
| 227 | + case svc.Spec.ClusterIP == corev1.ClusterIPNone: |
| 228 | + // Already headless (a prior run, or an override that never collided). |
| 229 | + return nil |
| 230 | + default: |
| 231 | + // A ClusterIP Service (the legacy client) holds the name. Delete it so |
| 232 | + // the headless Service can take the name. |
| 233 | + if err := c.Delete(ctx, svc); err != nil && !apierrors.IsNotFound(err) { |
| 234 | + return fmt.Errorf("delete legacy client Service %q: %w", name, err) |
| 235 | + } |
| 236 | + if err := waitGone(ctx, c, types.NamespacedName{Namespace: ns, Name: name}, &corev1.Service{}, time.Minute); err != nil { |
| 237 | + return fmt.Errorf("await legacy client Service %q deletion: %w", name, err) |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + gvk := lll.GroupVersion.WithKind("EtcdCluster") |
| 242 | + headless := &corev1.Service{ |
| 243 | + ObjectMeta: metav1.ObjectMeta{ |
| 244 | + Name: name, |
| 245 | + Namespace: ns, |
| 246 | + Labels: controllers.ClusterLabels(owner.Name), |
| 247 | + OwnerReferences: []metav1.OwnerReference{{ |
| 248 | + APIVersion: gvk.GroupVersion().String(), |
| 249 | + Kind: gvk.Kind, |
| 250 | + Name: owner.Name, |
| 251 | + UID: owner.UID, |
| 252 | + Controller: ptrTo(true), |
| 253 | + BlockOwnerDeletion: ptrTo(true), |
| 254 | + }}, |
| 255 | + }, |
| 256 | + // Matches the operator's native headless Service (ensureServices), so |
| 257 | + // its first reconcile finds no drift to reconcile. |
| 258 | + Spec: corev1.ServiceSpec{ |
| 259 | + ClusterIP: corev1.ClusterIPNone, |
| 260 | + PublishNotReadyAddresses: true, |
| 261 | + Selector: map[string]string{controllers.LabelCluster: owner.Name}, |
| 262 | + Ports: []corev1.ServicePort{ |
| 263 | + {Name: "client", Port: 2379}, |
| 264 | + {Name: "peer", Port: 2380}, |
| 265 | + }, |
| 266 | + }, |
| 267 | + } |
| 268 | + if err := c.Create(ctx, headless); err != nil && !apierrors.IsAlreadyExists(err) { |
| 269 | + return fmt.Errorf("create native headless Service %q: %w", name, err) |
| 270 | + } |
| 271 | + return nil |
| 272 | +} |
| 273 | + |
| 274 | +// adoptPod stamps the operator's member labels (incl. role=voter — every |
| 275 | +// adopted member is a voter) and rewrites the controller owner reference to |
| 276 | +// the EtcdMember. The pod itself is not restarted; labels and owner refs are |
| 277 | +// mutable on live pods. |
| 278 | +func adoptPod(ctx context.Context, c client.Client, ns, podName, clusterName string, owner *lll.EtcdMember) error { |
| 279 | + pod := &corev1.Pod{} |
| 280 | + if err := c.Get(ctx, types.NamespacedName{Namespace: ns, Name: podName}, pod); err != nil { |
| 281 | + return fmt.Errorf("read pod %q: %w", podName, err) |
| 282 | + } |
| 283 | + orig := pod.DeepCopy() |
| 284 | + if pod.Labels == nil { |
| 285 | + pod.Labels = map[string]string{} |
| 286 | + } |
| 287 | + for k, v := range controllers.MemberLabels(clusterName, podName) { |
| 288 | + pod.Labels[k] = v |
| 289 | + } |
| 290 | + pod.Labels[controllers.LabelRole] = controllers.RoleVoter |
| 291 | + setControllerOwner(&pod.ObjectMeta, owner) |
| 292 | + if err := c.Patch(ctx, pod, client.MergeFrom(orig)); err != nil { |
| 293 | + return fmt.Errorf("re-own pod %q: %w", podName, err) |
| 294 | + } |
| 295 | + return nil |
| 296 | +} |
| 297 | + |
| 298 | +// adoptPVC mirrors adoptPod for the member's data PVC. The new member |
| 299 | +// controller refuses PVCs without its own controller owner reference |
| 300 | +// (pvcOwnedBy), so this patch is what makes ensurePVC pass. |
| 301 | +func adoptPVC(ctx context.Context, c client.Client, ns, pvcName, clusterName string, owner *lll.EtcdMember) error { |
| 302 | + pvc := &corev1.PersistentVolumeClaim{} |
| 303 | + if err := c.Get(ctx, types.NamespacedName{Namespace: ns, Name: pvcName}, pvc); err != nil { |
| 304 | + return fmt.Errorf("read PVC %q: %w", pvcName, err) |
| 305 | + } |
| 306 | + orig := pvc.DeepCopy() |
| 307 | + if pvc.Labels == nil { |
| 308 | + pvc.Labels = map[string]string{} |
| 309 | + } |
| 310 | + for k, v := range controllers.MemberLabels(clusterName, owner.Name) { |
| 311 | + pvc.Labels[k] = v |
| 312 | + } |
| 313 | + setControllerOwner(&pvc.ObjectMeta, owner) |
| 314 | + if err := c.Patch(ctx, pvc, client.MergeFrom(orig)); err != nil { |
| 315 | + return fmt.Errorf("re-own PVC %q: %w", pvcName, err) |
| 316 | + } |
| 317 | + return nil |
| 318 | +} |
| 319 | + |
| 320 | +// setControllerOwner replaces any existing controller owner reference with |
| 321 | +// one pointing at the EtcdMember, matching what the member controller's |
| 322 | +// SetControllerReference would produce. |
| 323 | +func setControllerOwner(meta *metav1.ObjectMeta, owner *lll.EtcdMember) { |
| 324 | + gvk := lll.GroupVersion.WithKind("EtcdMember") |
| 325 | + replaceControllerRef(meta, metav1.OwnerReference{ |
| 326 | + APIVersion: gvk.GroupVersion().String(), |
| 327 | + Kind: gvk.Kind, |
| 328 | + Name: owner.Name, |
| 329 | + UID: owner.UID, |
| 330 | + Controller: ptrTo(true), |
| 331 | + BlockOwnerDeletion: ptrTo(true), |
| 332 | + }) |
| 333 | +} |
| 334 | + |
| 335 | +// replaceControllerRef drops any previous controller=true reference (the |
| 336 | +// orphaned StatefulSet's, a prior partial run's) and appends `ref`. |
| 337 | +// Idempotent: a matching ref is left in place. |
| 338 | +func replaceControllerRef(meta *metav1.ObjectMeta, ref metav1.OwnerReference) { |
| 339 | + kept := meta.OwnerReferences[:0] |
| 340 | + for _, o := range meta.OwnerReferences { |
| 341 | + if o.UID == ref.UID && o.Kind == ref.Kind { |
| 342 | + continue // re-added below in canonical form |
| 343 | + } |
| 344 | + if o.Controller != nil && *o.Controller { |
| 345 | + continue // displaced by the new controller owner |
| 346 | + } |
| 347 | + kept = append(kept, o) |
| 348 | + } |
| 349 | + meta.OwnerReferences = append(kept, ref) |
| 350 | +} |
| 351 | + |
| 352 | +// waitGone polls until the object disappears. |
| 353 | +func waitGone(ctx context.Context, c client.Client, key types.NamespacedName, obj client.Object, timeout time.Duration) error { |
| 354 | + deadline := time.After(timeout) |
| 355 | + for { |
| 356 | + err := c.Get(ctx, key, obj) |
| 357 | + if apierrors.IsNotFound(err) { |
| 358 | + return nil |
| 359 | + } |
| 360 | + if err != nil { |
| 361 | + return err |
| 362 | + } |
| 363 | + select { |
| 364 | + case <-ctx.Done(): |
| 365 | + return ctx.Err() |
| 366 | + case <-deadline: |
| 367 | + return fmt.Errorf("%s/%s still present after %s", key.Namespace, key.Name, timeout) |
| 368 | + case <-time.After(2 * time.Second): |
| 369 | + } |
| 370 | + } |
| 371 | +} |
| 372 | + |
| 373 | +func ptrTo[T any](v T) *T { return &v } |
0 commit comments