|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2017 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 | +# The root of the build/dist directory |
| 22 | +KUBE_ROOT="$(cd "$(dirname "${BASH_SOURCE}")/../.." && pwd -P)" |
| 23 | +source "${KUBE_ROOT}/hack/lib/init.sh" |
| 24 | + |
| 25 | +# Generates $1/api.pb.go from the protobuf file $1/api.proto |
| 26 | +# and formats it correctly |
| 27 | +# $1: Full path to the directory where the api.proto file is |
| 28 | +function kube::protoc::generate_proto() { |
| 29 | + kube::golang::setup_env |
| 30 | + local bins=( |
| 31 | + vendor/k8s.io/kube-gen/cmd/go-to-protobuf/protoc-gen-gogo |
| 32 | + ) |
| 33 | + make -C "${KUBE_ROOT}" WHAT="${bins[*]}" |
| 34 | + |
| 35 | + kube::protoc::check_protoc |
| 36 | + |
| 37 | + local package=${1} |
| 38 | + kube::protoc::protoc ${package} |
| 39 | + kube::protoc::format ${package} |
| 40 | +} |
| 41 | + |
| 42 | +# Checks that the current protoc version is at least version 3.0.0-beta1 |
| 43 | +# exit 1 if it's not the case |
| 44 | +function kube::protoc::check_protoc() { |
| 45 | + if [[ -z "$(which protoc)" || "$(protoc --version)" != "libprotoc 3."* ]]; then |
| 46 | + echo "Generating protobuf requires protoc 3.0.0-beta1 or newer. Please download and" |
| 47 | + echo "install the platform appropriate Protobuf package for your OS: " |
| 48 | + echo |
| 49 | + echo " https://github.com/google/protobuf/releases" |
| 50 | + echo |
| 51 | + echo "WARNING: Protobuf changes are not being validated" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +# Generates $1/api.pb.go from the protobuf file $1/api.proto |
| 57 | +# $1: Full path to the directory where the api.proto file is |
| 58 | +function kube::protoc::protoc() { |
| 59 | + local package=${1} |
| 60 | + gogopath=$(dirname $(kube::util::find-binary "protoc-gen-gogo")) |
| 61 | + |
| 62 | + PATH="${gogopath}:${PATH}" protoc \ |
| 63 | + --proto_path="${package}" \ |
| 64 | + --proto_path="${KUBE_ROOT}/vendor" \ |
| 65 | + --gogo_out=plugins=grpc:${package} ${package}/api.proto |
| 66 | +} |
| 67 | + |
| 68 | +# Formats $1/api.pb.go, adds the boilerplate comments and run gofmt on it |
| 69 | +# $1: Full path to the directory where the api.proto file is |
| 70 | +function kube::protoc::format() { |
| 71 | + local package=${1} |
| 72 | + |
| 73 | + # Update boilerplate for the generated file. |
| 74 | + echo "$(cat hack/boilerplate/boilerplate.go.txt ${package}/api.pb.go)" > ${package}/api.pb.go |
| 75 | + sed -i".bak" "s/Copyright YEAR/Copyright $(date '+%Y')/g" ${package}/api.pb.go |
| 76 | + |
| 77 | + # Run gofmt to clean up the generated code. |
| 78 | + kube::golang::verify_go_version |
| 79 | + gofmt -l -s -w ${package}/api.pb.go |
| 80 | +} |
0 commit comments