-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
api: add go proto generation script #8155
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7296975
go proto generation script
kyessenov 494ecd1
fix python format
kyessenov 6bea765
review feedback
kyessenov 55cd052
fix format
kyessenov 41e2254
fix format
kyessenov 4ab0339
merge fix
kyessenov 0b8c0df
update script
kyessenov b692406
fix test file
kyessenov 7745cca
review feedback
kyessenov 6a40cc0
update package import
kyessenov cd50630
Merge remote-tracking branch 'upstream/master' into generate_go_protos
kyessenov 07bba3b
adding sync
kyessenov 790fa31
format
kyessenov d3bdc13
update script
kyessenov 85923b3
update the script
kyessenov e415f56
hook it up
kyessenov cf27aa1
update branch
kyessenov 91dacaa
Merge remote-tracking branch 'upstream/master' into generate_go_protos
kyessenov bdf73b3
review feedback
kyessenov beee205
Merge remote-tracking branch 'upstream/master' into generate_go_protos
kyessenov f0e736e
review
kyessenov 21f10e7
fix format
kyessenov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
if [ -z "$CIRCLE_PULL_REQUEST" ] && [ "$CIRCLE_BRANCH" == "master" ] | ||
then | ||
tools/api/generate_go_protobuf.py | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from subprocess import check_output | ||
kyessenov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from subprocess import check_call | ||
import glob | ||
import os | ||
import shutil | ||
import sys | ||
import re | ||
|
||
TARGETS = '@envoy_api//...' | ||
IMPORT_BASE = 'github.com/envoyproxy/go-control-plane' | ||
OUTPUT_BASE = 'build_go' | ||
REPO_BASE = 'go-control-plane' | ||
BRANCH = 'master' | ||
MIRROR_MSG = 'Mirrored from envoyproxy/envoy @ ' | ||
USER_NAME = 'go-control-plane(CircleCI)' | ||
USER_EMAIL = '[email protected]' | ||
|
||
|
||
def generateProtobufs(output): | ||
bazel_bin = check_output(['bazel', 'info', 'bazel-bin']).decode().strip() | ||
go_protos = check_output([ | ||
'bazel', | ||
'query', | ||
'kind("go_proto_library", %s)' % TARGETS, | ||
]).split() | ||
|
||
# Each rule has the form @envoy_api//foo/bar:baz_go_proto. | ||
# First build all the rules to ensure we have the output files. | ||
check_call(['bazel', 'build', '-c', 'fastbuild'] + go_protos) | ||
|
||
for rule in go_protos: | ||
# Example rule: | ||
# @envoy_api//envoy/config/bootstrap/v2:pkg_go_proto | ||
# | ||
# Example generated directory: | ||
# bazel-bin/external/envoy_api/envoy/config/bootstrap/v2/linux_amd64_stripped/pkg_go_proto%/github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v2/ | ||
# | ||
# Example output directory: | ||
# go_out/envoy/config/bootstrap/v2 | ||
rule_dir, proto = rule.decode()[len('@envoy_api//'):].rsplit(':', 1) | ||
input_dir = os.path.join(bazel_bin, 'external', 'envoy_api', rule_dir, 'linux_amd64_stripped', | ||
proto + '%', IMPORT_BASE, rule_dir) | ||
input_files = glob.glob(os.path.join(input_dir, '*.go')) | ||
output_dir = os.path.join(output, rule_dir) | ||
|
||
# Ensure the output directory exists | ||
os.makedirs(output_dir, 0o755, exist_ok=True) | ||
for generated_file in input_files: | ||
shutil.copy(generated_file, output_dir) | ||
print('Go artifacts placed into: ' + output) | ||
|
||
|
||
def git(repo, *args): | ||
cmd = ['git'] | ||
if repo: | ||
cmd = cmd + ['-C', repo] | ||
for arg in args: | ||
cmd = cmd + [arg] | ||
return check_output(cmd).decode() | ||
|
||
|
||
def cloneGoProtobufs(repo): | ||
# Create a local clone of go-control-plane | ||
git(None, 'clone', '[email protected]:envoyproxy/go-control-plane', repo) | ||
git(repo, 'fetch') | ||
git(repo, 'checkout', '-B', BRANCH, 'origin/master') | ||
|
||
|
||
def findLastSyncSHA(repo): | ||
# Determine last envoyproxy/envoy SHA in envoyproxy/go-control-plane | ||
last_commit = git(repo, 'log', '--grep=' + MIRROR_MSG, '-n', '1', '--format=%B').strip() | ||
# Initial SHA from which the APIs start syncing. Prior to that it was done manually. | ||
if last_commit == "": | ||
return 'e7f0b7176efdc65f96eb1697b829d1e6187f4502' | ||
kyessenov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
m = re.search(MIRROR_MSG + '(\w+)', last_commit) | ||
return m.group(1) | ||
|
||
|
||
def updatedSinceSHA(repo, last_sha): | ||
# Determine if there are changes to API since last SHA | ||
return git(None, 'rev-list', '%s..HEAD' % last_sha, 'api/envoy').split() | ||
|
||
|
||
def syncGoProtobufs(output, repo): | ||
# Sync generated content against repo and return true if there is a commit necessary | ||
dst = os.path.join(repo, 'envoy') | ||
# Remove subtree at envoy in repo | ||
git(repo, 'rm', '-r', 'envoy') | ||
# Copy subtree at envoy from output to repo | ||
shutil.copytree(os.path.join(output, 'envoy'), dst) | ||
|
||
|
||
def publishGoProtobufs(repo, sha): | ||
# Publish generated files with the last SHA changes to API | ||
git(repo, 'config', 'user.name', USER_NAME) | ||
git(repo, 'config', 'user.email', USER_EMAIL) | ||
git(repo, 'add', 'envoy') | ||
git(repo, 'commit', '-s', '-m', MIRROR_MSG + sha) | ||
git(repo, 'push', 'origin', BRANCH) | ||
|
||
|
||
if __name__ == "__main__": | ||
workspace = check_output(['bazel', 'info', 'workspace']).decode().strip() | ||
output = os.path.join(workspace, OUTPUT_BASE) | ||
generateProtobufs(output) | ||
repo = os.path.join(workspace, REPO_BASE) | ||
cloneGoProtobufs(repo) | ||
last_sha = findLastSyncSHA(repo) | ||
changes = updatedSinceSHA(repo, last_sha) | ||
if changes: | ||
print('Changes detected: %s' % changes) | ||
syncGoProtobufs(output, repo) | ||
publishGoProtobufs(repo, changes[0]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this impact local development flows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This aligns the generated/bazel-internal build with the public bazel-less build, so I think it's an improvement.