sigs.k8s.io/cluster-api-provider-azure@v1.14.3/scripts/ci-build-kubernetes.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2021 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  ###############################################################################
    18  
    19  set -o errexit
    20  set -o nounset
    21  set -o pipefail
    22  
    23  REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    24  cd "${REPO_ROOT}" || exit 1
    25  
    26  # shellcheck source=hack/ensure-azcli.sh
    27  source "${REPO_ROOT}/hack/ensure-azcli.sh"
    28  # shellcheck source=hack/ensure-go.sh
    29  source "${REPO_ROOT}/hack/ensure-go.sh"
    30  # shellcheck source=hack/parse-prow-creds.sh
    31  source "${REPO_ROOT}/hack/parse-prow-creds.sh"
    32  # shellcheck source=hack/util.sh
    33  source "${REPO_ROOT}/hack/util.sh"
    34  
    35  : "${AZURE_STORAGE_ACCOUNT:?Environment variable empty or not defined.}"
    36  : "${AZURE_STORAGE_KEY:?Environment variable empty or not defined.}"
    37  : "${REGISTRY:?Environment variable empty or not defined.}"
    38  
    39  declare -a BINARIES=("kubeadm" "kubectl" "kubelet" "e2e.test")
    40  declare -a WINDOWS_BINARIES=("kubeadm" "kubectl" "kubelet" "kube-proxy")
    41  declare -a IMAGES=("kube-apiserver" "kube-controller-manager" "kube-proxy" "kube-scheduler")
    42  
    43  GREP_BINARY="grep"
    44  if [[ "${OSTYPE}" == "darwin"* ]]; then
    45    GREP_BINARY="ggrep"
    46  fi
    47  
    48  setup() {
    49      KUBE_ROOT="$(go env GOPATH)/src/k8s.io/kubernetes"
    50      export KUBE_ROOT
    51  
    52      # shellcheck disable=SC1091
    53      # extract KUBE_GIT_VERSION from k/k
    54      # ref: https://github.com/kubernetes/test-infra/blob/de07aa4b89f1161778856dc0fed310bd816aad72/experiment/kind-conformance-image-e2e.sh#L112-L115
    55      source "${KUBE_ROOT}/hack/lib/version.sh"
    56      pushd "${KUBE_ROOT}" && kube::version::get_version_vars && popd
    57      : "${KUBE_GIT_VERSION:?Environment variable empty or not defined.}"
    58      export KUBE_GIT_VERSION
    59      echo "using KUBE_GIT_VERSION=${KUBE_GIT_VERSION}"
    60  
    61      # allow both TEST_WINDOWS and WINDOWS for backwards compatibility.
    62      export TEST_WINDOWS="${TEST_WINDOWS:-${WINDOWS:-}}"
    63  
    64      # get the latest ci version for a particular release so that kubeadm is
    65      # able to pull existing images before being replaced by custom images
    66      major="$(echo "${KUBE_GIT_VERSION}" | ${GREP_BINARY} -Po "(?<=v)[0-9]+")"
    67      minor="$(echo "${KUBE_GIT_VERSION}" | ${GREP_BINARY} -Po "(?<=v${major}.)[0-9]+")"
    68      CI_VERSION="$(capz::util::get_latest_ci_version "${major}.${minor}")"
    69      export CI_VERSION
    70      echo "using CI_VERSION=${CI_VERSION}"
    71      export KUBERNETES_VERSION="${CI_VERSION}"
    72      echo "using KUBERNETES_VERSION=${KUBERNETES_VERSION}"
    73  
    74      # Docker tags cannot contain '+'
    75      # ref: https://github.com/kubernetes/kubernetes/blob/5491484aa91fd09a01a68042e7674bc24d42687a/build/lib/release.sh#L345-L346
    76      export KUBE_IMAGE_TAG="${KUBE_GIT_VERSION/+/_}"
    77      echo "using K8s KUBE_IMAGE_TAG=${KUBE_IMAGE_TAG}"
    78  
    79      export AZURE_BLOB_CONTAINER_NAME="${AZURE_BLOB_CONTAINER_NAME:-"kubernetes-ci"}"
    80  }
    81  
    82  main() {
    83      if [[ "$(az storage container exists --name "${AZURE_BLOB_CONTAINER_NAME}" --query exists --output tsv)" == "false" ]]; then
    84          echo "Creating ${AZURE_BLOB_CONTAINER_NAME} storage container"
    85          az storage container create --name "${AZURE_BLOB_CONTAINER_NAME}" > /dev/null
    86          az storage container set-permission --name "${AZURE_BLOB_CONTAINER_NAME}" --public-access container > /dev/null
    87      fi
    88  
    89      if [[ "${KUBE_BUILD_CONFORMANCE:-}" =~ [yY] ]]; then
    90          IMAGES+=("conformance")
    91          # consume by the conformance test suite
    92          export CONFORMANCE_IMAGE="${REGISTRY}/conformance:${KUBE_IMAGE_TAG}"
    93      fi
    94  
    95      if [[ "$(can_reuse_artifacts)" == "false" ]]; then
    96          echo "Building Kubernetes"
    97  
    98          # TODO(chewong): support multi-arch and Windows build
    99          make -C "${KUBE_ROOT}" quick-release
   100  
   101          if [[ "${KUBE_BUILD_CONFORMANCE:-}" =~ [yY] ]]; then
   102              # rename conformance image since it is the only image that has an amd64 suffix
   103              mv "${KUBE_ROOT}"/_output/release-images/amd64/conformance-amd64.tar "${KUBE_ROOT}"/_output/release-images/amd64/conformance.tar
   104          fi
   105  
   106          for IMAGE_NAME in "${IMAGES[@]}"; do
   107              # extract docker image URL form `docker load` output
   108              OLD_IMAGE_URL="$(docker load --input "${KUBE_ROOT}/_output/release-images/amd64/${IMAGE_NAME}.tar" | ${GREP_BINARY} -oP '(?<=Loaded image: )[^ ]*' | head -n 1)"
   109              NEW_IMAGE_URL="${REGISTRY}/${IMAGE_NAME}:${KUBE_IMAGE_TAG}"
   110              # retag and push images to ACR
   111              docker tag "${OLD_IMAGE_URL}" "${NEW_IMAGE_URL}" && docker push "${NEW_IMAGE_URL}"
   112          done
   113  
   114          echo "Uploading binaries to Azure storage container ${AZURE_BLOB_CONTAINER_NAME}"
   115  
   116          for BINARY in "${BINARIES[@]}"; do
   117              BIN_PATH="${KUBE_GIT_VERSION}/bin/linux/amd64/${BINARY}"
   118              echo "uploading ${BIN_PATH}"
   119              az storage blob upload --overwrite --container-name "${AZURE_BLOB_CONTAINER_NAME}" --file "${KUBE_ROOT}/_output/dockerized/bin/linux/amd64/${BINARY}" --name "${BIN_PATH}"
   120          done
   121  
   122          if [[ "${TEST_WINDOWS:-}" == "true" ]]; then
   123              echo "Building Kubernetes Windows binaries"
   124  
   125              for BINARY in "${WINDOWS_BINARIES[@]}"; do
   126                  "${KUBE_ROOT}"/build/run.sh make WHAT=cmd/"${BINARY}" KUBE_BUILD_PLATFORMS=windows/amd64 KUBE_VERBOSE=0
   127              done
   128  
   129              for BINARY in "${WINDOWS_BINARIES[@]}"; do
   130                  BIN_PATH="${KUBE_GIT_VERSION}/bin/windows/amd64/${BINARY}.exe"
   131                  echo "uploading ${BIN_PATH}"
   132                  az storage blob upload --overwrite --container-name "${AZURE_BLOB_CONTAINER_NAME}" --file "${KUBE_ROOT}/_output/dockerized/bin/windows/amd64/${BINARY}.exe" --name "${BIN_PATH}"
   133              done
   134          fi
   135      fi
   136  }
   137  
   138  # can_reuse_artifacts returns true if there exists Kubernetes artifacts built from a PR that we can reuse
   139  can_reuse_artifacts() {
   140      for IMAGE_NAME in "${IMAGES[@]}"; do
   141          if ! docker pull "${REGISTRY}/${IMAGE_NAME}:${KUBE_IMAGE_TAG}"; then
   142              echo "false" && return
   143          fi
   144      done
   145  
   146      for BINARY in "${BINARIES[@]}"; do
   147          if [[ "$(az storage blob exists --container-name "${AZURE_BLOB_CONTAINER_NAME}" --name "${KUBE_GIT_VERSION}/bin/linux/amd64/${BINARY}" --query exists --output tsv)" == "false" ]]; then
   148              echo "false" && return
   149          fi
   150      done
   151  
   152      if [[ "${TEST_WINDOWS:-}" == "true" ]]; then
   153          for BINARY in "${WINDOWS_BINARIES[@]}"; do
   154              if [[ "$(az storage blob exists --container-name "${AZURE_BLOB_CONTAINER_NAME}" --name "${KUBE_GIT_VERSION}/bin/windows/amd64/${BINARY}.exe" --query exists --output tsv)" == "false" ]]; then
   155                  echo "false" && return
   156              fi
   157          done
   158      fi
   159  
   160      echo "true"
   161  }
   162  
   163  capz::ci-build-kubernetes::cleanup() {
   164      if [[ -d "${KUBE_ROOT:-}" ]]; then
   165          make -C "${KUBE_ROOT}" clean || true
   166      fi
   167  }
   168  
   169  trap capz::ci-build-kubernetes::cleanup EXIT
   170  
   171  setup
   172  main