github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/src/scripts/util.sh (about)

     1  # Copyright 2014 Google Inc. All rights reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  # A library of helper functions and constant for the local config.
    16  
    17  # Use the config file specified in $KUBE_CONFIG_FILE, or default to
    18  # config-default.sh.
    19  source $(dirname ${BASH_SOURCE})/${KUBE_CONFIG_FILE-"config-default.sh"}
    20  
    21  # Find the release to use.  If passed in, go with that and validate.  If not use
    22  # the release/config.sh version assuming a dev workflow.
    23  function find-release() {
    24    if [ -n "$1" ]; then
    25      RELEASE_NORMALIZED=$1
    26    else
    27      local RELEASE_CONFIG_SCRIPT=$(dirname $0)/../release/config.sh
    28      if [ -f $(dirname $0)/../release/config.sh ]; then
    29        . $RELEASE_CONFIG_SCRIPT
    30        normalize_release
    31      fi
    32    fi
    33  
    34    # Do one final check that we have a good release
    35    if ! gsutil -q stat $RELEASE_NORMALIZED/master-release.tgz; then
    36      echo "Could not find release tar.  If developing, make sure you have run src/release/release.sh to create a release."
    37      exit 1
    38    fi
    39    echo "Release: ${RELEASE_NORMALIZED}"
    40  }
    41  
    42  # Use the gcloud defaults to find the project.  If it is already set in the
    43  # environment then go with that.
    44  function detect-project () {
    45    if [ -z "$PROJECT" ]; then
    46      PROJECT=$(gcloud config list project | tail -n 1 | cut -f 3 -d ' ')
    47    fi
    48  
    49    if [ -z "$PROJECT" ]; then
    50      echo "Could not detect Google Cloud Platform project.  Set the default project using 'gcloud config set project <PROJECT>'"
    51      exit 1
    52    fi
    53    echo "Project: $PROJECT (autodetected from gcloud config)"
    54  }
    55  
    56  function detect-minions () {
    57    KUBE_MINION_IP_ADDRESSES=()
    58    for (( i=0; i<${#MINION_NAMES[@]}; i++)); do
    59      local minion_ip=$(gcloud compute instances get ${MINION_NAMES[$i]} \
    60        --fields networkInterfaces[].accessConfigs[].natIP --format=text \
    61        | tail -n 1 | cut -f 2 -d ' ')
    62      echo "Found ${MINION_NAMES[$i]} at ${minion_ip}"
    63      KUBE_MINION_IP_ADDRESSES+=("${minion_ip}")
    64    done
    65    if [ -z "$KUBE_MINION_IP_ADDRESSES" ]; then
    66      echo "Could not detect Kubernetes minion nodes.  Make sure you've launched a cluster with 'kube-up.sh'"
    67      exit 1
    68    fi
    69  }
    70  
    71  function detect-master () {
    72    KUBE_MASTER=${MASTER_NAME}
    73    KUBE_MASTER_IP=$(gcloud compute instances get ${MASTER_NAME} \
    74      --fields networkInterfaces[].accessConfigs[].natIP --format=text \
    75      | tail -n 1 | cut -f 2 -d ' ')
    76    if [ -z "$KUBE_MASTER_IP" ]; then
    77      echo "Could not detect Kubernetes master node.  Make sure you've launched a cluster with 'kube-up.sh'"
    78      exit 1
    79    fi
    80    echo "Using master: $KUBE_MASTER (external IP: $KUBE_MASTER_IP)"
    81  }
    82  
    83  function get-password {
    84    file=${HOME}/.kubernetes_auth
    85    if [ -e ${file} ]; then 
    86      user=$(cat $file | python -c 'import json,sys;print json.load(sys.stdin)["User"]')
    87      passwd=$(cat $file | python -c 'import json,sys;print json.load(sys.stdin)["Password"]')
    88      return
    89    fi
    90    user=admin
    91    passwd=$(python -c 'import string,random; print "".join(random.choice(string.ascii_letters + string.digits) for _ in range(16))')
    92  }
    93