github.com/argoproj/argo-cd/v3@v3.2.1/hack/generate-proto.sh (about)

     1  #! /usr/bin/env bash
     2  
     3  # This script auto-generates protobuf related files. It is intended to be run manually when either
     4  # API types are added/modified, or server gRPC calls are added. The generated files should then
     5  # be checked into source control.
     6  
     7  set -x
     8  set -o errexit
     9  set -o nounset
    10  set -o pipefail
    11  
    12  # shellcheck disable=SC2128
    13  PROJECT_ROOT=$(
    14      cd "$(dirname "${BASH_SOURCE}")"/..
    15      pwd
    16  )
    17  PATH="${PROJECT_ROOT}/dist:${PATH}"
    18  GOPATH=$(go env GOPATH)
    19  GOPATH_PROJECT_ROOT="${GOPATH}/src/github.com/argoproj/argo-cd"
    20  
    21  # output tool versions
    22  go version
    23  protoc --version
    24  swagger version
    25  jq --version
    26  
    27  export GO111MODULE=off
    28  
    29  # Generate pkg/apis/<group>/<apiversion>/(generated.proto,generated.pb.go)
    30  # NOTE: any dependencies of our types to the k8s.io apimachinery types should be added to the
    31  # --apimachinery-packages= option so that go-to-protobuf can locate the types, but prefixed with a
    32  # '-' so that go-to-protobuf will not generate .proto files for it.
    33  PACKAGES=(
    34      github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1
    35  )
    36  APIMACHINERY_PKGS=(
    37      +k8s.io/apimachinery/pkg/util/intstr
    38      +k8s.io/apimachinery/pkg/api/resource
    39      +k8s.io/apimachinery/pkg/runtime/schema
    40      +k8s.io/apimachinery/pkg/runtime
    41      k8s.io/apimachinery/pkg/apis/meta/v1
    42      k8s.io/api/core/v1
    43      k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1
    44  )
    45  
    46  export GO111MODULE=on
    47  [ -e ./v3 ] || ln -s . v3
    48  [ -e "${GOPATH_PROJECT_ROOT}" ] || (mkdir -p "$(dirname "${GOPATH_PROJECT_ROOT}")" && ln -s "${PROJECT_ROOT}" "${GOPATH_PROJECT_ROOT}")
    49  
    50  # protoc_include is the include directory containing the .proto files distributed with protoc binary
    51  if [ -d /dist/protoc-include ]; then
    52      # containerized codegen build
    53      protoc_include=/dist/protoc-include
    54  else
    55      # local codegen build
    56      protoc_include=${PROJECT_ROOT}/dist/protoc-include
    57  fi
    58  
    59  # go-to-protobuf expects dependency proto files to be in $GOPATH/src. Copy them there.
    60  rm -rf "${GOPATH}/src/github.com/gogo/protobuf" && mkdir -p "${GOPATH}/src/github.com/gogo" && cp -r "${PROJECT_ROOT}/vendor/github.com/gogo/protobuf" "${GOPATH}/src/github.com/gogo"
    61  rm -rf "${GOPATH}/src/k8s.io/apimachinery" && mkdir -p "${GOPATH}/src/k8s.io" && cp -r "${PROJECT_ROOT}/vendor/k8s.io/apimachinery" "${GOPATH}/src/k8s.io"
    62  rm -rf "${GOPATH}/src/k8s.io/api" && mkdir -p "${GOPATH}/src/k8s.io" && cp -r "${PROJECT_ROOT}/vendor/k8s.io/api" "${GOPATH}/src/k8s.io"
    63  rm -rf "${GOPATH}/src/k8s.io/apiextensions-apiserver" && mkdir -p "${GOPATH}/src/k8s.io" && cp -r "${PROJECT_ROOT}/vendor/k8s.io/apiextensions-apiserver" "${GOPATH}/src/k8s.io"
    64  
    65  go-to-protobuf \
    66      --go-header-file="${PROJECT_ROOT}"/hack/custom-boilerplate.go.txt \
    67      --packages="$(
    68          IFS=,
    69          echo "${PACKAGES[*]}"
    70      )" \
    71      --apimachinery-packages="$(
    72          IFS=,
    73          echo "${APIMACHINERY_PKGS[*]}"
    74      )" \
    75      --proto-import="${PROJECT_ROOT}"/vendor \
    76      --proto-import="${protoc_include}" \
    77      --output-dir="${GOPATH}/src/"
    78  
    79  # go-to-protobuf modifies vendored code. Re-vendor code so it's available for subsequent steps.
    80  go mod vendor
    81  
    82  # Either protoc-gen-go, protoc-gen-gofast, or protoc-gen-gogofast can be used to build
    83  # server/*/<service>.pb.go from .proto files. golang/protobuf and gogo/protobuf can be used
    84  # interchangeably. The difference in the options are:
    85  # 1. protoc-gen-go - official golang/protobuf
    86  #GOPROTOBINARY=go
    87  # 2. protoc-gen-gofast - fork of golang golang/protobuf. Faster code generation
    88  #GOPROTOBINARY=gofast
    89  # 3. protoc-gen-gogofast - faster code generation and gogo extensions and flexibility in controlling
    90  # the generated go code (e.g. customizing field names, nullable fields)
    91  GOPROTOBINARY=gogofast
    92  
    93  # Generate server/<service>/(<service>.pb.go|<service>.pb.gw.go)
    94  MOD_ROOT=${GOPATH}/pkg/mod
    95  grpc_gateway_version=$(go list -m github.com/grpc-ecosystem/grpc-gateway | awk '{print $NF}' | head -1)
    96  GOOGLE_PROTO_API_PATH=${MOD_ROOT}/github.com/grpc-ecosystem/grpc-gateway@${grpc_gateway_version}/third_party/googleapis
    97  GOGO_PROTOBUF_PATH=${PROJECT_ROOT}/vendor/github.com/gogo/protobuf
    98  PROTO_FILES=$(find "$PROJECT_ROOT" \( -name "*.proto" -and -path '*/server/*' -or -path '*/reposerver/*' -and -name "*.proto" -or -path '*/cmpserver/*' -and -name "*.proto" -or -path '*/commitserver/*' -and -name "*.proto" -or -path '*/util/askpass/*' -and -name "*.proto" \) | sort)
    99  for i in ${PROTO_FILES}; do
   100      protoc \
   101          -I"${PROJECT_ROOT}" \
   102          -I"${protoc_include}" \
   103          -I./vendor \
   104          -I"$GOPATH"/src \
   105          -I"${GOOGLE_PROTO_API_PATH}" \
   106          -I"${GOGO_PROTOBUF_PATH}" \
   107          --${GOPROTOBINARY}_out=plugins=grpc:"$GOPATH"/src \
   108          --grpc-gateway_out=logtostderr=true:"$GOPATH"/src \
   109          --swagger_out=logtostderr=true:. \
   110          "$i"
   111  done
   112  
   113  # This file is generated but should not be checked in.
   114  rm util/askpass/askpass.swagger.json
   115  
   116  [ -L "${GOPATH_PROJECT_ROOT}" ] && rm -rf "${GOPATH_PROJECT_ROOT}"
   117  [ -L ./v3 ] && rm -rf v3
   118  
   119  # collect_swagger gathers swagger files into a subdirectory
   120  collect_swagger() {
   121      SWAGGER_ROOT="$1"
   122      SWAGGER_OUT="${PROJECT_ROOT}/assets/swagger.json"
   123      PRIMARY_SWAGGER=$(mktemp)
   124      COMBINED_SWAGGER=$(mktemp)
   125  
   126      cat <<EOF >"${PRIMARY_SWAGGER}"
   127  {
   128    "swagger": "2.0",
   129    "info": {
   130      "title": "Consolidate Services",
   131      "description": "Description of all APIs",
   132      "version": "version not set"
   133    },
   134    "paths": {}
   135  }
   136  EOF
   137  
   138      rm -f "${SWAGGER_OUT}"
   139  
   140      find "${SWAGGER_ROOT}" -name '*.swagger.json' -exec swagger mixin --ignore-conflicts "${PRIMARY_SWAGGER}" '{}' \+ >"${COMBINED_SWAGGER}"
   141      jq -r 'del(.definitions[].properties[]? | select(."$ref"!=null and .description!=null).description) | del(.definitions[].properties[]? | select(."$ref"!=null and .title!=null).title) |
   142        # The "array" and "map" fields have custom unmarshaling. Modify the swagger to reflect this.
   143        .definitions.v1alpha1ApplicationSourcePluginParameter.properties.array = {"description":"Array is the value of an array type parameter.","type":"array","items":{"type":"string"}} |
   144        del(.definitions.v1alpha1OptionalArray) |
   145        .definitions.v1alpha1ApplicationSourcePluginParameter.properties.map = {"description":"Map is the value of a map type parameter.","type":"object","additionalProperties":{"type":"string"}} |
   146        del(.definitions.v1alpha1OptionalMap) |
   147        # Output for int64 is incorrect, because it is based on proto definitions, where int64 is a string. In our JSON API, we expect int64 to be an integer. https://github.com/grpc-ecosystem/grpc-gateway/issues/219
   148        (.definitions[]?.properties[]? | select(.type == "string" and .format == "int64")) |= (.type = "integer")
   149      ' "${COMBINED_SWAGGER}" |
   150          jq '.definitions.v1Time.type = "string" | .definitions.v1Time.format = "date-time" | del(.definitions.v1Time.properties)' |
   151          jq '.definitions.v1alpha1ResourceNode.allOf = [{"$ref": "#/definitions/v1alpha1ResourceRef"}] | del(.definitions.v1alpha1ResourceNode.properties.resourceRef) ' \
   152              >"${SWAGGER_OUT}"
   153  
   154      /bin/rm "${PRIMARY_SWAGGER}" "${COMBINED_SWAGGER}"
   155  }
   156  
   157  # clean up generated swagger files (should come after collect_swagger)
   158  clean_swagger() {
   159      SWAGGER_ROOT="$1"
   160      find "${SWAGGER_ROOT}" -name '*.swagger.json' -delete
   161  }
   162  
   163  collect_swagger server
   164  clean_swagger server
   165  clean_swagger reposerver
   166  clean_swagger controller
   167  clean_swagger cmpserver
   168  clean_swagger commitserver