Skip to content

Commit 4eef8d5

Browse files
committed
Separate tag in the Docker API tag call
Closes gh-35358
1 parent cf269b6 commit 4eef8d5

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/DockerApi.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
* @author Phillip Webb
6666
* @author Scott Frederick
6767
* @author Rafael Ceccone
68+
* @author Moritz Halbritter
6869
* @since 2.3.0
6970
*/
7071
public class DockerApi {
@@ -346,7 +347,15 @@ public Image inspect(ImageReference reference) throws IOException {
346347
public void tag(ImageReference sourceReference, ImageReference targetReference) throws IOException {
347348
Assert.notNull(sourceReference, "SourceReference must not be null");
348349
Assert.notNull(targetReference, "TargetReference must not be null");
349-
URI uri = buildUrl("/images/" + sourceReference + "/tag", "repo", targetReference.toString());
350+
String tag = targetReference.getTag();
351+
URI uri;
352+
if (tag == null) {
353+
uri = buildUrl("/images/" + sourceReference + "/tag", "repo", targetReference.toString());
354+
}
355+
else {
356+
uri = buildUrl("/images/" + sourceReference + "/tag", "repo",
357+
targetReference.inTaglessForm().toString(), "tag", tag);
358+
}
350359
http().post(uri).close();
351360
}
352361

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/type/ImageReference.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
*
2929
* @author Phillip Webb
3030
* @author Scott Frederick
31+
* @author Moritz Halbritter
3132
* @since 2.3.0
3233
* @see ImageName
3334
*/
@@ -153,6 +154,17 @@ public ImageReference inTaggedForm() {
153154
return new ImageReference(this.name, (this.tag != null) ? this.tag : LATEST, null);
154155
}
155156

157+
/**
158+
* Return an {@link ImageReference} without the tag.
159+
* @return the image reference in tagless form
160+
*/
161+
public ImageReference inTaglessForm() {
162+
if (this.tag == null) {
163+
return this;
164+
}
165+
return new ImageReference(this.name, null, this.digest);
166+
}
167+
156168
/**
157169
* Return an {@link ImageReference} containing either a tag or a digest. If neither
158170
* the digest nor the tag has been defined then tag {@code latest} is used.

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/DockerApiTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
* @author Phillip Webb
7676
* @author Scott Frederick
7777
* @author Rafael Ceccone
78+
* @author Moritz Halbritter
7879
*/
7980
@ExtendWith(MockitoExtension.class)
8081
class DockerApiTests {
@@ -419,7 +420,17 @@ void tagWhenTargetIsNullThrowsException() {
419420
void tagTagsImage() throws Exception {
420421
ImageReference sourceReference = ImageReference.of("localhost:5000/ubuntu");
421422
ImageReference targetReference = ImageReference.of("localhost:5000/ubuntu:tagged");
422-
URI tagURI = new URI(IMAGES_URL + "/localhost:5000/ubuntu/tag?repo=localhost%3A5000%2Fubuntu%3Atagged");
423+
URI tagURI = new URI(IMAGES_URL + "/localhost:5000/ubuntu/tag?repo=localhost%3A5000%2Fubuntu&tag=tagged");
424+
given(http().post(tagURI)).willReturn(emptyResponse());
425+
this.api.tag(sourceReference, targetReference);
426+
then(http()).should().post(tagURI);
427+
}
428+
429+
@Test
430+
void tagRenamesImage() throws Exception {
431+
ImageReference sourceReference = ImageReference.of("localhost:5000/ubuntu");
432+
ImageReference targetReference = ImageReference.of("localhost:5000/ubuntu-2");
433+
URI tagURI = new URI(IMAGES_URL + "/localhost:5000/ubuntu/tag?repo=localhost%3A5000%2Fubuntu-2");
423434
given(http().post(tagURI)).willReturn(emptyResponse());
424435
this.api.tag(sourceReference, targetReference);
425436
then(http()).should().post(tagURI);

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/type/ImageReferenceTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*
3030
* @author Phillip Webb
3131
* @author Scott Frederick
32+
* @author Moritz Halbritter
3233
*/
3334
class ImageReferenceTests {
3435

@@ -272,4 +273,29 @@ void equalsAndHashCode() {
272273
assertThat(r1).isEqualTo(r1).isEqualTo(r2).isNotEqualTo(r3);
273274
}
274275

276+
@Test
277+
void withDigest() {
278+
ImageReference reference = ImageReference.of("docker.io/library/ubuntu:bionic");
279+
ImageReference updated = reference
280+
.withDigest("sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
281+
assertThat(updated).hasToString(
282+
"docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
283+
}
284+
285+
@Test
286+
void inTaglessFormWithDigest() {
287+
ImageReference reference = ImageReference
288+
.of("docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
289+
ImageReference updated = reference.inTaglessForm();
290+
assertThat(updated).hasToString(
291+
"docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
292+
}
293+
294+
@Test
295+
void inTaglessForm() {
296+
ImageReference reference = ImageReference.of("docker.io/library/ubuntu:bionic");
297+
ImageReference updated = reference.inTaglessForm();
298+
assertThat(updated).hasToString("docker.io/library/ubuntu");
299+
}
300+
275301
}

0 commit comments

Comments
 (0)