#!/bin/bash # Copyright 2017 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. set -o errexit set -o nounset set -o pipefail # The root of the build/dist directory global KUBE_ROOT := $[cd "$[dirname $(BASH_SOURCE)]/../.." && pwd -P] source "$(KUBE_ROOT)/hack/lib/init.sh" # Generates $1/api.pb.go from the protobuf file $1/api.proto # and formats it correctly # $1: Full path to the directory where the api.proto file is proc kube::protoc::generate_proto { kube::golang::setup_env var bins = '( 'vendor/k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo ) make -C $(KUBE_ROOT) WHAT="$(bins[*])" kube::protoc::check_protoc var package = $(1) kube::protoc::protoc $(package) kube::protoc::format $(package) } # Checks that the current protoc version is at least version 3.0.0-beta1 # exit 1 if it's not the case proc kube::protoc::check_protoc { if [[ -z "$(which protoc)" || "$(protoc --version)" != "libprotoc 3."* ]] { echo "Generating protobuf requires protoc 3.0.0-beta1 or newer. Please download and" echo "install the platform appropriate Protobuf package for your OS: " echo echo " https://github.com/google/protobuf/releases" echo echo "WARNING: Protobuf changes are not being validated" exit 1 } } # Generates $1/api.pb.go from the protobuf file $1/api.proto # $1: Full path to the directory where the api.proto file is proc kube::protoc::protoc { var package = $(1) global gogopath := $[dirname $[kube::util::find-binary "protoc-gen-gogo]] env PATH="$(gogopath):$(PATH)" protoc \ --proto_path="$(package)" \ --proto_path="$(KUBE_ROOT)/vendor" \ --gogo_out=plugins=grpc:$(package) $(package)/api.proto } # Formats $1/api.pb.go, adds the boilerplate comments and run gofmt on it # $1: Full path to the directory where the api.proto file is proc kube::protoc::format { var package = $(1) # Update boilerplate for the generated file. echo $[cat hack/boilerplate/boilerplate.go.txt $(package)/api.pb.go] > $(package)/api.pb.go sed -i".bak" "s/Copyright YEAR/Copyright $[date '+%Y]/g" $(package)/api.pb.go # Run gofmt to clean up the generated code. kube::golang::verify_go_version gofmt -l -s -w $(package)/api.pb.go } # Compares the contents of $1 and $2 # Echo's $3 in case of error and exits 1 proc kube::protoc::diff { var ret = '0' diff -I "gzipped FileDescriptorProto" -I "0x" -Naupr $(1) $(2) || ret := $Status if [[ $ret -ne 0 ]] { echo $(3) exit 1 } } (CommandList children: [ (C {(set)} {(-o)} {(errexit)}) (C {(set)} {(-o)} {(nounset)}) (C {(set)} {(-o)} {(pipefail)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:KUBE_ROOT) op: Equal rhs: { (DQ (CommandSubPart command_list: (CommandList children: [ (AndOr children: [ (C {(cd)} { (DQ (CommandSubPart command_list: (CommandList children: [(C {(dirname)} {(DQ (${ VSub_Name BASH_SOURCE))})] ) left_token: spids: [72 80] ) (/../..) ) } ) (C {(pwd)} {(-P)}) ] op_id: Op_DAmp ) ] ) left_token: spids: [68 89] ) ) } spids: [66] ) ] spids: [66] ) (C {(source)} {(DQ (${ VSub_Name KUBE_ROOT) (/hack/lib/init.sh))}) (FuncDef name: "kube::protoc::generate_proto" body: (BraceGroup children: [ (C {(kube) (Lit_Other ":") (Lit_Other ":") (golang) (Lit_Other ":") (Lit_Other ":") (setup_env) } ) (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:bins) op: Equal rhs: { (ArrayLiteralPart words: [{(vendor/k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo)}] ) } spids: [137] ) ] spids: [135] ) (C {(make)} {(-C)} {(DQ (${ VSub_Name KUBE_ROOT))} {(Lit_VarLike "WHAT=") (DQ (BracedVarSub token: bracket_op: (WholeArray op_id:Arith_Star) spids: [159 164] ) ) } ) (C {(kube) (Lit_Other ":") (Lit_Other ":") (protoc) (Lit_Other ":") (Lit_Other ":") (check_protoc) } ) (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:package) op: Equal rhs: {(${ VSub_Number 1)} spids: [181] ) ] spids: [179] ) (C {(kube) (Lit_Other ":") (Lit_Other ":") (protoc) (Lit_Other ":") (Lit_Other ":") (protoc)} {(${ VSub_Name package)} ) (C {(kube) (Lit_Other ":") (Lit_Other ":") (protoc) (Lit_Other ":") (Lit_Other ":") (format)} {(${ VSub_Name package)} ) ] spids: [123] ) spids: [111 122] ) (FuncDef name: "kube::protoc::check_protoc" body: (BraceGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalOr left: (BoolUnary op_id: BoolUnary_z child: { (DQ (CommandSubPart command_list: (CommandList children: [(C {(which)} {(protoc)})] ) left_token: spids: [243 247] ) ) } ) right: (BoolBinary op_id: BoolBinary_GlobNEqual left: { (DQ (CommandSubPart command_list: (CommandList children: [(C {(protoc)} {(--version)})] ) left_token: spids: [253 257] ) ) } right: {(DQ ("libprotoc 3.")) (Lit_Other "*")} ) ) ) terminator: ) ] action: [ (C {(echo)} { (DQ ( "Generating protobuf requires protoc 3.0.0-beta1 or newer. Please download and" ) ) } ) (C {(echo)} {(DQ ("install the platform appropriate Protobuf package for your OS: "))} ) (C {(echo)}) (C {(echo)} {(DQ (" https://github.com/google/protobuf/releases"))}) (C {(echo)}) (C {(echo)} {(DQ ("WARNING: Protobuf changes are not being validated"))}) (C {(exit)} {(1)}) ] spids: [-1 270] ) ] spids: [-1 312] ) ] spids: [233] ) spids: [221 232] ) (FuncDef name: "kube::protoc::protoc" body: (BraceGroup children: [ (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:package) op: Equal rhs: {(${ VSub_Number 1)} spids: [340] ) ] spids: [338] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:gogopath) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (C {(dirname)} { (CommandSubPart command_list: (CommandList children: [ (C {(kube) (Lit_Other ":") (Lit_Other ":") (util) (Lit_Other ":") (Lit_Other ":") (find-binary) } {(DQ (protoc-gen-gogo))} ) ] ) left_token: spids: [350 362] ) } ) ] ) left_token: spids: [347 363] ) } spids: [346] ) ] spids: [346] ) (SimpleCommand words: [ {(protoc)} {(--proto_path) (Lit_Other "=") (DQ (${ VSub_Name package))} {(--proto_path) (Lit_Other "=") (DQ (${ VSub_Name KUBE_ROOT) (/vendor))} {(--gogo_out) (Lit_Other "=") (Lit_VarLike "plugins=") (grpc) (Lit_Other ":") (${ VSub_Name package) } {(${ VSub_Name package) (/api.proto)} ] more_env: [ (env_pair name: PATH val: {(DQ (${ VSub_Name gogopath) (":") (${ VSub_Name PATH))} spids: [367] ) ] ) ] spids: [335] ) spids: [323 334] ) (FuncDef name: "kube::protoc::format" body: (BraceGroup children: [ (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:package) op: Equal rhs: {(${ VSub_Number 1)} spids: [443] ) ] spids: [441] ) (SimpleCommand words: [ {(echo)} { (DQ (CommandSubPart command_list: (CommandList children: [ (C {(cat)} {(hack/boilerplate/boilerplate.go.txt)} {(${ VSub_Name package) (/api.pb.go)} ) ] ) left_token: spids: [457 466] ) ) } ] redirects: [ (Redir op_id: Redir_Great fd: -1 arg_word: {(${ VSub_Name package) (/api.pb.go)} spids: [469] ) ] ) (C {(sed)} {(-i) (DQ (.bak))} { (DQ ("s/Copyright YEAR/Copyright ") (CommandSubPart command_list: (CommandList children:[(C {(date)} {(SQ <"+%Y">)})]) left_token: spids: [486 492] ) (/g) ) } {(${ VSub_Name package) (/api.pb.go)} ) (C {(kube) (Lit_Other ":") (Lit_Other ":") (golang) (Lit_Other ":") (Lit_Other ":") (verify_go_version) } ) (C {(gofmt)} {(-l)} {(-s)} {(-w)} {(${ VSub_Name package) (/api.pb.go)}) ] spids: [438] ) spids: [426 437] ) (FuncDef name: "kube::protoc::diff" body: (BraceGroup children: [ (Assignment keyword: Assign_Local pairs: [(assign_pair lhs:(LhsName name:ret) op:Equal rhs:{(0)} spids:[555])] spids: [553] ) (AndOr children: [ (C {(diff)} {(-I)} {(DQ ("gzipped FileDescriptorProto"))} {(-I)} {(DQ (0x))} {(-Naupr)} {(${ VSub_Number 1)} {(${ VSub_Number 2)} ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ret) op: Equal rhs: {($ VSub_QMark "$?")} spids: [585] ) ] spids: [585] ) ] op_id: Op_DPipe ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_ne left: {($ VSub_Name "$ret")} right: {(0)} ) ) terminator: ) ] action: [(C {(echo)} {(${ VSub_Number 3)}) (C {(exit)} {(1)})] spids: [-1 602] ) ] spids: [-1 617] ) ] spids: [550] ) spids: [538 549] ) ] )