Skip to content

Commit f9fe39c

Browse files
committed
[grid] Dynamic Grid can enforce platform for node browser images
Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent 35f5488 commit f9fe39c

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

java/src/org/openqa/selenium/docker/internal/Reference.java

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,24 @@ public class Reference {
3535
private final String name;
3636
private final String tag;
3737
private final String digest;
38+
private final String platform;
3839

3940
@VisibleForTesting
4041
Reference(String domain, String name, String tag, String digest) {
4142
this.domain = Require.nonNull("Domain", domain);
4243
this.name = Require.nonNull("Name", name);
4344
this.tag = tag;
4445
this.digest = digest;
46+
this.platform = getDefaultPlatform();
47+
}
48+
49+
@VisibleForTesting
50+
Reference(String domain, String name, String tag, String digest, String platform) {
51+
this.domain = Require.nonNull("Domain", domain);
52+
this.name = Require.nonNull("Name", name);
53+
this.tag = tag;
54+
this.digest = digest;
55+
this.platform = Require.nonNull("Platform", platform);
4556
}
4657

4758
// Logic taken from https://github.com/distribution/distribution/blob/main/reference/normalize.go
@@ -53,15 +64,15 @@ public static Reference parse(String input) {
5364
String remainder = splitDockerDomain.get("remainder");
5465

5566
String name;
56-
String digest = null;
67+
String digest =
68+
splitDockerDomain.get("digest").isEmpty() ? null : splitDockerDomain.get("digest");
69+
String platform = splitDockerDomain.get("platform");
5770
String tag = DEFAULT_TAG;
5871

59-
int digestSep = remainder.indexOf("@");
6072
int tagSep = remainder.indexOf(":");
61-
if (digestSep > -1 && tagSep > -1) {
62-
digest = remainder.substring(digestSep + 1);
63-
name = remainder.substring(0, digestSep);
73+
if (digest != null) {
6474
tag = null;
75+
name = remainder;
6576
} else if (tagSep > -1) {
6677
tag = remainder.substring(tagSep + 1);
6778
name = remainder.substring(0, tagSep);
@@ -74,12 +85,27 @@ public static Reference parse(String input) {
7485
String.format("Invalid reference format: repository name (%s) must be lowercase", name));
7586
}
7687

77-
return new Reference(domain, name, tag, digest);
88+
return new Reference(domain, name, tag, digest, platform);
7889
}
7990

8091
private static ImmutableMap<String, String> splitDockerDomain(String name) {
8192
String domain;
8293
String remainder;
94+
String platform = getDefaultPlatform();
95+
String digest = "";
96+
97+
// Check if the name contains a platform part
98+
int platformSep = name.lastIndexOf("@");
99+
if (platformSep > -1) {
100+
String[] parts = name.substring(platformSep + 1).split("/");
101+
if (parts.length == 2) {
102+
platform = name.substring(platformSep + 1);
103+
} else if (parts[0].contains(":")) {
104+
digest = name.substring(platformSep + 1);
105+
}
106+
name = name.substring(0, platformSep);
107+
}
108+
83109
int domSep = name.indexOf("/");
84110
String possibleDomain = domSep == -1 ? "" : name.substring(0, domSep);
85111
if (domSep == -1
@@ -99,7 +125,8 @@ private static ImmutableMap<String, String> splitDockerDomain(String name) {
99125
if (DEFAULT_DOMAIN.equals(domain) && !remainder.contains("/")) {
100126
remainder = String.format("%s/%s", DEFAULT_REPO, remainder);
101127
}
102-
return ImmutableMap.of("domain", domain, "remainder", remainder);
128+
return ImmutableMap.of(
129+
"domain", domain, "remainder", remainder, "platform", platform, "digest", digest);
103130
}
104131

105132
public String getDomain() {
@@ -118,6 +145,10 @@ public String getDigest() {
118145
return digest;
119146
}
120147

148+
public String getPlatform() {
149+
return platform;
150+
}
151+
121152
public String getFamiliarName() {
122153
StringBuilder familiar = new StringBuilder();
123154

@@ -142,6 +173,16 @@ public String getFamiliarName() {
142173
return familiar.toString();
143174
}
144175

176+
private static String getDefaultPlatform() {
177+
String arch = System.getProperty("os.arch").toLowerCase();
178+
if (arch.contains("amd64") || arch.contains("x86_64")) {
179+
arch = "amd64";
180+
} else if (arch.contains("arm64") || arch.contains("aarch64")) {
181+
arch = "arm64";
182+
}
183+
return "linux/" + arch;
184+
}
185+
145186
@Override
146187
public String toString() {
147188
return "Reference{"
@@ -157,6 +198,8 @@ public String toString() {
157198
+ ", digest='"
158199
+ digest
159200
+ '\''
201+
+ ", platform='"
202+
+ platform
160203
+ '}';
161204
}
162205

@@ -170,11 +213,12 @@ public boolean equals(Object o) {
170213
return this.domain.equals(that.domain)
171214
&& this.name.equals(that.name)
172215
&& Objects.equals(tag, that.tag)
173-
&& Objects.equals(digest, that.digest);
216+
&& Objects.equals(digest, that.digest)
217+
&& Objects.equals(platform, that.platform);
174218
}
175219

176220
@Override
177221
public int hashCode() {
178-
return Objects.hash(domain, name, tag, digest);
222+
return Objects.hash(domain, name, tag, digest, platform);
179223
}
180224
}

java/src/org/openqa/selenium/docker/v1_41/PullImage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public void apply(Reference ref) {
5151
HttpRequest req =
5252
new HttpRequest(POST, String.format("/v%s/images/create", DOCKER_API_VERSION))
5353
.addHeader("Content-Type", JSON_UTF_8)
54-
.addQueryParameter("fromImage", image);
54+
.addQueryParameter("fromImage", image)
55+
.addQueryParameter("platform", ref.getPlatform());
5556

5657
if (ref.getDigest() != null) {
5758
req.addQueryParameter("tag", ref.getDigest());

java/test/org/openqa/selenium/docker/internal/ReferenceTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ public static Stream<Arguments> data() {
8282
new Reference("localhost:5000", "gouda/brie/cheddar/img", null, sha256),
8383
String.format("localhost:5000/gouda/brie/cheddar/img@%s", sha256)
8484
},
85+
{
86+
"localhost:5000/gouda/brie/cheddar/img:4.30.0-20250101@linux/amd64",
87+
new Reference(
88+
"localhost:5000",
89+
"gouda/brie/cheddar/img",
90+
"4.30.0-20250101",
91+
null,
92+
"linux/amd64"),
93+
"localhost:5000/gouda/brie/cheddar/img:4.30.0-20250101"
94+
},
95+
{
96+
"localhost:5000/gouda/brie/cheddar/img@linux/amd64",
97+
new Reference(
98+
"localhost:5000", "gouda/brie/cheddar/img", "latest", null, "linux/amd64"),
99+
"localhost:5000/gouda/brie/cheddar/img:latest"
100+
},
85101
})
86102
.stream()
87103
.map(Arguments::of);

0 commit comments

Comments
 (0)