From 5a7e236a68ba17dff4a032f897b685b9d1d0a196 Mon Sep 17 00:00:00 2001 From: wouldgo <1098882+wouldgo@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:44:27 +0100 Subject: [PATCH] refactor(authentication): extract the operator-wait package Move the operator status waiting helpers from operator_status_helpers.go into the operatorwait package and export them. Add a wrapper-chain constructor that composes mutation, deployment-rollout, and operator-change waiting around a test action. Rewire the existing component-proxy tests onto the package. No behavior change. --- .../authentication/component_proxy.go | 354 +++++++++--------- .../authentication/component_proxy_helpers.go | 3 +- .../authentication/operatorwait/chain.go | 93 +++++ .../authentication/operatorwait/chain_test.go | 117 ++++++ .../status.go} | 11 +- .../status_test.go} | 2 +- 6 files changed, 400 insertions(+), 180 deletions(-) create mode 100644 test/extended/authentication/operatorwait/chain.go create mode 100644 test/extended/authentication/operatorwait/chain_test.go rename test/extended/authentication/{operator_status_helpers.go => operatorwait/status.go} (88%) rename test/extended/authentication/{operator_status_helpers_test.go => operatorwait/status_test.go} (99%) diff --git a/test/extended/authentication/component_proxy.go b/test/extended/authentication/component_proxy.go index 619cbdb1dcd1..dca0381dbd87 100644 --- a/test/extended/authentication/component_proxy.go +++ b/test/extended/authentication/component_proxy.go @@ -25,6 +25,8 @@ import ( "github.com/openshift/library-go/pkg/oauth/tokenrequest" "github.com/openshift/library-go/pkg/oauth/tokenrequest/challengehandlers" + "github.com/openshift/origin/test/extended/authentication/operatorwait" + exutil "github.com/openshift/origin/test/extended/util" operator "github.com/openshift/origin/test/extended/util/operator" ) @@ -108,7 +110,6 @@ var _ = g.Describe("[sig-auth][Suite:openshift/conformance/serial][Jira:\"Authen }) func testProxyConfigPerformOIDCLogin(ctx context.Context, oc *exutil.CLI, cleanups *[]removalFunc, kcSetup *keycloakProxySetup, httpProxyURL, proxyNamespace string) { - const namespace, name = "openshift-authentication", "oauth-openshift" g.By("Setting direct access grant for oauth flow") err := enableDirectAccessGrant(kcSetup) o.Expect(err).NotTo(o.HaveOccurred(), "direct access grant should be enabled") @@ -117,39 +118,40 @@ func testProxyConfigPerformOIDCLogin(ctx context.Context, oc *exutil.CLI, cleanu kcUser, kcPass, err := createKeycloakUserPassword(ctx, oc, cleanups, kcSetup) o.Expect(err).NotTo(o.HaveOccurred()) - g.By("Updating Auth Proxy Config") - deployment, err := oc.AdminKubeClient().AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeProxy := deployment.Generation - - err = updateAuthenticationProxy(ctx, oc, operatorv1.AuthenticationProxyConfig{ - HTTPSProxy: httpProxyURL, + g.By("Setting component-scoped proxy and waiting for the OAuth server rollout") + err = operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + return updateAuthenticationProxy(ctx, oc, operatorv1.AuthenticationProxyConfig{ + HTTPSProxy: httpProxyURL, + }) }) o.Expect(err).NotTo(o.HaveOccurred()) - g.By("Waiting for operator to pick up proxy changes and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after proxy changes before registering IdP") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeProxy) - o.Expect(err).NotTo(o.HaveOccurred()) - - deployment, err = oc.AdminKubeClient().AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeIdP := deployment.Generation - g.By("Registering Keycloak as OIDC IdP") - idpCleanups, err := addKeycloakOIDCIdPForProxy(ctx, oc, kcSetup) - *cleanups = append(*cleanups, idpCleanups...) - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for operator to pick up IdP changes and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after IdP changes") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeIdP) + err = operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + idpCleanups, err := addKeycloakOIDCIdPForProxy(ctx, oc, kcSetup) + *cleanups = append(*cleanups, idpCleanups...) + return err + }) o.Expect(err).NotTo(o.HaveOccurred()) g.By("Verifying oauth-server has HTTPS_PROXY but not HTTP_PROXY") @@ -174,7 +176,6 @@ func testProxyConfigPerformOIDCLogin(ctx context.Context, oc *exutil.CLI, cleanu } func testHotReloadCAFileChange(ctx context.Context, oc *exutil.CLI, cleanups *[]removalFunc, caCertPEM []byte, kcSetup *keycloakProxySetup, httpsProxyURL, proxyNamespace string) { - const namespace, name = "openshift-authentication", "oauth-openshift" g.By("Setting direct access grant for oauth flow") err := enableDirectAccessGrant(kcSetup) o.Expect(err).NotTo(o.HaveOccurred(), "direct access grant should be enabled") @@ -189,42 +190,43 @@ func testHotReloadCAFileChange(ctx context.Context, oc *exutil.CLI, cleanups *[] *cleanups = append(*cleanups, cmCleanup) o.Expect(err).NotTo(o.HaveOccurred()) - g.By("Setting component-scoped proxy with trustedCA") - deployment, err := kubeClient.AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeProxy := deployment.Generation - - err = updateAuthenticationProxy(ctx, oc, operatorv1.AuthenticationProxyConfig{ - HTTPSProxy: httpsProxyURL, - TrustedCA: operatorv1.AuthenticationConfigMapReference{ - Name: configMapName, - }, + g.By("Setting component-scoped proxy with trustedCA and waiting for the OAuth server rollout") + err = operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + return updateAuthenticationProxy(ctx, oc, operatorv1.AuthenticationProxyConfig{ + HTTPSProxy: httpsProxyURL, + TrustedCA: operatorv1.AuthenticationConfigMapReference{ + Name: configMapName, + }, + }) }) o.Expect(err).NotTo(o.HaveOccurred()) - g.By("Waiting for operator to pick up proxy and trustedCA changes and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after proxy changes before registering IdP") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeProxy) - o.Expect(err).NotTo(o.HaveOccurred()) - - deployment, err = kubeClient.AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeIdP := deployment.Generation - g.By("Registering Keycloak as OIDC IdP") - idpCleanups, err := addKeycloakOIDCIdPForProxy(ctx, oc, kcSetup) - *cleanups = append(*cleanups, idpCleanups...) - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for operator to pick up IdP changes and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after IdP changes") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeIdP) + err = operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + idpCleanups, err := addKeycloakOIDCIdPForProxy(ctx, oc, kcSetup) + *cleanups = append(*cleanups, idpCleanups...) + return err + }) o.Expect(err).NotTo(o.HaveOccurred()) g.By("Verifying trustedCA ConfigMap is synced to openshift-authentication namespace") @@ -248,7 +250,7 @@ func testHotReloadCAFileChange(ctx context.Context, oc *exutil.CLI, cleanups *[] o.Expect(err).NotTo(o.HaveOccurred()) g.By("Recording Deployment generation before CA rotation") - deployment, err = kubeClient.AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) + deployment, err := kubeClient.AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) o.Expect(err).NotTo(o.HaveOccurred()) generationBefore := deployment.Generation @@ -342,7 +344,6 @@ func testHotReloadCAFileChange(ctx context.Context, oc *exutil.CLI, cleanups *[] } func testBypassProxyNoProxyHost(ctx context.Context, oc *exutil.CLI, cleanups *[]removalFunc, caCertPEM []byte, kcSetup *keycloakProxySetup, httpProxyURL, httpsProxyURL, proxyNamespace string) { - const namespace, name = "openshift-authentication", "oauth-openshift" g.By("Setting direct access grant for oauth flow") err := enableDirectAccessGrant(kcSetup) o.Expect(err).NotTo(o.HaveOccurred(), "direct access grant should be enabled") @@ -359,44 +360,45 @@ func testBypassProxyNoProxyHost(ctx context.Context, oc *exutil.CLI, cleanups *[ o.Expect(err).NotTo(o.HaveOccurred()) keycloakHost := issuerURL.Hostname() - g.By("Setting component-scoped proxy with noProxy") - deployment, err := oc.AdminKubeClient().AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeProxy := deployment.Generation - - err = updateAuthenticationProxy(ctx, oc, operatorv1.AuthenticationProxyConfig{ - HTTPProxy: httpProxyURL, - HTTPSProxy: httpsProxyURL, - TrustedCA: operatorv1.AuthenticationConfigMapReference{ - Name: configMapName, - }, - NoProxy: []string{keycloakHost}, + g.By("Setting component-scoped proxy with noProxy and waiting for the OAuth server rollout") + err = operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + return updateAuthenticationProxy(ctx, oc, operatorv1.AuthenticationProxyConfig{ + HTTPProxy: httpProxyURL, + HTTPSProxy: httpsProxyURL, + TrustedCA: operatorv1.AuthenticationConfigMapReference{ + Name: configMapName, + }, + NoProxy: []string{keycloakHost}, + }) }) o.Expect(err).NotTo(o.HaveOccurred()) - g.By("Waiting for operator to pick up proxy changes and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after proxy changes before registering IdP") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeProxy) - o.Expect(err).NotTo(o.HaveOccurred()) - - deployment, err = oc.AdminKubeClient().AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeIdP := deployment.Generation - g.By("Registering Keycloak as OIDC IdP") - idpCleanups, err := addKeycloakOIDCIdPForProxy(ctx, oc, kcSetup) - *cleanups = append(*cleanups, idpCleanups...) - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for operator to pick up IdP changes and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after IdP changes") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeIdP) + err = operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + idpCleanups, err := addKeycloakOIDCIdPForProxy(ctx, oc, kcSetup) + *cleanups = append(*cleanups, idpCleanups...) + return err + }) o.Expect(err).NotTo(o.HaveOccurred()) g.By("Verifying oauth-server has HTTP_PROXY, HTTPS_PROXY, and NO_PROXY with custom entry") @@ -413,7 +415,6 @@ func testBypassProxyNoProxyHost(ctx context.Context, oc *exutil.CLI, cleanups *[ } func testOIDCIdPThroughComponentProxy(ctx context.Context, oc *exutil.CLI, kcSetup *keycloakProxySetup, proxyURL string, trustedCACertPEM []byte, proxyNamespace string) { - const namespace, name = "openshift-authentication", "oauth-openshift" withTrustedCA := len(trustedCACertPEM) > 0 var trustedCAConfigMapName string @@ -427,26 +428,26 @@ func testOIDCIdPThroughComponentProxy(ctx context.Context, oc *exutil.CLI, kcSet proxyTrafficStart := time.Now() - deployment, err := oc.AdminKubeClient().AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeProxy := deployment.Generation - - g.By("Setting component-scoped proxy") + g.By("Setting component-scoped proxy and waiting for the OAuth server rollout") proxyConfig := operatorv1.AuthenticationProxyConfig{ HTTPSProxy: proxyURL, } if withTrustedCA { proxyConfig.TrustedCA = operatorv1.AuthenticationConfigMapReference{Name: trustedCAConfigMapName} } - err = updateAuthenticationProxy(ctx, oc, proxyConfig) - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for operator to pick up proxy changes and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after proxy changes before registering IdP") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeProxy) + err := operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + return updateAuthenticationProxy(ctx, oc, proxyConfig) + }) o.Expect(err).NotTo(o.HaveOccurred()) if withTrustedCA { @@ -455,25 +456,28 @@ func testOIDCIdPThroughComponentProxy(ctx context.Context, oc *exutil.CLI, kcSet o.Expect(err).NotTo(o.HaveOccurred()) } - deployment, err = oc.AdminKubeClient().AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeIdP := deployment.Generation - g.By("Registering Keycloak as OIDC IdP (operator discovers it through the proxy)") - idpCleanups, err := addKeycloakOIDCIdPForProxy(ctx, oc, kcSetup) - g.DeferCleanup(func() { - _ = removeResources(ctx, idpCleanups...) + err = operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + idpCleanups, err := addKeycloakOIDCIdPForProxy(ctx, oc, kcSetup) + g.DeferCleanup(func(ctx context.Context) error { + err := removeResources(ctx, idpCleanups...) + g.GinkgoWriter.Printf("failed cleanup resources: %v", err) + return nil + }) + return err }) o.Expect(err).NotTo(o.HaveOccurred()) - g.By("Waiting for operator to pick up IdP changes and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after IdP changes") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeIdP) - o.Expect(err).NotTo(o.HaveOccurred()) - g.By("Verifying OAuth server deployment has proxy env vars and trustedCA volume/mount") err = verifyOAuthServerDeploymentProxyConfig(ctx, oc, "", proxyURL, ".cluster.local,.svc,127.0.0.1,localhost", withTrustedCA) o.Expect(err).NotTo(o.HaveOccurred()) @@ -493,7 +497,6 @@ func testOIDCIdPThroughComponentProxy(ctx context.Context, oc *exutil.CLI, kcSet } func testFallbackOnProxyRemoval(ctx context.Context, oc *exutil.CLI, cleanups *[]removalFunc, kcSetup *keycloakProxySetup, httpProxyURL string, proxyNamespace string) { - const namespace, name = "openshift-authentication", "oauth-openshift" g.By("Setting direct access grant for oauth flow") err := enableDirectAccessGrant(kcSetup) o.Expect(err).NotTo(o.HaveOccurred(), "direct access grant should be enabled") @@ -502,57 +505,60 @@ func testFallbackOnProxyRemoval(ctx context.Context, oc *exutil.CLI, cleanups *[ kcUser, kcPass, err := createKeycloakUserPassword(ctx, oc, cleanups, kcSetup) o.Expect(err).NotTo(o.HaveOccurred()) - deployment, err := oc.AdminKubeClient().AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeProxy := deployment.Generation - - g.By("Setting component-scoped proxy") - err = updateAuthenticationProxy(ctx, oc, operatorv1.AuthenticationProxyConfig{ - HTTPSProxy: httpProxyURL, + g.By("Setting component-scoped proxy and waiting for the OAuth server rollout") + err = operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + return updateAuthenticationProxy(ctx, oc, operatorv1.AuthenticationProxyConfig{ + HTTPSProxy: httpProxyURL, + }) }) o.Expect(err).NotTo(o.HaveOccurred()) - g.By("Waiting for operator to pick up proxy changes and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after proxy changes before registering IdP") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeProxy) - o.Expect(err).NotTo(o.HaveOccurred()) - - deployment, err = oc.AdminKubeClient().AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeIdP := deployment.Generation - g.By("Registering Keycloak as OIDC IdP") - idpCleanups, err := addKeycloakOIDCIdPForProxy(ctx, oc, kcSetup) - g.DeferCleanup(func() { - _ = removeResources(ctx, idpCleanups...) + err = operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + idpCleanups, err := addKeycloakOIDCIdPForProxy(ctx, oc, kcSetup) + g.DeferCleanup(func() error { + err := removeResources(ctx, idpCleanups...) + g.GinkgoWriter.Printf("failed cleanup resources: %v", err) + return nil + }) + return err }) o.Expect(err).NotTo(o.HaveOccurred()) - g.By("Waiting for operator to pick up IdP changes and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after IdP changes") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeIdP) - o.Expect(err).NotTo(o.HaveOccurred()) - - deployment, err = oc.AdminKubeClient().AppsV1().Deployments("openshift-authentication").Get(ctx, "oauth-openshift", metav1.GetOptions{}) - o.Expect(err).NotTo(o.HaveOccurred()) - generationBeforeProxyRemoval := deployment.Generation - - g.By("Removing spec.proxy from Authentication CR") - err = updateAuthenticationProxy(ctx, oc, operatorv1.AuthenticationProxyConfig{}) - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for operator to pick up proxy removal and stabilize") - err = waitForOperatorToPickUpChanges(ctx, oc, "authentication") - o.Expect(err).NotTo(o.HaveOccurred()) - - g.By("Waiting for OAuth server rollout after proxy removal") - err = waitForDeploymentRollout(ctx, oc, name, namespace, generationBeforeProxyRemoval) + g.By("Removing spec.proxy from Authentication CR and waiting for the OAuth server rollout") + err = operatorwait.Chain( + operatorwait.RolloutWaiter{ + CLI: oc, + DeploymentName: "oauth-openshift", + DeploymentNamespace: "openshift-authentication", + }.MutateAndWaitForDeploymentRollout, + operatorwait.OperatorChangesWaiter{ + CLI: oc, + OperatorName: "authentication", + }.WaitChanges, + )(ctx, func() error { + return updateAuthenticationProxy(ctx, oc, operatorv1.AuthenticationProxyConfig{}) + }) o.Expect(err).NotTo(o.HaveOccurred()) g.By("Verifying proxy env vars are no longer set on OAuth server deployment") diff --git a/test/extended/authentication/component_proxy_helpers.go b/test/extended/authentication/component_proxy_helpers.go index 934084644229..2a57461d9508 100644 --- a/test/extended/authentication/component_proxy_helpers.go +++ b/test/extended/authentication/component_proxy_helpers.go @@ -27,6 +27,7 @@ import ( watchtools "k8s.io/client-go/tools/watch" "k8s.io/client-go/util/retry" + operatorwait "github.com/openshift/origin/test/extended/authentication/operatorwait" exutil "github.com/openshift/origin/test/extended/util" "github.com/openshift/origin/test/extended/util/image" ) @@ -104,7 +105,7 @@ func saveAndRestoreAuthState(ctx context.Context, oc *exutil.CLI) (removalFunc, if changed { g.GinkgoWriter.Println("cleanup: waiting for operator to stabilize") - if err := waitForOperatorToPickUpChanges(ctx, oc, "authentication"); err != nil { + if err := operatorwait.WaitForOperatorToPickUpChanges(ctx, oc, "authentication"); err != nil { g.GinkgoWriter.Printf("cleanup: operator did not recover: %v\n", err) } } diff --git a/test/extended/authentication/operatorwait/chain.go b/test/extended/authentication/operatorwait/chain.go new file mode 100644 index 000000000000..2e33bb8e71a8 --- /dev/null +++ b/test/extended/authentication/operatorwait/chain.go @@ -0,0 +1,93 @@ +package operatorwait + +import ( + "context" + "fmt" + "slices" + + g "github.com/onsi/ginkgo/v2" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + exutil "github.com/openshift/origin/test/extended/util" +) + +// Wrapper decorates a mutation (next) with behavior that must run around it, such as +// capturing pre-mutation state or waiting for the cluster to reconcile afterwards. +type Wrapper func(ctx context.Context, next func() error) error + +// Chain composes wrappers into a single Wrapper. The FIRST wrapper is the OUTERMOST: +// given Chain(A, B)(ctx, f) the execution order is A-before -> B-before -> f -> B-after +// -> A-after. Order the wrappers from outermost concern to innermost; an error in a +// before step short-circuits everything inner to it and skips all afters. +func Chain(wrappers ...Wrapper) Wrapper { + return func(ctx context.Context, final func() error) error { + g.GinkgoHelper() + + current := final + for _, w := range slices.Backward(wrappers) { + next := current + current = func() error { + return w(ctx, next) + } + } + return current() + } +} + +// OperatorChangesWaiter is a Wrapper source that applies a mutation and then waits for +// the named cluster operator to pick the change up and settle. +type OperatorChangesWaiter struct { + CLI *exutil.CLI + OperatorName string +} + +// WaitChanges runs mutate, then waits for the operator to reconcile and settle. As a +// Wrapper it must be placed INNER of a rollout wrapper so the operator's +// Progressing=True window is observed right after the mutation (see Chain ordering). +func (ow OperatorChangesWaiter) WaitChanges( + ctx context.Context, + mutate func() error, +) error { + g.GinkgoHelper() + g.By("Applying changes to operator " + ow.OperatorName) + err := mutate() + if err != nil { + return fmt.Errorf("mutation error: %w", err) + } + + g.By("Waiting for operator " + ow.OperatorName + " to pick up changes and stabilize") + return WaitForOperatorToPickUpChanges(ctx, ow.CLI, ow.OperatorName) +} + +// RolloutWaiter is a Wrapper source that captures a Deployment's generation before a +// mutation and then waits for that Deployment to complete a new rollout. +type RolloutWaiter struct { + CLI *exutil.CLI + DeploymentNamespace, DeploymentName string +} + +// MutateAndWaitForDeploymentRollout captures the Deployment generation before mutate +// runs, then waits for the Deployment to roll past the captured generation. Capturing +// before the mutation keeps ready replicas from the previous configuration from +// satisfying the rollout check. As a Wrapper it must be the OUTERMOST. +func (rw RolloutWaiter) MutateAndWaitForDeploymentRollout( + ctx context.Context, + mutate func() error, +) error { + g.GinkgoHelper() + + deployment, err := rw.CLI.AdminKubeClient(). + AppsV1(). + Deployments(rw.DeploymentNamespace). + Get(ctx, rw.DeploymentName, metav1.GetOptions{}) + if err != nil { + return fmt.Errorf("getting deployment %s/%s before mutation: %w", rw.DeploymentNamespace, rw.DeploymentName, err) + } + previousGeneration := deployment.Generation + + if err := mutate(); err != nil { + return fmt.Errorf("applying mutation: %w", err) + } + + return WaitForDeploymentRollout(ctx, rw.CLI, rw.DeploymentName, rw.DeploymentNamespace, previousGeneration) +} diff --git a/test/extended/authentication/operatorwait/chain_test.go b/test/extended/authentication/operatorwait/chain_test.go new file mode 100644 index 000000000000..20db5ea0bcf6 --- /dev/null +++ b/test/extended/authentication/operatorwait/chain_test.go @@ -0,0 +1,117 @@ +package operatorwait + +import ( + "context" + "fmt" + "reflect" + "testing" +) + +// recordWrapper returns a Wrapper that appends ":before" before calling next and +// ":after" after, so tests can assert composition order. failBefore/failAfter make +// it return an error at the corresponding point. +func recordWrapper(log *[]string, name string, failBefore, failAfter bool) Wrapper { + return func(ctx context.Context, next func() error) error { + *log = append(*log, name+":before") + if failBefore { + return fmt.Errorf("%s before error", name) + } + if err := next(); err != nil { + return err + } + *log = append(*log, name+":after") + if failAfter { + return fmt.Errorf("%s after error", name) + } + return nil + } +} + +func TestChain(t *testing.T) { + tests := []struct { + name string + wrappers func(log *[]string) []Wrapper + finalErr bool + wantOrder []string + wantErr bool + }{ + { + name: "empty chain runs only final", + wrappers: func(*[]string) []Wrapper { return nil }, + wantOrder: []string{"final"}, + }, + { + name: "single wrapper brackets final", + wrappers: func(log *[]string) []Wrapper { + return []Wrapper{recordWrapper(log, "A", false, false)} + }, + wantOrder: []string{"A:before", "final", "A:after"}, + }, + { + name: "first wrapper is outermost", + wrappers: func(log *[]string) []Wrapper { + return []Wrapper{ + recordWrapper(log, "A", false, false), + recordWrapper(log, "B", false, false), + } + }, + wantOrder: []string{"A:before", "B:before", "final", "B:after", "A:after"}, + }, + { + name: "outer wrapper failing before next short-circuits inner and final", + wrappers: func(log *[]string) []Wrapper { + return []Wrapper{ + recordWrapper(log, "A", true, false), + recordWrapper(log, "B", false, false), + } + }, + wantOrder: []string{"A:before"}, + wantErr: true, + }, + { + name: "inner wrapper failing before next runs no final and no afters", + wrappers: func(log *[]string) []Wrapper { + return []Wrapper{ + recordWrapper(log, "A", false, false), + recordWrapper(log, "B", true, false), + } + }, + wantOrder: []string{"A:before", "B:before"}, + wantErr: true, + }, + { + name: "final error propagates and skips afters", + wrappers: func(log *[]string) []Wrapper { + return []Wrapper{ + recordWrapper(log, "A", false, false), + recordWrapper(log, "B", false, false), + } + }, + finalErr: true, + wantOrder: []string{"A:before", "B:before", "final"}, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var log []string + final := func() error { + log = append(log, "final") + if tt.finalErr { + return fmt.Errorf("final error") + } + return nil + } + + err := Chain(tt.wrappers(&log)...)(context.Background(), final) + + if tt.wantErr != (err != nil) { + t.Fatalf("Chain error = %v, wantErr = %v", err, tt.wantErr) + } + if !reflect.DeepEqual(log, tt.wantOrder) { + t.Fatalf("execution order = %v, want %v", log, tt.wantOrder) + } + }) + } +} diff --git a/test/extended/authentication/operator_status_helpers.go b/test/extended/authentication/operatorwait/status.go similarity index 88% rename from test/extended/authentication/operator_status_helpers.go rename to test/extended/authentication/operatorwait/status.go index 44e5e9d72746..2eb24ac32375 100644 --- a/test/extended/authentication/operator_status_helpers.go +++ b/test/extended/authentication/operatorwait/status.go @@ -1,4 +1,4 @@ -package authentication +package operatorwait import ( "context" @@ -14,7 +14,10 @@ import ( operator "github.com/openshift/origin/test/extended/util/operator" ) -func waitForOperatorToPickUpChanges(ctx context.Context, oc *exutil.CLI, name string) error { +// WaitForOperatorToPickUpChanges waits for the named operator to show Progressing=True +// after a mutation and then for all operators to settle. A missed Progressing=True window +// (fast reconcile) is tolerated and only logged. +func WaitForOperatorToPickUpChanges(ctx context.Context, oc *exutil.CLI, name string) error { progressCtx, cancel := context.WithTimeout(ctx, 5*time.Minute) defer cancel() if err := exutil.WaitForOperatorProgressingTrue(progressCtx, oc.AdminConfigClient(), name); err != nil { @@ -23,10 +26,10 @@ func waitForOperatorToPickUpChanges(ctx context.Context, oc *exutil.CLI, name st return operator.WaitForOperatorsToSettle(ctx, oc.AdminConfigClient(), 10) } -// waitForDeploymentRollout waits for a new Deployment generation and its complete +// WaitForDeploymentRollout waits for a new Deployment generation and its complete // rollout. Capture previousGeneration before changing configuration that triggers // a rollout so that ready replicas from the previous configuration cannot satisfy it. -func waitForDeploymentRollout( +func WaitForDeploymentRollout( ctx context.Context, oc *exutil.CLI, name, namespace string, diff --git a/test/extended/authentication/operator_status_helpers_test.go b/test/extended/authentication/operatorwait/status_test.go similarity index 99% rename from test/extended/authentication/operator_status_helpers_test.go rename to test/extended/authentication/operatorwait/status_test.go index 0598a0713b85..eeac987c66c3 100644 --- a/test/extended/authentication/operator_status_helpers_test.go +++ b/test/extended/authentication/operatorwait/status_test.go @@ -1,4 +1,4 @@ -package authentication +package operatorwait import ( "testing"