github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/pj-on-kind.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2019 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  # Requires go, docker, and kubectl.
    17  
    18  set -o errexit
    19  set -o nounset
    20  set -o pipefail
    21  
    22  function main() {
    23    # Point kubectl at the mkpod cluster.
    24    export KUBECONFIG="${HOME}/.kube/kind-config-mkpod"
    25    parseArgs "$@"
    26    ensureInstall
    27  
    28    # Generate PJ and Pod.
    29    docker pull gcr.io/k8s-prow/mkpj:latest
    30    docker run -i --rm --user "$(id -u):$(id -g)" -v "${PWD}:${PWD}" -v "${config}:${config}" ${job_config_mnt} -w "${PWD}" gcr.io/k8s-prow/mkpj:latest "--config-path=${config}" "--job=${job}" ${job_config_flag} > "${PWD}/pj.yaml"
    31    docker pull gcr.io/k8s-prow/mkpod:latest
    32    docker run -i --rm --user "$(id -u):$(id -g)" -v "${PWD}:${PWD}" -w "${PWD}" gcr.io/k8s-prow/mkpod:latest --build-id=snowflake "--prow-job=${PWD}/pj.yaml" --local "--out-dir=${out_dir}/${job}" > "${PWD}/pod.yaml"
    33  
    34    # Add any k8s resources that the pod depends on to the kind cluster here. (secrets, configmaps, etc.)
    35  
    36    # Deploy pod and watch.
    37    echo "Applying pod to the mkpod cluster. Configure kubectl for the mkpod cluster with:"
    38    echo ">  export KUBECONFIG='${KUBECONFIG}'"
    39    pod=$(kubectl apply -f "${PWD}/pod.yaml" | cut -d ' ' -f 1)
    40    kubectl get "${pod}" -w
    41  }
    42  
    43  # Prep and check args.
    44  function parseArgs() {
    45    # Use node mounts under /mnt/disks/ so pods behave well on COS nodes too. https://cloud.google.com/container-optimized-os/docs/concepts/disks-and-filesystem
    46    job="${1:-}"
    47    config="${CONFIG_PATH:-}"
    48    job_config_path="${JOB_CONFIG_PATH:-}"
    49    out_dir="${OUT_DIR:-/mnt/disks/prowjob-out}"
    50    kind_config="${KIND_CONFIG:-}"
    51    node_dir="${NODE_DIR:-/mnt/disks/kind-node}"  # Any pod hostPath mounts should be under this dir to reach the true host via the kind node.
    52  
    53    local new_only="  (Only used when creating a new kind cluster.)"
    54    echo "job=${job}"
    55    echo "CONFIG_PATH=${config}"
    56    echo "JOB_CONFIG_PATH=${job_config_path}"
    57    echo "OUT_DIR=${out_dir} ${new_only}"
    58    echo "KIND_CONFIG=${kind_config} ${new_only}"
    59    echo "NODE_DIR=${node_dir} ${new_only}"
    60  
    61    if [[ -z "${job}" ]]; then
    62      echo "Must specify a job name as the first argument."
    63      exit 2
    64    fi
    65    if [[ -z "${config}" ]]; then
    66      echo "Must specify config.yaml location via CONFIG_PATH env var."
    67      exit 2
    68    fi
    69    job_config_flag=""
    70    job_config_mnt=""
    71    if [[ -n "${job_config_path}" ]]; then
    72      job_config_flag="--job-config-path=${job_config_path}"
    73      job_config_mnt="-v ${job_config_path}:${job_config_path}"
    74    fi
    75  }
    76  
    77  # Ensures installation of prow tools, kind, and a kind cluster named "mkpod".
    78  function ensureInstall() {
    79    # Install kind and set up cluster if not already done.
    80    if ! command -v kind >/dev/null 2>&1; then
    81      # Extract the minor version from xx.{minor_version}.xx version format
    82      go_minor_version=$(go version | { read _ _ v _; TMP=${v#*.}; echo ${TMP%.*}; }; )
    83      echo "Current Go minor version: $go_minor_version"
    84      echo "Installing kind..."
    85      if [[ $go_minor_version -ge 18 ]]; then
    86        # `go get` is fully deprecated in Go 1.18, so use `go install` for version >= 18.
    87        GO111MODULE="on" go install sigs.k8s.io/kind@v0.17.0
    88      else
    89        GO111MODULE="on" go get sigs.k8s.io/kind@v0.17.0
    90      fi
    91    fi
    92    local found="false"
    93    for clust in $(kind get clusters); do
    94      if [[ "${clust}" == "mkpod" ]]; then
    95        found="true"
    96        break
    97      fi
    98    done
    99    if [[ "${found}" == "false" ]]; then
   100      # Need to create the "mkpod" kind cluster.
   101      if [[ -n "${kind_config}" ]]; then
   102        kind create cluster --name=mkpod "--config=${kind_config}" --wait=5m
   103      else
   104        # Create a temporary kind config file.
   105        local temp_config="${PWD}/temp-mkpod-kind-config.yaml"
   106        cat <<EOF > "${temp_config}"
   107  kind: Cluster
   108  apiVersion: kind.x-k8s.io/v1alpha4
   109  nodes:
   110    - extraMounts:
   111        - containerPath: ${out_dir}
   112          hostPath: ${out_dir}
   113        # host <-> node mount for hostPath volumes in Pods. (All hostPaths should be under ${node_dir} to reach the host.)
   114        - containerPath: ${node_dir}
   115          hostPath: ${node_dir}
   116  EOF
   117        kind create cluster --name=mkpod "--config=${temp_config}" --wait=5m
   118        rm "${temp_config}"
   119      fi
   120    fi
   121  }
   122  
   123  main "$@"