Skip to content

Commit

Permalink
Install protoc in tools/bin dir (gardener#8218)
Browse files Browse the repository at this point in the history
* Install `protoc` in tools/bin dir

* Address review

* Address review

* Move download script to tools folder

* Rename script for consistency

---------

Co-authored-by: Tim Ebert <timebertt@gmail.com>
  • Loading branch information
acumino and timebertt authored Jul 11, 2023
1 parent 7333b8f commit 9e54720
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ check: $(GO_ADD_LICENSE) $(GOIMPORTS) $(GOLANGCI_LINT) $(HELM) $(IMPORT_BOSS) $(
@hack/check-license-header.sh
@hack/check-skaffold-deps.sh

tools-for-generate: $(CONTROLLER_GEN) $(GEN_CRD_API_REFERENCE_DOCS) $(GOIMPORTS) $(GO_TO_PROTOBUF) $(HELM) $(MOCKGEN) $(OPENAPI_GEN) $(PROTOC_GEN_GOGO) $(YAML2JSON) $(YQ)
tools-for-generate: $(CONTROLLER_GEN) $(GEN_CRD_API_REFERENCE_DOCS) $(GOIMPORTS) $(GO_TO_PROTOBUF) $(HELM) $(MOCKGEN) $(OPENAPI_GEN) $(PROTOC) $(PROTOC_GEN_GOGO) $(YAML2JSON) $(YQ)

.PHONY: generate
generate: tools-for-generate
Expand Down
5 changes: 5 additions & 0 deletions hack/tools.mk
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ LOGCHECK := $(TOOLS_BIN_DIR)/logcheck.so # plugin binary
MOCKGEN := $(TOOLS_BIN_DIR)/mockgen
OPENAPI_GEN := $(TOOLS_BIN_DIR)/openapi-gen
PROMTOOL := $(TOOLS_BIN_DIR)/promtool
PROTOC := $(TOOLS_BIN_DIR)/protoc
PROTOC_GEN_GOGO := $(TOOLS_BIN_DIR)/protoc-gen-gogo
REPORT_COLLECTOR := $(TOOLS_BIN_DIR)/report-collector
SETUP_ENVTEST := $(TOOLS_BIN_DIR)/setup-envtest
Expand All @@ -66,6 +67,7 @@ GO_VULN_CHECK_VERSION ?= latest
HELM_VERSION ?= v3.11.2
KIND_VERSION ?= v0.18.0
KUBECTL_VERSION ?= v1.24.11
PROTOC_VERSION ?= 23.4
SKAFFOLD_VERSION ?= v2.2.0
YQ_VERSION ?= v4.31.2

Expand Down Expand Up @@ -175,6 +177,9 @@ $(OPENAPI_GEN): go.mod
$(PROMTOOL): $(TOOLS_PKG_PATH)/install-promtool.sh
@$(TOOLS_PKG_PATH)/install-promtool.sh

$(PROTOC): $(call tool_version_file,$(PROTOC),$(PROTOC_VERSION))
@PROTOC_VERSION=$(PROTOC_VERSION) $(TOOLS_PKG_PATH)/install-protoc.sh

$(PROTOC_GEN_GOGO): go.mod
go build -o $(PROTOC_GEN_GOGO) k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo

Expand Down
50 changes: 50 additions & 0 deletions hack/tools/install-protoc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
#
# Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# 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.

set -o errexit
set -o nounset
set -o pipefail

TOOLS_BIN_DIR=${TOOLS_BIN_DIR:-$(dirname "$0")/bin}

os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
protoc_zip=protoc-v"$PROTOC_VERSION"-"$os"-"$arch".zip

if [[ $os == "darwin" ]]; then
url="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-osx-universal_binary.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
if ! command -v protoc &>/dev/null; then
echo "Unable to automatically install protoc for ${os}/${arch}. Please install it yourself and retry."
exit 1
fi
fi

out_dir=$(mktemp -d)
function cleanup_output {
rm -rf "$out_dir"
}
trap cleanup_output EXIT

curl -L -o "$out_dir"/"$protoc_zip" "$url"
unzip -o "$out_dir"/"$protoc_zip" -d "$out_dir" >/dev/null
rm -rf "$TOOLS_BIN_DIR"/include
cp -f "$out_dir"/bin/protoc "$TOOLS_BIN_DIR"/protoc
cp -r "$out_dir"/include "$TOOLS_BIN_DIR"
15 changes: 0 additions & 15 deletions hack/update-protobuf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,6 @@ APIROOTS=${APIROOTS:-$(git grep --files-with-matches -e '// +k8s:protobuf-gen=pa
)}
popd > /dev/null

if [[ -z "$(which protoc)" || "$(protoc --version)" != "libprotoc 3."* ]]; then
# TODO: install to local hack/tools/bin dir
if [[ "$(uname -s)" == *"Darwin"* ]]; then
brew install protobuf
else
PROTOC_ZIP=protoc-3.7.1-linux-x86_64.zip
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/$PROTOC_ZIP
unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP
fi

echo "WARNING: Protobuf changes are not being validated"
fi

read -ra PACKAGES <<< $(echo ${APIROOTS})

# requires the 'proto' tag to build (will remove when ready)
Expand Down

0 comments on commit 9e54720

Please sign in to comment.