|
| 1 | +package steps |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "k8s.io/component-base/featuregate" |
| 10 | +) |
| 11 | + |
| 12 | +// catalogdHAFeature gates scenarios that require a multi-node cluster. |
| 13 | +// It is set to true in BeforeSuite when the cluster has at least 2 nodes, |
| 14 | +// which is the case for the experimental e2e suite (kind-config-2node.yaml) |
| 15 | +// but not the standard suite. |
| 16 | +const catalogdHAFeature featuregate.Feature = "CatalogdHA" |
| 17 | + |
| 18 | +// containerRuntime returns the container runtime from CONTAINER_RUNTIME, defaulting to "docker". |
| 19 | +func containerRuntime() string { |
| 20 | + if rt := os.Getenv("CONTAINER_RUNTIME"); rt != "" { |
| 21 | + return rt |
| 22 | + } |
| 23 | + return "docker" |
| 24 | +} |
| 25 | + |
| 26 | +// CatalogdLeaderPodIsForceDeleted force-deletes the catalogd leader pod to simulate leader loss. |
| 27 | +// The pod is identified from sc.leaderPods["catalogd"] (populated by a prior |
| 28 | +// "catalogd is ready to reconcile resources" step). Force-deletion is equivalent to |
| 29 | +// an abrupt process crash: the lease is no longer renewed and the surviving pod |
| 30 | +// acquires leadership after the lease expires. |
| 31 | +// |
| 32 | +// Note: stopping the kind node container is not used here because both nodes in the |
| 33 | +// experimental 2-node cluster are control-plane nodes that run etcd — stopping either |
| 34 | +// would break etcd quorum and make the API server unreachable for the rest of the test. |
| 35 | +func CatalogdLeaderPodIsForceDeleted(ctx context.Context) error { |
| 36 | + sc := scenarioCtx(ctx) |
| 37 | + leaderPod := sc.leaderPods["catalogd"] |
| 38 | + if leaderPod == "" { |
| 39 | + return fmt.Errorf("catalogd leader pod not found in scenario context; run 'catalogd is ready to reconcile resources' first") |
| 40 | + } |
| 41 | + |
| 42 | + logger.Info("Force-deleting catalogd leader pod", "pod", leaderPod) |
| 43 | + if _, err := k8sClient("delete", "pod", leaderPod, "-n", olmNamespace, |
| 44 | + "--force", "--grace-period=0"); err != nil { |
| 45 | + return fmt.Errorf("failed to force-delete catalogd leader pod %q: %w", leaderPod, err) |
| 46 | + } |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +// NewCatalogdLeaderIsElected polls the catalogd leader election lease until the holder |
| 51 | +// identity changes to a pod other than the deleted leader. It updates |
| 52 | +// sc.leaderPods["catalogd"] with the new leader pod name. |
| 53 | +func NewCatalogdLeaderIsElected(ctx context.Context) error { |
| 54 | + sc := scenarioCtx(ctx) |
| 55 | + oldLeader := sc.leaderPods["catalogd"] |
| 56 | + |
| 57 | + waitFor(ctx, func() bool { |
| 58 | + holder, err := k8sClient("get", "lease", leaseNames["catalogd"], "-n", olmNamespace, |
| 59 | + "-o", "jsonpath={.spec.holderIdentity}") |
| 60 | + if err != nil || holder == "" { |
| 61 | + return false |
| 62 | + } |
| 63 | + newPod := strings.Split(strings.TrimSpace(holder), "_")[0] |
| 64 | + if newPod == oldLeader { |
| 65 | + return false |
| 66 | + } |
| 67 | + sc.leaderPods["catalogd"] = newPod |
| 68 | + logger.Info("New catalogd leader elected", "pod", newPod) |
| 69 | + return true |
| 70 | + }) |
| 71 | + return nil |
| 72 | +} |
0 commit comments