|
| 1 | +package router |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/types" |
| 10 | + "k8s.io/apimachinery/pkg/util/wait" |
| 11 | + "k8s.io/kubernetes/test/e2e/framework" |
| 12 | + "k8s.io/kubernetes/test/e2e/upgrades" |
| 13 | + |
| 14 | + g "github.com/onsi/ginkgo/v2" |
| 15 | + o "github.com/onsi/gomega" |
| 16 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 17 | + operatorv1client "github.com/openshift/client-go/operator/clientset/versioned" |
| 18 | + exutil "github.com/openshift/origin/test/extended/util" |
| 19 | +) |
| 20 | + |
| 21 | +// HAProxyVersionUpgradeTest verifies if HAProxy version selection behaves |
| 22 | +// as expected during upgrades. |
| 23 | +// Pinned is a test parameter that should define whether the test uses a |
| 24 | +// pinned version during upgrades, or it should upgrade leaving version unset. |
| 25 | +type HAProxyVersionUpgradeTest struct { |
| 26 | + Pinned bool |
| 27 | + // |
| 28 | + oc *exutil.CLI |
| 29 | + operatorClient operatorv1client.Interface |
| 30 | + controllers *ingressControllers |
| 31 | + precheckErr error |
| 32 | + ic types.NamespacedName |
| 33 | + pinnedVersion operatorv1.HAProxyVersion // only used if Pinned is true |
| 34 | +} |
| 35 | + |
| 36 | +func (h *HAProxyVersionUpgradeTest) Name() string { |
| 37 | + if h.Pinned { |
| 38 | + return "haproxy-pinned-version-upgrade" |
| 39 | + } |
| 40 | + return "haproxy-unset-version-upgrade" |
| 41 | +} |
| 42 | + |
| 43 | +func (h *HAProxyVersionUpgradeTest) DisplayName() string { |
| 44 | + if h.Pinned { |
| 45 | + return "[sig-network-edge][Feature:Router][apigroup:route.openshift.io] Verify HAProxy pinned version state during upgrade" |
| 46 | + } |
| 47 | + return "[sig-network-edge][Feature:Router][apigroup:route.openshift.io] Verify HAProxy unset version state during upgrade" |
| 48 | +} |
| 49 | + |
| 50 | +// Skip defines if the test should be skipped. HAProxy version test is skipped |
| 51 | +// if the HAProxy version field cannot be found in the API. |
| 52 | +func (h *HAProxyVersionUpgradeTest) Skip(_ upgrades.UpgradeContext) bool { |
| 53 | + oc := exutil.NewCLIForMonitorTest(h.Name() + "-skip").AsAdmin() |
| 54 | + hasField, err := apiHasHAProxyVersionField(context.Background(), oc) |
| 55 | + if err != nil { |
| 56 | + h.precheckErr = fmt.Errorf("error checking for HAProxy version API: %w", err) |
| 57 | + return false |
| 58 | + } |
| 59 | + |
| 60 | + h.precheckErr = nil |
| 61 | + return !hasField |
| 62 | +} |
| 63 | + |
| 64 | +// Setup configures all the test attributes and creates an IngressController |
| 65 | +// resource that should be verified after the upgrade. |
| 66 | +func (h *HAProxyVersionUpgradeTest) Setup(ctx context.Context, f *framework.Framework) { |
| 67 | + o.Expect(h.precheckErr).NotTo(o.HaveOccurred(), "Skip() precheck failed: could not determine if HAProxy version upgrade test should run") |
| 68 | + |
| 69 | + g.By("Setting up HAProxy version test") |
| 70 | + |
| 71 | + h.oc = exutil.NewCLIWithFramework(f).AsAdmin() |
| 72 | + h.operatorClient = h.oc.AdminOperatorClient() |
| 73 | + h.controllers = &ingressControllers{} |
| 74 | + |
| 75 | + var customIngress func(*operatorv1.IngressController) |
| 76 | + versions, err := getHAProxyVersionParams(ctx, h.oc) |
| 77 | + o.Expect(err).NotTo(o.HaveOccurred(), "error getting HAProxy versions") |
| 78 | + if h.Pinned { |
| 79 | + customIngress = func(ic *operatorv1.IngressController) { |
| 80 | + ic.Spec.HAProxyVersion = versions.defaultVersion |
| 81 | + } |
| 82 | + h.pinnedVersion = versions.defaultVersion |
| 83 | + } else { |
| 84 | + h.pinnedVersion = "" |
| 85 | + } |
| 86 | + |
| 87 | + g.By("Creating the IngressController resource") |
| 88 | + |
| 89 | + const createControllerTimeout = 2 * time.Minute |
| 90 | + ic, err := h.controllers.createIngressController(ctx, h.oc, createControllerTimeout, customIngress) |
| 91 | + o.Expect(err).NotTo(o.HaveOccurred(), "error creating IngressController resource") |
| 92 | + h.ic = types.NamespacedName{ |
| 93 | + Namespace: ic.Namespace, |
| 94 | + Name: ic.Name, |
| 95 | + } |
| 96 | + |
| 97 | + g.By("Checking HAProxy version for Ingress " + ic.Name) |
| 98 | + |
| 99 | + err = waitForHAProxyVersion(ctx, h.oc, ic.Name, versions.defaultVersion) |
| 100 | + o.Expect(err).NotTo(o.HaveOccurred(), "error getting HAProxy version from runtime API") |
| 101 | +} |
| 102 | + |
| 103 | +// Test verifies if the expected HAProxy version is found after the upgrade. |
| 104 | +// Current version is read from the IngressController status and from the |
| 105 | +// HAProxy's runtime API. |
| 106 | +func (h *HAProxyVersionUpgradeTest) Test(ctx context.Context, f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) { |
| 107 | + g.By("Waiting for upgrade to complete") |
| 108 | + <-done |
| 109 | + |
| 110 | + g.By("Validating HAProxy version after upgrade") |
| 111 | + |
| 112 | + var expectedVersion operatorv1.HAProxyVersion |
| 113 | + if h.Pinned { |
| 114 | + expectedVersion = h.pinnedVersion |
| 115 | + } else { |
| 116 | + versions, err := getHAProxyVersionParams(ctx, h.oc) |
| 117 | + o.Expect(err).NotTo(o.HaveOccurred(), "error getting HAProxy versions") |
| 118 | + expectedVersion = versions.defaultVersion |
| 119 | + } |
| 120 | + |
| 121 | + const rollingOutTimeout = 15 * time.Minute |
| 122 | + err := wait.PollUntilContextTimeout(ctx, time.Second, rollingOutTimeout, true, func(ctx context.Context) (ready bool, err error) { |
| 123 | + ic, err := h.operatorClient.OperatorV1().IngressControllers(h.ic.Namespace).Get(ctx, h.ic.Name, metav1.GetOptions{}) |
| 124 | + if err != nil { |
| 125 | + framework.Logf("error getting IngressController resource: %s", err.Error()) |
| 126 | + return false, nil |
| 127 | + } |
| 128 | + if ic.Status.EffectiveHAProxyVersion != expectedVersion { |
| 129 | + framework.Logf("HAProxy version from IngressResource status %q does not match expected value %q", ic.Status.EffectiveHAProxyVersion, expectedVersion) |
| 130 | + return false, nil |
| 131 | + } |
| 132 | + return true, nil |
| 133 | + }) |
| 134 | + o.Expect(err).NotTo(o.HaveOccurred(), "timed out waiting for EffectiveHAProxyVersion to match expected version") |
| 135 | + |
| 136 | + g.By("Validating HAProxy version from runtime API") |
| 137 | + |
| 138 | + err = waitForHAProxyVersion(ctx, h.oc, h.ic.Name, expectedVersion) |
| 139 | + o.Expect(err).NotTo(o.HaveOccurred(), "error getting HAProxy version from runtime API") |
| 140 | +} |
| 141 | + |
| 142 | +// Teardown removes the configured IngressController after the test runs. |
| 143 | +func (h *HAProxyVersionUpgradeTest) Teardown(ctx context.Context, f *framework.Framework) { |
| 144 | + if h.operatorClient == nil { |
| 145 | + framework.Logf("Skipping cleanup because setup did not initialize test resources") |
| 146 | + return |
| 147 | + } |
| 148 | + if err := h.controllers.deleteAll(ctx, h.operatorClient); err != nil { |
| 149 | + framework.Logf("error deleting IngressController resource: %s", err.Error()) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// haproxyVersionParams has HAProxy version parameters from the Ingress operator. |
| 154 | +type haproxyVersionParams struct { |
| 155 | + defaultVersion operatorv1.HAProxyVersion |
| 156 | +} |
| 157 | + |
| 158 | +// getHAProxyVersionParams parses the current Ingress operator configuration |
| 159 | +// and extracts HAProxy version parameters. |
| 160 | +func getHAProxyVersionParams(ctx context.Context, oc *exutil.CLI) (haproxyVersionParams, error) { |
| 161 | + operatorNamespace := "openshift-ingress-operator" |
| 162 | + operatorName := "ingress-operator" |
| 163 | + deploy, err := oc.AdminKubeClient().AppsV1().Deployments(operatorNamespace).Get(ctx, operatorName, metav1.GetOptions{}) |
| 164 | + if err != nil { |
| 165 | + return haproxyVersionParams{}, err |
| 166 | + } |
| 167 | + |
| 168 | + containers := deploy.Spec.Template.Spec.Containers |
| 169 | + if len(containers) < 1 { |
| 170 | + return haproxyVersionParams{}, fmt.Errorf("ingress-operator deployment is missing the operator container") |
| 171 | + } |
| 172 | + |
| 173 | + operator := containers[0] |
| 174 | + if operator.Name != "ingress-operator" { |
| 175 | + return haproxyVersionParams{}, fmt.Errorf("ingress-operator deployment has an unexpected container name: %s", operator.Name) |
| 176 | + } |
| 177 | + |
| 178 | + defaultVersion := func() string { |
| 179 | + for _, env := range operator.Env { |
| 180 | + if env.Name == "DEFAULT_HAPROXY_VERSION" { |
| 181 | + return env.Value |
| 182 | + } |
| 183 | + } |
| 184 | + // DEFAULT_HAPROXY_VERSION envvar not found, so this is pre 4.23/5.0, assume "2.8" |
| 185 | + return "2.8" |
| 186 | + }() |
| 187 | + |
| 188 | + return haproxyVersionParams{ |
| 189 | + defaultVersion: operatorv1.HAProxyVersion(defaultVersion), |
| 190 | + }, nil |
| 191 | +} |
0 commit comments