github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/scripts/lib/etcd.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2014 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  # A set of helpers for starting/running etcd for tests
    18  
    19  ETCD_VERSION=${ETCD_VERSION:-3.4.3}
    20  ETCD_HOST=${ETCD_HOST:-127.0.0.1}
    21  ETCD_PORT=${ETCD_PORT:-2379}
    22  export KUBE_INTEGRATION_ETCD_URL="http://${ETCD_HOST}:${ETCD_PORT}"
    23  
    24  kube::etcd::validate() {
    25    # validate if in path
    26    command -v etcd >/dev/null || {
    27      kube::log::usage "etcd must be in your PATH"
    28      kube::log::info "You can use 'hack/install-etcd.sh' to install a copy in third_party/."
    29      exit 1
    30    }
    31  
    32    # validate etcd port is free
    33    local port_check_command
    34    if command -v ss &> /dev/null && ss -Version | grep 'iproute2' &> /dev/null; then
    35      port_check_command="ss"
    36    elif command -v netstat &>/dev/null; then
    37      port_check_command="netstat"
    38    else
    39      kube::log::usage "unable to identify if etcd is bound to port ${ETCD_PORT}. unable to find ss or netstat utilities."
    40      exit 1
    41    fi
    42    if ${port_check_command} -nat | grep "LISTEN" | grep "[\.:]${ETCD_PORT:?}" >/dev/null 2>&1; then
    43      kube::log::usage "unable to start etcd as port ${ETCD_PORT} is in use. please stop the process listening on this port and retry."
    44      kube::log::usage "$(netstat -nat | grep "[\.:]${ETCD_PORT:?} .*LISTEN")"
    45      exit 1
    46    fi
    47  
    48    # validate installed version is at least equal to minimum
    49    version=$(etcd --version | grep Version | tail -n +1 | head -n 1 | cut -d " " -f 3)
    50    if [[ $(kube::etcd::version "${ETCD_VERSION}") -gt $(kube::etcd::version "${version}") ]]; then
    51     export PATH=${KUBE_ROOT}/third_party/etcd:${PATH}
    52     hash etcd
    53     echo "${PATH}"
    54     version=$(etcd --version | head -n 1 | cut -d " " -f 3)
    55     if [[ $(kube::etcd::version "${ETCD_VERSION}") -gt $(kube::etcd::version "${version}") ]]; then
    56      kube::log::usage "etcd version ${ETCD_VERSION} or greater required."
    57      kube::log::info "You can use 'hack/install-etcd.sh' to install a copy in third_party/."
    58      exit 1
    59     fi
    60    fi
    61  }
    62  
    63  kube::etcd::version() {
    64    printf '%s\n' "${@}" | awk -F . '{ printf("%d%03d%03d\n", $1, $2, $3) }'
    65  }
    66  
    67  kube::etcd::start() {
    68    # validate before running
    69    kube::etcd::validate
    70  
    71    # Start etcd
    72    ETCD_DIR=${ETCD_DIR:-$(mktemp -d 2>/dev/null || mktemp -d -t test-etcd.XXXXXX)}
    73    if [[ -d "${ARTIFACTS:-}" ]]; then
    74      ETCD_LOGFILE="${ARTIFACTS}/etcd.$(uname -n).$(id -un).log.DEBUG.$(date +%Y%m%d-%H%M%S).$$"
    75    else
    76      ETCD_LOGFILE=${ETCD_LOGFILE:-"/dev/null"}
    77    fi
    78    kube::log::info "etcd --advertise-client-urls ${KUBE_INTEGRATION_ETCD_URL} --data-dir ${ETCD_DIR} --listen-client-urls http://${ETCD_HOST}:${ETCD_PORT} --debug > \"${ETCD_LOGFILE}\" 2>/dev/null"
    79    etcd --advertise-client-urls "${KUBE_INTEGRATION_ETCD_URL}" --data-dir "${ETCD_DIR}" --listen-client-urls "${KUBE_INTEGRATION_ETCD_URL}" --debug 2> "${ETCD_LOGFILE}" >/dev/null &
    80    ETCD_PID=$!
    81  
    82    echo "Waiting for etcd to come up."
    83    kube::util::wait_for_url "${KUBE_INTEGRATION_ETCD_URL}/health" "etcd: " 0.25 80
    84    curl -fs -X POST "${KUBE_INTEGRATION_ETCD_URL}/v3/kv/put" -d '{"key": "X3Rlc3Q=", "value": ""}'
    85  }
    86  
    87  kube::etcd::stop() {
    88    if [[ -n "${ETCD_PID-}" ]]; then
    89      kill "${ETCD_PID}" &>/dev/null || :
    90      wait "${ETCD_PID}" &>/dev/null || :
    91    fi
    92  }
    93  
    94  kube::etcd::clean_etcd_dir() {
    95    if [[ -n "${ETCD_DIR-}" ]]; then
    96      rm -rf "${ETCD_DIR}"
    97    fi
    98  }
    99  
   100  kube::etcd::cleanup() {
   101    kube::etcd::stop
   102    kube::etcd::clean_etcd_dir
   103  }
   104  
   105  kube::etcd::install() {
   106    (
   107      local os
   108      local arch
   109  
   110      os=$(kube::util::host_os)
   111      arch=$(kube::util::host_arch)
   112  
   113      cd "${KUBE_ROOT}/third_party" || return 1
   114      if [[ $(readlink etcd) == etcd-v${ETCD_VERSION}-${os}-* ]]; then
   115        kube::log::info "etcd v${ETCD_VERSION} already installed. To use:"
   116        kube::log::info "export PATH=\"$(pwd)/etcd:\${PATH}\""
   117        return  #already installed
   118      fi
   119  
   120      if [[ ${os} == "darwin" ]]; then
   121        download_file="etcd-v${ETCD_VERSION}-darwin-amd64.zip"
   122        url="https://github.com/coreos/etcd/releases/download/v${ETCD_VERSION}/${download_file}"
   123        kube::util::download_file "${url}" "${download_file}"
   124        unzip -o "${download_file}"
   125        ln -fns "etcd-v${ETCD_VERSION}-darwin-amd64" etcd
   126        rm "${download_file}"
   127      else
   128        url="https://github.com/coreos/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-linux-${arch}.tar.gz"
   129        download_file="etcd-v${ETCD_VERSION}-linux-${arch}.tar.gz"
   130        kube::util::download_file "${url}" "${download_file}"
   131        tar xzf "${download_file}"
   132        ln -fns "etcd-v${ETCD_VERSION}-linux-${arch}" etcd
   133        rm "${download_file}"
   134      fi
   135      kube::log::info "etcd v${ETCD_VERSION} installed. To use:"
   136      kube::log::info "export PATH=\"$(pwd)/etcd:\${PATH}\""
   137    )
   138  }