k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/experiment/kind-conformance-image-e2e.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2018 The Kubernetes Authors.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  set -o errexit
    17  set -o nounset
    18  set -o pipefail
    19  set -o xtrace
    20  
    21  # our exit handler (trap)
    22  cleanup() {
    23      # always attempt to dump logs
    24      kind "export" logs "${ARTIFACTS}/logs" || true
    25      # KIND_IS_UP is true once we: kind create
    26      if [[ "${KIND_IS_UP:-}" = true ]]; then
    27          kind delete cluster || true
    28      fi
    29      # clean up e2e.test symlink
    30      rm -f _output/bin/e2e.test
    31      # remove our tempdir
    32      # NOTE: this needs to be last, or it will prevent kind delete
    33      if [[ -n "${TMP_DIR:-}" ]]; then
    34          rm -rf "${TMP_DIR}"
    35      fi
    36  }
    37  
    38  # install kind to a tempdir GOPATH from this script's kind checkout
    39  install_kind() {
    40      # install `kind` to tempdir
    41      TMP_DIR=$(mktemp -d)
    42      curl -sLo "${TMP_DIR}/kind" https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
    43      chmod +x "${TMP_DIR}/kind"
    44      PATH="${TMP_DIR}:${PATH}"
    45      export PATH
    46  }
    47  
    48  # build kubernetes / node image, e2e binaries
    49  build() {
    50      # build the node image w/ kubernetes
    51      kind build node-image --kube-root="${PWD}"
    52  
    53      # ensure kubectl is in path
    54      local maybe_kubectl
    55      maybe_kubectl="$(find "${PWD}/_output" -name "kubectl" -type f)"
    56      if [[ -n "${maybe_kubectl}" ]]; then
    57          PATH="$(dirname "${maybe_kubectl}"):${PATH}"
    58          export PATH
    59      fi
    60  
    61      # release some memory after building
    62      sync || true
    63      echo 1 > /proc/sys/vm/drop_caches || true
    64  }
    65  
    66  
    67  # up a cluster with kind
    68  create_cluster() {
    69      # create the config file
    70      cat <<EOF > "${ARTIFACTS}/kind-config.yaml"
    71  # config for 1 control plane node and 2 workers
    72  # necessary for conformance
    73  kind: Cluster
    74  apiVersion: kind.x-k8s.io/v1alpha4
    75  nodes:
    76  # the control plane node
    77  - role: control-plane
    78  - role: worker
    79  - role: worker
    80  EOF
    81      # mark the cluster as up for cleanup
    82      # even if kind create fails, kind delete can clean up after it
    83      KIND_IS_UP=true
    84  
    85      KUBECONFIG="${HOME}/.kube/kind-config-default"
    86      export KUBECONFIG
    87  
    88      # actually create, with:
    89      # - do not delete created nodes from a failed cluster create (for debugging)
    90      # - wait up to one minute for the nodes to be "READY"
    91      # - set log leve to debug
    92      # - use our multi node config
    93      kind create cluster \
    94          --image=kindest/node:latest \
    95          --retain \
    96          --wait=1m \
    97          --loglevel=debug \
    98          "--config=${ARTIFACTS}/kind-config.yaml"
    99  }
   100  
   101  
   102  run_tests() {
   103    # binaries needed by the conformance image
   104    rm -rf _output/bin
   105    # after https://github.com/kubernetes/kubernetes/pull/103874
   106    NEW_CONFORMANCE_DIR="test/conformance/image"
   107    # before https://github.com/kubernetes/kubernetes/pull/103874
   108    OLD_CONFORMANCE_DIR="cluster/images/conformance"
   109  
   110    # Ginkgo v1 is deprecated, perfer to build Ginkgo V2.
   111    GINKGO_SRC_V2="vendor/github.com/onsi/ginkgo/v2/ginkgo"
   112    if [ -d "$GINKGO_SRC_V2" ]; then
   113        GINKGO_SRC_DIR="$GINKGO_SRC_V2"
   114    else
   115        GINKGO_SRC_DIR="vendor/github.com/onsi/ginkgo/ginkgo"
   116    fi
   117  
   118    if [ -d "${NEW_CONFORMANCE_DIR}/go-runner" ]; then
   119        make WHAT="test/e2e/e2e.test $GINKGO_SRC_DIR cmd/kubectl ${NEW_CONFORMANCE_DIR}/go-runner"
   120    elif [ -d "${OLD_CONFORMANCE_DIR}/go-runner" ]; then
   121        make WHAT="test/e2e/e2e.test $GINKGO_SRC_DIR cmd/kubectl ${OLD_CONFORMANCE_DIR}/go-runner"
   122    else
   123        make WHAT="test/e2e/e2e.test $GINKGO_SRC_DIR cmd/kubectl"
   124    fi
   125  
   126    # grab the version number for kubernetes
   127    export KUBE_ROOT=${PWD}
   128    source "${KUBE_ROOT}/hack/lib/version.sh"
   129    kube::version::get_version_vars
   130  
   131    VERSION=$(echo -n "${KUBE_GIT_VERSION}" | cut -f 1 -d '+')
   132    export VERSION
   133  
   134    if [ -d "${NEW_CONFORMANCE_DIR}" ]; then
   135        pushd "${PWD}/${NEW_CONFORMANCE_DIR}"
   136    elif [ -d "${OLD_CONFORMANCE_DIR}" ]; then
   137        pushd "${PWD}/${OLD_CONFORMANCE_DIR}"
   138    else
   139        echo "Conformance dir not found"
   140        exit 1
   141    fi
   142  
   143    # build and load the conformance image into the kind nodes
   144    make build ARCH=amd64
   145    kind load docker-image registry.k8s.io/conformance-amd64:${VERSION}
   146  
   147    # patch the image in manifest
   148    sed -i "s|conformance-amd64:.*|conformance-amd64:${VERSION}|g" conformance-e2e.yaml
   149    ./conformance-e2e.sh
   150  
   151    popd
   152  
   153    # extract the test results
   154    NODE_NAME=$(kubectl get po -n conformance e2e-conformance-test -o 'jsonpath={.spec.nodeName}')
   155    docker exec "${NODE_NAME}" tar cvf - /tmp/results | tar -C "${ARTIFACTS}" --strip-components 2 -xf -
   156  }
   157  
   158  # setup kind, build kubernetes, create a cluster, run the e2es
   159  main() {
   160      # ensure artifacts exists when not in CI
   161      ARTIFACTS="${ARTIFACTS:-${PWD}/_artifacts}"
   162      mkdir -p "${ARTIFACTS}"
   163      export ARTIFACTS
   164      # now build an run the cluster and tests
   165      trap cleanup EXIT
   166      install_kind
   167      build
   168      create_cluster
   169      run_tests
   170  }
   171  
   172  main