Skip to content

Commit 0bf8887

Browse files
Merge branch 'main' into use-graalvm-ci-project
2 parents e800bf1 + 816b160 commit 0bf8887

File tree

11 files changed

+90
-22
lines changed

11 files changed

+90
-22
lines changed

.cloudbuild/library_generation/cloudbuild-library-generation-push-prod.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ steps:
3030
"--file", ".cloudbuild/library_generation/library_generation.Dockerfile", "."]
3131
id: library-generation-build
3232
waitFor: ["-"]
33+
env:
34+
- 'DOCKER_BUILDKIT=1'
3335

3436
options:
3537
logging: CLOUD_LOGGING_ONLY

.cloudbuild/library_generation/cloudbuild-library-generation-push.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ steps:
3030
"--file", ".cloudbuild/library_generation/library_generation.Dockerfile", "."]
3131
id: library-generation-build
3232
waitFor: ["-"]
33+
env:
34+
- 'DOCKER_BUILDKIT=1'
3335

3436
options:
3537
logging: CLOUD_LOGGING_ONLY

.github/scripts/hermetic_library_generation.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ git checkout "${current_branch}"
8181
# copy generation configuration from target branch to current branch.
8282
git show "${target_branch}":"${generation_config}" > "${baseline_generation_config}"
8383

84-
# get .m2 folder so it's mapped into the docker container
85-
m2_folder=$(dirname "$(mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout)")
86-
8784
# download api definitions from googleapis repository
8885
googleapis_commitish=$(grep googleapis_commitish "${generation_config}" | cut -d ":" -f 2 | xargs)
8986
api_def_dir=$(mktemp -d)

gax-java/gax/src/main/java/com/google/api/gax/core/GaxProperties.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class GaxProperties {
4949
private static final String GAX_VERSION = getLibraryVersion(GaxProperties.class, "version.gax");
5050
private static final String JAVA_VERSION = getRuntimeVersion();
5151
private static final String PROTOBUF_VERSION =
52-
getBundleVersion(Any.class).orElse(DEFAULT_VERSION);
52+
getProtobufVersion(Any.class, "com.google.protobuf.RuntimeVersion");;
5353

5454
private GaxProperties() {}
5555

@@ -148,4 +148,29 @@ static Optional<String> getBundleVersion(Class<?> clazz) {
148148
return Optional.empty();
149149
}
150150
}
151+
152+
/**
153+
* Returns the Protobuf runtime version as reported by com.google.protobuf.RuntimeVersion, if
154+
* class is available, otherwise by reading from MANIFEST file. If niether option is available
155+
* defaults to protobuf version 3 as RuntimeVersion class is available in protobuf version 4+
156+
*/
157+
@VisibleForTesting
158+
static String getProtobufVersion(Class clazz, String protobufRuntimeVersionClassName) {
159+
try {
160+
Class<?> protobufRuntimeVersionClass = Class.forName(protobufRuntimeVersionClassName);
161+
return protobufRuntimeVersionClass.getField("MAJOR").get(null)
162+
+ "."
163+
+ protobufRuntimeVersionClass.getField("MINOR").get(null)
164+
+ "."
165+
+ protobufRuntimeVersionClass.getField("PATCH").get(null);
166+
} catch (ClassNotFoundException
167+
| NoSuchFieldException
168+
| IllegalAccessException
169+
| SecurityException
170+
| NullPointerException e) {
171+
// If manifest file is not available default to protobuf generic version 3 as we know
172+
// RuntimeVersion class is available in protobuf jar 4+.
173+
return getBundleVersion(clazz).orElse("3");
174+
}
175+
}
151176
}

gax-java/gax/src/main/java/com/google/api/gax/rpc/ApiClientHeaderProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static String checkAndAppendProtobufVersionIfNecessary(
8888
// TODO(b/366417603): appending protobuf version to existing client library token until resolved
8989
Pattern pattern = Pattern.compile("(gccl|gapic)\\S*");
9090
Matcher matcher = pattern.matcher(apiClientHeaderValue);
91-
if (matcher.find()) {
91+
if (matcher.find() && GaxProperties.getProtobufVersion() != null) {
9292
return apiClientHeaderValue.substring(0, matcher.end())
9393
+ "--"
9494
+ PROTOBUF_HEADER_VERSION_KEY
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"name": "com.google.protobuf.RuntimeVersion",
4+
"fields" : [
5+
{ "name" : "MAJOR" },
6+
{ "name" : "MINOR" },
7+
{ "name" : "PATCH" }
8+
]
9+
}
10+
]

gax-java/gax/src/test/java/com/google/api/gax/core/GaxPropertiesTest.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static org.junit.jupiter.api.Assertions.assertTrue;
3636

3737
import com.google.common.base.Strings;
38+
import com.google.protobuf.Any;
3839
import java.io.IOException;
3940
import java.util.Optional;
4041
import java.util.regex.Pattern;
@@ -160,12 +161,8 @@ void testGetJavaRuntimeInfo_nullJavaVersion() {
160161

161162
@Test
162163
public void testGetProtobufVersion() throws IOException {
163-
Version version = readVersion(GaxProperties.getProtobufVersion());
164-
165-
assertTrue(version.major >= 3);
166-
if (version.major == 3) {
167-
assertTrue(version.minor >= 25);
168-
}
164+
assertTrue(
165+
Pattern.compile("^\\d+\\.\\d+\\.\\d+").matcher(GaxProperties.getProtobufVersion()).find());
169166
}
170167

171168
@Test
@@ -175,6 +172,36 @@ public void testGetBundleVersion_noManifestFile() throws IOException {
175172
assertFalse(version.isPresent());
176173
}
177174

175+
@Test
176+
void testGetProtobufVersion_success() {
177+
String version =
178+
GaxProperties.getProtobufVersion(
179+
Any.class, "com.google.api.gax.core.GaxPropertiesTest$RuntimeVersion");
180+
181+
assertEquals("3.13.6", version);
182+
}
183+
184+
@Test
185+
void testGetProtobufVersion_classNotFoundException() throws Exception {
186+
String version = GaxProperties.getProtobufVersion(Any.class, "foo.NonExistantClass");
187+
188+
assertTrue(Pattern.compile("^\\d+\\.\\d+\\.\\d+").matcher(version).find());
189+
}
190+
191+
@Test
192+
void testgetProtobufVersion_noSuchFieldException() throws Exception {
193+
String version = GaxProperties.getProtobufVersion(Any.class, "java.lang.Class");
194+
195+
assertTrue(Pattern.compile("^\\d+\\.\\d+\\.\\d+").matcher(version).find());
196+
}
197+
198+
@Test
199+
void testGetProtobufVersion_noManifest() throws Exception {
200+
String version = GaxProperties.getProtobufVersion(GaxProperties.class, "foo.NonExistantClass");
201+
202+
assertEquals("3", version);
203+
}
204+
178205
private Version readVersion(String version) {
179206
assertTrue(Pattern.compile("^\\d+\\.\\d+\\.\\d+").matcher(version).find());
180207
String[] versionComponents = version.split("\\.");
@@ -194,4 +221,11 @@ public Version(int major, int minor) {
194221
this.minor = minor;
195222
}
196223
}
224+
225+
// Test class that emulates com.google.protobuf.RuntimeVersion for reflection lookup of fields
226+
class RuntimeVersion {
227+
public static final int MAJOR = 3;
228+
public static final int MINOR = 13;
229+
public static final int PATCH = 6;
230+
}
197231
}

hermetic_build/DEVELOPMENT.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,10 @@ python hermetic_build/library_generation/cli/entry_point.py generate \
165165
-t local:image-tag \
166166
.
167167
```
168-
Please note that the build only works when using the new
169-
[Docker BuildKit](https://docs.docker.com/build/buildkit/) (enabled through the `DOCKER_BUILDKIT` variable).
170-
This is meant for local development only (in CloudTops) - GH Actions' Ubuntu-22.04 \
171-
[comes with the latest Docker version](https://github.com/actions/runner-images/blob/e74605cd6d5407469cf224802f25057bafc23d70/images/ubuntu/Ubuntu2204-Readme.md?plain=1#L81-L83)
172-
and is able to handle the build properly using the (updated) legacy builder.
168+
Please note that the build only works when using the new [Docker BuildKit](https://docs.docker.com/build/buildkit/)
169+
(enabled through the `DOCKER_BUILDKIT` variable).
173170

174-
3. Set the version of gapic-generator-java
171+
2. Set the version of gapic-generator-java
175172

176173
```shell
177174
LOCAL_GENERATOR_VERSION=$(mvn \
@@ -182,7 +179,7 @@ python hermetic_build/library_generation/cli/entry_point.py generate \
182179
-q)
183180
```
184181

185-
4. Run the image
182+
3. Run the image
186183

187184
```shell
188185
# Assume you want to generate the library in the current working directory
@@ -199,7 +196,7 @@ python hermetic_build/library_generation/cli/entry_point.py generate \
199196
--library-names=apigee-connect,asset \
200197
--repository-path=/workspace \
201198
--api-definitions-path=/workspace/apis
202-
```
199+
```
203200
Note that if you specify the generator version using environment variable,
204201
`-e GENERATOR_VERSION="${LOCAL_GENERATOR_VERSION}"` in the above example,
205202
you should not set `gapic_generator_version` and `protoc_version` in the

hermetic_build/library_generation/generate_library.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ if [ -z "${os_architecture}" ]; then
115115
os_architecture=$(detect_os_architecture)
116116
fi
117117

118-
temp_destination_path="${output_folder}/temp_preprocessed"
118+
temp_destination_path="${output_folder}/temp_preprocessed-$RANDOM"
119119
mkdir -p "${output_folder}/${destination_path}"
120120
if [ -d "${temp_destination_path}" ]; then
121121
# we don't want the preprocessed sources of a previous run

hermetic_build/library_generation/tests/integration_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def __build_image(cls, docker_file: str, cwd: str):
202202
subprocess.check_call(
203203
["docker", "build", "-f", docker_file, "-t", image_tag, "."],
204204
cwd=cwd,
205+
env=dict(os.environ, DOCKER_BUILDKIT="1"),
205206
)
206207

207208
@classmethod

0 commit comments

Comments
 (0)