Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - chore(backend): remove bazel from backend code generation #5115

Closed
wants to merge 55 commits into from
Closed
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
e93f6d4
updated to remove bazel
NikeNano Dec 16, 2020
e17a96b
rename@
NikeNano Dec 16, 2020
bbb2b8d
script to run in container
NikeNano Dec 18, 2020
9696224
update the generation
NikeNano Dec 18, 2020
7c98945
updated docker image
NikeNano Dec 18, 2020
3c2179f
changed name
NikeNano Dec 19, 2020
8696f8b
updated the code
NikeNano Dec 27, 2020
e04d852
regenerated API
NikeNano Dec 27, 2020
84e18f5
fix env variables
NikeNano Dec 28, 2020
2bd56c4
updated version of go swagger
NikeNano Dec 29, 2020
a79d7d6
rerun generate with updated package
NikeNano Jan 3, 2021
dda0445
added healthz
NikeNano Jan 4, 2021
6e2fc06
typo with folders fixed
NikeNano Jan 4, 2021
b599ce6
changed version
NikeNano Jan 6, 2021
8e94d8f
set version of protoc compiler
NikeNano Jan 7, 2021
a8afe58
test if version is correct
NikeNano Jan 11, 2021
b57406c
test version
NikeNano Jan 11, 2021
c1be66d
changed version
NikeNano Jan 11, 2021
99fa74b
updated version agian
NikeNano Jan 11, 2021
eafd36c
test version
NikeNano Jan 11, 2021
280ac94
the latest test
NikeNano Jan 12, 2021
1c55741
updated docker image
NikeNano Jan 15, 2021
c6dd5fb
fixed some stuff
NikeNano Jan 18, 2021
6d69b7f
new test
NikeNano Jan 18, 2021
86ed6b0
tested other version
NikeNano Jan 18, 2021
bc96ab1
new tests
NikeNano Jan 19, 2021
37150ca
changed swagger
NikeNano Jan 19, 2021
21de412
new test
NikeNano Jan 21, 2021
4ccb894
updated versions
NikeNano Jan 21, 2021
a1532b3
missed docker file
NikeNano Jan 21, 2021
a0a01fe
updated files@
NikeNano Jan 21, 2021
644701a
change back
NikeNano Jan 21, 2021
588d320
updated after feedback@
NikeNano Jan 21, 2021
643a8ca
clean up
NikeNano Jan 31, 2021
b2280df
Merge branch 'master' into nikenano/GenerateAPIRemoveBazel
Bobgy Feb 8, 2021
32defa9
chore(backend): tidy go.mod and update tools.go
Bobgy Feb 8, 2021
3405b69
go install, instead of go get
Bobgy Feb 8, 2021
ec3abc1
fix problems reported by go vet
Bobgy Feb 8, 2021
0b99bed
simplify some ide reported redundant syntax
Bobgy Feb 8, 2021
e0925a0
license is not required for generated code
Bobgy Feb 8, 2021
b220f93
remove licenses for generated code
Bobgy Feb 8, 2021
92a0d54
cleanup
Bobgy Feb 8, 2021
383851a
remove license more
Bobgy Feb 8, 2021
79d574b
Merge remote-tracking branch 'upstream/master' into bazel-cleanup
Bobgy Feb 9, 2021
ec313eb
Merge remote-tracking branch 'upstream/master' into rm-bazel-gen-api
Bobgy Feb 9, 2021
72695fa
fix problems
Bobgy Feb 9, 2021
3725626
regenerate all go clients
Bobgy Feb 9, 2021
80bb1ea
Merge remote-tracking branch 'upstream/master' into bazel-cleanup
Bobgy Feb 9, 2021
ec1d7b8
rm unused BUILD.bazel files
Bobgy Feb 9, 2021
50cb605
fixed generate_api.sh
Bobgy Feb 9, 2021
d691d8c
reimport error.proto
Bobgy Feb 9, 2021
c91d47d
Merge branch 'bazel-cleanup' into rm-bazel-gen-api
Bobgy Feb 9, 2021
e3b33ee
no longer use protobuffers repo
Bobgy Feb 9, 2021
64400ed
update version
Bobgy Feb 9, 2021
1e7a338
wip
Bobgy Feb 10, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions backend/Dockerfile.buildapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 0. Generate client code (go & json) from API protocol buffers
FROM golang:1.13.1-stretch as generator
ENV KFP_VERSION 1.0.0
ENV GRPC_GATEWAY_VERSION v1.9.0
ENV GO_SWAGGER_VERSION v0.18.0
ENV GOLANG_PROTOBUF_VERSION v1.3.2
ENV PROTOCOLBUFFERS_PROTOBUF_VERSION v3.11.3
ENV GRPC_VERSION v1.23.0

# Install protoc.
RUN apt-get update -y && apt-get install -y jq sed unzip
ENV PROTOC_ZIP=protoc-3.13.0-linux-x86_64.zip
RUN curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.13.0/$PROTOC_ZIP
RUN unzip -o $PROTOC_ZIP -d /usr/ bin/protoc
RUN unzip -o $PROTOC_ZIP -d /usr/ 'include/*'
RUN rm -f $PROTOC_ZIP
ENV PROTOCCOMPILER /usr/bin/protoc
ENV PROTOCINCLUDE /usr/include/google/protobuf

# Need grpc-gateway source code for -I in protoc command.
WORKDIR /go/src/github.com
RUN mkdir grpc-ecosystem && cd grpc-ecosystem && git clone --depth 1 --branch $GRPC_GATEWAY_VERSION https://github.com/grpc-ecosystem/grpc-gateway.git
RUN mkdir grpc && git clone --depth 1 --branch $GRPC_VERSION https://github.com/grpc/grpc-go

# Install protoc-gen-rpc-gateway && protoc-gen-swagger.
RUN cd grpc-ecosystem/grpc-gateway && GO111MODULE=on go mod vendor
RUN GOBIN=/go/bin go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
RUN GOBIN=/go/bin go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger

# Download go-swagger binary.
RUN curl -LO "https://github.com/go-swagger/go-swagger/releases/download/${GO_SWAGGER_VERSION}/swagger_linux_amd64"
RUN chmod +x swagger_linux_amd64 && mv swagger_linux_amd64 /usr/bin/swagger

# Need protobuf source code for -I in protoc command.
RUN mkdir golang && cd golang && git clone --depth 1 --branch $GOLANG_PROTOBUF_VERSION https://github.com/golang/protobuf.git
# Install protoc-gen-go.
RUN cd golang/protobuf && GO111MODULE=on go mod vendor
RUN GOBIN=/go/bin go install github.com/golang/protobuf/protoc-gen-go

# Need protobuffers/protobuf source code for -I in proto command.
RUN mkdir protocolbuffers && cd protocolbuffers && git clone --depth 1 --branch $PROTOCOLBUFFERS_PROTOBUF_VERSION https://github.com/protocolbuffers/protobuf.git
6 changes: 5 additions & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ Change it to point to your own build, after saving and closing the file, apiserv

After making changes to proto files, the Go client libraries and swagger files
need to be regenerated and checked-in. The `backend/api/generate_api.sh` script
takes care of this. It should be noted that this requires [Bazel](https://bazel.build/), version 0.24.0`
takes care of this. In order for the generated code to be owned by root, root credentials has to be provided.

```bash
sh generate_api.sh
```

# Visualization Server Instructions

Expand Down
133 changes: 12 additions & 121 deletions backend/api/generate_api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,124 +21,15 @@

set -ex

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null && pwd)"
REPO_ROOT="$DIR/../.."
VERSION="$(cat $REPO_ROOT/VERSION)"
if [ -z "$VERSION" ]; then
echo "ERROR: $REPO_ROOT/VERSION is empty"
exit 1
fi

BAZEL_BINDIR=$(bazel info bazel-bin)
SWAGGER_CMD=${DIR}/../../bazel-bin/external/com_github_go_swagger/cmd/swagger/*stripped/swagger
AUTOGEN_CMD="${DIR}/../../bazel-bin/external/com_github_mbrukman_autogen/autogen_tool"
GENERATED_GO_PROTO_FILES="${BAZEL_BINDIR}/backend/api/api_generated_go_sources/src/github.com/kubeflow/pipelines/backend/api/go_client/*.go"

# TODO this script should be able to be run from anywhere, not just within .../backend/api/

# Delete currently generated code.
rm -r -f ${DIR}/go_http_client/*
rm -r -f ${DIR}/go_client/*

# Build required tools.
bazel build @com_github_mbrukman_autogen//:autogen_tool
bazel build @com_github_go_swagger//cmd/swagger

# Build .pb.go and .gw.pb.go files from the proto sources.
bazel build //backend/api:api_generated_go_sources

# Copy the generated files into the source tree and add license.
for f in $GENERATED_GO_PROTO_FILES; do
target=${DIR}/go_client/$(basename ${f})
cp $f $target
chmod 766 $target
${AUTOGEN_CMD} -i --no-tlc -c "Google LLC" -l apache $target
done

# Generate and copy back into source tree .swagger.json files.
bazel build //backend/api:api_swagger
cp ${BAZEL_BINDIR}/backend/api/*.swagger.json ${DIR}/swagger

jq -s '
reduce .[] as $item ({}; . * $item) |
.info.title = "Kubeflow Pipelines API" |
.info.description = "This file contains REST API specification for Kubeflow Pipelines. The file is autogenerated from the swagger definition." |
.info.version = "'$VERSION'" |
.info.contact = { "name": "google", "email": "kubeflow-pipelines@google.com", "url": "https://www.google.com" } |
.info.license = { "name": "Apache 2.0", "url": "https://raw.githubusercontent.com/kubeflow/pipelines/master/LICENSE" }
' ${DIR}/swagger/{run,job,pipeline,experiment,pipeline.upload,healthz}.swagger.json > "${DIR}/swagger/kfp_api_single_file.swagger.json"

# Generate Go HTTP client from the swagger files.
${SWAGGER_CMD} generate client \
-f ${DIR}/swagger/job.swagger.json \
-A job \
--principal models.Principal \
-c job_client \
-m job_model \
-t ${DIR}/go_http_client

${SWAGGER_CMD} generate client \
-f ${DIR}/swagger/run.swagger.json \
-A run \
--principal models.Principal \
-c run_client \
-m run_model \
-t ${DIR}/go_http_client

${SWAGGER_CMD} generate client \
-f ${DIR}/swagger/experiment.swagger.json \
-A experiment \
--principal models.Principal \
-c experiment_client \
-m experiment_model \
-t ${DIR}/go_http_client

${SWAGGER_CMD} generate client \
-f ${DIR}/swagger/pipeline.upload.swagger.json \
-A pipeline_upload \
--principal models.Principal \
-c pipeline_upload_client \
-m pipeline_upload_model \
-t ${DIR}/go_http_client

${SWAGGER_CMD} generate client \
-f ${DIR}/swagger/pipeline.swagger.json \
-A pipeline \
--principal models.Principal \
-c pipeline_client \
-m pipeline_model \
-t ${DIR}/go_http_client

${SWAGGER_CMD} generate client \
-f ${DIR}/swagger/visualization.swagger.json \
-A visualization \
--principal models.Principal \
-c visualization_client \
-m visualization_model \
-t ${DIR}/go_http_client

${SWAGGER_CMD} generate client \
-f ${DIR}/swagger/healthz.swagger.json \
-A healthz \
--principal models.Principal \
-c healthz_client \
-m healthz_model \
-t ${DIR}/go_http_client

# Hack to fix an issue with go-swagger
# See https://github.com/go-swagger/go-swagger/issues/1381 for details.
sed -i -- 's/MaxConcurrency int64 `json:"max_concurrency,omitempty"`/MaxConcurrency int64 `json:"max_concurrency,omitempty,string"`/g' ${DIR}/go_http_client/job_model/api_job.go
sed -i -- 's/IntervalSecond int64 `json:"interval_second,omitempty"`/IntervalSecond int64 `json:"interval_second,omitempty,string"`/g' ${DIR}/go_http_client/job_model/api_periodic_schedule.go
sed -i -- 's/MaxConcurrency string `json:"max_concurrency,omitempty"`/MaxConcurrency int64 `json:"max_concurrency,omitempty,string"`/g' ${DIR}/go_http_client/job_model/api_job.go
sed -i -- 's/IntervalSecond string `json:"interval_second,omitempty"`/IntervalSecond int64 `json:"interval_second,omitempty,string"`/g' ${DIR}/go_http_client/job_model/api_periodic_schedule.go

# Executes the //go:generate directives in the generated code.
go generate ./...

# Add license to files in go_http_client.
find ${DIR}/go_http_client/ -name "*.go" -exec ${AUTOGEN_CMD} -i --no-tlc -c "Google LLC" -l apache {} \;

# Finally, run gazelle to add BUILD files for the generated code.
bazel run //:gazelle
# HACK: remove unnecessary BUILD.bazels
rm -f "$REPO_ROOT/sdk/python/kfp/components/structures/BUILD.bazel" "$REPO_ROOT/tools/metadatastore-upgrade/BUILD.bazel"
cd ../..
# docker run -it --rm \
# --mount type=bind,source="$(pwd)",target=/app/pipelines \
# builder /bin/bash

# Generate API
docker run --interactive --rm \
--mount type=bind,source="$(pwd)",target=/go/src/github.com/kubeflow/pipelines \
builder /go/src/github.com/kubeflow/pipelines/backend/api/generator.sh

# Change owner to user for generate files, explanation of command: https://askubuntu.com/questions/829537/how-do-i-change-owner-to-current-user-on-folder-and-containing-folders-inside-my
sudo find backend/api -user root -exec sudo chown $USER: {} +
119 changes: 119 additions & 0 deletions backend/api/generator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash

# Copyright 2018-2020 Google LLC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we generally do with updating licenses?

Copy link
Contributor Author

@Bobgy Bobgy Feb 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually update the date to include the last year there's a change, so we should update this as
Copyright 2020-2021 The Kubeflow Authors, because you created it.

#
# 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 -ex

export TMP_OUTPUT=/tmp

# Change directory\
cd /go/src/github.com/kubeflow/pipelines
# Delete currently generated code.
rm -r -f backend/api/go_http_client/*
rm -f -f backend/api/go_client/*

# Generate *.pb.go (grpc api client) from *.proto .
${PROTOCCOMPILER} -I. -Ibackend/api \
-I/go/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
-I/go/src/github.com/grpc-ecosystem/grpc-gateway/ \
-I/go/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options/ \
-I/usr/include/ \
--plugin=protoc-gen-go=/go/bin/protoc-gen-go \
--go_out=plugins=grpc:${TMP_OUTPUT} \
backend/api/*.proto
# Generate *.pb.gw.go (grpc api rest client) from *.proto.
${PROTOCCOMPILER} -I. -Ibackend/api \
-I/go/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
-I/go/src/github.com/grpc-ecosystem/grpc-gateway/ \
-I/go/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options/ \
-I/usr/include/ \
--plugin=protoc-gen-grpc-gateway=/go/bin/protoc-gen-grpc-gateway \
--grpc-gateway_out=logtostderr=true:${TMP_OUTPUT} \
backend/api/*.proto
# Move *.pb.go and *.gw.go to go_client folder.
cp ${TMP_OUTPUT}/github.com/kubeflow/pipelines/backend/api/go_client/* ./backend/api/go_client
# Generate *.swagger.json from *.proto into swagger folder.
${PROTOCCOMPILER} -I. -Ibackend/api \
-I/go/src/github.com/protocolbuffers/protobuf/src \
-I/go/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
-I/go/src/github.com/grpc-ecosystem/grpc-gateway/ \
-I/go/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options/ \
-I/go/src/github.com/protocolbuffers/protobuf/src/google/protobuf \
--plugin=protoc-gen-swagger=/go/bin/protoc-gen-swagger \
--swagger_out=logtostderr=true:${TMP_OUTPUT} \
backend/api/*.proto
cp ${TMP_OUTPUT}/backend/api/*.swagger.json ./backend/api/swagger
# Generate a single swagger json file from the swagger json files of all models.
# Note: use backend/backend/api/swagger/{run,job,experiment,pipeline,pipeline.upload}.swagger.json when apt-get can install jq-1.6
jq -s 'reduce .[] as $item ({}; . * $item) | .info.title = "Kubeflow Pipelines API" | .info.description = "This file contains REST API specification for Kubeflow Pipelines. The file is autogenerated from the swagger definition." | .info.version = "'$KFP_VERSION'" | .info.contact = { "name": "google", "email": "kubeflow-pipelines@google.com", "url": "https://www.google.com" } | .info.license = { "name": "Apache 2.0", "url": "https://raw.githubusercontent.com/kubeflow/pipelines/master/LICENSE" }' backend/api/swagger/run.swagger.json backend/api/swagger/job.swagger.json backend/api/swagger/experiment.swagger.json backend/api/swagger/pipeline.swagger.json backend/api/swagger/pipeline.upload.swagger.json > "backend/api/swagger/kfp_api_single_file.swagger.json"
# Generate go_http_client from swagger json.
swagger generate client \
-f backend/api/swagger/job.swagger.json \
-A job \
--principal models.Principal \
-c job_client \
-m job_model \
-t backend/api/go_http_client
swagger generate client \
-f backend/api/swagger/run.swagger.json \
-A run \
--principal models.Principal \
-c run_client \
-m run_model \
-t backend/api/go_http_client
swagger generate client \
-f backend/api/swagger/experiment.swagger.json \
-A experiment \
--principal models.Principal \
-c experiment_client \
-m experiment_model \
-t backend/api/go_http_client
swagger generate client \
-f backend/api/swagger/pipeline.swagger.json \
-A pipeline \
--principal models.Principal \
-c pipeline_client \
-m pipeline_model \
-t backend/api/go_http_client
swagger generate client \
-f backend/api/swagger/pipeline.upload.swagger.json \
-A pipeline_upload \
--principal models.Principal \
-c pipeline_upload_client \
-m pipeline_upload_model \
-t backend/api/go_http_client
swagger generate client \
-f backend/api/swagger/visualization.swagger.json \
-A visualization \
--principal models.Principal \
-c visualization_client \
-m visualization_model \
-t backend/api/go_http_client
swagger generate client \
-f backend/api/swagger/healthz.swagger.json \
-A healthz \
--principal models.Principal \
-c healthz_client \
-m healthz_model \
-t backend/api/go_http_client
# Hack to fix an issue with go-swagger
# See https://github.com/go-swagger/go-swagger/issues/1381 for details.
sed -i -- 's/MaxConcurrency int64 `json:"max_concurrency,omitempty"`/MaxConcurrency int64 `json:"max_concurrency,omitempty,string"`/g' backend/api/go_http_client/job_model/api_job.go
sed -i -- 's/IntervalSecond int64 `json:"interval_second,omitempty"`/IntervalSecond int64 `json:"interval_second,omitempty,string"`/g' backend/api/go_http_client/job_model/api_periodic_schedule.go
sed -i -- 's/MaxConcurrency string `json:"max_concurrency,omitempty"`/MaxConcurrency int64 `json:"max_concurrency,omitempty,string"`/g' backend/api/go_http_client/job_model/api_job.go
sed -i -- 's/IntervalSecond string `json:"interval_second,omitempty"`/IntervalSecond int64 `json:"interval_second,omitempty,string"`/g' backend/api/go_http_client/job_model/api_periodic_schedule.go
# Execute the //go:generate directives in the generated code.
cd backend/api && go generate ./...
cd ../..
45 changes: 0 additions & 45 deletions backend/api/go_client/BUILD.bazel

This file was deleted.

Loading