|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package e2e |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "path/filepath" |
| 14 | + "testing" |
| 15 | + "time" |
| 16 | + |
| 17 | + . "github.com/onsi/gomega" |
| 18 | + appsv1 "k8s.io/api/apps/v1" |
| 19 | + corev1 "k8s.io/api/core/v1" |
| 20 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 21 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 24 | + |
| 25 | + bootcv1alpha1 "github.com/bootc-dev/bootc-operator/api/v1alpha1" |
| 26 | + "github.com/bootc-dev/bootc-operator/test/e2e/e2eutil" |
| 27 | + testutil "github.com/bootc-dev/bootc-operator/test/util" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + releaseManifestURL = "https://github.com/bootc-dev/bootc-operator/releases/download/%s/install.yaml" |
| 32 | +) |
| 33 | + |
| 34 | +// TestOperatorUpgrade installs the released version of the operator |
| 35 | +// from its published manifest, verifies basic functionality, then |
| 36 | +// upgrades by re-applying the current manifests (including CRDs) on |
| 37 | +// top and verifies the operator still works. |
| 38 | +func TestOperatorUpgrade(t *testing.T) { |
| 39 | + e2eutil.Providers(t, "bink") |
| 40 | + |
| 41 | + releaseTag := os.Getenv("E2E_OPERATOR_RELEASE_TAG") |
| 42 | + if releaseTag == "" { |
| 43 | + t.Skip("E2E_OPERATOR_RELEASE_TAG not set") |
| 44 | + } |
| 45 | + releasedImg := os.Getenv("E2E_OPERATOR_RELEASED_IMG") |
| 46 | + if releasedImg == "" { |
| 47 | + t.Skip("E2E_OPERATOR_RELEASED_IMG not set") |
| 48 | + } |
| 49 | + |
| 50 | + g := NewWithT(t) |
| 51 | + g.SetDefaultEventuallyTimeout(pollTimeout) |
| 52 | + g.SetDefaultEventuallyPollingInterval(pollInterval) |
| 53 | + |
| 54 | + env := e2eutil.New(t) |
| 55 | + ctx := context.Background() |
| 56 | + |
| 57 | + // Capture the current operator image before we replace it. |
| 58 | + var origDeploy appsv1.Deployment |
| 59 | + g.Expect(env.Client.Get(ctx, operatorDeployKey(), &origDeploy)).To(Succeed()) |
| 60 | + currentImg := containerImage(origDeploy.Spec.Template.Spec.Containers, "manager") |
| 61 | + t.Logf("Current operator image: %s", currentImg) |
| 62 | + t.Logf("Released operator tag: %s, image: %s", releaseTag, releasedImg) |
| 63 | + |
| 64 | + // Phase 1: Delete the current operator and install the released |
| 65 | + // version from its published manifest. |
| 66 | + installReleasedOperator(t, g, env, ctx, releaseTag, releasedImg) |
| 67 | + |
| 68 | + t.Cleanup(func() { |
| 69 | + t.Logf("Restoring operator to current version...") |
| 70 | + applyCurrentManifests(t, currentImg) |
| 71 | + waitForOperatorReady(t, g, env.Client, ctx) |
| 72 | + t.Logf("Operator restored") |
| 73 | + }) |
| 74 | + |
| 75 | + // Phase 2: Verify the released version works. |
| 76 | + nodeName := env.AddNode(t) |
| 77 | + |
| 78 | + pool := env.NewPool("upgrade", env.NodeImageDigestedPullSpec()) |
| 79 | + g.Expect(env.Client.Create(ctx, pool)).To(Succeed()) |
| 80 | + |
| 81 | + g.Eventually(func() (bootcv1alpha1.BootcNodeStatus, error) { |
| 82 | + var bn bootcv1alpha1.BootcNode |
| 83 | + err := env.Client.Get(ctx, client.ObjectKey{Name: nodeName}, &bn) |
| 84 | + return bn.Status, err |
| 85 | + }).WithTimeout(3 * time.Minute).Should(And( |
| 86 | + HaveField("Booted", Not(BeNil())), |
| 87 | + HaveField("Conditions", ContainElement(And( |
| 88 | + HaveField("Type", bootcv1alpha1.NodeIdle), |
| 89 | + HaveField("Status", metav1.ConditionTrue), |
| 90 | + HaveField("Reason", bootcv1alpha1.NodeReasonIdle), |
| 91 | + ))), |
| 92 | + )) |
| 93 | + |
| 94 | + t.Logf("Node %q is Idle with released operator", nodeName) |
| 95 | + |
| 96 | + // Phase 3: Upgrade by applying the current manifests on top. |
| 97 | + applyCurrentManifests(t, currentImg) |
| 98 | + waitForOperatorReady(t, g, env.Client, ctx) |
| 99 | + |
| 100 | + t.Logf("Upgraded operator to current version via manifest apply") |
| 101 | + |
| 102 | + // Phase 4: Verify the operator still works after upgrade by |
| 103 | + // triggering a node image update and verifying the full update |
| 104 | + // lifecycle completes. |
| 105 | + updateRef := env.NodeImageUpdateDigestedPullSpec() |
| 106 | + |
| 107 | + modified := pool.DeepCopy() |
| 108 | + modified.Spec.Image.Ref = updateRef |
| 109 | + g.Expect(env.Client.Patch(ctx, modified, client.MergeFrom(pool))).To(Succeed()) |
| 110 | + *pool = *modified |
| 111 | + |
| 112 | + t.Logf("Patched pool to update image %s", updateRef) |
| 113 | + |
| 114 | + g.Eventually(func() (bootcv1alpha1.BootcNodeStatus, error) { |
| 115 | + var bn bootcv1alpha1.BootcNode |
| 116 | + err := env.Client.Get(ctx, client.ObjectKey{Name: nodeName}, &bn) |
| 117 | + return bn.Status, err |
| 118 | + }).WithTimeout(5*time.Minute).Should(And( |
| 119 | + HaveField("Booted", And( |
| 120 | + Not(BeNil()), |
| 121 | + HaveField("ImageDigest", env.NodeImageUpdateDigest()), |
| 122 | + )), |
| 123 | + HaveField("Conditions", ContainElement(And( |
| 124 | + HaveField("Type", bootcv1alpha1.NodeIdle), |
| 125 | + HaveField("Status", metav1.ConditionTrue), |
| 126 | + HaveField("Reason", bootcv1alpha1.NodeReasonIdle), |
| 127 | + ))), |
| 128 | + ), "expected node to complete update after operator upgrade") |
| 129 | + |
| 130 | + t.Logf("Node %q completed update with upgraded operator", nodeName) |
| 131 | + |
| 132 | + g.Eventually(fetchPoolStatus(ctx, env.Client, pool)). |
| 133 | + Should(poolAllUpdated(1, env.NodeImageUpdateDigest())) |
| 134 | +} |
| 135 | + |
| 136 | +// installReleasedOperator deletes the current operator, downloads the |
| 137 | +// released install manifest, patches the image reference, and applies |
| 138 | +// it. |
| 139 | +func installReleasedOperator( |
| 140 | + t *testing.T, |
| 141 | + g Gomega, |
| 142 | + env *e2eutil.Env, |
| 143 | + ctx context.Context, |
| 144 | + releaseTag, releasedImg string, |
| 145 | +) { |
| 146 | + t.Helper() |
| 147 | + |
| 148 | + deleteCurrentOperator(t, g, env, ctx) |
| 149 | + |
| 150 | + manifest := downloadReleaseManifest(t, releaseTag) |
| 151 | + patchedManifest := patchManifestImage(t, manifest, releasedImg) |
| 152 | + |
| 153 | + kubectlApply(t, patchedManifest) |
| 154 | + waitForOperatorReady(t, g, env.Client, ctx) |
| 155 | + |
| 156 | + t.Logf("Released operator %s installed from manifest", releaseTag) |
| 157 | +} |
| 158 | + |
| 159 | +// deleteCurrentOperator removes the operator Deployment and DaemonSet |
| 160 | +// and waits for all operator pods to terminate. |
| 161 | +func deleteCurrentOperator( |
| 162 | + t *testing.T, |
| 163 | + g Gomega, |
| 164 | + env *e2eutil.Env, |
| 165 | + ctx context.Context, |
| 166 | +) { |
| 167 | + t.Helper() |
| 168 | + |
| 169 | + var deploy appsv1.Deployment |
| 170 | + g.Expect(env.Client.Get(ctx, operatorDeployKey(), &deploy)).To(Succeed()) |
| 171 | + var ds appsv1.DaemonSet |
| 172 | + g.Expect(env.Client.Get(ctx, operatorDaemonSetKey(), &ds)).To(Succeed()) |
| 173 | + |
| 174 | + t.Logf("Deleting operator deployment, daemonset, and CRDs...") |
| 175 | + g.Expect(env.Client.Delete(ctx, &deploy)).To(Succeed()) |
| 176 | + g.Expect(env.Client.Delete(ctx, &ds)).To(Succeed()) |
| 177 | + |
| 178 | + for _, crdName := range []string{ |
| 179 | + "bootcnodepools.node.bootc.dev", |
| 180 | + "bootcnodes.node.bootc.dev", |
| 181 | + } { |
| 182 | + var crd apiextensionsv1.CustomResourceDefinition |
| 183 | + if err := env.Client.Get(ctx, client.ObjectKey{Name: crdName}, &crd); err == nil { |
| 184 | + g.Expect(env.Client.Delete(ctx, &crd)).To(Succeed()) |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + g.Eventually(func(g Gomega) { |
| 189 | + var d appsv1.Deployment |
| 190 | + err := env.Client.Get(ctx, operatorDeployKey(), &d) |
| 191 | + g.Expect(apierrors.IsNotFound(err)).To(BeTrue()) |
| 192 | + var dset appsv1.DaemonSet |
| 193 | + err = env.Client.Get(ctx, operatorDaemonSetKey(), &dset) |
| 194 | + g.Expect(apierrors.IsNotFound(err)).To(BeTrue()) |
| 195 | + for _, crdName := range []string{ |
| 196 | + "bootcnodepools.node.bootc.dev", |
| 197 | + "bootcnodes.node.bootc.dev", |
| 198 | + } { |
| 199 | + var crd apiextensionsv1.CustomResourceDefinition |
| 200 | + err = env.Client.Get(ctx, client.ObjectKey{Name: crdName}, &crd) |
| 201 | + g.Expect(apierrors.IsNotFound(err)).To(BeTrue()) |
| 202 | + } |
| 203 | + }).WithTimeout(2*time.Minute).Should(Succeed(), |
| 204 | + "expected operator resources to be deleted") |
| 205 | + |
| 206 | + t.Logf("Operator deployment, daemonset, and CRDs deleted") |
| 207 | +} |
| 208 | + |
| 209 | +// downloadReleaseManifest fetches install.yaml from a GitHub release. |
| 210 | +func downloadReleaseManifest(t *testing.T, tag string) []byte { |
| 211 | + t.Helper() |
| 212 | + |
| 213 | + url := fmt.Sprintf(releaseManifestURL, tag) |
| 214 | + t.Logf("Downloading release manifest from %s", url) |
| 215 | + |
| 216 | + resp, err := http.Get(url) //nolint:gosec |
| 217 | + if err != nil { |
| 218 | + t.Fatalf("downloading release manifest: %v", err) |
| 219 | + } |
| 220 | + defer func() { _ = resp.Body.Close() }() |
| 221 | + |
| 222 | + if resp.StatusCode != http.StatusOK { |
| 223 | + t.Fatalf("downloading release manifest: HTTP %d", resp.StatusCode) |
| 224 | + } |
| 225 | + |
| 226 | + data, err := io.ReadAll(resp.Body) |
| 227 | + if err != nil { |
| 228 | + t.Fatalf("reading release manifest body: %v", err) |
| 229 | + } |
| 230 | + |
| 231 | + return data |
| 232 | +} |
| 233 | + |
| 234 | +// patchManifestImage replaces all container image references in the |
| 235 | +// manifest with the given image using yq. |
| 236 | +func patchManifestImage(t *testing.T, manifest []byte, img string) []byte { |
| 237 | + t.Helper() |
| 238 | + |
| 239 | + yqExpr := fmt.Sprintf( |
| 240 | + `(select(.kind == "Deployment") | .spec.template.spec.containers[] | select(.name == "manager")).image = "%s" | `+ |
| 241 | + `(select(.kind == "DaemonSet") | .spec.template.spec.containers[] | select(.name == "daemon")).image = "%s"`, |
| 242 | + img, |
| 243 | + img, |
| 244 | + ) |
| 245 | + |
| 246 | + cmd := exec.Command("yq", yqExpr) |
| 247 | + cmd.Stdin = bytesReader(manifest) |
| 248 | + |
| 249 | + out, err := cmd.CombinedOutput() |
| 250 | + if err != nil { |
| 251 | + t.Fatalf("patching manifest image with yq: %v\n%s", err, out) |
| 252 | + } |
| 253 | + |
| 254 | + return out |
| 255 | +} |
| 256 | + |
| 257 | +// kubectlApply applies the given manifest bytes via kubectl. |
| 258 | +func kubectlApply(t *testing.T, manifest []byte) { |
| 259 | + t.Helper() |
| 260 | + |
| 261 | + kubeconfigPath := os.Getenv("KUBECONFIG") |
| 262 | + args := []string{"apply", "--server-side", "-f", "-"} |
| 263 | + if kubeconfigPath != "" { |
| 264 | + args = append([]string{"--kubeconfig", kubeconfigPath}, args...) |
| 265 | + } |
| 266 | + |
| 267 | + cmd := exec.Command("kubectl", args...) |
| 268 | + cmd.Stdin = bytesReader(manifest) |
| 269 | + |
| 270 | + out, err := cmd.CombinedOutput() |
| 271 | + if err != nil { |
| 272 | + t.Fatalf("kubectl apply failed: %v\n%s", err, out) |
| 273 | + } |
| 274 | + |
| 275 | + t.Logf("kubectl apply:\n%s", out) |
| 276 | +} |
| 277 | + |
| 278 | +// applyCurrentManifests runs the equivalent of "make deploy" for the |
| 279 | +// current version: kustomize build + image patch + kubectl apply. |
| 280 | +func applyCurrentManifests(t *testing.T, img string) { |
| 281 | + t.Helper() |
| 282 | + |
| 283 | + repoRoot := findRepoRoot(t) |
| 284 | + |
| 285 | + kustomize := filepath.Join(repoRoot, "bin", "kustomize") |
| 286 | + if _, err := os.Stat(kustomize); err != nil { |
| 287 | + kustomize = "kustomize" |
| 288 | + } |
| 289 | + |
| 290 | + cmd := exec.Command(kustomize, "build", filepath.Join(repoRoot, "config", "default")) |
| 291 | + manifest, err := cmd.CombinedOutput() |
| 292 | + if err != nil { |
| 293 | + t.Fatalf("kustomize build failed: %v\n%s", err, manifest) |
| 294 | + } |
| 295 | + |
| 296 | + manifest = patchManifestImage(t, manifest, img) |
| 297 | + kubectlApply(t, manifest) |
| 298 | +} |
| 299 | + |
| 300 | +// findRepoRoot walks up from the test directory to find the repository |
| 301 | +// root (containing go.mod). |
| 302 | +func findRepoRoot(t *testing.T) string { |
| 303 | + t.Helper() |
| 304 | + |
| 305 | + dir, err := os.Getwd() |
| 306 | + if err != nil { |
| 307 | + t.Fatalf("getting working directory: %v", err) |
| 308 | + } |
| 309 | + |
| 310 | + for { |
| 311 | + if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { |
| 312 | + return dir |
| 313 | + } |
| 314 | + parent := filepath.Dir(dir) |
| 315 | + if parent == dir { |
| 316 | + t.Fatal("could not find repository root (go.mod)") |
| 317 | + } |
| 318 | + dir = parent |
| 319 | + } |
| 320 | +} |
| 321 | + |
| 322 | +func waitForOperatorReady( |
| 323 | + t *testing.T, |
| 324 | + g Gomega, |
| 325 | + c client.Client, |
| 326 | + ctx context.Context, |
| 327 | +) { |
| 328 | + t.Helper() |
| 329 | + g.Eventually(func(g Gomega) { |
| 330 | + var d appsv1.Deployment |
| 331 | + g.Expect(c.Get(ctx, operatorDeployKey(), &d)).To(Succeed()) |
| 332 | + g.Expect(d.Status.Replicas).To(Equal(int32(1))) |
| 333 | + g.Expect(d.Status.UpdatedReplicas).To(Equal(int32(1))) |
| 334 | + g.Expect(d.Status.AvailableReplicas).To(Equal(int32(1))) |
| 335 | + }).WithTimeout(3*time.Minute).Should(Succeed(), |
| 336 | + "expected operator deployment to be ready") |
| 337 | +} |
| 338 | + |
| 339 | +func operatorDeployKey() client.ObjectKey { |
| 340 | + return client.ObjectKey{ |
| 341 | + Namespace: testutil.OperatorNamespaceName, |
| 342 | + Name: "bootc-operator-controller-manager", |
| 343 | + } |
| 344 | +} |
| 345 | + |
| 346 | +func operatorDaemonSetKey() client.ObjectKey { |
| 347 | + return client.ObjectKey{ |
| 348 | + Namespace: testutil.OperatorNamespaceName, |
| 349 | + Name: "bootc-operator-daemon", |
| 350 | + } |
| 351 | +} |
| 352 | + |
| 353 | +func containerImage(containers []corev1.Container, name string) string { |
| 354 | + for _, c := range containers { |
| 355 | + if c.Name == name { |
| 356 | + return c.Image |
| 357 | + } |
| 358 | + } |
| 359 | + return "" |
| 360 | +} |
| 361 | + |
| 362 | +func bytesReader(b []byte) io.Reader { |
| 363 | + return bytes.NewReader(b) |
| 364 | +} |
0 commit comments