Separate tag in the Docker API tag call

Closes gh-35358
This commit is contained in:
Moritz Halbritter 2023-05-10 09:20:10 +02:00
parent 736e95b5d8
commit e930801eb1
4 changed files with 61 additions and 3 deletions

View File

@ -65,6 +65,7 @@ import org.springframework.util.StringUtils;
* @author Phillip Webb
* @author Scott Frederick
* @author Rafael Ceccone
* @author Moritz Halbritter
* @since 2.3.0
*/
public class DockerApi {
@ -346,7 +347,15 @@ public class DockerApi {
public void tag(ImageReference sourceReference, ImageReference targetReference) throws IOException {
Assert.notNull(sourceReference, "SourceReference must not be null");
Assert.notNull(targetReference, "TargetReference must not be null");
URI uri = buildUrl("/images/" + sourceReference + "/tag", "repo", targetReference.toString());
String tag = targetReference.getTag();
URI uri;
if (tag == null) {
uri = buildUrl("/images/" + sourceReference + "/tag", "repo", targetReference.toString());
}
else {
uri = buildUrl("/images/" + sourceReference + "/tag", "repo",
targetReference.inTaglessForm().toString(), "tag", tag);
}
http().post(uri).close();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import org.springframework.util.ObjectUtils;
*
* @author Phillip Webb
* @author Scott Frederick
* @author Moritz Halbritter
* @since 2.3.0
* @see ImageName
*/
@ -153,6 +154,17 @@ public final class ImageReference {
return new ImageReference(this.name, (this.tag != null) ? this.tag : LATEST, null);
}
/**
* Return an {@link ImageReference} without the tag.
* @return the image reference in tagless form
*/
public ImageReference inTaglessForm() {
if (this.tag == null) {
return this;
}
return new ImageReference(this.name, null, this.digest);
}
/**
* Return an {@link ImageReference} containing either a tag or a digest. If neither
* the digest nor the tag has been defined then tag {@code latest} is used.

View File

@ -75,6 +75,7 @@ import static org.mockito.Mockito.times;
* @author Phillip Webb
* @author Scott Frederick
* @author Rafael Ceccone
* @author Moritz Halbritter
*/
@ExtendWith(MockitoExtension.class)
class DockerApiTests {
@ -419,7 +420,17 @@ class DockerApiTests {
void tagTagsImage() throws Exception {
ImageReference sourceReference = ImageReference.of("localhost:5000/ubuntu");
ImageReference targetReference = ImageReference.of("localhost:5000/ubuntu:tagged");
URI tagURI = new URI(IMAGES_URL + "/localhost:5000/ubuntu/tag?repo=localhost%3A5000%2Fubuntu%3Atagged");
URI tagURI = new URI(IMAGES_URL + "/localhost:5000/ubuntu/tag?repo=localhost%3A5000%2Fubuntu&tag=tagged");
given(http().post(tagURI)).willReturn(emptyResponse());
this.api.tag(sourceReference, targetReference);
then(http()).should().post(tagURI);
}
@Test
void tagRenamesImage() throws Exception {
ImageReference sourceReference = ImageReference.of("localhost:5000/ubuntu");
ImageReference targetReference = ImageReference.of("localhost:5000/ubuntu-2");
URI tagURI = new URI(IMAGES_URL + "/localhost:5000/ubuntu/tag?repo=localhost%3A5000%2Fubuntu-2");
given(http().post(tagURI)).willReturn(emptyResponse());
this.api.tag(sourceReference, targetReference);
then(http()).should().post(tagURI);

View File

@ -29,6 +29,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
*
* @author Phillip Webb
* @author Scott Frederick
* @author Moritz Halbritter
*/
class ImageReferenceTests {
@ -272,4 +273,29 @@ class ImageReferenceTests {
assertThat(r1).isEqualTo(r1).isEqualTo(r2).isNotEqualTo(r3);
}
@Test
void withDigest() {
ImageReference reference = ImageReference.of("docker.io/library/ubuntu:bionic");
ImageReference updated = reference
.withDigest("sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
assertThat(updated).hasToString(
"docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
}
@Test
void inTaglessFormWithDigest() {
ImageReference reference = ImageReference
.of("docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
ImageReference updated = reference.inTaglessForm();
assertThat(updated).hasToString(
"docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
}
@Test
void inTaglessForm() {
ImageReference reference = ImageReference.of("docker.io/library/ubuntu:bionic");
ImageReference updated = reference.inTaglessForm();
assertThat(updated).hasToString("docker.io/library/ubuntu");
}
}