From 3548e7febed0533fa079b9c4228bcccfbc53db89 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Mon, 23 Jan 2023 09:11:50 -0500 Subject: [PATCH] Add helper script to install protoc --- .gitignore | 3 +++ hack/install-protoc.sh | 27 +++++++++++++++++++++++++++ hack/lib/protoc.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100755 hack/install-protoc.sh diff --git a/.gitignore b/.gitignore index 889e9c8481e..de1b35148e5 100644 --- a/.gitignore +++ b/.gitignore @@ -64,6 +64,9 @@ network_closure.sh /third_party/etcd* /default.etcd +# Also ignore protoc installed by hack/install-protoc.sh +/third_party/protoc* + # User cluster configs .kubeconfig diff --git a/hack/install-protoc.sh b/hack/install-protoc.sh new file mode 100755 index 00000000000..9361b01c98b --- /dev/null +++ b/hack/install-protoc.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# Copyright 2023 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script is convenience to download and install protoc in third_party. +# Usage: `hack/install-protoc.sh`. + +set -o errexit +set -o nounset +set -o pipefail + +KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. +source "${KUBE_ROOT}/hack/lib/protoc.sh" + +kube::protoc::install diff --git a/hack/lib/protoc.sh b/hack/lib/protoc.sh index e761012183f..4c8a3920146 100644 --- a/hack/lib/protoc.sh +++ b/hack/lib/protoc.sh @@ -94,3 +94,43 @@ function kube::protoc::diff() { exit 1 fi } + +function kube::protoc::install() { + # run in a subshell to isolate caller from directory changes + ( + local os + local arch + local download_folder + local download_file + + os=$(kube::util::host_os) + arch=$(kube::util::host_arch) + download_folder="protoc-v${PROTOC_VERSION}-${os}-${arch}" + download_file="${download_folder}.zip" + + cd "${KUBE_ROOT}/third_party" || return 1 + if [[ $(readlink protoc) != "${download_folder}" ]]; then + local url + if [[ ${os} == "darwin" ]]; then + # TODO: switch to universal binary when updating to 3.20+ + url="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-osx-x86_64.zip" + elif [[ ${os} == "linux" && ${arch} == "amd64" ]]; then + url="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-x86_64.zip" + elif [[ ${os} == "linux" && ${arch} == "arm64" ]]; then + url="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-aarch_64.zip" + else + kube::log::info "This install script does not support ${os}/${arch}" + return 1 + fi + kube::util::download_file "${url}" "${download_file}" + unzip -o "${download_file}" -d "${download_folder}" + ln -fns "${download_folder}" protoc + mv protoc/bin/protoc protoc/protoc + chmod -R +rX protoc/protoc + rm -fr protoc/include + rm "${download_file}" + fi + kube::log::info "protoc v${PROTOC_VERSION} installed. To use:" + kube::log::info "export PATH=\"$(pwd)/protoc:\${PATH}\"" + ) +}