diff --git a/scripts/ci-e2e-lib.sh b/scripts/ci-e2e-lib.sh index 6457e26f4e45..0502442bbe03 100644 --- a/scripts/ci-e2e-lib.sh +++ b/scripts/ci-e2e-lib.sh @@ -34,6 +34,44 @@ capi:buildDockerImages () { fi } +# k8s::prepareKindestImagesVariables defaults the environment variables KUBERNETES_VERSION, +# KUBERNETES_VERSION_UPGRADE_TO, KUBERNETES_VERSION_UPGRADE_FROM and KUBERNETES_VERSION_LATEST_CI +# depending on what is set in GINKGO_FOCUS. +# Note: We do this to ensure that the kindest/node image gets built if it does +# not already exist, e.g. for pre-releases, but only if necessary. +k8s::prepareKindestImagesVariables() { + if [[ ${GINKGO_FOCUS:-} == *"K8s-Install-ci-latest"* ]]; then + # If the test focuses on [K8s-Install-ci-latest], only default KUBERNETES_VERSION_LATEST_CI + # to the value in the e2e config and only if it is not set. + # Note: We do this because we want to specify KUBERNETES_VERSION_LATEST_CI *only* in the e2e config. + if [[ -z "${KUBERNETES_VERSION_LATEST_CI:-}" ]]; then + KUBERNETES_VERSION_LATEST_CI=$(grep KUBERNETES_VERSION_LATEST_CI: < "$E2E_CONF_FILE" | awk -F'"' '{ print $2}') + echo "Defaulting KUBERNETES_VERSION_LATEST_CI to ${KUBERNETES_VERSION_LATEST_CI} to trigger image build (because env var is not set)" + fi + elif [[ ${GINKGO_FOCUS:-} != *"K8s-Upgrade"* ]]; then + # In any other case which is not [K8s-Upgrade], default KUBERNETES_VERSION if it is not set to make sure + # the corresponding kindest/node image exists. + if [[ -z "${KUBERNETES_VERSION:-}" ]]; then + KUBERNETES_VERSION=$(grep KUBERNETES_VERSION: < "$E2E_CONF_FILE" | awk -F'"' '{ print $2}') + echo "Defaulting KUBERNETES_VERSION to ${KUBERNETES_VERSION} to trigger image build (because env var is not set)" + fi + fi + + # Tests not focusing on [PR-Blocking], [K8s-Install] or [K8s-Install-ci-latest], + # also run upgrade tests so default KUBERNETES_VERSION_UPGRADE_TO and KUBERNETES_VERSION_UPGRADE_FROM + # to the value in the e2e config if they are not set. + if [[ ${GINKGO_FOCUS:-} != *"PR-Blocking"* ]] && [[ ${GINKGO_FOCUS:-} != *"K8s-Install"* ]]; then + if [[ -z "${KUBERNETES_VERSION_UPGRADE_TO:-}" ]]; then + KUBERNETES_VERSION_UPGRADE_TO=$(grep KUBERNETES_VERSION_UPGRADE_TO: < "$E2E_CONF_FILE" | awk -F'"' '{ print $2}') + echo "Defaulting KUBERNETES_VERSION_UPGRADE_TO to ${KUBERNETES_VERSION_UPGRADE_TO} to trigger image build (because env var is not set)" + fi + if [[ -z "${KUBERNETES_VERSION_UPGRADE_FROM:-}" ]]; then + KUBERNETES_VERSION_UPGRADE_FROM=$(grep KUBERNETES_VERSION_UPGRADE_FROM: < "$E2E_CONF_FILE" | awk -F'"' '{ print $2}') + echo "Defaulting KUBERNETES_VERSION_UPGRADE_FROM to ${KUBERNETES_VERSION_UPGRADE_FROM} to trigger image build (because env var is not set)" + fi + fi +} + # k8s::prepareKindestImages checks all the e2e test variables representing a Kubernetes version, # and makes sure a corresponding kindest/node image is available locally. k8s::prepareKindestImages() { @@ -65,15 +103,6 @@ k8s::prepareKindestImages() { kind::prepareKindestImage "$resolveVersion" fi - # If the test focuses on [K8s-Install-ci-latest], default KUBERNETES_VERSION_LATEST_CI - # to the value in the e2e config if it is not set. - # Note: We do this because we want to specify KUBERNETES_VERSION_LATEST_CI *only* in the e2e config. - if [[ ${GINKGO_FOCUS:-} == *"K8s-Install-ci-latest"* ]]; then - if [[ -z "${KUBERNETES_VERSION_LATEST_CI:-}" ]]; then - KUBERNETES_VERSION_LATEST_CI=$(grep KUBERNETES_VERSION_LATEST_CI < "$E2E_CONF_FILE" | awk -F'"' '{ print $2}') - echo "Defaulting KUBERNETES_VERSION_LATEST_CI to ${KUBERNETES_VERSION_LATEST_CI} to trigger image build (because env var is not set)" - fi - fi if [ -n "${KUBERNETES_VERSION_LATEST_CI:-}" ]; then k8s::resolveVersion "KUBERNETES_VERSION_LATEST_CI" "$KUBERNETES_VERSION_LATEST_CI" export KUBERNETES_VERSION_LATEST_CI=$resolveVersion diff --git a/scripts/ci-e2e.sh b/scripts/ci-e2e.sh index 8e45da3e83d1..f8b6e501eb2d 100755 --- a/scripts/ci-e2e.sh +++ b/scripts/ci-e2e.sh @@ -54,6 +54,7 @@ export USE_EXISTING_CLUSTER=false # - KUBERNETES_VERSION_UPGRADE_TO # - KUBERNETES_VERSION_UPGRADE_FROM # - KUBERNETES_VERSION_LATEST_CI +k8s::prepareKindestImagesVariables k8s::prepareKindestImages # pre-pull all the images that will be used in the e2e, thus making the actual test run diff --git a/test/extension/handlers/topologymutation/handler.go b/test/extension/handlers/topologymutation/handler.go index 4b9325b17168..e3bf87636f5e 100644 --- a/test/extension/handlers/topologymutation/handler.go +++ b/test/extension/handlers/topologymutation/handler.go @@ -331,13 +331,13 @@ func patchDockerMachineTemplate(ctx context.Context, dockerMachineTemplate *infr // if found if err == nil { - semVer, err := version.ParseMajorMinorPatchTolerant(cpVersion) + semVer, err := semver.ParseTolerant(cpVersion) if err != nil { return errors.Wrap(err, "could not parse control plane version") } kindMapping := kind.GetMapping(semVer, "") - log.Info(fmt.Sprintf("Setting MachineDeployment custom image to %q", kindMapping.Image)) + log.Info(fmt.Sprintf("Setting control plane custom image to %q", kindMapping.Image)) dockerMachineTemplate.Spec.Template.Spec.CustomImage = kindMapping.Image // return early if we have successfully patched a control plane dockerMachineTemplate return nil @@ -357,7 +357,7 @@ func patchDockerMachineTemplate(ctx context.Context, dockerMachineTemplate *infr return errors.Wrap(err, "could not set customImage to MachineDeployment DockerMachineTemplate") } - semVer, err := version.ParseMajorMinorPatchTolerant(mdVersion) + semVer, err := semver.ParseTolerant(mdVersion) if err != nil { return errors.Wrap(err, "could not parse MachineDeployment version") } diff --git a/test/extension/handlers/topologymutation/handler_test.go b/test/extension/handlers/topologymutation/handler_test.go index 1b693ee064f3..40640835a1e4 100644 --- a/test/extension/handlers/topologymutation/handler_test.go +++ b/test/extension/handlers/topologymutation/handler_test.go @@ -324,6 +324,26 @@ func Test_patchDockerMachineTemplate(t *testing.T) { }, }, }, + { + name: "sets customImage for templates linked to ControlPlane for pre versions", + template: &infrav1.DockerMachineTemplate{}, + variables: map[string]apiextensionsv1.JSON{ + runtimehooksv1.BuiltinsName: {Raw: toJSON(runtimehooksv1.Builtins{ + ControlPlane: &runtimehooksv1.ControlPlaneBuiltins{ + Version: "v1.23.0-rc.0", + }, + })}, + }, + expectedTemplate: &infrav1.DockerMachineTemplate{ + Spec: infrav1.DockerMachineTemplateSpec{ + Template: infrav1.DockerMachineTemplateResource{ + Spec: infrav1.DockerMachineSpec{ + CustomImage: "kindest/node:v1.23.0-rc.0", + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(*testing.T) { diff --git a/test/framework/kubetest/run.go b/test/framework/kubetest/run.go index ed13628d100f..9c7b77fdc41d 100644 --- a/test/framework/kubetest/run.go +++ b/test/framework/kubetest/run.go @@ -219,7 +219,7 @@ func parseKubetestConfig(kubetestConfigFile string) (kubetestConfig, error) { } func isUsingCIArtifactsVersion(k8sVersion string) bool { - return strings.Contains(k8sVersion, "-") + return strings.Contains(k8sVersion, "+") } func discoverClusterKubernetesVersion(proxy framework.ClusterProxy) (string, error) { diff --git a/test/infrastructure/kind/mapper.go b/test/infrastructure/kind/mapper.go index 4e9b08c131db..71b17f180f9a 100644 --- a/test/infrastructure/kind/mapper.go +++ b/test/infrastructure/kind/mapper.go @@ -376,8 +376,8 @@ func GetMapping(k8sVersion semver.Version, customImage string) Mapping { continue } - // If the mapping is for the same patch version, return it - if k8sVersion.Patch == m.KubernetesVersion.Patch { + // If the mapping is for the same patch version and no pre version is defined, return it + if k8sVersion.Patch == m.KubernetesVersion.Patch && len(k8sVersion.Pre) == 0 { return Mapping{ KubernetesVersion: m.KubernetesVersion, Mode: m.Mode,