From 02a47f18ece046e16916bab2b9ebc0906efba55f Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Thu, 10 Jun 2021 12:42:14 +0200 Subject: [PATCH] use the kubernetesversions package of cluster-api again --- go.mod | 6 +- go.sum | 10 +- ...injection_script_control_plane.envsubst.sh | 116 ---------- ...debian_injection_script_worker.envsubst.sh | 116 ---------- .../data/kustomization.yaml | 20 -- .../e2e/shared/kubernetesversions/template.go | 218 ------------------ test/e2e/shared/suite.go | 3 +- 7 files changed, 10 insertions(+), 479 deletions(-) delete mode 100644 test/e2e/shared/kubernetesversions/data/debian_injection_script_control_plane.envsubst.sh delete mode 100644 test/e2e/shared/kubernetesversions/data/debian_injection_script_worker.envsubst.sh delete mode 100644 test/e2e/shared/kubernetesversions/data/kustomization.yaml delete mode 100644 test/e2e/shared/kubernetesversions/template.go diff --git a/go.mod b/go.mod index 0fe9afed14..d09637baf3 100644 --- a/go.mod +++ b/go.mod @@ -18,10 +18,10 @@ require ( k8s.io/client-go v0.21.2 k8s.io/component-base v0.21.2 k8s.io/klog/v2 v2.9.0 - k8s.io/utils v0.0.0-20210527160623-6fdb442a123b + k8s.io/utils v0.0.0-20210629042839-4a2b36d8d73f sigs.k8s.io/cluster-api v0.4.0 - sigs.k8s.io/cluster-api/test v0.4.0 - sigs.k8s.io/controller-runtime v0.9.1 + sigs.k8s.io/cluster-api/test v0.4.1-0.20210713191401-bc94459d95c6 + sigs.k8s.io/controller-runtime v0.9.2 sigs.k8s.io/yaml v1.2.0 ) diff --git a/go.sum b/go.sum index 437fe993d5..08452959e1 100644 --- a/go.sum +++ b/go.sum @@ -1406,8 +1406,9 @@ k8s.io/kubectl v0.21.2/go.mod h1:PgeUclpG8VVmmQIl8zpLar3IQEpFc9mrmvlwY3CK1xo= k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk= k8s.io/metrics v0.21.2/go.mod h1:wzlOINZMCtWq8dR9gHlyaOemmYlOpAoldEIXE82gAhI= k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= -k8s.io/utils v0.0.0-20210527160623-6fdb442a123b h1:MSqsVQ3pZvPGTqCjptfimO2WjG7A9un2zcpiHkA6M/s= k8s.io/utils v0.0.0-20210527160623-6fdb442a123b/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= +k8s.io/utils v0.0.0-20210629042839-4a2b36d8d73f h1:6Cyc8f2OS555SrragQyv4rQ5G7F2haZ6KY2oxO1wzlE= +k8s.io/utils v0.0.0-20210629042839-4a2b36d8d73f/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= @@ -1416,10 +1417,11 @@ sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyz sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.19/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg= sigs.k8s.io/cluster-api v0.4.0 h1:y9MxtU1uW9r9JtDyOQ/9BRXZEau2PGl2yOIozaxXO0E= sigs.k8s.io/cluster-api v0.4.0/go.mod h1:9ALETQ/6KGZ/kYiqvQGfjOx0CfVGE39d4VP3UrS5B24= -sigs.k8s.io/cluster-api/test v0.4.0 h1:iD9WWGPQWCk85xRWNIdLYKvaZvk3wYD+rTzyqFg7yAg= -sigs.k8s.io/cluster-api/test v0.4.0/go.mod h1:Smn0J1FPtwLmd4ipXLb42RSXhKW+ur46qHWXoOAfpgw= -sigs.k8s.io/controller-runtime v0.9.1 h1:+LAqHAhkVW4lt/jLlrKmnGPA7OORMw/xEUH3Ey1h1Bs= +sigs.k8s.io/cluster-api/test v0.4.1-0.20210713191401-bc94459d95c6 h1:jM7T/F+FpjX0BCdv+tsauwuZBHk2lZ1UGSSlxfnuLkI= +sigs.k8s.io/cluster-api/test v0.4.1-0.20210713191401-bc94459d95c6/go.mod h1:7CaMJ+sHNDvFK2xWWOL2ulUTPFOoieqxhn8Nw2/2g0Y= sigs.k8s.io/controller-runtime v0.9.1/go.mod h1:cTqsgnwSOsYS03XwySYZj8k6vf0+eC4FJRcCgQ9elb4= +sigs.k8s.io/controller-runtime v0.9.2 h1:MnCAsopQno6+hI9SgJHKddzXpmv2wtouZz6931Eax+Q= +sigs.k8s.io/controller-runtime v0.9.2/go.mod h1:TxzMCHyEUpaeuOiZx/bIdc2T81vfs/aKdvJt9wuu0zk= sigs.k8s.io/kind v0.11.1 h1:pVzOkhUwMBrCB0Q/WllQDO3v14Y+o2V0tFgjTqIUjwA= sigs.k8s.io/kind v0.11.1/go.mod h1:fRpgVhtqAWrtLB9ED7zQahUimpUXuG/iHT88xYqEGIA= sigs.k8s.io/kustomize/api v0.8.8/go.mod h1:He1zoK0nk43Pc6NlV085xDXDXTNprtcyKZVm3swsdNY= diff --git a/test/e2e/shared/kubernetesversions/data/debian_injection_script_control_plane.envsubst.sh b/test/e2e/shared/kubernetesversions/data/debian_injection_script_control_plane.envsubst.sh deleted file mode 100644 index 4c621b96c0..0000000000 --- a/test/e2e/shared/kubernetesversions/data/debian_injection_script_control_plane.envsubst.sh +++ /dev/null @@ -1,116 +0,0 @@ -#!/bin/bash - -# Copyright 2020 The Kubernetes Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -## Please note that this file needs to be escaped for envsubst to function - -# shellcheck disable=SC1083,SC2034,SC2066,SC2193 - -set -o nounset -set -o pipefail -set -o errexit - -function retry { - attempt=0 - max_attempts=$${1} - interval=$${2} - shift; shift - until [[ $${attempt} -ge "$${max_attempts}" ]] ; do - attempt=$((attempt+1)) - set +e - eval "$*" && return || echo "failed $${attempt} times: $*" - set -e - sleep "$${interval}" - done - echo "error: reached max attempts at retry($*)" - return 1 -} - -[[ $(id -u) != 0 ]] && SUDO="sudo" || SUDO="" - -USE_CI_ARTIFACTS=${USE_CI_ARTIFACTS:=false} - -if [ ! "${USE_CI_ARTIFACTS}" = true ]; then - echo "No CI Artifacts installation, exiting" - exit 0 -fi - -# This test installs release packages or binaries that are a result of the CI and release builds. -# It runs '... --version' commands to verify that the binaries are correctly installed -# and finally uninstalls the packages. -# For the release packages it tests all versions in the support skew. -LINE_SEPARATOR="*************************************************" -echo "$${LINE_SEPARATOR}" - -## Clusterctl set variables -## -# $${KUBERNETES_VERSION} will be replaced by clusterctl -KUBERNETES_VERSION=${KUBERNETES_VERSION} -## -## End clusterctl set variables - -if [[ "$${KUBERNETES_VERSION}" != "" ]]; then - CI_DIR=/tmp/k8s-ci - mkdir -p "$${CI_DIR}" - declare -a PACKAGES_TO_TEST=("kubectl" "kubelet" "kubeadm") - declare -a CONTAINERS_TO_TEST=("kube-apiserver" "kube-controller-manager" "kube-proxy" "kube-scheduler") - CONTAINER_EXT="tar" - echo "* testing CI version $${KUBERNETES_VERSION}" - # Check for semver - if [[ "$${KUBERNETES_VERSION}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then - CI_URL="https://storage.googleapis.com/kubernetes-release/release/$${KUBERNETES_VERSION}/bin/linux/amd64" - VERSION_WITHOUT_PREFIX="$${KUBERNETES_VERSION#v}" - export DEBIAN_FRONTEND=noninteractive - # sometimes the network is not immediately available, so we have to retry the apt-get update - retry 10 5 "apt-get update" - apt-get install -y apt-transport-https ca-certificates curl - curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - - echo 'deb https://apt.kubernetes.io/ kubernetes-xenial main' >/etc/apt/sources.list.d/kubernetes.list - apt-get update - # replace . with \. - VERSION_REGEX="$${VERSION_WITHOUT_PREFIX//./\\.}" - PACKAGE_VERSION="$(apt-cache madison kubelet | grep "$${VERSION_REGEX}-" | head -n1 | cut -d '|' -f 2 | tr -d '[:space:]')" - for CI_PACKAGE in "$${PACKAGES_TO_TEST[@]}"; do - echo "* installing package: $${CI_PACKAGE} $${PACKAGE_VERSION}" - apt-get install -y "$${CI_PACKAGE}=$${PACKAGE_VERSION}" - done - else - CI_URL="https://storage.googleapis.com/kubernetes-release-dev/ci/$${KUBERNETES_VERSION}/bin/linux/amd64" - for CI_PACKAGE in "$${PACKAGES_TO_TEST[@]}"; do - # Browser: https://console.cloud.google.com/storage/browser/kubernetes-release-dev?project=kubernetes-release-dev - # e.g.: https://storage.googleapis.com/kubernetes-release-dev/ci/v1.21.0-beta.1.378+cf3374e43491c5/bin/linux/amd64/kubectl - echo "* downloading binary: $${CI_URL}/$${CI_PACKAGE}" - wget "$${CI_URL}/$${CI_PACKAGE}" -O "$${CI_DIR}/$${CI_PACKAGE}" - chmod +x "$${CI_DIR}/$${CI_PACKAGE}" - mv "$${CI_DIR}/$${CI_PACKAGE}" "/usr/bin/$${CI_PACKAGE}" - done - systemctl restart kubelet - fi - for CI_CONTAINER in "$${CONTAINERS_TO_TEST[@]}"; do - # Browser: https://console.cloud.google.com/storage/browser/kubernetes-release?project=kubernetes-release - # e.g.: https://storage.googleapis.com/kubernetes-release/release/v1.20.4/bin/linux/amd64/kube-apiserver.tar - echo "* downloading package: $${CI_URL}/$${CI_CONTAINER}.$${CONTAINER_EXT}" - wget "$${CI_URL}/$${CI_CONTAINER}.$${CONTAINER_EXT}" -O "$${CI_DIR}/$${CI_CONTAINER}.$${CONTAINER_EXT}" - $${SUDO} ctr -n k8s.io images import "$${CI_DIR}/$${CI_CONTAINER}.$${CONTAINER_EXT}" || echo "* ignoring expected 'ctr images import' result" - $${SUDO} ctr -n k8s.io images tag "k8s.gcr.io/$${CI_CONTAINER}-amd64:$${KUBERNETES_VERSION//+/_}" "k8s.gcr.io/$${CI_CONTAINER}:$${KUBERNETES_VERSION//+/_}" - $${SUDO} ctr -n k8s.io images tag "k8s.gcr.io/$${CI_CONTAINER}-amd64:$${KUBERNETES_VERSION//+/_}" "gcr.io/kubernetes-ci-images/$${CI_CONTAINER}:$${KUBERNETES_VERSION//+/_}" - done -fi -echo "* checking binary versions" -echo "ctr version: " "$(ctr version)" -echo "kubeadm version: " "$(kubeadm version -o=short)" -echo "kubectl version: " "$(kubectl version --client=true --short=true)" -echo "kubelet version: " "$(kubelet --version)" -echo "$${LINE_SEPARATOR}" diff --git a/test/e2e/shared/kubernetesversions/data/debian_injection_script_worker.envsubst.sh b/test/e2e/shared/kubernetesversions/data/debian_injection_script_worker.envsubst.sh deleted file mode 100644 index 644a0c7ade..0000000000 --- a/test/e2e/shared/kubernetesversions/data/debian_injection_script_worker.envsubst.sh +++ /dev/null @@ -1,116 +0,0 @@ -#!/bin/bash - -# Copyright 2020 The Kubernetes Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -## Please note that this file needs to be escaped for envsubst to function - -# shellcheck disable=SC1083,SC2034,SC2066,SC2193 - -set -o nounset -set -o pipefail -set -o errexit - -function retry { - attempt=0 - max_attempts=$${1} - interval=$${2} - shift; shift - until [[ $${attempt} -ge "$${max_attempts}" ]] ; do - attempt=$((attempt+1)) - set +e - eval "$*" && return || echo "failed $${attempt} times: $*" - set -e - sleep "$${interval}" - done - echo "error: reached max attempts at retry($*)" - return 1 -} - -[[ $(id -u) != 0 ]] && SUDO="sudo" || SUDO="" - -USE_CI_ARTIFACTS=${USE_CI_ARTIFACTS:=false} - -if [ ! "${USE_CI_ARTIFACTS}" = true ]; then - echo "No CI Artifacts installation, exiting" - exit 0 -fi - -# This test installs release packages or binaries that are a result of the CI and release builds. -# It runs '... --version' commands to verify that the binaries are correctly installed -# and finally uninstalls the packages. -# For the release packages it tests all versions in the support skew. -LINE_SEPARATOR="*************************************************" -echo "$${LINE_SEPARATOR}" - -## Clusterctl set variables -## -# $${KUBERNETES_VERSION} will be replaced by clusterctl -KUBERNETES_VERSION=${KUBERNETES_VERSION} -## -## End clusterctl set variables - -if [[ "$${KUBERNETES_VERSION}" != "" ]]; then - CI_DIR=/tmp/k8s-ci - mkdir -p "$${CI_DIR}" - declare -a PACKAGES_TO_TEST=("kubectl" "kubelet" "kubeadm") - declare -a CONTAINERS_TO_TEST=("kube-proxy") - CONTAINER_EXT="tar" - echo "* testing CI version $${KUBERNETES_VERSION}" - # Check for semver - if [[ "$${KUBERNETES_VERSION}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then - CI_URL="https://storage.googleapis.com/kubernetes-release/release/$${KUBERNETES_VERSION}/bin/linux/amd64" - VERSION_WITHOUT_PREFIX="$${KUBERNETES_VERSION#v}" - export DEBIAN_FRONTEND=noninteractive - # sometimes the network is not immediately available, so we have to retry the apt-get update - retry 10 5 "apt-get update" - apt-get install -y apt-transport-https ca-certificates curl - curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - - echo 'deb https://apt.kubernetes.io/ kubernetes-xenial main' >/etc/apt/sources.list.d/kubernetes.list - apt-get update - # replace . with \. - VERSION_REGEX="$${VERSION_WITHOUT_PREFIX//./\\.}" - PACKAGE_VERSION="$(apt-cache madison kubelet | grep "$${VERSION_REGEX}-" | head -n1 | cut -d '|' -f 2 | tr -d '[:space:]')" - for CI_PACKAGE in "$${PACKAGES_TO_TEST[@]}"; do - echo "* installing package: $${CI_PACKAGE} $${PACKAGE_VERSION}" - apt-get install -y "$${CI_PACKAGE}=$${PACKAGE_VERSION}" - done - else - CI_URL="https://storage.googleapis.com/kubernetes-release-dev/ci/$${KUBERNETES_VERSION}/bin/linux/amd64" - for CI_PACKAGE in "$${PACKAGES_TO_TEST[@]}"; do - # Browser: https://console.cloud.google.com/storage/browser/kubernetes-release-dev?project=kubernetes-release-dev - # e.g.: https://storage.googleapis.com/kubernetes-release-dev/ci/v1.21.0-beta.1.378+cf3374e43491c5/bin/linux/amd64/kubectl - echo "* downloading binary: $${CI_URL}/$${CI_PACKAGE}" - wget "$${CI_URL}/$${CI_PACKAGE}" -O "$${CI_DIR}/$${CI_PACKAGE}" - chmod +x "$${CI_DIR}/$${CI_PACKAGE}" - mv "$${CI_DIR}/$${CI_PACKAGE}" "/usr/bin/$${CI_PACKAGE}" - done - systemctl restart kubelet - fi - for CI_CONTAINER in "$${CONTAINERS_TO_TEST[@]}"; do - # Browser: https://console.cloud.google.com/storage/browser/kubernetes-release?project=kubernetes-release - # e.g.: https://storage.googleapis.com/kubernetes-release/release/v1.20.4/bin/linux/amd64/kube-apiserver.tar - echo "* downloading package: $${CI_URL}/$${CI_CONTAINER}.$${CONTAINER_EXT}" - wget "$${CI_URL}/$${CI_CONTAINER}.$${CONTAINER_EXT}" -O "$${CI_DIR}/$${CI_CONTAINER}.$${CONTAINER_EXT}" - $${SUDO} ctr -n k8s.io images import "$${CI_DIR}/$${CI_CONTAINER}.$${CONTAINER_EXT}" || echo "* ignoring expected 'ctr images import' result" - $${SUDO} ctr -n k8s.io images tag "k8s.gcr.io/$${CI_CONTAINER}-amd64:$${KUBERNETES_VERSION//+/_}" "k8s.gcr.io/$${CI_CONTAINER}:$${KUBERNETES_VERSION//+/_}" - $${SUDO} ctr -n k8s.io images tag "k8s.gcr.io/$${CI_CONTAINER}-amd64:$${KUBERNETES_VERSION//+/_}" "gcr.io/kubernetes-ci-images/$${CI_CONTAINER}:$${KUBERNETES_VERSION//+/_}" - done -fi -echo "* checking binary versions" -echo "ctr version: " "$(ctr version)" -echo "kubeadm version: " "$(kubeadm version -o=short)" -echo "kubectl version: " "$(kubectl version --client=true --short=true)" -echo "kubelet version: " "$(kubelet --version)" -echo "$${LINE_SEPARATOR}" diff --git a/test/e2e/shared/kubernetesversions/data/kustomization.yaml b/test/e2e/shared/kubernetesversions/data/kustomization.yaml deleted file mode 100644 index 65b2222b01..0000000000 --- a/test/e2e/shared/kubernetesversions/data/kustomization.yaml +++ /dev/null @@ -1,20 +0,0 @@ -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization -namespace: default -resources: -- ci-artifacts-source-template.yaml -patchesStrategicMerge: -- platform-kustomization.yaml -patchesJson6902: -- path: kubeadmcontrolplane-patch.yaml - target: - group: controlplane.cluster.x-k8s.io - kind: KubeadmControlPlane - name: ".*-control-plane" - version: v1alpha4 -- path: kubeadmconfigtemplate-patch.yaml - target: - group: bootstrap.cluster.x-k8s.io - kind: KubeadmConfigTemplate - name: ".*-md-0" - version: v1alpha4 diff --git a/test/e2e/shared/kubernetesversions/template.go b/test/e2e/shared/kubernetesversions/template.go deleted file mode 100644 index 520d90550f..0000000000 --- a/test/e2e/shared/kubernetesversions/template.go +++ /dev/null @@ -1,218 +0,0 @@ -/* -Copyright 2021 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package kubernetesversions - -import ( - _ "embed" - "errors" - "fmt" - "io/ioutil" - "os" - "os/exec" - "path" - "strings" - - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "sigs.k8s.io/cluster-api/test/framework" - "sigs.k8s.io/yaml" -) - -var ( - //go:embed data/debian_injection_script_control_plane.envsubst.sh - debianInjectionScriptControlPlaneBytes string - - //go:embed data/debian_injection_script_worker.envsubst.sh - debianInjectionScriptWorkerBytes string - - //go:embed data/kustomization.yaml - kustomizationYAMLBytes string -) - -type GenerateCIArtifactsInjectedTemplateForDebianInput struct { - // ArtifactsDirectory is where conformance suite output will go. Defaults to _artifacts - ArtifactsDirectory string - // SourceTemplate is an input YAML clusterctl template which is to have - // the CI artifact script injection - SourceTemplate []byte - // PlatformKustomization is an SMP (strategic-merge-style) patch for adding - // platform specific kustomizations required for use with CI, such as - // referencing a specific image - PlatformKustomization []byte - // KubeadmConfigTemplateName is the name of the KubeadmConfigTemplate resource - // that needs to have the Debian install script injected. Defaults to "${CLUSTER_NAME}-md-0". - KubeadmConfigTemplateName string - // KubeadmControlPlaneName is the name of the KubeadmControlPlane resource - // that needs to have the Debian install script injected. Defaults to "${CLUSTER_NAME}-control-plane". - KubeadmControlPlaneName string - // KubeadmConfigName is the name of a KubeadmConfig that needs kustomizing. To be used in conjunction with MachinePools. Optional. - KubeadmConfigName string -} - -// GenerateCIArtifactsInjectedTemplateForDebian takes a source clusterctl template -// and a platform-specific Kustomize SMP patch and injects a bash script to download -// and install the debian packages for the given Kubernetes version, returning the -// location of the outputted file. -func GenerateCIArtifactsInjectedTemplateForDebian(input GenerateCIArtifactsInjectedTemplateForDebianInput) (string, error) { - if input.SourceTemplate == nil { - return "", errors.New("SourceTemplate must be provided") - } - input.ArtifactsDirectory = framework.ResolveArtifactsDirectory(input.ArtifactsDirectory) - if input.KubeadmConfigTemplateName == "" { - input.KubeadmConfigTemplateName = "${CLUSTER_NAME}-md-0" - } - if input.KubeadmControlPlaneName == "" { - input.KubeadmControlPlaneName = "${CLUSTER_NAME}-control-plane" - } - templateDir := path.Join(input.ArtifactsDirectory, "templates") - overlayDir := path.Join(input.ArtifactsDirectory, "overlay") - - if err := os.MkdirAll(templateDir, 0o750); err != nil { - return "", err - } - if err := os.MkdirAll(overlayDir, 0o750); err != nil { - return "", err - } - - kustomizedTemplate := path.Join(templateDir, "cluster-template-conformance-ci-artifacts.yaml") - - if err := ioutil.WriteFile(path.Join(overlayDir, "kustomization.yaml"), []byte(kustomizationYAMLBytes), 0o600); err != nil { - return "", err - } - - patch, err := generateInjectScriptJSONPatch(input.SourceTemplate, "KubeadmControlPlane", input.KubeadmControlPlaneName, "/spec/kubeadmConfigSpec", "/usr/local/bin/ci-artifacts.sh", debianInjectionScriptControlPlaneBytes) - if err != nil { - return "", err - } - if err := os.WriteFile(path.Join(overlayDir, "kubeadmcontrolplane-patch.yaml"), patch, 0o600); err != nil { - return "", err - } - - patch, err = generateInjectScriptJSONPatch(input.SourceTemplate, "KubeadmConfigTemplate", input.KubeadmConfigTemplateName, "/spec/template/spec", "/usr/local/bin/ci-artifacts.sh", debianInjectionScriptWorkerBytes) - if err != nil { - return "", err - } - if err := os.WriteFile(path.Join(overlayDir, "kubeadmconfigtemplate-patch.yaml"), patch, 0o600); err != nil { - return "", err - } - - if err := os.WriteFile(path.Join(overlayDir, "ci-artifacts-source-template.yaml"), input.SourceTemplate, 0o600); err != nil { - return "", err - } - if err := os.WriteFile(path.Join(overlayDir, "platform-kustomization.yaml"), input.PlatformKustomization, 0o600); err != nil { - return "", err - } - cmd := exec.Command("kustomize", "build", overlayDir) - data, err := cmd.CombinedOutput() - if err != nil { - return "", err - } - if err := os.WriteFile(kustomizedTemplate, data, 0o600); err != nil { - return "", err - } - return kustomizedTemplate, nil -} - -type jsonPatch struct { - Op string `json:"op"` - Path string `json:"path"` - Value interface{} `json:"value"` -} - -// generateInjectScriptJSONPatch generates a JSON patch which injects a script -// * objectKind: is the kind of the object we want to inject the script into -// * objectName: is the name of the object we want to inject the script into -// * jsonPatchPathPrefix: is the prefix of the 'files' and `preKubeadmCommands` arrays where we append the script -// * scriptPath: is the path where the script will be stored at -// * scriptContent: content of the script. -func generateInjectScriptJSONPatch(sourceTemplate []byte, objectKind, objectName, jsonPatchPathPrefix, scriptPath, scriptContent string) ([]byte, error) { - filesPathExists, preKubeadmCommandsPathExists, err := checkIfArraysAlreadyExist(sourceTemplate, objectKind, objectName, jsonPatchPathPrefix) - if err != nil { - return nil, err - } - - var patches []jsonPatch - if !filesPathExists { - patches = append(patches, jsonPatch{ - Op: "add", - Path: fmt.Sprintf("%s/files", jsonPatchPathPrefix), - Value: []interface{}{}, - }) - } - patches = append(patches, jsonPatch{ - Op: "add", - Path: fmt.Sprintf("%s/files/-", jsonPatchPathPrefix), - Value: map[string]string{ - "content": scriptContent, - "owner": "root:root", - "path": scriptPath, - "permissions": "0750", - }, - }) - if !preKubeadmCommandsPathExists { - patches = append(patches, jsonPatch{ - Op: "add", - Path: fmt.Sprintf("%s/preKubeadmCommands", jsonPatchPathPrefix), - Value: []string{}, - }) - } - patches = append(patches, jsonPatch{ - Op: "add", - Path: fmt.Sprintf("%s/preKubeadmCommands/-", jsonPatchPathPrefix), - Value: scriptPath, - }) - - return yaml.Marshal(patches) -} - -// checkIfArraysAlreadyExist check is the 'files' and 'preKubeadmCommands' arrays already exist below jsonPatchPathPrefix. -func checkIfArraysAlreadyExist(sourceTemplate []byte, objectKind, objectName, jsonPatchPathPrefix string) (bool, bool, error) { - yamlDocs := strings.Split(string(sourceTemplate), "---") - for _, yamlDoc := range yamlDocs { - if yamlDoc == "" { - continue - } - var obj unstructured.Unstructured - if err := yaml.Unmarshal([]byte(yamlDoc), &obj); err != nil { - return false, false, err - } - - if obj.GetKind() != objectKind { - continue - } - if obj.GetName() != objectName { - continue - } - - pathSplit := strings.Split(strings.TrimPrefix(jsonPatchPathPrefix, "/"), "/") - // false positive, we intentionally append to another array - //nolint:gocritic - filesPath := append(pathSplit, "files") - // false positive, we intentionally append to another array - //nolint:gocritic - preKubeadmCommandsPath := append(pathSplit, "preKubeadmCommands") - _, filesPathExists, err := unstructured.NestedFieldCopy(obj.Object, filesPath...) - if err != nil { - return false, false, err - } - _, preKubeadmCommandsPathExists, err := unstructured.NestedFieldCopy(obj.Object, preKubeadmCommandsPath...) - if err != nil { - return false, false, err - } - return filesPathExists, preKubeadmCommandsPathExists, nil - } - return false, false, fmt.Errorf("could not find document with kind %q and name %q", objectKind, objectName) -} diff --git a/test/e2e/shared/suite.go b/test/e2e/shared/suite.go index 0876705fd6..e5abe37488 100644 --- a/test/e2e/shared/suite.go +++ b/test/e2e/shared/suite.go @@ -34,9 +34,8 @@ import ( . "github.com/onsi/gomega" "sigs.k8s.io/cluster-api/test/framework" "sigs.k8s.io/cluster-api/test/framework/clusterctl" + "sigs.k8s.io/cluster-api/test/framework/kubernetesversions" "sigs.k8s.io/yaml" - - "sigs.k8s.io/cluster-api-provider-openstack/test/e2e/shared/kubernetesversions" ) type synchronizedBeforeTestSuiteConfig struct {