github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/scripts/lib/init.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  set -o errexit
    18  set -o nounset
    19  set -o pipefail
    20  
    21  # Unset CDPATH so that path interpolation can work correctly
    22  # https://github.com/kubernetes/kubernetes/issues/52255
    23  unset CDPATH
    24  
    25  # Until all GOPATH references are removed from all build scripts as well,
    26  # explicitly disable module mode to avoid picking up user-set GO111MODULE preferences.
    27  # As individual scripts (like scripts/update-vendor.sh) make use of go modules,
    28  # they can explicitly set GO111MODULE=on
    29  export GO111MODULE=off
    30  
    31  # The root of the build/dist directory
    32  KUBE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"
    33  
    34  KUBE_OUTPUT_SUBPATH="${KUBE_OUTPUT_SUBPATH:-_output/local}"
    35  KUBE_OUTPUT="${KUBE_ROOT}/${KUBE_OUTPUT_SUBPATH}"
    36  KUBE_OUTPUT_BINPATH="${KUBE_OUTPUT}/bin"
    37  
    38  # This controls rsync compression. Set to a value > 0 to enable rsync
    39  # compression for build container
    40  KUBE_RSYNC_COMPRESS="${KUBE_RSYNC_COMPRESS:-0}"
    41  
    42  # Set no_proxy for localhost if behind a proxy, otherwise,
    43  # the connections to localhost in scripts will time out
    44  export no_proxy=127.0.0.1,localhost
    45  
    46  # This is a symlink to binaries for "this platform", e.g. build tools.
    47  export THIS_PLATFORM_BIN="${KUBE_ROOT}/_output/bin"
    48  
    49  source "${KUBE_ROOT}/scripts/lib/util.sh"
    50  source "${KUBE_ROOT}/scripts/lib/logging.sh"
    51  
    52  kube::log::install_errexit
    53  
    54  source "${KUBE_ROOT}/scripts/lib/version.sh"
    55  source "${KUBE_ROOT}/scripts/lib/golang.sh"
    56  source "${KUBE_ROOT}/scripts/lib/etcd.sh"
    57  
    58  KUBE_OUTPUT_HOSTBIN="${KUBE_OUTPUT_BINPATH}/$(kube::util::host_platform)"
    59  export KUBE_OUTPUT_HOSTBIN
    60  
    61  # list of all available group versions.  This should be used when generated code
    62  # or when starting an API server that you want to have everything.
    63  # most preferred version for a group should appear first
    64  KUBE_AVAILABLE_GROUP_VERSIONS="${KUBE_AVAILABLE_GROUP_VERSIONS:-\
    65  v1 \
    66  admissionregistration.k8s.io/v1 \
    67  admissionregistration.k8s.io/v1beta1 \
    68  admission.k8s.io/v1 \
    69  admission.k8s.io/v1beta1 \
    70  apps/v1 \
    71  apps/v1beta1 \
    72  apps/v1beta2 \
    73  auditregistration.k8s.io/v1alpha1 \
    74  authentication.k8s.io/v1 \
    75  authentication.k8s.io/v1beta1 \
    76  authorization.k8s.io/v1 \
    77  authorization.k8s.io/v1beta1 \
    78  autoscaling/v1 \
    79  autoscaling/v2beta1 \
    80  autoscaling/v2beta2 \
    81  batch/v1 \
    82  batch/v1beta1 \
    83  batch/v2alpha1 \
    84  certificates.k8s.io/v1beta1 \
    85  coordination.k8s.io/v1beta1 \
    86  coordination.k8s.io/v1 \
    87  discovery.k8s.io/v1alpha1 \
    88  discovery.k8s.io/v1beta1 \
    89  extensions/v1beta1 \
    90  events.k8s.io/v1beta1 \
    91  imagepolicy.k8s.io/v1alpha1 \
    92  networking.k8s.io/v1 \
    93  networking.k8s.io/v1beta1 \
    94  node.k8s.io/v1alpha1 \
    95  node.k8s.io/v1beta1 \
    96  policy/v1beta1 \
    97  rbac.authorization.k8s.io/v1 \
    98  rbac.authorization.k8s.io/v1beta1 \
    99  rbac.authorization.k8s.io/v1alpha1 \
   100  scheduling.k8s.io/v1alpha1 \
   101  scheduling.k8s.io/v1beta1 \
   102  scheduling.k8s.io/v1 \
   103  settings.k8s.io/v1alpha1 \
   104  storage.k8s.io/v1beta1 \
   105  storage.k8s.io/v1 \
   106  storage.k8s.io/v1alpha1 \
   107  flowcontrol.apiserver.k8s.io/v1alpha1 \
   108  }"
   109  
   110  # not all group versions are exposed by the server.  This list contains those
   111  # which are not available so we don't generate clients or swagger for them
   112  KUBE_NONSERVER_GROUP_VERSIONS="
   113   abac.authorization.kubernetes.io/v0 \
   114   abac.authorization.kubernetes.io/v1beta1 \
   115   componentconfig/v1alpha1 \
   116   imagepolicy.k8s.io/v1alpha1\
   117   admission.k8s.io/v1\
   118   admission.k8s.io/v1beta1\
   119  "
   120  export KUBE_NONSERVER_GROUP_VERSIONS
   121  
   122  # This emulates "readlink -f" which is not available on MacOS X.
   123  # Test:
   124  # T=/tmp/$$.$RANDOM
   125  # mkdir $T
   126  # touch $T/file
   127  # mkdir $T/dir
   128  # ln -s $T/file $T/linkfile
   129  # ln -s $T/dir $T/linkdir
   130  # function testone() {
   131  #   X=$(readlink -f $1 2>&1)
   132  #   Y=$(kube::readlinkdashf $1 2>&1)
   133  #   if [ "$X" != "$Y" ]; then
   134  #     echo readlinkdashf $1: expected "$X", got "$Y"
   135  #   fi
   136  # }
   137  # testone /
   138  # testone /tmp
   139  # testone $T
   140  # testone $T/file
   141  # testone $T/dir
   142  # testone $T/linkfile
   143  # testone $T/linkdir
   144  # testone $T/nonexistant
   145  # testone $T/linkdir/file
   146  # testone $T/linkdir/dir
   147  # testone $T/linkdir/linkfile
   148  # testone $T/linkdir/linkdir
   149  function kube::readlinkdashf {
   150    # run in a subshell for simpler 'cd'
   151    (
   152      if [[ -d "${1}" ]]; then # This also catch symlinks to dirs.
   153        cd "${1}"
   154        pwd -P
   155      else
   156        cd "$(dirname "${1}")"
   157        local f
   158        f=$(basename "${1}")
   159        if [[ -L "${f}" ]]; then
   160          readlink "${f}"
   161        else
   162          echo "$(pwd -P)/${f}"
   163        fi
   164      fi
   165    )
   166  }
   167  
   168  # This emulates "realpath" which is not available on MacOS X
   169  # Test:
   170  # T=/tmp/$$.$RANDOM
   171  # mkdir $T
   172  # touch $T/file
   173  # mkdir $T/dir
   174  # ln -s $T/file $T/linkfile
   175  # ln -s $T/dir $T/linkdir
   176  # function testone() {
   177  #   X=$(realpath $1 2>&1)
   178  #   Y=$(kube::realpath $1 2>&1)
   179  #   if [ "$X" != "$Y" ]; then
   180  #     echo realpath $1: expected "$X", got "$Y"
   181  #   fi
   182  # }
   183  # testone /
   184  # testone /tmp
   185  # testone $T
   186  # testone $T/file
   187  # testone $T/dir
   188  # testone $T/linkfile
   189  # testone $T/linkdir
   190  # testone $T/nonexistant
   191  # testone $T/linkdir/file
   192  # testone $T/linkdir/dir
   193  # testone $T/linkdir/linkfile
   194  # testone $T/linkdir/linkdir
   195  kube::realpath() {
   196    if [[ ! -e "${1}" ]]; then
   197      echo "${1}: No such file or directory" >&2
   198      return 1
   199    fi
   200    kube::readlinkdashf "${1}"
   201  }